blob: 2cccd7d34b143f3df8cc3b4e0958fb79c38a9d9c [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>
Per Åhgren5dca3f12020-01-28 09:08:11 +010016#include <algorithm>
17
18#include "rtc_base/checks.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000019
20namespace webrtc {
21
Per Åhgren5dca3f12020-01-28 09:08:11 +010022// Interface providing header reading functionality.
23class WavHeaderReader {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000024 public:
25 // Returns the number of bytes read.
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010026 virtual size_t Read(void* buf, size_t num_bytes) = 0;
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010027 virtual bool SeekForward(uint32_t num_bytes) = 0;
Per Åhgren5dca3f12020-01-28 09:08:11 +010028 virtual ~WavHeaderReader() = default;
29 virtual int64_t GetPosition() = 0;
andrew@webrtc.org048c5022014-12-16 20:17:21 +000030};
31
Per Åhgren5dca3f12020-01-28 09:08:11 +010032// Possible WAV formats.
33enum 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.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000038};
39
Per Åhgren5dca3f12020-01-28 09:08:11 +010040// Header sizes for supported WAV formats.
41constexpr size_t kPcmWavHeaderSize = 44;
42constexpr size_t kIeeeFloatWavHeaderSize = 58;
43
44// Returns the size of the WAV header for the specified format.
45constexpr 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.
54constexpr size_t MaxWavHeaderSize() {
55 return std::max(WavHeaderSize(WavFormat::kWavFormatPcm),
56 WavHeaderSize(WavFormat::kWavFormatIeeeFloat));
57}
58
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000059// Return true if the given parameters will make a well-formed WAV header.
Peter Kasting69558702016-01-12 16:26:35 -080060bool CheckWavParameters(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000061 int sample_rate,
62 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080063 size_t num_samples);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000064
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
Per Åhgren5dca3f12020-01-28 09:08:11 +010068// type. The size of the header is returned in header_size. CHECKs the input
69// parameters for validity.
70void WriteWavHeader(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000071 int sample_rate,
72 WavFormat format,
Per Åhgren5dca3f12020-01-28 09:08:11 +010073 size_t num_samples,
74 uint8_t* buf,
75 size_t* header_size);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000076
Per Åhgren5dca3f12020-01-28 09:08:11 +010077// 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.
80bool ReadWavHeader(WavHeaderReader* readable,
Peter Kasting69558702016-01-12 16:26:35 -080081 size_t* num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000082 int* sample_rate,
83 WavFormat* format,
pkasting25702cb2016-01-08 13:50:27 -080084 size_t* bytes_per_sample,
Per Åhgren5dca3f12020-01-28 09:08:11 +010085 size_t* num_samples,
86 int64_t* data_start_pos);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000087
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000088} // namespace webrtc
89
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020090#endif // COMMON_AUDIO_WAV_HEADER_H_