blob: 2295fbe6adc7e2cdb3b544cf1fe7712089103365 [file] [log] [blame]
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_AUDIO_WAV_HEADER_H_
12#define COMMON_AUDIO_WAV_HEADER_H_
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000013
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000014#include <stddef.h>
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000015#include <stdint.h>
16
17namespace webrtc {
18
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000019static const size_t kWavHeaderSize = 44;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000020
andrew@webrtc.org048c5022014-12-16 20:17:21 +000021class 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.org877083c2014-08-20 07:42:46 +000028enum 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 Kasting69558702016-01-12 16:26:35 -080035bool CheckWavParameters(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000036 int sample_rate,
37 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080038 size_t bytes_per_sample,
39 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000040
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.orgf866b2d2014-11-03 18:20:06 +000044// type. CHECKs the input parameters for validity.
45void WriteWavHeader(uint8_t* buf,
Peter Kasting69558702016-01-12 16:26:35 -080046 size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000047 int sample_rate,
48 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080049 size_t bytes_per_sample,
50 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000051
andrew@webrtc.org048c5022014-12-16 20:17:21 +000052// 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.
55bool ReadWavHeader(ReadableWav* readable,
Peter Kasting69558702016-01-12 16:26:35 -080056 size_t* num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000057 int* sample_rate,
58 WavFormat* format,
pkasting25702cb2016-01-08 13:50:27 -080059 size_t* bytes_per_sample,
60 size_t* num_samples);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000061
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000062} // namespace webrtc
63
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#endif // COMMON_AUDIO_WAV_HEADER_H_