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