niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 14 | #include "webrtc/common_audio/include/audio_util.h" |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 15 | #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 16 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
kjellander@webrtc.org | 035e912 | 2015-01-28 19:57:00 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/channel_buffer.h" |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/common.h" |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | namespace { |
| 22 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 23 | const size_t kSamplesPer16kHzChannel = 160; |
| 24 | const size_t kSamplesPer32kHzChannel = 320; |
| 25 | const size_t kSamplesPer48kHzChannel = 480; |
Alejandro Luebs | 5a92aa8 | 2015-04-27 11:34:45 -0700 | [diff] [blame] | 26 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 27 | int KeyboardChannelIndex(const StreamConfig& stream_config) { |
| 28 | if (!stream_config.has_keyboard()) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 29 | RTC_NOTREACHED(); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 30 | return 0; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 31 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 32 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 33 | return stream_config.num_channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 36 | size_t NumBandsFromSamplesPerChannel(size_t num_frames) { |
| 37 | size_t num_bands = 1; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 38 | if (num_frames == kSamplesPer32kHzChannel || |
| 39 | num_frames == kSamplesPer48kHzChannel) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 40 | num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 41 | } |
| 42 | return num_bands; |
| 43 | } |
| 44 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } // namespace |
| 46 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 47 | AudioBuffer::AudioBuffer(size_t input_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 48 | size_t num_input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 49 | size_t process_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 50 | size_t num_process_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | size_t output_num_frames) |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 52 | : input_num_frames_(input_num_frames), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 53 | num_input_channels_(num_input_channels), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 54 | proc_num_frames_(process_num_frames), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 55 | num_proc_channels_(num_process_channels), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 56 | output_num_frames_(output_num_frames), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 57 | num_channels_(num_process_channels), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 58 | num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 59 | num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 60 | mixed_low_pass_valid_(false), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 61 | reference_copied_(false), |
| 62 | activity_(AudioFrame::kVadUnknown), |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 63 | keyboard_data_(NULL), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 64 | data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 65 | RTC_DCHECK_GT(input_num_frames_, 0); |
| 66 | RTC_DCHECK_GT(proc_num_frames_, 0); |
| 67 | RTC_DCHECK_GT(output_num_frames_, 0); |
| 68 | RTC_DCHECK_GT(num_input_channels_, 0); |
| 69 | RTC_DCHECK_GT(num_proc_channels_, 0); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 70 | RTC_DCHECK_LE(num_proc_channels_, num_input_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 72 | if (input_num_frames_ != proc_num_frames_ || |
| 73 | output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 74 | // Create an intermediate buffer for resampling. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 75 | process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 76 | num_proc_channels_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 77 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 78 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 79 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 80 | input_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 81 | new PushSincResampler(input_num_frames_, proc_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | if (output_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 86 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 87 | output_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 88 | new PushSincResampler(proc_num_frames_, output_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 89 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 93 | if (num_bands_ > 1) { |
| 94 | split_data_.reset(new IFChannelBuffer(proc_num_frames_, |
| 95 | num_proc_channels_, |
| 96 | num_bands_)); |
Alejandro Luebs | 5a92aa8 | 2015-04-27 11:34:45 -0700 | [diff] [blame] | 97 | splitting_filter_.reset(new SplittingFilter(num_proc_channels_, |
| 98 | num_bands_, |
| 99 | proc_num_frames_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 103 | AudioBuffer::~AudioBuffer() {} |
| 104 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 105 | void AudioBuffer::CopyFrom(const float* const* data, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 106 | const StreamConfig& stream_config) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 107 | RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_); |
| 108 | RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 109 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 110 | // Initialized lazily because there's a different condition in |
| 111 | // DeinterleaveFrom. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 112 | const bool need_to_downmix = |
| 113 | num_input_channels_ > 1 && num_proc_channels_ == 1; |
| 114 | if (need_to_downmix && !input_buffer_) { |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 115 | input_buffer_.reset( |
| 116 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 117 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 118 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 119 | if (stream_config.has_keyboard()) { |
| 120 | keyboard_data_ = data[KeyboardChannelIndex(stream_config)]; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 121 | } |
| 122 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 123 | // Downmix. |
| 124 | const float* const* data_ptr = data; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 125 | if (need_to_downmix) { |
| 126 | DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_, |
| 127 | input_buffer_->fbuf()->channels()[0]); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 128 | data_ptr = input_buffer_->fbuf_const()->channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 132 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 133 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 134 | input_resamplers_[i]->Resample(data_ptr[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 135 | input_num_frames_, |
| 136 | process_buffer_->channels()[i], |
| 137 | proc_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 138 | } |
| 139 | data_ptr = process_buffer_->channels(); |
| 140 | } |
| 141 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 142 | // Convert to the S16 range. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 143 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 144 | FloatToFloatS16(data_ptr[i], |
| 145 | proc_num_frames_, |
| 146 | data_->fbuf()->channels()[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 150 | void AudioBuffer::CopyTo(const StreamConfig& stream_config, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 151 | float* const* data) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 152 | RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_); |
| 153 | RTC_DCHECK(stream_config.num_channels() == num_channels_ || |
| 154 | num_channels_ == 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 155 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 156 | // Convert to the float range. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 157 | float* const* data_ptr = data; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 158 | if (output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 159 | // Convert to an intermediate buffer for subsequent resampling. |
| 160 | data_ptr = process_buffer_->channels(); |
| 161 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 162 | for (size_t i = 0; i < num_channels_; ++i) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 163 | FloatS16ToFloat(data_->fbuf()->channels()[i], |
| 164 | proc_num_frames_, |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 165 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 169 | if (output_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 170 | for (size_t i = 0; i < num_channels_; ++i) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 171 | output_resamplers_[i]->Resample(data_ptr[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 172 | proc_num_frames_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 173 | data[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 174 | output_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 175 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | } |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 177 | |
| 178 | // Upmix. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 179 | for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 180 | memcpy(data[i], data[0], output_num_frames_ * sizeof(**data)); |
| 181 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 184 | void AudioBuffer::InitForNewData() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 185 | keyboard_data_ = NULL; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 186 | mixed_low_pass_valid_ = false; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 187 | reference_copied_ = false; |
| 188 | activity_ = AudioFrame::kVadUnknown; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 189 | num_channels_ = num_proc_channels_; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 190 | data_->set_num_channels(num_proc_channels_); |
| 191 | if (split_data_.get()) { |
| 192 | split_data_->set_num_channels(num_proc_channels_); |
| 193 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 194 | } |
| 195 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 196 | const int16_t* const* AudioBuffer::channels_const() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 197 | return data_->ibuf_const()->channels(); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | int16_t* const* AudioBuffer::channels() { |
| 201 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 202 | return data_->ibuf()->channels(); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 205 | const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 206 | return split_data_.get() ? |
| 207 | split_data_->ibuf_const()->bands(channel) : |
| 208 | data_->ibuf_const()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 211 | int16_t* const* AudioBuffer::split_bands(size_t channel) { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 212 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 213 | return split_data_.get() ? |
| 214 | split_data_->ibuf()->bands(channel) : |
| 215 | data_->ibuf()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | const int16_t* const* AudioBuffer::split_channels_const(Band band) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 219 | if (split_data_.get()) { |
| 220 | return split_data_->ibuf_const()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 221 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 222 | return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | int16_t* const* AudioBuffer::split_channels(Band band) { |
| 227 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 228 | if (split_data_.get()) { |
| 229 | return split_data_->ibuf()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 230 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 231 | return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 235 | ChannelBuffer<int16_t>* AudioBuffer::data() { |
| 236 | mixed_low_pass_valid_ = false; |
| 237 | return data_->ibuf(); |
| 238 | } |
| 239 | |
| 240 | const ChannelBuffer<int16_t>* AudioBuffer::data() const { |
| 241 | return data_->ibuf_const(); |
| 242 | } |
| 243 | |
| 244 | ChannelBuffer<int16_t>* AudioBuffer::split_data() { |
| 245 | mixed_low_pass_valid_ = false; |
| 246 | return split_data_.get() ? split_data_->ibuf() : data_->ibuf(); |
| 247 | } |
| 248 | |
| 249 | const ChannelBuffer<int16_t>* AudioBuffer::split_data() const { |
| 250 | return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const(); |
| 251 | } |
| 252 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 253 | const float* const* AudioBuffer::channels_const_f() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 254 | return data_->fbuf_const()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | float* const* AudioBuffer::channels_f() { |
| 258 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 259 | return data_->fbuf()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 262 | const float* const* AudioBuffer::split_bands_const_f(size_t channel) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 263 | return split_data_.get() ? |
| 264 | split_data_->fbuf_const()->bands(channel) : |
| 265 | data_->fbuf_const()->bands(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 268 | float* const* AudioBuffer::split_bands_f(size_t channel) { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 269 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 270 | return split_data_.get() ? |
| 271 | split_data_->fbuf()->bands(channel) : |
| 272 | data_->fbuf()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | const float* const* AudioBuffer::split_channels_const_f(Band band) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 276 | if (split_data_.get()) { |
| 277 | return split_data_->fbuf_const()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 278 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 279 | return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | float* const* AudioBuffer::split_channels_f(Band band) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 284 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 285 | if (split_data_.get()) { |
| 286 | return split_data_->fbuf()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 287 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 288 | return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 289 | } |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 290 | } |
| 291 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 292 | ChannelBuffer<float>* AudioBuffer::data_f() { |
| 293 | mixed_low_pass_valid_ = false; |
| 294 | return data_->fbuf(); |
| 295 | } |
| 296 | |
| 297 | const ChannelBuffer<float>* AudioBuffer::data_f() const { |
| 298 | return data_->fbuf_const(); |
| 299 | } |
| 300 | |
| 301 | ChannelBuffer<float>* AudioBuffer::split_data_f() { |
| 302 | mixed_low_pass_valid_ = false; |
| 303 | return split_data_.get() ? split_data_->fbuf() : data_->fbuf(); |
| 304 | } |
| 305 | |
| 306 | const ChannelBuffer<float>* AudioBuffer::split_data_f() const { |
| 307 | return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const(); |
| 308 | } |
| 309 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 310 | const int16_t* AudioBuffer::mixed_low_pass_data() { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 311 | if (num_proc_channels_ == 1) { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 312 | return split_bands_const(0)[kBand0To8kHz]; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (!mixed_low_pass_valid_) { |
| 316 | if (!mixed_low_pass_channels_.get()) { |
| 317 | mixed_low_pass_channels_.reset( |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 318 | new ChannelBuffer<int16_t>(num_split_frames_, 1)); |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 319 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 320 | |
| 321 | DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz), |
| 322 | num_split_frames_, num_channels_, |
| 323 | mixed_low_pass_channels_->channels()[0]); |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 324 | mixed_low_pass_valid_ = true; |
| 325 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 326 | return mixed_low_pass_channels_->channels()[0]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 327 | } |
| 328 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 329 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 330 | if (!reference_copied_) { |
| 331 | return NULL; |
| 332 | } |
| 333 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 334 | return low_pass_reference_channels_->channels()[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 335 | } |
| 336 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 337 | const float* AudioBuffer::keyboard_data() const { |
| 338 | return keyboard_data_; |
| 339 | } |
| 340 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 341 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 342 | activity_ = activity; |
| 343 | } |
| 344 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 345 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 346 | return activity_; |
| 347 | } |
| 348 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 349 | size_t AudioBuffer::num_channels() const { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 350 | return num_channels_; |
| 351 | } |
| 352 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 353 | void AudioBuffer::set_num_channels(size_t num_channels) { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 354 | num_channels_ = num_channels; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 355 | data_->set_num_channels(num_channels); |
| 356 | if (split_data_.get()) { |
| 357 | split_data_->set_num_channels(num_channels); |
| 358 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 361 | size_t AudioBuffer::num_frames() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 362 | return proc_num_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 365 | size_t AudioBuffer::num_frames_per_band() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 366 | return num_split_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 369 | size_t AudioBuffer::num_keyboard_frames() const { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 370 | // We don't resample the keyboard channel. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 371 | return input_num_frames_; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 374 | size_t AudioBuffer::num_bands() const { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 375 | return num_bands_; |
| 376 | } |
| 377 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 378 | // The resampler is only for supporting 48kHz to 16kHz in the reverse stream. |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 379 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 380 | RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_); |
| 381 | RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 382 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 383 | // Initialized lazily because there's a different condition in CopyFrom. |
| 384 | if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) { |
| 385 | input_buffer_.reset( |
| 386 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 387 | } |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 388 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 389 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 390 | int16_t* const* deinterleaved; |
| 391 | if (input_num_frames_ == proc_num_frames_) { |
| 392 | deinterleaved = data_->ibuf()->channels(); |
| 393 | } else { |
| 394 | deinterleaved = input_buffer_->ibuf()->channels(); |
| 395 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 396 | if (num_proc_channels_ == 1) { |
| 397 | // Downmix and deinterleave simultaneously. |
| 398 | DownmixInterleavedToMono(frame->data_, input_num_frames_, |
| 399 | num_input_channels_, deinterleaved[0]); |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 400 | } else { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 401 | RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 402 | Deinterleave(frame->data_, |
| 403 | input_num_frames_, |
| 404 | num_proc_channels_, |
| 405 | deinterleaved); |
| 406 | } |
| 407 | |
| 408 | // Resample. |
| 409 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 410 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 411 | input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i], |
| 412 | input_num_frames_, |
| 413 | data_->fbuf()->channels()[i], |
| 414 | proc_num_frames_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 419 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 420 | frame->vad_activity_ = activity_; |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 421 | if (!data_changed) { |
| 422 | return; |
| 423 | } |
| 424 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 425 | RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1); |
| 426 | RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 427 | |
| 428 | // Resample if necessary. |
| 429 | IFChannelBuffer* data_ptr = data_.get(); |
| 430 | if (proc_num_frames_ != output_num_frames_) { |
| 431 | if (!output_buffer_) { |
| 432 | output_buffer_.reset( |
| 433 | new IFChannelBuffer(output_num_frames_, num_channels_)); |
| 434 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 435 | for (size_t i = 0; i < num_channels_; ++i) { |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 436 | output_resamplers_[i]->Resample( |
| 437 | data_->fbuf()->channels()[i], proc_num_frames_, |
| 438 | output_buffer_->fbuf()->channels()[i], output_num_frames_); |
| 439 | } |
| 440 | data_ptr = output_buffer_.get(); |
| 441 | } |
| 442 | |
| 443 | if (frame->num_channels_ == num_channels_) { |
Alejandro Luebs | 40cbec5 | 2016-04-05 17:29:19 -0700 | [diff] [blame] | 444 | Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 445 | frame->data_); |
| 446 | } else { |
Alejandro Luebs | 40cbec5 | 2016-04-05 17:29:19 -0700 | [diff] [blame] | 447 | UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_, |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 448 | frame->num_channels_, frame->data_); |
| 449 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 450 | } |
| 451 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 452 | void AudioBuffer::CopyLowPassToReference() { |
| 453 | reference_copied_ = true; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 454 | if (!low_pass_reference_channels_.get() || |
| 455 | low_pass_reference_channels_->num_channels() != num_channels_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 456 | low_pass_reference_channels_.reset( |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 457 | new ChannelBuffer<int16_t>(num_split_frames_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 458 | num_proc_channels_)); |
| 459 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 460 | for (size_t i = 0; i < num_proc_channels_; i++) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 461 | memcpy(low_pass_reference_channels_->channels()[i], |
| 462 | split_bands_const(i)[kBand0To8kHz], |
| 463 | low_pass_reference_channels_->num_frames_per_band() * |
| 464 | sizeof(split_bands_const(i)[kBand0To8kHz][0])); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 467 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 468 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 469 | splitting_filter_->Analysis(data_.get(), split_data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 473 | splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 474 | } |
| 475 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 476 | } // namespace webrtc |