blob: 584111c29acffad9777b6224a79ae579389d753e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/channel_buffer.h"
18#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/push_sinc_resampler.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/audio_processing/splitting_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
24namespace {
25
Peter Kastingdce40cf2015-08-24 14:52:23 -070026const size_t kSamplesPer16kHzChannel = 160;
27const size_t kSamplesPer32kHzChannel = 320;
28const size_t kSamplesPer48kHzChannel = 480;
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070029
Peter Kastingdce40cf2015-08-24 14:52:23 -070030size_t NumBandsFromSamplesPerChannel(size_t num_frames) {
31 size_t num_bands = 1;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000032 if (num_frames == kSamplesPer32kHzChannel ||
33 num_frames == kSamplesPer48kHzChannel) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070034 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000035 }
36 return num_bands;
37}
38
niklase@google.com470e71d2011-07-07 08:21:25 +000039} // namespace
40
Peter Kastingdce40cf2015-08-24 14:52:23 -070041AudioBuffer::AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080042 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t process_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080044 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070045 size_t output_num_frames)
Yves Gerey665174f2018-06-19 15:03:05 +020046 : input_num_frames_(input_num_frames),
47 num_input_channels_(num_input_channels),
48 proc_num_frames_(process_num_frames),
49 num_proc_channels_(num_process_channels),
50 output_num_frames_(output_num_frames),
51 num_channels_(num_process_channels),
52 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
53 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
Yves Gerey665174f2018-06-19 15:03:05 +020054 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)),
55 output_buffer_(new IFChannelBuffer(output_num_frames_, num_channels_)) {
kwibergaf476c72016-11-28 15:21:39 -080056 RTC_DCHECK_GT(input_num_frames_, 0);
57 RTC_DCHECK_GT(proc_num_frames_, 0);
58 RTC_DCHECK_GT(output_num_frames_, 0);
59 RTC_DCHECK_GT(num_input_channels_, 0);
60 RTC_DCHECK_GT(num_proc_channels_, 0);
kwiberg9e2be5f2016-09-14 05:23:22 -070061 RTC_DCHECK_LE(num_proc_channels_, num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000063 if (input_num_frames_ != proc_num_frames_ ||
64 output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000065 // Create an intermediate buffer for resampling.
Yves Gerey665174f2018-06-19 15:03:05 +020066 process_buffer_.reset(
67 new ChannelBuffer<float>(proc_num_frames_, num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000068
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000069 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080070 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070071 input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
72 new PushSincResampler(input_num_frames_, proc_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000073 }
74 }
75
76 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080077 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070078 output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
79 new PushSincResampler(proc_num_frames_, output_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 }
82 }
83
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 if (num_bands_ > 1) {
Yves Gerey665174f2018-06-19 15:03:05 +020085 split_data_.reset(
86 new IFChannelBuffer(proc_num_frames_, num_proc_channels_, num_bands_));
87 splitting_filter_.reset(
88 new SplittingFilter(num_proc_channels_, num_bands_, proc_num_frames_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000089 }
90}
91
andrew@webrtc.org103657b2014-04-24 18:28:56 +000092AudioBuffer::~AudioBuffer() {}
93
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094void AudioBuffer::CopyFrom(const float* const* data,
Michael Graczyk86c6d332015-07-23 11:41:39 -070095 const StreamConfig& stream_config) {
kwiberg9e2be5f2016-09-14 05:23:22 -070096 RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_);
97 RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000098 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -070099 // Initialized lazily because there's a different condition in
100 // DeinterleaveFrom.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700101 const bool need_to_downmix =
102 num_input_channels_ > 1 && num_proc_channels_ == 1;
103 if (need_to_downmix && !input_buffer_) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700104 input_buffer_.reset(
105 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
106 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000107
108 // Downmix.
109 const float* const* data_ptr = data;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700110 if (need_to_downmix) {
111 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
112 input_buffer_->fbuf()->channels()[0]);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700113 data_ptr = input_buffer_->fbuf_const()->channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114 }
115
116 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000117 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800118 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200119 input_resamplers_[i]->Resample(data_ptr[i], input_num_frames_,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000120 process_buffer_->channels()[i],
121 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 }
123 data_ptr = process_buffer_->channels();
124 }
125
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000126 // Convert to the S16 range.
Peter Kasting69558702016-01-12 16:26:35 -0800127 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200128 FloatToFloatS16(data_ptr[i], proc_num_frames_,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000129 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000130 }
131}
132
Michael Graczyk86c6d332015-07-23 11:41:39 -0700133void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000134 float* const* data) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700135 RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_);
136 RTC_DCHECK(stream_config.num_channels() == num_channels_ ||
137 num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000138
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000139 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000140 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000141 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000142 // Convert to an intermediate buffer for subsequent resampling.
143 data_ptr = process_buffer_->channels();
144 }
Peter Kasting69558702016-01-12 16:26:35 -0800145 for (size_t i = 0; i < num_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200146 FloatS16ToFloat(data_->fbuf()->channels()[i], proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000147 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000148 }
149
150 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000151 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800152 for (size_t i = 0; i < num_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200153 output_resamplers_[i]->Resample(data_ptr[i], proc_num_frames_, data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000154 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000155 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 }
aluebsb2328d12016-01-11 20:32:29 -0800157
158 // Upmix.
Peter Kasting69558702016-01-12 16:26:35 -0800159 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
aluebsb2328d12016-01-11 20:32:29 -0800160 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
161 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000162}
163
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000164void AudioBuffer::InitForNewData() {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000165 num_channels_ = num_proc_channels_;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700166 data_->set_num_channels(num_proc_channels_);
167 if (split_data_.get()) {
168 split_data_->set_num_channels(num_proc_channels_);
169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170}
171
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000172const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000173 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000174}
175
176int16_t* const* AudioBuffer::channels() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000177 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000178}
179
Peter Kasting69558702016-01-12 16:26:35 -0800180const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200181 return split_data_.get() ? split_data_->ibuf_const()->bands(channel)
182 : data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000183}
184
Peter Kasting69558702016-01-12 16:26:35 -0800185int16_t* const* AudioBuffer::split_bands(size_t channel) {
Yves Gerey665174f2018-06-19 15:03:05 +0200186 return split_data_.get() ? split_data_->ibuf()->bands(channel)
187 : data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000188}
189
190const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000191 if (split_data_.get()) {
192 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000193 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000194 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000195 }
196}
197
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000198const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000199 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000200}
201
202float* const* AudioBuffer::channels_f() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000203 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000204}
205
Peter Kasting69558702016-01-12 16:26:35 -0800206const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200207 return split_data_.get() ? split_data_->fbuf_const()->bands(channel)
208 : data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209}
210
Peter Kasting69558702016-01-12 16:26:35 -0800211float* const* AudioBuffer::split_bands_f(size_t channel) {
Yves Gerey665174f2018-06-19 15:03:05 +0200212 return split_data_.get() ? split_data_->fbuf()->bands(channel)
213 : data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000214}
215
Peter Kasting69558702016-01-12 16:26:35 -0800216size_t AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000217 return num_channels_;
218}
219
Peter Kasting69558702016-01-12 16:26:35 -0800220void AudioBuffer::set_num_channels(size_t num_channels) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000221 num_channels_ = num_channels;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700222 data_->set_num_channels(num_channels);
223 if (split_data_.get()) {
224 split_data_->set_num_channels(num_channels);
225 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
Peter Kastingdce40cf2015-08-24 14:52:23 -0700228size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000229 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230}
231
Peter Kastingdce40cf2015-08-24 14:52:23 -0700232size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000233 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
Peter Kastingdce40cf2015-08-24 14:52:23 -0700236size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000237 return num_bands_;
238}
239
Alejandro Luebs05c76052015-05-20 14:39:39 -0700240// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
Per Åhgrena1351272019-08-15 12:15:46 +0200241void AudioBuffer::DeinterleaveFrom(const AudioFrame* frame) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700242 RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_);
243 RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000244 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700245 // Initialized lazily because there's a different condition in CopyFrom.
246 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
247 input_buffer_.reset(
248 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
249 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
Alejandro Luebs05c76052015-05-20 14:39:39 -0700251 int16_t* const* deinterleaved;
252 if (input_num_frames_ == proc_num_frames_) {
253 deinterleaved = data_->ibuf()->channels();
254 } else {
255 deinterleaved = input_buffer_->ibuf()->channels();
256 }
yujo36b1a5f2017-06-12 12:45:32 -0700257 // TODO(yujo): handle muted frames more efficiently.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700258 if (num_proc_channels_ == 1) {
259 // Downmix and deinterleave simultaneously.
yujo36b1a5f2017-06-12 12:45:32 -0700260 DownmixInterleavedToMono(frame->data(), input_num_frames_,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700261 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000262 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700263 RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_);
Yves Gerey665174f2018-06-19 15:03:05 +0200264 Deinterleave(frame->data(), input_num_frames_, num_proc_channels_,
Alejandro Luebs05c76052015-05-20 14:39:39 -0700265 deinterleaved);
266 }
267
268 // Resample.
269 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800270 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200271 input_resamplers_[i]->Resample(
272 input_buffer_->fbuf_const()->channels()[i], input_num_frames_,
273 data_->fbuf()->channels()[i], proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000274 }
275 }
276}
277
Per Åhgrena1351272019-08-15 12:15:46 +0200278void AudioBuffer::InterleaveTo(AudioFrame* frame) const {
kwiberg9e2be5f2016-09-14 05:23:22 -0700279 RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1);
280 RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700281
282 // Resample if necessary.
283 IFChannelBuffer* data_ptr = data_.get();
284 if (proc_num_frames_ != output_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800285 for (size_t i = 0; i < num_channels_; ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -0700286 output_resamplers_[i]->Resample(
287 data_->fbuf()->channels()[i], proc_num_frames_,
288 output_buffer_->fbuf()->channels()[i], output_num_frames_);
289 }
290 data_ptr = output_buffer_.get();
291 }
292
yujo36b1a5f2017-06-12 12:45:32 -0700293 // TODO(yujo): handle muted frames more efficiently.
ekmeyerson60d9b332015-08-14 10:35:55 -0700294 if (frame->num_channels_ == num_channels_) {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700295 Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_,
yujo36b1a5f2017-06-12 12:45:32 -0700296 frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700297 } else {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700298 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_,
yujo36b1a5f2017-06-12 12:45:32 -0700299 frame->num_channels_, frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700300 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000301}
302
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000303void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000304 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000305}
306
307void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000308 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000309}
310
niklase@google.com470e71d2011-07-07 08:21:25 +0000311} // namespace webrtc