blob: 0c83d8d84dbd5567e2d44730d8eae99e9063dbbc [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.
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010024 virtual size_t Read(void* buf, size_t num_bytes) = 0;
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010025 virtual bool SeekForward(uint32_t num_bytes) = 0;
26 virtual ~ReadableWav() = default;
andrew@webrtc.org048c5022014-12-16 20:17:21 +000027};
28
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000029enum WavFormat {
Yves Gerey665174f2018-06-19 15:03:05 +020030 kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample
31 kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000032 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 Kasting69558702016-01-12 16:26:35 -080036bool CheckWavParameters(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000037 int sample_rate,
38 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080039 size_t bytes_per_sample,
40 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000041
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.orgf866b2d2014-11-03 18:20:06 +000045// type. CHECKs the input parameters for validity.
46void WriteWavHeader(uint8_t* buf,
Peter Kasting69558702016-01-12 16:26:35 -080047 size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000048 int sample_rate,
49 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080050 size_t bytes_per_sample,
51 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000052
andrew@webrtc.org048c5022014-12-16 20:17:21 +000053// 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.
56bool ReadWavHeader(ReadableWav* readable,
Peter Kasting69558702016-01-12 16:26:35 -080057 size_t* num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000058 int* sample_rate,
59 WavFormat* format,
pkasting25702cb2016-01-08 13:50:27 -080060 size_t* bytes_per_sample,
61 size_t* num_samples);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000062
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000063} // namespace webrtc
64
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#endif // COMMON_AUDIO_WAV_HEADER_H_