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 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | #include <cstddef> |
| 16 | #include <string> |
| 17 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/constructor_magic.h" |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 19 | #include "rtc_base/platform_file.h" |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 20 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 23 | // Interface to provide access to WAV file parameters. |
| 24 | class WavFile { |
| 25 | public: |
| 26 | virtual ~WavFile() {} |
| 27 | |
| 28 | virtual int sample_rate() const = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 29 | virtual size_t num_channels() const = 0; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 30 | virtual size_t num_samples() const = 0; |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 33 | // 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] | 34 | // 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] | 35 | class WavWriter final : public WavFile { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 36 | public: |
| 37 | // Open a new WAV file for writing. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 38 | 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] | 39 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 40 | // Open a new WAV file for writing. |
| 41 | WavWriter(rtc::PlatformFile file, int sample_rate, size_t num_channels); |
| 42 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 43 | // Close the WAV file, after writing its header. |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 44 | ~WavWriter() override; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 45 | |
| 46 | // Write additional samples to the file. Each sample is in the range |
| 47 | // [-32768,32767], and there must be the previously specified number of |
| 48 | // interleaved channels. |
| 49 | void WriteSamples(const float* samples, size_t num_samples); |
| 50 | void WriteSamples(const int16_t* samples, size_t num_samples); |
| 51 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 52 | int sample_rate() const override; |
| 53 | size_t num_channels() const override; |
| 54 | size_t num_samples() const override; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | void Close(); |
| 58 | const int sample_rate_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 59 | const size_t num_channels_; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 60 | size_t num_samples_; // Total number of samples written to file. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 61 | FILE* file_handle_; // Output file, owned by this class |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 62 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 63 | RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Follows the conventions of WavWriter. |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 67 | class WavReader final : public WavFile { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 68 | public: |
| 69 | // Opens an existing WAV file for reading. |
| 70 | explicit WavReader(const std::string& filename); |
| 71 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 72 | // Opens an existing WAV file for reading. |
| 73 | explicit WavReader(rtc::PlatformFile file); |
| 74 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 75 | // Close the WAV file. |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 76 | ~WavReader() override; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 77 | |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 78 | // Resets position to the beginning of the file. |
| 79 | void Reset(); |
| 80 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 81 | // Returns the number of samples read. If this is less than requested, |
| 82 | // verifies that the end of the file was reached. |
| 83 | size_t ReadSamples(size_t num_samples, float* samples); |
| 84 | size_t ReadSamples(size_t num_samples, int16_t* samples); |
| 85 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 86 | int sample_rate() const override; |
| 87 | size_t num_channels() const override; |
| 88 | size_t num_samples() const override; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | void Close(); |
| 92 | int sample_rate_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 93 | size_t num_channels_; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 94 | size_t num_samples_; // Total number of samples in the file. |
| 95 | size_t num_samples_remaining_; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 96 | FILE* file_handle_; // Input file, owned by this class. |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 97 | fpos_t data_start_pos_; // Position in the file immediately after WAV header. |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 98 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 99 | RTC_DISALLOW_COPY_AND_ASSIGN(WavReader); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace webrtc |
| 103 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 104 | #endif // COMMON_AUDIO_WAV_FILE_H_ |