• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 COMMON_AUDIO_WAV_HEADER_H_
12 #define COMMON_AUDIO_WAV_HEADER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <algorithm>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 // Interface providing header reading functionality.
23 class WavHeaderReader {
24  public:
25   // Returns the number of bytes read.
26   virtual size_t Read(void* buf, size_t num_bytes) = 0;
27   virtual bool SeekForward(uint32_t num_bytes) = 0;
28   virtual ~WavHeaderReader() = default;
29   virtual int64_t GetPosition() = 0;
30 };
31 
32 // Possible WAV formats.
33 enum class WavFormat {
34   kWavFormatPcm = 1,        // PCM, each sample of size bytes_per_sample.
35   kWavFormatIeeeFloat = 3,  // IEEE float.
36   kWavFormatALaw = 6,       // 8-bit ITU-T G.711 A-law.
37   kWavFormatMuLaw = 7,      // 8-bit ITU-T G.711 mu-law.
38 };
39 
40 // Header sizes for supported WAV formats.
41 constexpr size_t kPcmWavHeaderSize = 44;
42 constexpr size_t kIeeeFloatWavHeaderSize = 58;
43 
44 // Returns the size of the WAV header for the specified format.
WavHeaderSize(WavFormat format)45 constexpr size_t WavHeaderSize(WavFormat format) {
46   if (format == WavFormat::kWavFormatPcm) {
47     return kPcmWavHeaderSize;
48   }
49   RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat);
50   return kIeeeFloatWavHeaderSize;
51 }
52 
53 // Returns the maximum size of the supported WAV formats.
MaxWavHeaderSize()54 constexpr size_t MaxWavHeaderSize() {
55   return std::max(WavHeaderSize(WavFormat::kWavFormatPcm),
56                   WavHeaderSize(WavFormat::kWavFormatIeeeFloat));
57 }
58 
59 // Return true if the given parameters will make a well-formed WAV header.
60 bool CheckWavParameters(size_t num_channels,
61                         int sample_rate,
62                         WavFormat format,
63                         size_t num_samples);
64 
65 // Write a kWavHeaderSize bytes long WAV header to buf. The payload that
66 // follows the header is supposed to have the specified number of interleaved
67 // channels and contain the specified total number of samples of the specified
68 // type. The size of the header is returned in header_size. CHECKs the input
69 // parameters for validity.
70 void WriteWavHeader(size_t num_channels,
71                     int sample_rate,
72                     WavFormat format,
73                     size_t num_samples,
74                     uint8_t* buf,
75                     size_t* header_size);
76 
77 // Read a WAV header from an implemented WavHeaderReader and parse the values
78 // into the provided output parameters. WavHeaderReader is used because the
79 // header can be variably sized. Returns false if the header is invalid.
80 bool ReadWavHeader(WavHeaderReader* readable,
81                    size_t* num_channels,
82                    int* sample_rate,
83                    WavFormat* format,
84                    size_t* bytes_per_sample,
85                    size_t* num_samples,
86                    int64_t* data_start_pos);
87 
88 }  // namespace webrtc
89 
90 #endif  // COMMON_AUDIO_WAV_HEADER_H_
91