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