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> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 16 | #include <cstddef> |
| 17 | #include <string> |
| 18 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 19 | #include "common_audio/wav_header.h" |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 20 | #include "rtc_base/system/file_wrapper.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 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 24 | // Interface to provide access WAV file parameters. |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 25 | class WavFile { |
| 26 | public: |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 27 | enum class SampleFormat { kInt16, kFloat }; |
| 28 | |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 29 | virtual ~WavFile() {} |
| 30 | |
| 31 | virtual int sample_rate() const = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 32 | virtual size_t num_channels() const = 0; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 33 | virtual size_t num_samples() const = 0; |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 36 | // Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV |
| 37 | // files. All error handling is by calls to RTC_CHECK(), making it unsuitable |
| 38 | // 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: |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 41 | // Opens a new WAV file for writing. |
| 42 | WavWriter(const std::string& filename, |
| 43 | int sample_rate, |
| 44 | size_t num_channels, |
| 45 | SampleFormat sample_format = SampleFormat::kInt16); |
| 46 | WavWriter(FileWrapper file, |
| 47 | int sample_rate, |
| 48 | size_t num_channels, |
| 49 | SampleFormat sample_format = SampleFormat::kInt16); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 50 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 51 | // Closes the WAV file, after writing its header. |
| 52 | ~WavWriter() { Close(); } |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 53 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 54 | WavWriter(const WavWriter&) = delete; |
| 55 | WavWriter& operator=(const WavWriter&) = delete; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 56 | |
| 57 | // Write additional samples to the file. Each sample is in the range |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 58 | // [-32768.0,32767.0], and there must be the previously specified number of |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 59 | // interleaved channels. |
| 60 | void WriteSamples(const float* samples, size_t num_samples); |
| 61 | void WriteSamples(const int16_t* samples, size_t num_samples); |
| 62 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 63 | int sample_rate() const override { return sample_rate_; } |
| 64 | size_t num_channels() const override { return num_channels_; } |
| 65 | size_t num_samples() const override { return num_samples_written_; } |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | void Close(); |
| 69 | const int sample_rate_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 70 | const size_t num_channels_; |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 71 | size_t num_samples_written_; |
| 72 | WavFormat format_; |
| 73 | FileWrapper file_; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | // Follows the conventions of WavWriter. |
Andrew MacDonald | bc2296d | 2015-08-24 17:29:26 -0700 | [diff] [blame] | 77 | class WavReader final : public WavFile { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 78 | public: |
| 79 | // Opens an existing WAV file for reading. |
| 80 | explicit WavReader(const std::string& filename); |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 81 | explicit WavReader(FileWrapper file); |
| 82 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 83 | // Close the WAV file. |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 84 | ~WavReader() { Close(); } |
| 85 | |
| 86 | WavReader(const WavReader&) = delete; |
| 87 | WavReader& operator=(const WavReader&) = delete; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 88 | |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 89 | // Resets position to the beginning of the file. |
| 90 | void Reset(); |
| 91 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 92 | // Returns the number of samples read. If this is less than requested, |
| 93 | // verifies that the end of the file was reached. |
| 94 | size_t ReadSamples(size_t num_samples, float* samples); |
| 95 | size_t ReadSamples(size_t num_samples, int16_t* samples); |
| 96 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 97 | int sample_rate() const override { return sample_rate_; } |
| 98 | size_t num_channels() const override { return num_channels_; } |
| 99 | size_t num_samples() const override { return num_samples_in_file_; } |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | void Close(); |
| 103 | int sample_rate_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 104 | size_t num_channels_; |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 105 | WavFormat format_; |
| 106 | size_t num_samples_in_file_; |
| 107 | size_t num_unread_samples_; |
| 108 | FileWrapper file_; |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 109 | int64_t |
| 110 | data_start_pos_; // Position in the file immediately after WAV header. |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace webrtc |
| 114 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 115 | #endif // COMMON_AUDIO_WAV_FILE_H_ |