wav_file.h (3579B)
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 #ifndef COMMON_AUDIO_WAV_FILE_H_ 12 #define COMMON_AUDIO_WAV_FILE_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 17 #include "absl/strings/string_view.h" 18 #include "common_audio/wav_header.h" 19 #include "rtc_base/system/file_wrapper.h" 20 21 namespace webrtc { 22 23 // Interface to provide access WAV file parameters. 24 class WavFile { 25 public: 26 enum class SampleFormat { kInt16, kFloat }; 27 28 virtual ~WavFile() {} 29 30 virtual int sample_rate() const = 0; 31 virtual size_t num_channels() const = 0; 32 virtual size_t num_samples() const = 0; 33 }; 34 35 // Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV 36 // files. All error handling is by calls to RTC_CHECK(), making it unsuitable 37 // for anything but debug code. 38 class WavWriter final : public WavFile { 39 public: 40 // Opens a new WAV file for writing. 41 WavWriter(absl::string_view filename, 42 int sample_rate, 43 size_t num_channels, 44 SampleFormat sample_format = SampleFormat::kInt16); 45 WavWriter(FileWrapper file, 46 int sample_rate, 47 size_t num_channels, 48 SampleFormat sample_format = SampleFormat::kInt16); 49 50 // Closes the WAV file, after writing its header. 51 ~WavWriter() { Close(); } 52 53 WavWriter(const WavWriter&) = delete; 54 WavWriter& operator=(const WavWriter&) = delete; 55 56 // Write additional samples to the file. Each sample is in the range 57 // [-32768.0,32767.0], and there must be the previously specified number of 58 // interleaved channels. 59 void WriteSamples(const float* samples, size_t num_samples); 60 void WriteSamples(const int16_t* samples, size_t num_samples); 61 62 int sample_rate() const override { return sample_rate_; } 63 size_t num_channels() const override { return num_channels_; } 64 size_t num_samples() const override { return num_samples_written_; } 65 66 private: 67 void Close(); 68 const int sample_rate_; 69 const size_t num_channels_; 70 size_t num_samples_written_; 71 WavFormat format_; 72 FileWrapper file_; 73 }; 74 75 // Follows the conventions of WavWriter. 76 class WavReader final : public WavFile { 77 public: 78 // Opens an existing WAV file for reading. 79 explicit WavReader(absl::string_view filename); 80 explicit WavReader(FileWrapper file); 81 82 // Close the WAV file. 83 ~WavReader() { Close(); } 84 85 WavReader(const WavReader&) = delete; 86 WavReader& operator=(const WavReader&) = delete; 87 88 // Resets position to the beginning of the file. 89 void Reset(); 90 91 // Returns the number of samples read. If this is less than requested, 92 // verifies that the end of the file was reached. 93 size_t ReadSamples(size_t num_samples, float* samples); 94 size_t ReadSamples(size_t num_samples, int16_t* samples); 95 96 int sample_rate() const override { return sample_rate_; } 97 size_t num_channels() const override { return num_channels_; } 98 size_t num_samples() const override { return num_samples_in_file_; } 99 100 private: 101 void Close(); 102 int sample_rate_; 103 size_t num_channels_; 104 WavFormat format_; 105 size_t num_samples_in_file_; 106 size_t num_unread_samples_; 107 FileWrapper file_; 108 int64_t 109 data_start_pos_; // Position in the file immediately after WAV header. 110 }; 111 112 } // namespace webrtc 113 114 #endif // COMMON_AUDIO_WAV_FILE_H_