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 | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 22 | const size_t kSamplesPer16kHzChannel = 160; |
| 23 | const size_t kSamplesPer32kHzChannel = 320; |
| 24 | const size_t kSamplesPer48kHzChannel = 480; |
Alejandro Luebs | 5a92aa8 | 2015-04-27 11:34:45 -0700 | [diff] [blame] | 25 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 26 | int KeyboardChannelIndex(const StreamConfig& stream_config) { |
| 27 | if (!stream_config.has_keyboard()) { |
| 28 | assert(false); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 29 | return 0; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 30 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 31 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 32 | return stream_config.num_channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 35 | size_t NumBandsFromSamplesPerChannel(size_t num_frames) { |
| 36 | size_t num_bands = 1; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 37 | if (num_frames == kSamplesPer32kHzChannel || |
| 38 | num_frames == kSamplesPer48kHzChannel) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 39 | num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 40 | } |
| 41 | return num_bands; |
| 42 | } |
| 43 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } // namespace |
| 45 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 46 | AudioBuffer::AudioBuffer(size_t input_num_frames, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 47 | int num_input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 48 | size_t process_num_frames, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 49 | int num_process_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 50 | size_t output_num_frames) |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 51 | : input_num_frames_(input_num_frames), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 52 | num_input_channels_(num_input_channels), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 53 | proc_num_frames_(process_num_frames), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 54 | num_proc_channels_(num_process_channels), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 55 | output_num_frames_(output_num_frames), |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 56 | num_channels_(num_process_channels), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 57 | num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 58 | num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 59 | mixed_low_pass_valid_(false), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 60 | reference_copied_(false), |
| 61 | activity_(AudioFrame::kVadUnknown), |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 62 | keyboard_data_(NULL), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 63 | data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { |
| 64 | assert(input_num_frames_ > 0); |
| 65 | assert(proc_num_frames_ > 0); |
| 66 | assert(output_num_frames_ > 0); |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 67 | assert(num_input_channels_ > 0); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 68 | assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 70 | if (input_num_frames_ != proc_num_frames_ || |
| 71 | output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 72 | // Create an intermediate buffer for resampling. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 73 | process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 74 | num_proc_channels_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 75 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 76 | if (input_num_frames_ != proc_num_frames_) { |
| 77 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 78 | input_resamplers_.push_back( |
| 79 | new PushSincResampler(input_num_frames_, |
| 80 | proc_num_frames_)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (output_num_frames_ != proc_num_frames_) { |
| 85 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 86 | output_resamplers_.push_back( |
| 87 | new PushSincResampler(proc_num_frames_, |
| 88 | output_num_frames_)); |
| 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) { |
| 107 | assert(stream_config.num_frames() == input_num_frames_); |
| 108 | assert(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_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 133 | for (int i = 0; i < num_proc_channels_; ++i) { |
| 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. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 143 | for (int 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) { |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 152 | assert(stream_config.num_frames() == output_num_frames_); |
| 153 | assert(stream_config.num_channels() == num_channels_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 154 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 155 | // Convert to the float range. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 156 | float* const* data_ptr = data; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 157 | if (output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 158 | // Convert to an intermediate buffer for subsequent resampling. |
| 159 | data_ptr = process_buffer_->channels(); |
| 160 | } |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 161 | for (int i = 0; i < num_channels_; ++i) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 162 | FloatS16ToFloat(data_->fbuf()->channels()[i], |
| 163 | proc_num_frames_, |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 164 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 168 | if (output_num_frames_ != proc_num_frames_) { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 169 | for (int i = 0; i < num_channels_; ++i) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 170 | output_resamplers_[i]->Resample(data_ptr[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 171 | proc_num_frames_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 172 | data[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 173 | output_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 174 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 178 | void AudioBuffer::InitForNewData() { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 179 | keyboard_data_ = NULL; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 180 | mixed_low_pass_valid_ = false; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 181 | reference_copied_ = false; |
| 182 | activity_ = AudioFrame::kVadUnknown; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 183 | num_channels_ = num_proc_channels_; |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 184 | } |
| 185 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 186 | const int16_t* const* AudioBuffer::channels_const() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 187 | return data_->ibuf_const()->channels(); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int16_t* const* AudioBuffer::channels() { |
| 191 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 192 | return data_->ibuf()->channels(); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 193 | } |
| 194 | |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 195 | const int16_t* const* AudioBuffer::split_bands_const(int channel) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 196 | return split_data_.get() ? |
| 197 | split_data_->ibuf_const()->bands(channel) : |
| 198 | data_->ibuf_const()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 199 | } |
| 200 | |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 201 | int16_t* const* AudioBuffer::split_bands(int channel) { |
| 202 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 203 | return split_data_.get() ? |
| 204 | split_data_->ibuf()->bands(channel) : |
| 205 | data_->ibuf()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | const int16_t* const* AudioBuffer::split_channels_const(Band band) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 209 | if (split_data_.get()) { |
| 210 | return split_data_->ibuf_const()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 211 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 212 | return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | int16_t* const* AudioBuffer::split_channels(Band band) { |
| 217 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 218 | if (split_data_.get()) { |
| 219 | return split_data_->ibuf()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 220 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 221 | return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 225 | ChannelBuffer<int16_t>* AudioBuffer::data() { |
| 226 | mixed_low_pass_valid_ = false; |
| 227 | return data_->ibuf(); |
| 228 | } |
| 229 | |
| 230 | const ChannelBuffer<int16_t>* AudioBuffer::data() const { |
| 231 | return data_->ibuf_const(); |
| 232 | } |
| 233 | |
| 234 | ChannelBuffer<int16_t>* AudioBuffer::split_data() { |
| 235 | mixed_low_pass_valid_ = false; |
| 236 | return split_data_.get() ? split_data_->ibuf() : data_->ibuf(); |
| 237 | } |
| 238 | |
| 239 | const ChannelBuffer<int16_t>* AudioBuffer::split_data() const { |
| 240 | return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const(); |
| 241 | } |
| 242 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 243 | const float* const* AudioBuffer::channels_const_f() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 244 | return data_->fbuf_const()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | float* const* AudioBuffer::channels_f() { |
| 248 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 249 | return data_->fbuf()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 250 | } |
| 251 | |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 252 | const float* const* AudioBuffer::split_bands_const_f(int channel) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 253 | return split_data_.get() ? |
| 254 | split_data_->fbuf_const()->bands(channel) : |
| 255 | data_->fbuf_const()->bands(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 256 | } |
| 257 | |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 258 | float* const* AudioBuffer::split_bands_f(int channel) { |
| 259 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 260 | return split_data_.get() ? |
| 261 | split_data_->fbuf()->bands(channel) : |
| 262 | data_->fbuf()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | const float* const* AudioBuffer::split_channels_const_f(Band band) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 266 | if (split_data_.get()) { |
| 267 | return split_data_->fbuf_const()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 268 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 269 | return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | float* const* AudioBuffer::split_channels_f(Band band) { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 274 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 275 | if (split_data_.get()) { |
| 276 | return split_data_->fbuf()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 277 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 278 | return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 279 | } |
aluebs@webrtc.org | 087da13 | 2014-11-17 23:01:23 +0000 | [diff] [blame] | 280 | } |
| 281 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 282 | ChannelBuffer<float>* AudioBuffer::data_f() { |
| 283 | mixed_low_pass_valid_ = false; |
| 284 | return data_->fbuf(); |
| 285 | } |
| 286 | |
| 287 | const ChannelBuffer<float>* AudioBuffer::data_f() const { |
| 288 | return data_->fbuf_const(); |
| 289 | } |
| 290 | |
| 291 | ChannelBuffer<float>* AudioBuffer::split_data_f() { |
| 292 | mixed_low_pass_valid_ = false; |
| 293 | return split_data_.get() ? split_data_->fbuf() : data_->fbuf(); |
| 294 | } |
| 295 | |
| 296 | const ChannelBuffer<float>* AudioBuffer::split_data_f() const { |
| 297 | return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const(); |
| 298 | } |
| 299 | |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 300 | const int16_t* AudioBuffer::mixed_low_pass_data() { |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 301 | if (num_proc_channels_ == 1) { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 302 | return split_bands_const(0)[kBand0To8kHz]; |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if (!mixed_low_pass_valid_) { |
| 306 | if (!mixed_low_pass_channels_.get()) { |
| 307 | mixed_low_pass_channels_.reset( |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 308 | new ChannelBuffer<int16_t>(num_split_frames_, 1)); |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 309 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 310 | |
| 311 | DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz), |
| 312 | num_split_frames_, num_channels_, |
| 313 | mixed_low_pass_channels_->channels()[0]); |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 314 | mixed_low_pass_valid_ = true; |
| 315 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 316 | return mixed_low_pass_channels_->channels()[0]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 319 | const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 320 | if (!reference_copied_) { |
| 321 | return NULL; |
| 322 | } |
| 323 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 324 | return low_pass_reference_channels_->channels()[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 325 | } |
| 326 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 327 | const float* AudioBuffer::keyboard_data() const { |
| 328 | return keyboard_data_; |
| 329 | } |
| 330 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 331 | void AudioBuffer::set_activity(AudioFrame::VADActivity activity) { |
| 332 | activity_ = activity; |
| 333 | } |
| 334 | |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 335 | AudioFrame::VADActivity AudioBuffer::activity() const { |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 336 | return activity_; |
| 337 | } |
| 338 | |
| 339 | int AudioBuffer::num_channels() const { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 340 | return num_channels_; |
| 341 | } |
| 342 | |
| 343 | void AudioBuffer::set_num_channels(int num_channels) { |
| 344 | num_channels_ = num_channels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 347 | size_t AudioBuffer::num_frames() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 348 | return proc_num_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 351 | size_t AudioBuffer::num_frames_per_band() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 352 | return num_split_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 355 | size_t AudioBuffer::num_keyboard_frames() const { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 356 | // We don't resample the keyboard channel. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 357 | return input_num_frames_; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 360 | size_t AudioBuffer::num_bands() const { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 361 | return num_bands_; |
| 362 | } |
| 363 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 364 | // 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] | 365 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 366 | assert(frame->num_channels_ == num_input_channels_); |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 367 | assert(frame->samples_per_channel_ == input_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 368 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 369 | // Initialized lazily because there's a different condition in CopyFrom. |
| 370 | if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) { |
| 371 | input_buffer_.reset( |
| 372 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 373 | } |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 374 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 375 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 376 | int16_t* const* deinterleaved; |
| 377 | if (input_num_frames_ == proc_num_frames_) { |
| 378 | deinterleaved = data_->ibuf()->channels(); |
| 379 | } else { |
| 380 | deinterleaved = input_buffer_->ibuf()->channels(); |
| 381 | } |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 382 | if (num_proc_channels_ == 1) { |
| 383 | // Downmix and deinterleave simultaneously. |
| 384 | DownmixInterleavedToMono(frame->data_, input_num_frames_, |
| 385 | num_input_channels_, deinterleaved[0]); |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 386 | } else { |
| 387 | assert(num_proc_channels_ == num_input_channels_); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 388 | Deinterleave(frame->data_, |
| 389 | input_num_frames_, |
| 390 | num_proc_channels_, |
| 391 | deinterleaved); |
| 392 | } |
| 393 | |
| 394 | // Resample. |
| 395 | if (input_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 396 | for (int i = 0; i < num_proc_channels_; ++i) { |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 397 | input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i], |
| 398 | input_num_frames_, |
| 399 | data_->fbuf()->channels()[i], |
| 400 | proc_num_frames_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 405 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 406 | frame->vad_activity_ = activity_; |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 407 | if (!data_changed) { |
| 408 | return; |
| 409 | } |
| 410 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 411 | assert(frame->num_channels_ == num_channels_ || num_channels_ == 1); |
| 412 | assert(frame->samples_per_channel_ == output_num_frames_); |
| 413 | |
| 414 | // Resample if necessary. |
| 415 | IFChannelBuffer* data_ptr = data_.get(); |
| 416 | if (proc_num_frames_ != output_num_frames_) { |
| 417 | if (!output_buffer_) { |
| 418 | output_buffer_.reset( |
| 419 | new IFChannelBuffer(output_num_frames_, num_channels_)); |
| 420 | } |
| 421 | for (int i = 0; i < num_channels_; ++i) { |
| 422 | output_resamplers_[i]->Resample( |
| 423 | data_->fbuf()->channels()[i], proc_num_frames_, |
| 424 | output_buffer_->fbuf()->channels()[i], output_num_frames_); |
| 425 | } |
| 426 | data_ptr = output_buffer_.get(); |
| 427 | } |
| 428 | |
| 429 | if (frame->num_channels_ == num_channels_) { |
| 430 | Interleave(data_ptr->ibuf()->channels(), proc_num_frames_, num_channels_, |
| 431 | frame->data_); |
| 432 | } else { |
| 433 | UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], proc_num_frames_, |
| 434 | frame->num_channels_, frame->data_); |
| 435 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 436 | } |
| 437 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | void AudioBuffer::CopyLowPassToReference() { |
| 439 | reference_copied_ = true; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 440 | if (!low_pass_reference_channels_.get() || |
| 441 | low_pass_reference_channels_->num_channels() != num_channels_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 442 | low_pass_reference_channels_.reset( |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 443 | new ChannelBuffer<int16_t>(num_split_frames_, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 444 | num_proc_channels_)); |
| 445 | } |
| 446 | for (int i = 0; i < num_proc_channels_; i++) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 447 | memcpy(low_pass_reference_channels_->channels()[i], |
| 448 | split_bands_const(i)[kBand0To8kHz], |
| 449 | low_pass_reference_channels_->num_frames_per_band() * |
| 450 | sizeof(split_bands_const(i)[kBand0To8kHz][0])); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 453 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 454 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 455 | splitting_filter_->Analysis(data_.get(), split_data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 459 | splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 460 | } |
| 461 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 462 | } // namespace webrtc |