• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 
11 #include "audio/remix_resample.h"
12 
13 #include "api/audio/audio_frame.h"
14 #include "audio/utility/audio_frame_operations.h"
15 #include "common_audio/resampler/include/push_resampler.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 namespace voe {
20 
RemixAndResample(const AudioFrame & src_frame,PushResampler<int16_t> * resampler,AudioFrame * dst_frame)21 void RemixAndResample(const AudioFrame& src_frame,
22                       PushResampler<int16_t>* resampler,
23                       AudioFrame* dst_frame) {
24   RemixAndResample(src_frame.data(), src_frame.samples_per_channel_,
25                    src_frame.num_channels_, src_frame.sample_rate_hz_,
26                    resampler, dst_frame);
27   dst_frame->timestamp_ = src_frame.timestamp_;
28   dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
29   dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
30   dst_frame->packet_infos_ = src_frame.packet_infos_;
31 }
32 
RemixAndResample(const int16_t * src_data,size_t samples_per_channel,size_t num_channels,int sample_rate_hz,PushResampler<int16_t> * resampler,AudioFrame * dst_frame)33 void RemixAndResample(const int16_t* src_data,
34                       size_t samples_per_channel,
35                       size_t num_channels,
36                       int sample_rate_hz,
37                       PushResampler<int16_t>* resampler,
38                       AudioFrame* dst_frame) {
39   const int16_t* audio_ptr = src_data;
40   size_t audio_ptr_num_channels = num_channels;
41   int16_t downmixed_audio[AudioFrame::kMaxDataSizeSamples];
42 
43   // Downmix before resampling.
44   if (num_channels > dst_frame->num_channels_) {
45     RTC_DCHECK(num_channels == 2 || num_channels == 4)
46         << "num_channels: " << num_channels;
47     RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
48         << "dst_frame->num_channels_: " << dst_frame->num_channels_;
49 
50     AudioFrameOperations::DownmixChannels(
51         src_data, num_channels, samples_per_channel, dst_frame->num_channels_,
52         downmixed_audio);
53     audio_ptr = downmixed_audio;
54     audio_ptr_num_channels = dst_frame->num_channels_;
55   }
56 
57   if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
58                                     audio_ptr_num_channels) == -1) {
59     RTC_FATAL() << "InitializeIfNeeded failed: sample_rate_hz = "
60                 << sample_rate_hz << ", dst_frame->sample_rate_hz_ = "
61                 << dst_frame->sample_rate_hz_
62                 << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
63   }
64 
65   // TODO(yujo): for muted input frames, don't resample. Either 1) allow
66   // resampler to return output length without doing the resample, so we know
67   // how much to zero here; or 2) make resampler accept a hint that the input is
68   // zeroed.
69   const size_t src_length = samples_per_channel * audio_ptr_num_channels;
70   int out_length =
71       resampler->Resample(audio_ptr, src_length, dst_frame->mutable_data(),
72                           AudioFrame::kMaxDataSizeSamples);
73   if (out_length == -1) {
74     RTC_FATAL() << "Resample failed: audio_ptr = " << audio_ptr
75                 << ", src_length = " << src_length
76                 << ", dst_frame->mutable_data() = "
77                 << dst_frame->mutable_data();
78   }
79   dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
80 
81   // Upmix after resampling.
82   if (num_channels == 1 && dst_frame->num_channels_ == 2) {
83     // The audio in dst_frame really is mono at this point; MonoToStereo will
84     // set this back to stereo.
85     dst_frame->num_channels_ = 1;
86     AudioFrameOperations::UpmixChannels(2, dst_frame);
87   }
88 }
89 
90 }  // namespace voe
91 }  // namespace webrtc
92