blob: 93e86517aed31bc7a2b88d9be99a2b7ea29dbd02 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
braveyao@webrtc.orgd7131432012-03-29 10:39:442 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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
Fredrik Solenberga8b7c7f2018-01-17 10:18:3111#include "audio/remix_resample.h"
niklase@google.com470e71d2011-07-07 08:21:2512
Tommi19c51ea2024-05-29 07:52:5513#include <array>
14
Fredrik Solenbergbbf21a32018-04-12 20:44:0915#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "audio/utility/audio_frame_operations.h"
17#include "common_audio/resampler/include/push_resampler.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
Tommi1f367982024-04-30 12:04:4419#include "rtc_base/logging.h"
niklase@google.com470e71d2011-07-07 08:21:2520
andrew@webrtc.org40ee3d02014-04-03 21:56:0121namespace webrtc {
22namespace voe {
niklase@google.com470e71d2011-07-07 08:21:2523
andrew@webrtc.org40ee3d02014-04-03 21:56:0124void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:0725 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:0126 AudioFrame* dst_frame) {
Tommi32c33982024-07-03 07:34:1827 RemixAndResample(src_frame.data_view(), src_frame.sample_rate_hz_, resampler,
28 dst_frame);
Alejandro Luebscdfe20b2015-09-23 19:49:1229 dst_frame->timestamp_ = src_frame.timestamp_;
30 dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
31 dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
Alessio Bazzica8f319a32019-07-24 16:47:0232 dst_frame->packet_infos_ = src_frame.packet_infos_;
Alejandro Luebscdfe20b2015-09-23 19:49:1233}
34
Tommi32c33982024-07-03 07:34:1835void RemixAndResample(InterleavedView<const int16_t> src_data,
Alejandro Luebscdfe20b2015-09-23 19:49:1236 int sample_rate_hz,
37 PushResampler<int16_t>* resampler,
38 AudioFrame* dst_frame) {
Tommi32c33982024-07-03 07:34:1839 // The `samples_per_channel_` members must have been set correctly based on
40 // the associated sample rate and the assumed 10ms buffer size.
41 // TODO(tommi): Remove the `sample_rate_hz` param.
42 RTC_DCHECK_EQ(SampleRateToDefaultChannelSize(sample_rate_hz),
43 src_data.samples_per_channel());
44 RTC_DCHECK_EQ(SampleRateToDefaultChannelSize(dst_frame->sample_rate_hz_),
45 dst_frame->samples_per_channel());
46
47 // Temporary buffer in case downmixing is required.
Tommi19c51ea2024-05-29 07:52:5548 std::array<int16_t, AudioFrame::kMaxDataSizeSamples> downmixed_audio;
niklase@google.com470e71d2011-07-07 08:21:2549
andrew@webrtc.org40ee3d02014-04-03 21:56:0150 // Downmix before resampling.
Tommi32c33982024-07-03 07:34:1851 if (src_data.num_channels() > dst_frame->num_channels_) {
52 RTC_DCHECK(src_data.num_channels() == 2 || src_data.num_channels() == 4)
53 << "num_channels: " << src_data.num_channels();
jens.nielsen228c2682017-03-01 13:11:2254 RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
55 << "dst_frame->num_channels_: " << dst_frame->num_channels_;
56
Tommi32c33982024-07-03 07:34:1857 InterleavedView<int16_t> downmixed(downmixed_audio.data(),
58 src_data.samples_per_channel(),
59 dst_frame->num_channels_);
60 AudioFrameOperations::DownmixChannels(src_data, downmixed);
61 src_data = downmixed;
andrew@webrtc.org40ee3d02014-04-03 21:56:0162 }
braveyao@webrtc.orgd7131432012-03-29 10:39:4463
yujo36b1a5f2017-06-12 19:45:3264 // TODO(yujo): for muted input frames, don't resample. Either 1) allow
65 // resampler to return output length without doing the resample, so we know
66 // how much to zero here; or 2) make resampler accept a hint that the input is
67 // zeroed.
Tommi5d3e6802024-05-24 14:43:5568
Tommi5d3e6802024-05-24 14:43:5569 // Stash away the originally requested number of channels. Then provide
70 // `dst_frame` as a target buffer with the same number of channels as the
71 // source.
72 auto original_dst_number_of_channels = dst_frame->num_channels_;
Tommi1f367982024-04-30 12:04:4473 int out_length = resampler->Resample(
Tommi32c33982024-07-03 07:34:1874 src_data, dst_frame->mutable_data(dst_frame->samples_per_channel_,
75 src_data.num_channels()));
76 RTC_CHECK_NE(out_length, -1) << "src_data.size=" << src_data.size();
Tommi5d3e6802024-05-24 14:43:5577 RTC_DCHECK_EQ(dst_frame->samples_per_channel(),
Tommi32c33982024-07-03 07:34:1878 out_length / src_data.num_channels());
andrew@webrtc.org40ee3d02014-04-03 21:56:0179
80 // Upmix after resampling.
Tommi32c33982024-07-03 07:34:1881 if (src_data.num_channels() == 1 && original_dst_number_of_channels == 2) {
andrew@webrtc.org40ee3d02014-04-03 21:56:0182 // The audio in dst_frame really is mono at this point; MonoToStereo will
83 // set this back to stereo.
Tommi5d3e6802024-05-24 14:43:5584 RTC_DCHECK_EQ(dst_frame->num_channels_, 1);
Alex Loikob4977de2019-01-28 15:38:3885 AudioFrameOperations::UpmixChannels(2, dst_frame);
andrew@webrtc.org40ee3d02014-04-03 21:56:0186 }
niklase@google.com470e71d2011-07-07 08:21:2587}
88
pbos@webrtc.orgd900e8b2013-07-03 15:12:2689} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:2690} // namespace webrtc