blob: e38cd94a97887da69f31d190f1f931f478e50b65 [file] [log] [blame]
turaj@webrtc.org7959e162013-09-12 18:30:26 +00001/*
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
kjellander3e6db232015-11-26 04:44:54 -080011#include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000012
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000013#include <assert.h>
turaj@webrtc.org7959e162013-09-12 18:30:26 +000014#include <string.h>
15
16#include "webrtc/common_audio/resampler/include/resampler.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/logging.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000018
19namespace webrtc {
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000020namespace acm2 {
21
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000022ACMResampler::ACMResampler() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000023}
24
25ACMResampler::~ACMResampler() {
turaj@webrtc.org7959e162013-09-12 18:30:26 +000026}
27
28int ACMResampler::Resample10Msec(const int16_t* in_audio,
29 int in_freq_hz,
30 int out_freq_hz,
31 int num_audio_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070032 size_t out_capacity_samples,
turaj@webrtc.org7959e162013-09-12 18:30:26 +000033 int16_t* out_audio) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070034 size_t in_length = static_cast<size_t>(in_freq_hz * num_audio_channels / 100);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000035 int out_length = out_freq_hz * num_audio_channels / 100;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000036 if (in_freq_hz == out_freq_hz) {
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +000037 if (out_capacity_samples < in_length) {
38 assert(false);
39 return -1;
40 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000041 memcpy(out_audio, in_audio, in_length * sizeof(int16_t));
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 return static_cast<int>(in_length / num_audio_channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000043 }
44
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000045 if (resampler_.InitializeIfNeeded(in_freq_hz, out_freq_hz,
46 num_audio_channels) != 0) {
47 LOG_FERR3(LS_ERROR, InitializeIfNeeded, in_freq_hz, out_freq_hz,
48 num_audio_channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000049 return -1;
50 }
51
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +000052 out_length =
53 resampler_.Resample(in_audio, in_length, out_audio, out_capacity_samples);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000054 if (out_length == -1) {
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +000055 LOG_FERR4(LS_ERROR,
56 Resample,
57 in_audio,
58 in_length,
59 out_audio,
60 out_capacity_samples);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000061 return -1;
62 }
63
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000064 return out_length / num_audio_channels;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000065}
66
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000067} // namespace acm2
turaj@webrtc.org7959e162013-09-12 18:30:26 +000068} // namespace webrtc