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