kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +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_HEADER_H_ |
| 12 | #define COMMON_AUDIO_WAV_HEADER_H_ |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 13 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 14 | #include <stddef.h> |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 15 | #include <stdint.h> |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | |
| 18 | #include "rtc_base/checks.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 22 | // Interface providing header reading functionality. |
| 23 | class WavHeaderReader { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 24 | public: |
| 25 | // Returns the number of bytes read. |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 26 | virtual size_t Read(void* buf, size_t num_bytes) = 0; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 27 | virtual bool SeekForward(uint32_t num_bytes) = 0; |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 28 | virtual ~WavHeaderReader() = default; |
| 29 | virtual int64_t GetPosition() = 0; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 32 | // Possible WAV formats. |
| 33 | enum class WavFormat { |
| 34 | kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample. |
| 35 | kWavFormatIeeeFloat = 3, // IEEE float. |
| 36 | kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law. |
| 37 | kWavFormatMuLaw = 7, // 8-bit ITU-T G.711 mu-law. |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 40 | // Header sizes for supported WAV formats. |
| 41 | constexpr size_t kPcmWavHeaderSize = 44; |
| 42 | constexpr size_t kIeeeFloatWavHeaderSize = 58; |
| 43 | |
| 44 | // Returns the size of the WAV header for the specified format. |
| 45 | constexpr size_t WavHeaderSize(WavFormat format) { |
| 46 | if (format == WavFormat::kWavFormatPcm) { |
| 47 | return kPcmWavHeaderSize; |
| 48 | } |
| 49 | RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat); |
| 50 | return kIeeeFloatWavHeaderSize; |
| 51 | } |
| 52 | |
| 53 | // Returns the maximum size of the supported WAV formats. |
| 54 | constexpr size_t MaxWavHeaderSize() { |
| 55 | return std::max(WavHeaderSize(WavFormat::kWavFormatPcm), |
| 56 | WavHeaderSize(WavFormat::kWavFormatIeeeFloat)); |
| 57 | } |
| 58 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 59 | // Return true if the given parameters will make a well-formed WAV header. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 60 | bool CheckWavParameters(size_t num_channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 61 | int sample_rate, |
| 62 | WavFormat format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 63 | size_t num_samples); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 64 | |
| 65 | // Write a kWavHeaderSize bytes long WAV header to buf. The payload that |
| 66 | // follows the header is supposed to have the specified number of interleaved |
| 67 | // channels and contain the specified total number of samples of the specified |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 68 | // type. The size of the header is returned in header_size. CHECKs the input |
| 69 | // parameters for validity. |
| 70 | void WriteWavHeader(size_t num_channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 71 | int sample_rate, |
| 72 | WavFormat format, |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 73 | size_t num_samples, |
| 74 | uint8_t* buf, |
| 75 | size_t* header_size); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 76 | |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 77 | // Read a WAV header from an implemented WavHeaderReader and parse the values |
| 78 | // into the provided output parameters. WavHeaderReader is used because the |
| 79 | // header can be variably sized. Returns false if the header is invalid. |
| 80 | bool ReadWavHeader(WavHeaderReader* readable, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 81 | size_t* num_channels, |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 82 | int* sample_rate, |
| 83 | WavFormat* format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 84 | size_t* bytes_per_sample, |
Per Åhgren | 5dca3f1 | 2020-01-28 09:08:11 +0100 | [diff] [blame] | 85 | size_t* num_samples, |
| 86 | int64_t* data_start_pos); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 87 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 88 | } // namespace webrtc |
| 89 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 90 | #endif // COMMON_AUDIO_WAV_HEADER_H_ |