• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 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/input_audio_file.h"
12 
13 #include "rtc_base/checks.h"
14 
15 namespace webrtc {
16 namespace test {
17 
InputAudioFile(const std::string file_name,bool loop_at_end)18 InputAudioFile::InputAudioFile(const std::string file_name, bool loop_at_end)
19     : loop_at_end_(loop_at_end) {
20   fp_ = fopen(file_name.c_str(), "rb");
21   RTC_DCHECK(fp_) << file_name << " could not be opened.";
22 }
23 
~InputAudioFile()24 InputAudioFile::~InputAudioFile() {
25   RTC_DCHECK(fp_);
26   fclose(fp_);
27 }
28 
Read(size_t samples,int16_t * destination)29 bool InputAudioFile::Read(size_t samples, int16_t* destination) {
30   if (!fp_) {
31     return false;
32   }
33   size_t samples_read = fread(destination, sizeof(int16_t), samples, fp_);
34   if (samples_read < samples) {
35     if (!loop_at_end_) {
36       return false;
37     }
38     // Rewind and read the missing samples.
39     rewind(fp_);
40     size_t missing_samples = samples - samples_read;
41     if (fread(destination + samples_read, sizeof(int16_t), missing_samples,
42               fp_) < missing_samples) {
43       // Could not read enough even after rewinding the file.
44       return false;
45     }
46   }
47   return true;
48 }
49 
Seek(int samples)50 bool InputAudioFile::Seek(int samples) {
51   if (!fp_) {
52     return false;
53   }
54   // Find file boundaries.
55   const long current_pos = ftell(fp_);
56   RTC_CHECK_NE(EOF, current_pos)
57       << "Error returned when getting file position.";
58   RTC_CHECK_EQ(0, fseek(fp_, 0, SEEK_END));  // Move to end of file.
59   const long file_size = ftell(fp_);
60   RTC_CHECK_NE(EOF, file_size) << "Error returned when getting file position.";
61   // Find new position.
62   long new_pos = current_pos + sizeof(int16_t) * samples;  // Samples to bytes.
63   if (loop_at_end_) {
64     new_pos = new_pos % file_size;  // Wrap around the end of the file.
65     if (new_pos < 0) {
66       // For negative values of new_pos, newpos % file_size will also be
67       // negative. To get the correct result it's needed to add file_size.
68       new_pos += file_size;
69     }
70   } else {
71     new_pos = new_pos > file_size ? file_size : new_pos;  // Don't loop.
72   }
73   RTC_CHECK_GE(new_pos, 0)
74       << "Trying to move to before the beginning of the file";
75   // Move to new position relative to the beginning of the file.
76   RTC_CHECK_EQ(0, fseek(fp_, new_pos, SEEK_SET));
77   return true;
78 }
79 
DuplicateInterleaved(const int16_t * source,size_t samples,size_t channels,int16_t * destination)80 void InputAudioFile::DuplicateInterleaved(const int16_t* source,
81                                           size_t samples,
82                                           size_t channels,
83                                           int16_t* destination) {
84   // Start from the end of |source| and |destination|, and work towards the
85   // beginning. This is to allow in-place interleaving of the same array (i.e.,
86   // |source| and |destination| are the same array).
87   for (int i = static_cast<int>(samples - 1); i >= 0; --i) {
88     for (int j = static_cast<int>(channels - 1); j >= 0; --j) {
89       destination[i * channels + j] = source[i];
90     }
91   }
92 }
93 
94 }  // namespace test
95 }  // namespace webrtc
96