blob: ca3583e32cdea464b9946c6ab9c8c5fe5c374c57 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/logging.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000017
18namespace webrtc {
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000019namespace acm2 {
20
Yves Gerey665174f2018-06-19 15:03:05 +020021ACMResampler::ACMResampler() {}
turaj@webrtc.org7959e162013-09-12 18:30:26 +000022
Yves Gerey665174f2018-06-19 15:03:05 +020023ACMResampler::~ACMResampler() {}
turaj@webrtc.org7959e162013-09-12 18:30:26 +000024
25int ACMResampler::Resample10Msec(const int16_t* in_audio,
26 int in_freq_hz,
27 int out_freq_hz,
Peter Kasting69558702016-01-12 16:26:35 -080028 size_t num_audio_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070029 size_t out_capacity_samples,
turaj@webrtc.org7959e162013-09-12 18:30:26 +000030 int16_t* out_audio) {
Peter Kasting69558702016-01-12 16:26:35 -080031 size_t in_length = in_freq_hz * num_audio_channels / 100;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000032 if (in_freq_hz == out_freq_hz) {
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +000033 if (out_capacity_samples < in_length) {
34 assert(false);
35 return -1;
36 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000037 memcpy(out_audio, in_audio, in_length * sizeof(int16_t));
Peter Kastingdce40cf2015-08-24 14:52:23 -070038 return static_cast<int>(in_length / num_audio_channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000039 }
40
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000041 if (resampler_.InitializeIfNeeded(in_freq_hz, out_freq_hz,
42 num_audio_channels) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010043 RTC_LOG(LS_ERROR) << "InitializeIfNeeded(" << in_freq_hz << ", "
44 << out_freq_hz << ", " << num_audio_channels
45 << ") failed.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +000046 return -1;
47 }
48
pkasting25702cb2016-01-08 13:50:27 -080049 int out_length =
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +000050 resampler_.Resample(in_audio, in_length, out_audio, out_capacity_samples);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000051 if (out_length == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010052 RTC_LOG(LS_ERROR) << "Resample(" << in_audio << ", " << in_length << ", "
53 << out_audio << ", " << out_capacity_samples
54 << ") failed.";
turaj@webrtc.org7959e162013-09-12 18:30:26 +000055 return -1;
56 }
57
Peter Kasting69558702016-01-12 16:26:35 -080058 return static_cast<int>(out_length / num_audio_channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000059}
60
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000061} // namespace acm2
turaj@webrtc.org7959e162013-09-12 18:30:26 +000062} // namespace webrtc