turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/acm2/acm_resampler.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 13 | #include <assert.h> |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "common_audio/resampler/include/resampler.h" |
| 17 | #include "rtc_base/logging.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 20 | namespace acm2 { |
| 21 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 22 | ACMResampler::ACMResampler() {} |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 23 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 24 | ACMResampler::~ACMResampler() {} |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 25 | |
| 26 | int ACMResampler::Resample10Msec(const int16_t* in_audio, |
| 27 | int in_freq_hz, |
| 28 | int out_freq_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 29 | size_t num_audio_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 30 | size_t out_capacity_samples, |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 31 | int16_t* out_audio) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 32 | size_t in_length = in_freq_hz * num_audio_channels / 100; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 33 | if (in_freq_hz == out_freq_hz) { |
henrik.lundin@webrtc.org | 439a4c4 | 2014-04-24 19:05:33 +0000 | [diff] [blame] | 34 | if (out_capacity_samples < in_length) { |
| 35 | assert(false); |
| 36 | return -1; |
| 37 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 38 | memcpy(out_audio, in_audio, in_length * sizeof(int16_t)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 39 | return static_cast<int>(in_length / num_audio_channels); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 42 | if (resampler_.InitializeIfNeeded(in_freq_hz, out_freq_hz, |
| 43 | num_audio_channels) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 44 | RTC_LOG(LS_ERROR) << "InitializeIfNeeded(" << in_freq_hz << ", " |
| 45 | << out_freq_hz << ", " << num_audio_channels |
| 46 | << ") failed."; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 47 | return -1; |
| 48 | } |
| 49 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 50 | int out_length = |
henrik.lundin@webrtc.org | 439a4c4 | 2014-04-24 19:05:33 +0000 | [diff] [blame] | 51 | resampler_.Resample(in_audio, in_length, out_audio, out_capacity_samples); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 52 | if (out_length == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 53 | RTC_LOG(LS_ERROR) << "Resample(" << in_audio << ", " << in_length << ", " |
| 54 | << out_audio << ", " << out_capacity_samples |
| 55 | << ") failed."; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 56 | return -1; |
| 57 | } |
| 58 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 59 | return static_cast<int>(out_length / num_audio_channels); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 60 | } |
| 61 | |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 62 | } // namespace acm2 |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 63 | } // namespace webrtc |