19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace test {
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
16dd1b19d950158fbb1d7e1792c4db95460d5f8c49pbos@webrtc.orgInputAudioFile::InputAudioFile(const std::string file_name) {
17dd1b19d950158fbb1d7e1792c4db95460d5f8c49pbos@webrtc.org  fp_ = fopen(file_name.c_str(), "rb");
18dd1b19d950158fbb1d7e1792c4db95460d5f8c49pbos@webrtc.org}
19dd1b19d950158fbb1d7e1792c4db95460d5f8c49pbos@webrtc.org
20dd1b19d950158fbb1d7e1792c4db95460d5f8c49pbos@webrtc.orgInputAudioFile::~InputAudioFile() { fclose(fp_); }
21dd1b19d950158fbb1d7e1792c4db95460d5f8c49pbos@webrtc.org
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgbool InputAudioFile::Read(size_t samples, int16_t* destination) {
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (!fp_) {
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return false;
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
261c9d3fe36e20364f84c4252f96663669d7ef675dhenrik.lundin@webrtc.org  size_t samples_read = fread(destination, sizeof(int16_t), samples, fp_);
271c9d3fe36e20364f84c4252f96663669d7ef675dhenrik.lundin@webrtc.org  if (samples_read < samples) {
281c9d3fe36e20364f84c4252f96663669d7ef675dhenrik.lundin@webrtc.org    // Rewind and read the missing samples.
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    rewind(fp_);
301c9d3fe36e20364f84c4252f96663669d7ef675dhenrik.lundin@webrtc.org    size_t missing_samples = samples - samples_read;
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (fread(destination, sizeof(int16_t), missing_samples, fp_) <
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        missing_samples) {
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // Could not read enough even after rewinding the file.
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      return false;
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return true;
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgvoid InputAudioFile::DuplicateInterleaved(const int16_t* source, size_t samples,
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                          size_t channels,
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                          int16_t* destination) {
4306c375d5437f9db90a59152a585882eaaea6fb29henrik.lundin@webrtc.org  // Start from the end of |source| and |destination|, and work towards the
4406c375d5437f9db90a59152a585882eaaea6fb29henrik.lundin@webrtc.org  // beginning. This is to allow in-place interleaving of the same array (i.e.,
4506c375d5437f9db90a59152a585882eaaea6fb29henrik.lundin@webrtc.org  // |source| and |destination| are the same array).
4606c375d5437f9db90a59152a585882eaaea6fb29henrik.lundin@webrtc.org  for (int i = static_cast<int>(samples - 1); i >= 0; --i) {
4706c375d5437f9db90a59152a585882eaaea6fb29henrik.lundin@webrtc.org    for (int j = static_cast<int>(channels - 1); j >= 0; --j) {
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      destination[i * channels + j] = source[i];
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace test
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
55