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 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 namespace webrtc {
18 
19 static const size_t kWavHeaderSize = 44;
20 
21 class ReadableWav {
22  public:
23   // Returns the number of bytes read.
24   size_t virtual Read(void* buf, size_t num_bytes) = 0;
~ReadableWav()25   virtual ~ReadableWav() {}
26 };
27 
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.
35 bool CheckWavParameters(int num_channels,
36                         int sample_rate,
37                         WavFormat format,
38                         int bytes_per_sample,
39                         uint32_t num_samples);
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
44 // type. CHECKs the input parameters for validity.
45 void WriteWavHeader(uint8_t* buf,
46                     int num_channels,
47                     int sample_rate,
48                     WavFormat format,
49                     int bytes_per_sample,
50                     uint32_t num_samples);
51 
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,
56                    int* num_channels,
57                    int* sample_rate,
58                    WavFormat* format,
59                    int* bytes_per_sample,
60                    uint32_t* num_samples);
61 
62 }  // namespace webrtc
63 
64 #endif  // WEBRTC_COMMON_AUDIO_WAV_HEADER_H_
65