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> |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 19 | static const size_t kWavHeaderSize = 44; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 20 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 21 | class ReadableWav { |
| 22 | public: |
| 23 | // Returns the number of bytes read. |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 24 | virtual size_t Read(void* buf, size_t num_bytes) = 0; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 25 | virtual bool SeekForward(uint32_t num_bytes) = 0; |
| 26 | virtual ~ReadableWav() = default; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 29 | enum WavFormat { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample |
| 31 | kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 32 | kWavFormatMuLaw = 7, // 8-bit ITU-T G.711 mu-law |
| 33 | }; |
| 34 | |
| 35 | // 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] | 36 | bool CheckWavParameters(size_t num_channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 37 | int sample_rate, |
| 38 | WavFormat format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 39 | size_t bytes_per_sample, |
| 40 | size_t num_samples); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 41 | |
| 42 | // Write a kWavHeaderSize bytes long WAV header to buf. The payload that |
| 43 | // follows the header is supposed to have the specified number of interleaved |
| 44 | // channels and contain the specified total number of samples of the specified |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 45 | // type. CHECKs the input parameters for validity. |
| 46 | void WriteWavHeader(uint8_t* buf, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 47 | size_t num_channels, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 48 | int sample_rate, |
| 49 | WavFormat format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 50 | size_t bytes_per_sample, |
| 51 | size_t num_samples); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 52 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 53 | // Read a WAV header from an implemented ReadableWav and parse the values into |
| 54 | // the provided output parameters. ReadableWav is used because the header can |
| 55 | // be variably sized. Returns false if the header is invalid. |
| 56 | bool ReadWavHeader(ReadableWav* readable, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 57 | size_t* num_channels, |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 58 | int* sample_rate, |
| 59 | WavFormat* format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 60 | size_t* bytes_per_sample, |
| 61 | size_t* num_samples); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 62 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 63 | } // namespace webrtc |
| 64 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 65 | #endif // COMMON_AUDIO_WAV_HEADER_H_ |