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