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