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