blob: a519ba556f54f0365494beb637d8d6b7ba249670 [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;
25 // Returns true if the end-of-file has been reached.
26 virtual bool Eof() const = 0;
27 virtual bool SeekForward(uint32_t num_bytes) = 0;
28 virtual ~ReadableWav() = default;
andrew@webrtc.org048c5022014-12-16 20:17:21 +000029};
30
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000031enum WavFormat {
Yves Gerey665174f2018-06-19 15:03:05 +020032 kWavFormatPcm = 1, // PCM, each sample of size bytes_per_sample
33 kWavFormatALaw = 6, // 8-bit ITU-T G.711 A-law
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000034 kWavFormatMuLaw = 7, // 8-bit ITU-T G.711 mu-law
35};
36
37// Return true if the given parameters will make a well-formed WAV header.
Peter Kasting69558702016-01-12 16:26:35 -080038bool CheckWavParameters(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000039 int sample_rate,
40 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080041 size_t bytes_per_sample,
42 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000043
44// Write a kWavHeaderSize bytes long WAV header to buf. The payload that
45// follows the header is supposed to have the specified number of interleaved
46// channels and contain the specified total number of samples of the specified
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +000047// type. CHECKs the input parameters for validity.
48void WriteWavHeader(uint8_t* buf,
Peter Kasting69558702016-01-12 16:26:35 -080049 size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000050 int sample_rate,
51 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080052 size_t bytes_per_sample,
53 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000054
andrew@webrtc.org048c5022014-12-16 20:17:21 +000055// Read a WAV header from an implemented ReadableWav and parse the values into
56// the provided output parameters. ReadableWav is used because the header can
57// be variably sized. Returns false if the header is invalid.
58bool ReadWavHeader(ReadableWav* readable,
Peter Kasting69558702016-01-12 16:26:35 -080059 size_t* num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000060 int* sample_rate,
61 WavFormat* format,
pkasting25702cb2016-01-08 13:50:27 -080062 size_t* bytes_per_sample,
63 size_t* num_samples);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000064
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000065} // namespace webrtc
66
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#endif // COMMON_AUDIO_WAV_HEADER_H_