niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [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 | |
Fredrik Solenberg | a8b7c7f | 2018-01-17 10:18:31 | [diff] [blame] | 11 | #include "audio/remix_resample.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
Tommi | 19c51ea | 2024-05-29 07:52:55 | [diff] [blame] | 13 | #include <array> |
| 14 | |
Fredrik Solenberg | bbf21a3 | 2018-04-12 20:44:09 | [diff] [blame] | 15 | #include "api/audio/audio_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "audio/utility/audio_frame_operations.h" |
| 17 | #include "common_audio/resampler/include/push_resampler.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Tommi | 1f36798 | 2024-04-30 12:04:44 | [diff] [blame] | 19 | #include "rtc_base/logging.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 20 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 23 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 24 | void RemixAndResample(const AudioFrame& src_frame, |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 | [diff] [blame] | 25 | PushResampler<int16_t>* resampler, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 26 | AudioFrame* dst_frame) { |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 27 | RemixAndResample(src_frame.data_view(), src_frame.sample_rate_hz_, resampler, |
| 28 | dst_frame); |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 29 | 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 Bazzica | 8f319a3 | 2019-07-24 16:47:02 | [diff] [blame] | 32 | dst_frame->packet_infos_ = src_frame.packet_infos_; |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 33 | } |
| 34 | |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 35 | void RemixAndResample(InterleavedView<const int16_t> src_data, |
Alejandro Luebs | cdfe20b | 2015-09-23 19:49:12 | [diff] [blame] | 36 | int sample_rate_hz, |
| 37 | PushResampler<int16_t>* resampler, |
| 38 | AudioFrame* dst_frame) { |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 39 | // 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. |
Tommi | 19c51ea | 2024-05-29 07:52:55 | [diff] [blame] | 48 | std::array<int16_t, AudioFrame::kMaxDataSizeSamples> downmixed_audio; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 49 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 50 | // Downmix before resampling. |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 51 | 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.nielsen | 228c268 | 2017-03-01 13:11:22 | [diff] [blame] | 54 | RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2) |
| 55 | << "dst_frame->num_channels_: " << dst_frame->num_channels_; |
| 56 | |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 57 | 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.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 62 | } |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 | [diff] [blame] | 63 | |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame] | 64 | // 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. |
Tommi | 5d3e680 | 2024-05-24 14:43:55 | [diff] [blame] | 68 | |
Tommi | 5d3e680 | 2024-05-24 14:43:55 | [diff] [blame] | 69 | // 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_; |
Tommi | 1f36798 | 2024-04-30 12:04:44 | [diff] [blame] | 73 | int out_length = resampler->Resample( |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 74 | 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(); |
Tommi | 5d3e680 | 2024-05-24 14:43:55 | [diff] [blame] | 77 | RTC_DCHECK_EQ(dst_frame->samples_per_channel(), |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 78 | out_length / src_data.num_channels()); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 79 | |
| 80 | // Upmix after resampling. |
Tommi | 32c3398 | 2024-07-03 07:34:18 | [diff] [blame] | 81 | if (src_data.num_channels() == 1 && original_dst_number_of_channels == 2) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 82 | // The audio in dst_frame really is mono at this point; MonoToStereo will |
| 83 | // set this back to stereo. |
Tommi | 5d3e680 | 2024-05-24 14:43:55 | [diff] [blame] | 84 | RTC_DCHECK_EQ(dst_frame->num_channels_, 1); |
Alex Loiko | b4977de | 2019-01-28 15:38:38 | [diff] [blame] | 85 | AudioFrameOperations::UpmixChannels(2, dst_frame); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 | [diff] [blame] | 86 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 87 | } |
| 88 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 89 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 90 | } // namespace webrtc |