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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "common_audio/channel_buffer.h" |
| 14 | #include "common_audio/include/audio_util.h" |
| 15 | #include "common_audio/resampler/push_sinc_resampler.h" |
| 16 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 17 | #include "modules/audio_processing/common.h" |
| 18 | #include "rtc_base/checks.h" |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | namespace { |
| 22 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 23 | const size_t kSamplesPer16kHzChannel = 160; |
| 24 | const size_t kSamplesPer32kHzChannel = 320; |
| 25 | const size_t kSamplesPer48kHzChannel = 480; |
Alejandro Luebs | 5a92aa8 | 2015-04-27 11:34:45 -0700 | [diff] [blame] | 26 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 27 | int KeyboardChannelIndex(const StreamConfig& stream_config) { |
| 28 | if (!stream_config.has_keyboard()) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 29 | RTC_NOTREACHED(); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 30 | return 0; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 31 | } |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 32 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 33 | return stream_config.num_channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 36 | size_t NumBandsFromSamplesPerChannel(size_t num_frames) { |
| 37 | size_t num_bands = 1; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 38 | if (num_frames == kSamplesPer32kHzChannel || |
| 39 | num_frames == kSamplesPer48kHzChannel) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 40 | num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 41 | } |
| 42 | return num_bands; |
| 43 | } |
| 44 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } // namespace |
| 46 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 47 | AudioBuffer::AudioBuffer(size_t input_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 48 | size_t num_input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 49 | size_t process_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 50 | size_t num_process_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | size_t output_num_frames) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 52 | : input_num_frames_(input_num_frames), |
| 53 | num_input_channels_(num_input_channels), |
| 54 | proc_num_frames_(process_num_frames), |
| 55 | num_proc_channels_(num_process_channels), |
| 56 | output_num_frames_(output_num_frames), |
| 57 | num_channels_(num_process_channels), |
| 58 | num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
| 59 | num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
| 60 | mixed_low_pass_valid_(false), |
| 61 | reference_copied_(false), |
| 62 | activity_(AudioFrame::kVadUnknown), |
| 63 | keyboard_data_(NULL), |
| 64 | data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)), |
| 65 | output_buffer_(new IFChannelBuffer(output_num_frames_, num_channels_)) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 66 | RTC_DCHECK_GT(input_num_frames_, 0); |
| 67 | RTC_DCHECK_GT(proc_num_frames_, 0); |
| 68 | RTC_DCHECK_GT(output_num_frames_, 0); |
| 69 | RTC_DCHECK_GT(num_input_channels_, 0); |
| 70 | RTC_DCHECK_GT(num_proc_channels_, 0); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 71 | RTC_DCHECK_LE(num_proc_channels_, num_input_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 73 | if (input_num_frames_ != proc_num_frames_ || |
| 74 | output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 75 | // Create an intermediate buffer for resampling. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 76 | process_buffer_.reset( |
| 77 | new ChannelBuffer<float>(proc_num_frames_, num_proc_channels_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 78 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 79 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 80 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 81 | input_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 82 | new PushSincResampler(input_num_frames_, proc_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | if (output_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 87 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 88 | output_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 89 | new PushSincResampler(proc_num_frames_, output_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 90 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 94 | if (num_bands_ > 1) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 95 | split_data_.reset( |
| 96 | new IFChannelBuffer(proc_num_frames_, num_proc_channels_, num_bands_)); |
| 97 | splitting_filter_.reset( |
| 98 | new SplittingFilter(num_proc_channels_, num_bands_, proc_num_frames_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 102 | AudioBuffer::~AudioBuffer() {} |
| 103 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 104 | void AudioBuffer::CopyFrom(const float* const* data, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 105 | const StreamConfig& stream_config) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 106 | RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_); |
| 107 | RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 108 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 109 | // Initialized lazily because there's a different condition in |
| 110 | // DeinterleaveFrom. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 111 | const bool need_to_downmix = |
| 112 | num_input_channels_ > 1 && num_proc_channels_ == 1; |
| 113 | if (need_to_downmix && !input_buffer_) { |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 114 | input_buffer_.reset( |
| 115 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 116 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 117 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 118 | if (stream_config.has_keyboard()) { |
| 119 | keyboard_data_ = data[KeyboardChannelIndex(stream_config)]; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 120 | } |
| 121 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 122 | // Downmix. |
| 123 | const float* const* data_ptr = data; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 124 | if (need_to_downmix) { |
| 125 | DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_, |
| 126 | input_buffer_->fbuf()->channels()[0]); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 127 | data_ptr = input_buffer_->fbuf_const()->channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 131 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 132 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 133 | input_resamplers_[i]->Resample(data_ptr[i], input_num_frames_, |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 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) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 142 | FloatToFloatS16(data_ptr[i], proc_num_frames_, |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 143 | data_->fbuf()->channels()[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 147 | void AudioBuffer::CopyTo(const StreamConfig& stream_config, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 148 | float* const* data) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 149 | RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_); |
| 150 | RTC_DCHECK(stream_config.num_channels() == num_channels_ || |
| 151 | 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) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 160 | FloatS16ToFloat(data_->fbuf()->channels()[i], proc_num_frames_, |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 161 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 165 | if (output_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 166 | for (size_t i = 0; i < num_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 167 | output_resamplers_[i]->Resample(data_ptr[i], proc_num_frames_, data[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 168 | output_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 169 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | } |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 171 | |
| 172 | // Upmix. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 173 | for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 174 | memcpy(data[i], data[0], output_num_frames_ * sizeof(**data)); |
| 175 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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_; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 184 | data_->set_num_channels(num_proc_channels_); |
| 185 | if (split_data_.get()) { |
| 186 | split_data_->set_num_channels(num_proc_channels_); |
| 187 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 188 | } |
| 189 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 190 | const int16_t* const* AudioBuffer::channels_const() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 191 | return data_->ibuf_const()->channels(); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | int16_t* const* AudioBuffer::channels() { |
| 195 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 196 | return data_->ibuf()->channels(); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 199 | const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 200 | return split_data_.get() ? split_data_->ibuf_const()->bands(channel) |
| 201 | : data_->ibuf_const()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 204 | int16_t* const* AudioBuffer::split_bands(size_t channel) { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 205 | mixed_low_pass_valid_ = false; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 206 | return split_data_.get() ? split_data_->ibuf()->bands(channel) |
| 207 | : data_->ibuf()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | const int16_t* const* AudioBuffer::split_channels_const(Band band) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 211 | if (split_data_.get()) { |
| 212 | return split_data_->ibuf_const()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 213 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 214 | return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | int16_t* const* AudioBuffer::split_channels(Band band) { |
| 219 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 220 | if (split_data_.get()) { |
| 221 | return split_data_->ibuf()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 222 | } else { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 223 | return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
aluebs@webrtc.org | 3aca0b0 | 2015-02-26 21:52:20 +0000 | [diff] [blame] | 227 | ChannelBuffer<int16_t>* AudioBuffer::data() { |
| 228 | mixed_low_pass_valid_ = false; |
| 229 | return data_->ibuf(); |
| 230 | } |
| 231 | |
| 232 | const ChannelBuffer<int16_t>* AudioBuffer::data() const { |
| 233 | return data_->ibuf_const(); |
| 234 | } |
| 235 | |
| 236 | ChannelBuffer<int16_t>* AudioBuffer::split_data() { |
| 237 | mixed_low_pass_valid_ = false; |
| 238 | return split_data_.get() ? split_data_->ibuf() : data_->ibuf(); |
| 239 | } |
| 240 | |
| 241 | const ChannelBuffer<int16_t>* AudioBuffer::split_data() const { |
| 242 | return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const(); |
| 243 | } |
| 244 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 245 | const float* const* AudioBuffer::channels_const_f() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 246 | return data_->fbuf_const()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | float* const* AudioBuffer::channels_f() { |
| 250 | mixed_low_pass_valid_ = false; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 251 | return data_->fbuf()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 254 | const float* const* AudioBuffer::split_bands_const_f(size_t channel) const { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 255 | return split_data_.get() ? split_data_->fbuf_const()->bands(channel) |
| 256 | : data_->fbuf_const()->bands(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 259 | float* const* AudioBuffer::split_bands_f(size_t channel) { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 260 | mixed_low_pass_valid_ = false; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 261 | return split_data_.get() ? 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 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 339 | size_t AudioBuffer::num_channels() const { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 340 | return num_channels_; |
| 341 | } |
| 342 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 343 | void AudioBuffer::set_num_channels(size_t num_channels) { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 344 | num_channels_ = num_channels; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 345 | data_->set_num_channels(num_channels); |
| 346 | if (split_data_.get()) { |
| 347 | split_data_->set_num_channels(num_channels); |
| 348 | } |
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() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 352 | return proc_num_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_frames_per_band() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 356 | return num_split_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 359 | size_t AudioBuffer::num_keyboard_frames() const { |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 360 | // We don't resample the keyboard channel. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 361 | return input_num_frames_; |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 364 | size_t AudioBuffer::num_bands() const { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 365 | return num_bands_; |
| 366 | } |
| 367 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 368 | // 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] | 369 | void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 370 | RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_); |
| 371 | RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 372 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 373 | // Initialized lazily because there's a different condition in CopyFrom. |
| 374 | if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) { |
| 375 | input_buffer_.reset( |
| 376 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 377 | } |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 378 | activity_ = frame->vad_activity_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 379 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 380 | int16_t* const* deinterleaved; |
| 381 | if (input_num_frames_ == proc_num_frames_) { |
| 382 | deinterleaved = data_->ibuf()->channels(); |
| 383 | } else { |
| 384 | deinterleaved = input_buffer_->ibuf()->channels(); |
| 385 | } |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 386 | // TODO(yujo): handle muted frames more efficiently. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 387 | if (num_proc_channels_ == 1) { |
| 388 | // Downmix and deinterleave simultaneously. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 389 | DownmixInterleavedToMono(frame->data(), input_num_frames_, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 390 | num_input_channels_, deinterleaved[0]); |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 391 | } else { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 392 | RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 393 | Deinterleave(frame->data(), input_num_frames_, num_proc_channels_, |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 394 | deinterleaved); |
| 395 | } |
| 396 | |
| 397 | // Resample. |
| 398 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 399 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 400 | input_resamplers_[i]->Resample( |
| 401 | input_buffer_->fbuf_const()->channels()[i], input_num_frames_, |
| 402 | data_->fbuf()->channels()[i], proc_num_frames_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
kthelgason | c7daea8 | 2017-03-14 03:10:07 -0700 | [diff] [blame] | 407 | void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 408 | frame->vad_activity_ = activity_; |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 409 | if (!data_changed) { |
| 410 | return; |
| 411 | } |
| 412 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 413 | RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1); |
| 414 | RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 415 | |
| 416 | // Resample if necessary. |
| 417 | IFChannelBuffer* data_ptr = data_.get(); |
| 418 | if (proc_num_frames_ != output_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 419 | for (size_t i = 0; i < num_channels_; ++i) { |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 420 | output_resamplers_[i]->Resample( |
| 421 | data_->fbuf()->channels()[i], proc_num_frames_, |
| 422 | output_buffer_->fbuf()->channels()[i], output_num_frames_); |
| 423 | } |
| 424 | data_ptr = output_buffer_.get(); |
| 425 | } |
| 426 | |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 427 | // TODO(yujo): handle muted frames more efficiently. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 428 | if (frame->num_channels_ == num_channels_) { |
Alejandro Luebs | 40cbec5 | 2016-04-05 17:29:19 -0700 | [diff] [blame] | 429 | Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 430 | frame->mutable_data()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 431 | } else { |
Alejandro Luebs | 40cbec5 | 2016-04-05 17:29:19 -0700 | [diff] [blame] | 432 | UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 433 | frame->num_channels_, frame->mutable_data()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 434 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 435 | } |
| 436 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 437 | void AudioBuffer::CopyLowPassToReference() { |
| 438 | reference_copied_ = true; |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 439 | if (!low_pass_reference_channels_.get() || |
| 440 | low_pass_reference_channels_->num_channels() != num_channels_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 441 | low_pass_reference_channels_.reset( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 442 | new ChannelBuffer<int16_t>(num_split_frames_, num_proc_channels_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 443 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 444 | for (size_t i = 0; i < num_proc_channels_; i++) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 445 | memcpy(low_pass_reference_channels_->channels()[i], |
| 446 | split_bands_const(i)[kBand0To8kHz], |
| 447 | low_pass_reference_channels_->num_frames_per_band() * |
| 448 | sizeof(split_bands_const(i)[kBand0To8kHz][0])); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 451 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 452 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 453 | splitting_filter_->Analysis(data_.get(), split_data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 457 | splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 458 | } |
| 459 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 460 | } // namespace webrtc |