1 /*
2  *  Copyright (c) 2014 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 
11 #include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
12 
13 #include <memory>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 namespace test {
19 
Read(size_t samples,int output_rate_hz,int16_t * destination)20 bool ResampleInputAudioFile::Read(size_t samples,
21                                   int output_rate_hz,
22                                   int16_t* destination) {
23   const size_t samples_to_read = samples * file_rate_hz_ / output_rate_hz;
24   RTC_CHECK_EQ(samples_to_read * output_rate_hz, samples * file_rate_hz_)
25       << "Frame size and sample rates don't add up to an integer.";
26   std::unique_ptr<int16_t[]> temp_destination(new int16_t[samples_to_read]);
27   if (!InputAudioFile::Read(samples_to_read, temp_destination.get()))
28     return false;
29   resampler_.ResetIfNeeded(file_rate_hz_, output_rate_hz, 1);
30   size_t output_length = 0;
31   RTC_CHECK_EQ(resampler_.Push(temp_destination.get(), samples_to_read,
32                                destination, samples, output_length),
33                0);
34   RTC_CHECK_EQ(samples, output_length);
35   return true;
36 }
37 
Read(size_t samples,int16_t * destination)38 bool ResampleInputAudioFile::Read(size_t samples, int16_t* destination) {
39   RTC_CHECK_GT(output_rate_hz_, 0) << "Output rate not set.";
40   return Read(samples, output_rate_hz_, destination);
41 }
42 
set_output_rate_hz(int rate_hz)43 void ResampleInputAudioFile::set_output_rate_hz(int rate_hz) {
44   output_rate_hz_ = rate_hz;
45 }
46 
47 }  // namespace test
48 }  // namespace webrtc
49