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