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