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 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 26 | constexpr size_t kSamplesPer32kHzChannel = 320; |
| 27 | constexpr size_t kSamplesPer48kHzChannel = 480; |
Per Åhgren | 3e8bf28 | 2019-08-29 23:38:40 +0200 | [diff] [blame] | 28 | constexpr size_t kMaxSamplesPerChannel = AudioBuffer::kMaxSampleRate / 100; |
Alejandro Luebs | 5a92aa8 | 2015-04-27 11:34:45 -0700 | [diff] [blame] | 29 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 30 | size_t NumBandsFromFramesPerChannel(size_t num_frames) { |
| 31 | if (num_frames == kSamplesPer32kHzChannel) { |
| 32 | return 2; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 33 | } |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 34 | if (num_frames == kSamplesPer48kHzChannel) { |
| 35 | return 3; |
| 36 | } |
| 37 | return 1; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 38 | } |
| 39 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } // namespace |
| 41 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 42 | AudioBuffer::AudioBuffer(size_t input_rate, |
| 43 | size_t input_num_channels, |
| 44 | size_t buffer_rate, |
| 45 | size_t buffer_num_channels, |
| 46 | size_t output_rate, |
| 47 | size_t output_num_channels) |
Per Åhgren | 2ca0b36 | 2019-08-26 11:21:16 +0200 | [diff] [blame] | 48 | : AudioBuffer(static_cast<int>(input_rate) / 100, |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 49 | input_num_channels, |
Per Åhgren | 2ca0b36 | 2019-08-26 11:21:16 +0200 | [diff] [blame] | 50 | static_cast<int>(buffer_rate) / 100, |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 51 | buffer_num_channels, |
Per Åhgren | 2ca0b36 | 2019-08-26 11:21:16 +0200 | [diff] [blame] | 52 | static_cast<int>(output_rate) / 100) {} |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 53 | |
Steve Anton | f254e9e | 2019-08-21 17:52:28 +0000 | [diff] [blame] | 54 | AudioBuffer::AudioBuffer(size_t input_num_frames, |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 55 | size_t input_num_channels, |
| 56 | size_t buffer_num_frames, |
| 57 | size_t buffer_num_channels, |
Steve Anton | f254e9e | 2019-08-21 17:52:28 +0000 | [diff] [blame] | 58 | size_t output_num_frames) |
| 59 | : input_num_frames_(input_num_frames), |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 60 | input_num_channels_(input_num_channels), |
| 61 | buffer_num_frames_(buffer_num_frames), |
| 62 | buffer_num_channels_(buffer_num_channels), |
Steve Anton | f254e9e | 2019-08-21 17:52:28 +0000 | [diff] [blame] | 63 | output_num_frames_(output_num_frames), |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 64 | output_num_channels_(0), |
| 65 | num_channels_(buffer_num_channels), |
| 66 | num_bands_(NumBandsFromFramesPerChannel(buffer_num_frames_)), |
| 67 | num_split_frames_(rtc::CheckedDivExact(buffer_num_frames_, num_bands_)), |
Gustaf Ullberg | 422b9e0 | 2019-10-09 13:02:14 +0200 | [diff] [blame] | 68 | data_( |
| 69 | new ChannelBuffer<float>(buffer_num_frames_, buffer_num_channels_)) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 70 | RTC_DCHECK_GT(input_num_frames_, 0); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 71 | RTC_DCHECK_GT(buffer_num_frames_, 0); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 72 | RTC_DCHECK_GT(output_num_frames_, 0); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 73 | RTC_DCHECK_GT(input_num_channels_, 0); |
| 74 | RTC_DCHECK_GT(buffer_num_channels_, 0); |
| 75 | RTC_DCHECK_LE(buffer_num_channels_, input_num_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 77 | const bool input_resampling_needed = input_num_frames_ != buffer_num_frames_; |
| 78 | const bool output_resampling_needed = |
| 79 | output_num_frames_ != buffer_num_frames_; |
| 80 | if (input_resampling_needed) { |
| 81 | for (size_t i = 0; i < buffer_num_channels_; ++i) { |
| 82 | input_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 83 | new PushSincResampler(input_num_frames_, buffer_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 84 | } |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 85 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 86 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 87 | if (output_resampling_needed) { |
| 88 | for (size_t i = 0; i < buffer_num_channels_; ++i) { |
| 89 | output_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 90 | new PushSincResampler(buffer_num_frames_, output_num_frames_))); |
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) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 95 | split_data_.reset(new ChannelBuffer<float>( |
| 96 | buffer_num_frames_, buffer_num_channels_, num_bands_)); |
| 97 | splitting_filter_.reset(new SplittingFilter( |
| 98 | buffer_num_channels_, num_bands_, buffer_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 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 104 | void AudioBuffer::set_downmixing_to_specific_channel(size_t channel) { |
| 105 | downmix_by_averaging_ = false; |
| 106 | RTC_DCHECK_GT(input_num_channels_, channel); |
| 107 | channel_for_downmixing_ = std::min(channel, input_num_channels_ - 1); |
| 108 | } |
| 109 | |
| 110 | void AudioBuffer::set_downmixing_by_averaging() { |
| 111 | downmix_by_averaging_ = true; |
| 112 | } |
| 113 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 114 | void AudioBuffer::CopyFrom(const float* const* data, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 115 | const StreamConfig& stream_config) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 116 | RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 117 | RTC_DCHECK_EQ(stream_config.num_channels(), input_num_channels_); |
| 118 | RestoreNumChannels(); |
| 119 | const bool downmix_needed = input_num_channels_ > 1 && num_channels_ == 1; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 120 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 121 | const bool resampling_needed = input_num_frames_ != buffer_num_frames_; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 122 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 123 | if (downmix_needed) { |
Per Åhgren | 3e8bf28 | 2019-08-29 23:38:40 +0200 | [diff] [blame] | 124 | RTC_DCHECK_GE(kMaxSamplesPerChannel, input_num_frames_); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 125 | |
| 126 | std::array<float, kMaxSamplesPerChannel> downmix; |
| 127 | if (downmix_by_averaging_) { |
| 128 | const float kOneByNumChannels = 1.f / input_num_channels_; |
| 129 | for (size_t i = 0; i < input_num_frames_; ++i) { |
| 130 | float value = data[0][i]; |
| 131 | for (size_t j = 1; j < input_num_channels_; ++j) { |
| 132 | value += data[j][i]; |
| 133 | } |
| 134 | downmix[i] = value * kOneByNumChannels; |
| 135 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 136 | } |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 137 | const float* downmixed_data = |
| 138 | downmix_by_averaging_ ? downmix.data() : data[channel_for_downmixing_]; |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 139 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 140 | if (resampling_needed) { |
| 141 | input_resamplers_[0]->Resample(downmixed_data, input_num_frames_, |
| 142 | data_->channels()[0], buffer_num_frames_); |
| 143 | } |
| 144 | const float* data_to_convert = |
| 145 | resampling_needed ? data_->channels()[0] : downmixed_data; |
| 146 | FloatToFloatS16(data_to_convert, buffer_num_frames_, data_->channels()[0]); |
| 147 | } else { |
| 148 | if (resampling_needed) { |
| 149 | for (size_t i = 0; i < num_channels_; ++i) { |
| 150 | input_resamplers_[i]->Resample(data[i], input_num_frames_, |
| 151 | data_->channels()[i], |
| 152 | buffer_num_frames_); |
| 153 | FloatToFloatS16(data_->channels()[i], buffer_num_frames_, |
| 154 | data_->channels()[i]); |
| 155 | } |
| 156 | } else { |
| 157 | for (size_t i = 0; i < num_channels_; ++i) { |
| 158 | FloatToFloatS16(data[i], buffer_num_frames_, data_->channels()[i]); |
| 159 | } |
| 160 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 164 | void AudioBuffer::CopyTo(const StreamConfig& stream_config, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 165 | float* const* data) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 166 | RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 167 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 168 | const bool resampling_needed = output_num_frames_ != buffer_num_frames_; |
| 169 | if (resampling_needed) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 170 | for (size_t i = 0; i < num_channels_; ++i) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 171 | FloatS16ToFloat(data_->channels()[i], buffer_num_frames_, |
| 172 | data_->channels()[i]); |
| 173 | output_resamplers_[i]->Resample(data_->channels()[i], buffer_num_frames_, |
| 174 | data[i], output_num_frames_); |
| 175 | } |
| 176 | } else { |
| 177 | for (size_t i = 0; i < num_channels_; ++i) { |
| 178 | FloatS16ToFloat(data_->channels()[i], buffer_num_frames_, data[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 179 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | } |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 181 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 182 | for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 183 | memcpy(data[i], data[0], output_num_frames_ * sizeof(**data)); |
| 184 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Gustaf Ullberg | 422b9e0 | 2019-10-09 13:02:14 +0200 | [diff] [blame] | 187 | void AudioBuffer::CopyTo(AudioBuffer* buffer) const { |
| 188 | RTC_DCHECK_EQ(buffer->num_frames(), output_num_frames_); |
| 189 | |
| 190 | const bool resampling_needed = output_num_frames_ != buffer_num_frames_; |
| 191 | if (resampling_needed) { |
| 192 | for (size_t i = 0; i < num_channels_; ++i) { |
| 193 | output_resamplers_[i]->Resample(data_->channels()[i], buffer_num_frames_, |
| 194 | buffer->channels()[i], |
| 195 | buffer->num_frames()); |
| 196 | } |
| 197 | } else { |
| 198 | for (size_t i = 0; i < num_channels_; ++i) { |
| 199 | memcpy(buffer->channels()[i], data_->channels()[i], |
| 200 | buffer_num_frames_ * sizeof(**buffer->channels())); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | for (size_t i = num_channels_; i < buffer->num_channels(); ++i) { |
| 205 | memcpy(buffer->channels()[i], buffer->channels()[0], |
| 206 | output_num_frames_ * sizeof(**buffer->channels())); |
| 207 | } |
| 208 | } |
| 209 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 210 | void AudioBuffer::RestoreNumChannels() { |
| 211 | num_channels_ = buffer_num_channels_; |
| 212 | data_->set_num_channels(buffer_num_channels_); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 213 | if (split_data_.get()) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 214 | split_data_->set_num_channels(buffer_num_channels_); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 215 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 218 | void AudioBuffer::set_num_channels(size_t num_channels) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 219 | RTC_DCHECK_GE(buffer_num_channels_, num_channels); |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 220 | num_channels_ = num_channels; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 221 | data_->set_num_channels(num_channels); |
| 222 | if (split_data_.get()) { |
| 223 | split_data_->set_num_channels(num_channels); |
| 224 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 227 | // The resampler is only for supporting 48kHz to 16kHz in the reverse stream. |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 228 | void AudioBuffer::CopyFrom(const AudioFrame* frame) { |
| 229 | RTC_DCHECK_EQ(frame->num_channels_, input_num_channels_); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 230 | RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 231 | RestoreNumChannels(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 233 | const bool resampling_required = input_num_frames_ != buffer_num_frames_; |
Per Åhgren | 81c0cf2 | 2019-08-21 15:02:37 +0200 | [diff] [blame] | 234 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 235 | const int16_t* interleaved = frame->data(); |
| 236 | if (num_channels_ == 1) { |
| 237 | if (input_num_channels_ == 1) { |
| 238 | if (resampling_required) { |
| 239 | std::array<float, kMaxSamplesPerChannel> float_buffer; |
| 240 | S16ToFloatS16(interleaved, input_num_frames_, float_buffer.data()); |
| 241 | input_resamplers_[0]->Resample(float_buffer.data(), input_num_frames_, |
| 242 | data_->channels()[0], |
| 243 | buffer_num_frames_); |
| 244 | } else { |
| 245 | S16ToFloatS16(interleaved, input_num_frames_, data_->channels()[0]); |
| 246 | } |
| 247 | } else { |
| 248 | std::array<float, kMaxSamplesPerChannel> float_buffer; |
| 249 | float* downmixed_data = |
| 250 | resampling_required ? float_buffer.data() : data_->channels()[0]; |
| 251 | if (downmix_by_averaging_) { |
| 252 | for (size_t j = 0, k = 0; j < input_num_frames_; ++j) { |
| 253 | int32_t sum = 0; |
| 254 | for (size_t i = 0; i < input_num_channels_; ++i, ++k) { |
| 255 | sum += interleaved[k]; |
| 256 | } |
| 257 | downmixed_data[j] = sum / static_cast<int16_t>(input_num_channels_); |
| 258 | } |
| 259 | } else { |
| 260 | for (size_t j = 0, k = channel_for_downmixing_; j < input_num_frames_; |
| 261 | ++j, k += input_num_channels_) { |
| 262 | downmixed_data[j] = interleaved[k]; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (resampling_required) { |
| 267 | input_resamplers_[0]->Resample(downmixed_data, input_num_frames_, |
| 268 | data_->channels()[0], |
| 269 | buffer_num_frames_); |
| 270 | } |
| 271 | } |
| 272 | } else { |
| 273 | auto deinterleave_channel = [](size_t channel, size_t num_channels, |
| 274 | size_t samples_per_channel, const int16_t* x, |
| 275 | float* y) { |
| 276 | for (size_t j = 0, k = channel; j < samples_per_channel; |
| 277 | ++j, k += num_channels) { |
| 278 | y[j] = x[k]; |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | if (resampling_required) { |
| 283 | std::array<float, kMaxSamplesPerChannel> float_buffer; |
| 284 | for (size_t i = 0; i < num_channels_; ++i) { |
| 285 | deinterleave_channel(i, num_channels_, input_num_frames_, interleaved, |
| 286 | float_buffer.data()); |
| 287 | input_resamplers_[i]->Resample(float_buffer.data(), input_num_frames_, |
| 288 | data_->channels()[i], |
| 289 | buffer_num_frames_); |
| 290 | } |
| 291 | } else { |
| 292 | for (size_t i = 0; i < num_channels_; ++i) { |
| 293 | deinterleave_channel(i, num_channels_, input_num_frames_, interleaved, |
| 294 | data_->channels()[i]); |
| 295 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 300 | void AudioBuffer::CopyTo(AudioFrame* frame) const { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 301 | RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1); |
| 302 | RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 303 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 304 | const bool resampling_required = buffer_num_frames_ != output_num_frames_; |
Per Åhgren | 81c0cf2 | 2019-08-21 15:02:37 +0200 | [diff] [blame] | 305 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 306 | int16_t* interleaved = frame->mutable_data(); |
| 307 | if (num_channels_ == 1) { |
| 308 | std::array<float, kMaxSamplesPerChannel> float_buffer; |
| 309 | |
| 310 | if (resampling_required) { |
| 311 | output_resamplers_[0]->Resample(data_->channels()[0], buffer_num_frames_, |
| 312 | float_buffer.data(), output_num_frames_); |
| 313 | } |
| 314 | const float* deinterleaved = |
| 315 | resampling_required ? float_buffer.data() : data_->channels()[0]; |
| 316 | |
| 317 | if (frame->num_channels_ == 1) { |
| 318 | for (size_t j = 0; j < output_num_frames_; ++j) { |
| 319 | interleaved[j] = FloatS16ToS16(deinterleaved[j]); |
| 320 | } |
| 321 | } else { |
| 322 | for (size_t i = 0, k = 0; i < output_num_frames_; ++i) { |
| 323 | float tmp = FloatS16ToS16(deinterleaved[i]); |
| 324 | for (size_t j = 0; j < frame->num_channels_; ++j, ++k) { |
| 325 | interleaved[k] = tmp; |
| 326 | } |
| 327 | } |
| 328 | } |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 329 | } else { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 330 | auto interleave_channel = [](size_t channel, size_t num_channels, |
| 331 | size_t samples_per_channel, const float* x, |
| 332 | int16_t* y) { |
| 333 | for (size_t k = 0, j = channel; k < samples_per_channel; |
| 334 | ++k, j += num_channels) { |
| 335 | y[j] = FloatS16ToS16(x[k]); |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | if (resampling_required) { |
| 340 | for (size_t i = 0; i < num_channels_; ++i) { |
| 341 | std::array<float, kMaxSamplesPerChannel> float_buffer; |
| 342 | output_resamplers_[i]->Resample(data_->channels()[i], |
| 343 | buffer_num_frames_, float_buffer.data(), |
| 344 | output_num_frames_); |
| 345 | interleave_channel(i, frame->num_channels_, output_num_frames_, |
| 346 | float_buffer.data(), interleaved); |
| 347 | } |
| 348 | } else { |
| 349 | for (size_t i = 0; i < num_channels_; ++i) { |
| 350 | interleave_channel(i, frame->num_channels_, output_num_frames_, |
| 351 | data_->channels()[i], interleaved); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | for (size_t i = num_channels_; i < frame->num_channels_; ++i) { |
| 356 | for (size_t j = 0, k = i, n = num_channels_; j < output_num_frames_; |
| 357 | ++j, k += frame->num_channels_, n += frame->num_channels_) { |
| 358 | interleaved[k] = interleaved[n]; |
| 359 | } |
| 360 | } |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 361 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 362 | } |
| 363 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 364 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 365 | splitting_filter_->Analysis(data_.get(), split_data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 369 | splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 372 | void AudioBuffer::ExportSplitChannelData(size_t channel, |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 373 | int16_t* const* split_band_data) { |
| 374 | for (size_t k = 0; k < num_bands(); ++k) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 375 | const float* band_data = split_bands(channel)[k]; |
| 376 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 377 | RTC_DCHECK(split_band_data[k]); |
| 378 | RTC_DCHECK(band_data); |
| 379 | for (size_t i = 0; i < num_frames_per_band(); ++i) { |
| 380 | split_band_data[k][i] = FloatS16ToS16(band_data[i]); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 385 | void AudioBuffer::ImportSplitChannelData( |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 386 | size_t channel, |
| 387 | const int16_t* const* split_band_data) { |
| 388 | for (size_t k = 0; k < num_bands(); ++k) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 389 | float* band_data = split_bands(channel)[k]; |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 390 | RTC_DCHECK(split_band_data[k]); |
| 391 | RTC_DCHECK(band_data); |
| 392 | for (size_t i = 0; i < num_frames_per_band(); ++i) { |
| 393 | band_data[i] = split_band_data[k][i]; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 398 | } // namespace webrtc |