diff options
Diffstat (limited to 'webrtc/common_audio/wav_file.h')
-rw-r--r-- | webrtc/common_audio/wav_file.h | 109 |
1 files changed, 53 insertions, 56 deletions
diff --git a/webrtc/common_audio/wav_file.h b/webrtc/common_audio/wav_file.h index 42b0618..dda611b 100644 --- a/webrtc/common_audio/wav_file.h +++ b/webrtc/common_audio/wav_file.h @@ -8,60 +8,69 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef WEBRTC_COMMON_AUDIO_WAV_FILE_H_ -#define WEBRTC_COMMON_AUDIO_WAV_FILE_H_ - -#ifdef __cplusplus +#ifndef COMMON_AUDIO_WAV_FILE_H_ +#define COMMON_AUDIO_WAV_FILE_H_ #include <stdint.h> + #include <cstddef> #include <string> -#include "webrtc/base/constructormagic.h" +#include "common_audio/wav_header.h" +#include "rtc_base/system/file_wrapper.h" namespace webrtc { -// Interface to provide access to WAV file parameters. +// Interface to provide access WAV file parameters. class WavFile { public: + enum class SampleFormat { kInt16, kFloat }; + virtual ~WavFile() {} virtual int sample_rate() const = 0; - virtual int num_channels() const = 0; - virtual uint32_t num_samples() const = 0; - - // Returns a human-readable string containing the audio format. - std::string FormatAsString() const; + virtual size_t num_channels() const = 0; + virtual size_t num_samples() const = 0; }; -// Simple C++ class for writing 16-bit PCM WAV files. All error handling is -// by calls to RTC_CHECK(), making it unsuitable for anything but debug code. +// Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV +// files. All error handling is by calls to RTC_CHECK(), making it unsuitable +// for anything but debug code. class WavWriter final : public WavFile { public: - // Open a new WAV file for writing. - WavWriter(const std::string& filename, int sample_rate, int num_channels); - - // Close the WAV file, after writing its header. - ~WavWriter(); + // Opens a new WAV file for writing. + WavWriter(const std::string& filename, + int sample_rate, + size_t num_channels, + SampleFormat sample_format = SampleFormat::kInt16); + WavWriter(FileWrapper file, + int sample_rate, + size_t num_channels, + SampleFormat sample_format = SampleFormat::kInt16); + + // Closes the WAV file, after writing its header. + ~WavWriter() { Close(); } + + WavWriter(const WavWriter&) = delete; + WavWriter& operator=(const WavWriter&) = delete; // Write additional samples to the file. Each sample is in the range - // [-32768,32767], and there must be the previously specified number of + // [-32768.0,32767.0], and there must be the previously specified number of // interleaved channels. void WriteSamples(const float* samples, size_t num_samples); void WriteSamples(const int16_t* samples, size_t num_samples); int sample_rate() const override { return sample_rate_; } - int num_channels() const override { return num_channels_; } - uint32_t num_samples() const override { return num_samples_; } + size_t num_channels() const override { return num_channels_; } + size_t num_samples() const override { return num_samples_written_; } private: void Close(); const int sample_rate_; - const int num_channels_; - uint32_t num_samples_; // Total number of samples written to file. - FILE* file_handle_; // Output file, owned by this class - - RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter); + const size_t num_channels_; + size_t num_samples_written_; + WavFormat format_; + FileWrapper file_; }; // Follows the conventions of WavWriter. @@ -69,9 +78,16 @@ class WavReader final : public WavFile { public: // Opens an existing WAV file for reading. explicit WavReader(const std::string& filename); + explicit WavReader(FileWrapper file); // Close the WAV file. - ~WavReader(); + ~WavReader() { Close(); } + + WavReader(const WavReader&) = delete; + WavReader& operator=(const WavReader&) = delete; + + // Resets position to the beginning of the file. + void Reset(); // Returns the number of samples read. If this is less than requested, // verifies that the end of the file was reached. @@ -79,40 +95,21 @@ class WavReader final : public WavFile { size_t ReadSamples(size_t num_samples, int16_t* samples); int sample_rate() const override { return sample_rate_; } - int num_channels() const override { return num_channels_; } - uint32_t num_samples() const override { return num_samples_; } + size_t num_channels() const override { return num_channels_; } + size_t num_samples() const override { return num_samples_in_file_; } private: void Close(); int sample_rate_; - int num_channels_; - uint32_t num_samples_; // Total number of samples in the file. - uint32_t num_samples_remaining_; - FILE* file_handle_; // Input file, owned by this class. - - RTC_DISALLOW_COPY_AND_ASSIGN(WavReader); + size_t num_channels_; + WavFormat format_; + size_t num_samples_in_file_; + size_t num_unread_samples_; + FileWrapper file_; + int64_t + data_start_pos_; // Position in the file immediately after WAV header. }; } // namespace webrtc -extern "C" { -#endif // __cplusplus - -// C wrappers for the WavWriter class. -typedef struct rtc_WavWriter rtc_WavWriter; -rtc_WavWriter* rtc_WavOpen(const char* filename, - int sample_rate, - int num_channels); -void rtc_WavClose(rtc_WavWriter* wf); -void rtc_WavWriteSamples(rtc_WavWriter* wf, - const float* samples, - size_t num_samples); -int rtc_WavSampleRate(const rtc_WavWriter* wf); -int rtc_WavNumChannels(const rtc_WavWriter* wf); -uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_ +#endif // COMMON_AUDIO_WAV_FILE_H_ |