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