acm_resampler.cc revision 7959e16cc2da9ebf5eda61bdf0eb423d8d3006ba
17959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org/*
27959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
37959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *
47959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *  Use of this source code is governed by a BSD-style license
57959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *  that can be found in the LICENSE file in the root of the source
67959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *  tree. An additional intellectual property rights grant can be found
77959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *  in the file PATENTS.  All contributing project authors may
87959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
97959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org */
107959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
117959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include "webrtc/modules/audio_coding/main/source/acm_resampler.h"
127959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
137959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include <string.h>
147959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
157959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include "webrtc/common_audio/resampler/include/resampler.h"
167959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
177959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
187959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include "webrtc/system_wrappers/interface/trace.h"
197959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
207959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgnamespace webrtc {
217959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
227959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgACMResampler::ACMResampler()
237959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    : resampler_crit_sect_(CriticalSectionWrapper::CreateCriticalSection()) {
247959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}
257959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
267959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgACMResampler::~ACMResampler() {
277959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  delete resampler_crit_sect_;
287959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}
297959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
307959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgint ACMResampler::Resample10Msec(const int16_t* in_audio,
317959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int in_freq_hz,
327959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int out_freq_hz,
337959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int num_audio_channels,
347959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int16_t* out_audio) {
357959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  CriticalSectionScoped cs(resampler_crit_sect_);
367959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
377959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  if (in_freq_hz == out_freq_hz) {
387959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    size_t length = static_cast<size_t>(in_freq_hz * num_audio_channels / 100);
397959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    memcpy(out_audio, in_audio, length * sizeof(int16_t));
407959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    return static_cast<int16_t>(in_freq_hz / 100);
417959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  }
427959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
437959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  // |maxLen| is maximum number of samples for 10ms at 48kHz.
447959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  int max_len = 480 * num_audio_channels;
457959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  int length_in = (in_freq_hz / 100) * num_audio_channels;
467959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  int out_len;
477959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
487959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  ResamplerType type = (num_audio_channels == 1) ? kResamplerSynchronous :
497959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org      kResamplerSynchronousStereo;
507959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
517959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  if (resampler_.ResetIfNeeded(in_freq_hz, out_freq_hz, type) < 0) {
527959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, 0,
537959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                 "Error in reset of resampler");
547959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    return -1;
557959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  }
567959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
577959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  if (resampler_.Push(in_audio, length_in, out_audio, max_len, out_len) < 0) {
587959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, 0,
597959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                 "Error in resampler: resampler.Push");
607959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    return -1;
617959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  }
627959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
637959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  return out_len / num_audio_channels;
647959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}
657959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
667959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}  // namespace webrtc
67