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