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