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 "modules/audio_coding/acm2/acm_resampler.h"
12
13 #include <string.h>
14
15 #include "rtc_base/logging.h"
16
17 namespace webrtc {
18 namespace acm2 {
19
ACMResampler()20 ACMResampler::ACMResampler() {}
21
~ACMResampler()22 ACMResampler::~ACMResampler() {}
23
Resample10Msec(const int16_t * in_audio,int in_freq_hz,int out_freq_hz,size_t num_audio_channels,size_t out_capacity_samples,int16_t * out_audio)24 int ACMResampler::Resample10Msec(const int16_t* in_audio,
25 int in_freq_hz,
26 int out_freq_hz,
27 size_t num_audio_channels,
28 size_t out_capacity_samples,
29 int16_t* out_audio) {
30 size_t in_length = in_freq_hz * num_audio_channels / 100;
31 if (in_freq_hz == out_freq_hz) {
32 if (out_capacity_samples < in_length) {
33 RTC_DCHECK_NOTREACHED();
34 return -1;
35 }
36 memcpy(out_audio, in_audio, in_length * sizeof(int16_t));
37 return static_cast<int>(in_length / num_audio_channels);
38 }
39
40 if (resampler_.InitializeIfNeeded(in_freq_hz, out_freq_hz,
41 num_audio_channels) != 0) {
42 RTC_LOG(LS_ERROR) << "InitializeIfNeeded(" << in_freq_hz << ", "
43 << out_freq_hz << ", " << num_audio_channels
44 << ") failed.";
45 return -1;
46 }
47
48 int out_length =
49 resampler_.Resample(in_audio, in_length, out_audio, out_capacity_samples);
50 if (out_length == -1) {
51 RTC_LOG(LS_ERROR) << "Resample(" << in_audio << ", " << in_length << ", "
52 << out_audio << ", " << out_capacity_samples
53 << ") failed.";
54 return -1;
55 }
56
57 return static_cast<int>(out_length / num_audio_channels);
58 }
59
60 } // namespace acm2
61 } // namespace webrtc
62