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 | |
kjellander | 3e6db23 | 2015-11-26 04:44:54 -0800 | [diff] [blame^] | 11 | #include "webrtc/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 | |
| 16 | #include "webrtc/common_audio/resampler/include/resampler.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/include/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 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 22 | ACMResampler::ACMResampler() { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | ACMResampler::~ACMResampler() { |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | int ACMResampler::Resample10Msec(const int16_t* in_audio, |
| 29 | int in_freq_hz, |
| 30 | int out_freq_hz, |
| 31 | int num_audio_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 32 | size_t out_capacity_samples, |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 33 | int16_t* out_audio) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 34 | size_t in_length = static_cast<size_t>(in_freq_hz * num_audio_channels / 100); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 35 | int out_length = out_freq_hz * num_audio_channels / 100; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 36 | if (in_freq_hz == out_freq_hz) { |
henrik.lundin@webrtc.org | 439a4c4 | 2014-04-24 19:05:33 +0000 | [diff] [blame] | 37 | if (out_capacity_samples < in_length) { |
| 38 | assert(false); |
| 39 | return -1; |
| 40 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 41 | memcpy(out_audio, in_audio, in_length * sizeof(int16_t)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 42 | return static_cast<int>(in_length / num_audio_channels); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 43 | } |
| 44 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 45 | 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.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
henrik.lundin@webrtc.org | 439a4c4 | 2014-04-24 19:05:33 +0000 | [diff] [blame] | 52 | out_length = |
| 53 | resampler_.Resample(in_audio, in_length, out_audio, out_capacity_samples); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 54 | if (out_length == -1) { |
henrik.lundin@webrtc.org | 439a4c4 | 2014-04-24 19:05:33 +0000 | [diff] [blame] | 55 | LOG_FERR4(LS_ERROR, |
| 56 | Resample, |
| 57 | in_audio, |
| 58 | in_length, |
| 59 | out_audio, |
| 60 | out_capacity_samples); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 64 | return out_length / num_audio_channels; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 65 | } |
| 66 | |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 67 | } // namespace acm2 |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 68 | } // namespace webrtc |