andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 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 WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |
| 12 | #define WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <cstddef> |
| 18 | #include <string> |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // Simple C++ class for writing 16-bit PCM WAV files. All error handling is |
| 23 | // by calls to CHECK(), making it unsuitable for anything but debug code. |
| 24 | class WavWriter { |
| 25 | public: |
| 26 | // Open a new WAV file for writing. |
| 27 | WavWriter(const std::string& filename, int sample_rate, int num_channels); |
| 28 | |
| 29 | // Close the WAV file, after writing its header. |
| 30 | ~WavWriter(); |
| 31 | |
| 32 | // Write additional samples to the file. Each sample is in the range |
| 33 | // [-32768,32767], and there must be the previously specified number of |
| 34 | // interleaved channels. |
| 35 | void WriteSamples(const float* samples, size_t num_samples); |
| 36 | void WriteSamples(const int16_t* samples, size_t num_samples); |
| 37 | |
| 38 | int sample_rate() const { return sample_rate_; } |
| 39 | int num_channels() const { return num_channels_; } |
| 40 | uint32_t num_samples() const { return num_samples_; } |
| 41 | |
| 42 | private: |
| 43 | void Close(); |
| 44 | const int sample_rate_; |
| 45 | const int num_channels_; |
| 46 | uint32_t num_samples_; // Total number of samples written to file. |
| 47 | FILE* file_handle_; // Output file, owned by this class |
| 48 | }; |
| 49 | |
| 50 | // Follows the conventions of WavWriter. |
| 51 | class WavReader { |
| 52 | public: |
| 53 | // Opens an existing WAV file for reading. |
| 54 | explicit WavReader(const std::string& filename); |
| 55 | |
| 56 | // Close the WAV file. |
| 57 | ~WavReader(); |
| 58 | |
| 59 | // Returns the number of samples read. If this is less than requested, |
| 60 | // verifies that the end of the file was reached. |
| 61 | size_t ReadSamples(size_t num_samples, float* samples); |
| 62 | size_t ReadSamples(size_t num_samples, int16_t* samples); |
| 63 | |
| 64 | int sample_rate() const { return sample_rate_; } |
| 65 | int num_channels() const { return num_channels_; } |
| 66 | uint32_t num_samples() const { return num_samples_; } |
| 67 | |
| 68 | private: |
| 69 | void Close(); |
| 70 | int sample_rate_; |
| 71 | int num_channels_; |
| 72 | uint32_t num_samples_; // Total number of samples in the file. |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame^] | 73 | uint32_t num_samples_remaining_; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 74 | FILE* file_handle_; // Input file, owned by this class. |
| 75 | }; |
| 76 | |
| 77 | } // namespace webrtc |
| 78 | |
| 79 | extern "C" { |
| 80 | #endif // __cplusplus |
| 81 | |
| 82 | // C wrappers for the WavWriter class. |
| 83 | typedef struct rtc_WavWriter rtc_WavWriter; |
| 84 | rtc_WavWriter* rtc_WavOpen(const char* filename, |
| 85 | int sample_rate, |
| 86 | int num_channels); |
| 87 | void rtc_WavClose(rtc_WavWriter* wf); |
| 88 | void rtc_WavWriteSamples(rtc_WavWriter* wf, |
| 89 | const float* samples, |
| 90 | size_t num_samples); |
| 91 | int rtc_WavSampleRate(const rtc_WavWriter* wf); |
| 92 | int rtc_WavNumChannels(const rtc_WavWriter* wf); |
| 93 | uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); |
| 94 | |
| 95 | #ifdef __cplusplus |
| 96 | } // extern "C" |
| 97 | #endif |
| 98 | |
| 99 | #endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |