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
113e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#include "webrtc/modules/audio_coding/acm2/acm_resampler.h"
127959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
13f2aafe4355c4b7ecbd122798f08a5c5ec5d2693ahenrike@webrtc.org#include <assert.h>
147959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include <string.h>
157959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
167959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org#include "webrtc/common_audio/resampler/include/resampler.h"
1798f53510b222f71fdd8b799b2f33737ceeb28c61Henrik Kjellander#include "webrtc/system_wrappers/include/logging.h"
187959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
197959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgnamespace webrtc {
206d5d2480757a87c7f2fb40be88cfdeca419fc6a4turaj@webrtc.orgnamespace acm2 {
216d5d2480757a87c7f2fb40be88cfdeca419fc6a4turaj@webrtc.org
2240ee3d07eda24b8e8214429d9885d9ad9a2c04f7andrew@webrtc.orgACMResampler::ACMResampler() {
237959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}
247959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
257959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgACMResampler::~ACMResampler() {
267959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}
277959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
287959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.orgint ACMResampler::Resample10Msec(const int16_t* in_audio,
297959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int in_freq_hz,
307959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int out_freq_hz,
316955870806624479723addfae6dcf5d13968796cPeter Kasting                                 size_t num_audio_channels,
32dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting                                 size_t out_capacity_samples,
337959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org                                 int16_t* out_audio) {
346955870806624479723addfae6dcf5d13968796cPeter Kasting  size_t in_length = in_freq_hz * num_audio_channels / 100;
357959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  if (in_freq_hz == out_freq_hz) {
36439a4c49f9f34481cb691b7789b691990fed2fe1henrik.lundin@webrtc.org    if (out_capacity_samples < in_length) {
37439a4c49f9f34481cb691b7789b691990fed2fe1henrik.lundin@webrtc.org      assert(false);
38439a4c49f9f34481cb691b7789b691990fed2fe1henrik.lundin@webrtc.org      return -1;
39439a4c49f9f34481cb691b7789b691990fed2fe1henrik.lundin@webrtc.org    }
4040ee3d07eda24b8e8214429d9885d9ad9a2c04f7andrew@webrtc.org    memcpy(out_audio, in_audio, in_length * sizeof(int16_t));
41dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting    return static_cast<int>(in_length / num_audio_channels);
427959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  }
437959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
4440ee3d07eda24b8e8214429d9885d9ad9a2c04f7andrew@webrtc.org  if (resampler_.InitializeIfNeeded(in_freq_hz, out_freq_hz,
4540ee3d07eda24b8e8214429d9885d9ad9a2c04f7andrew@webrtc.org                                    num_audio_channels) != 0) {
4682ccfcf5cae798d21881e41a7123e9ca3016988asolenberg    LOG(LS_ERROR) << "InitializeIfNeeded(" << in_freq_hz << ", " << out_freq_hz
4782ccfcf5cae798d21881e41a7123e9ca3016988asolenberg                  << ", " << num_audio_channels << ") failed.";
487959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    return -1;
497959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  }
507959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
5125702cb1628941427fa55e528f53483f239ae011pkasting  int out_length =
52439a4c49f9f34481cb691b7789b691990fed2fe1henrik.lundin@webrtc.org      resampler_.Resample(in_audio, in_length, out_audio, out_capacity_samples);
5340ee3d07eda24b8e8214429d9885d9ad9a2c04f7andrew@webrtc.org  if (out_length == -1) {
5482ccfcf5cae798d21881e41a7123e9ca3016988asolenberg    LOG(LS_ERROR) << "Resample(" << in_audio << ", " << in_length << ", "
5582ccfcf5cae798d21881e41a7123e9ca3016988asolenberg                  << out_audio << ", " << out_capacity_samples << ") failed.";
567959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org    return -1;
577959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org  }
587959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
596955870806624479723addfae6dcf5d13968796cPeter Kasting  return static_cast<int>(out_length / num_audio_channels);
607959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}
617959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org
626d5d2480757a87c7f2fb40be88cfdeca419fc6a4turaj@webrtc.org}  // namespace acm2
637959e16cc2da9ebf5eda61bdf0eb423d8d3006baturaj@webrtc.org}  // namespace webrtc
64