New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Based on the WAV file format documentation at
|
|
|
|
|
// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ and
|
|
|
|
|
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "common_audio/wav_header.h"
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <limits>
|
2014-12-16 20:17:21 +00:00
|
|
|
#include <string>
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2018-11-08 12:16:11 +01:00
|
|
|
#include "rtc_base/logging.h"
|
|
|
|
|
#include "rtc_base/sanitizer.h"
|
2018-07-25 16:05:48 +02:00
|
|
|
#include "rtc_base/system/arch.h"
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2014-10-31 21:51:03 +00:00
|
|
|
namespace {
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
|
|
|
|
|
#error "Code not working properly for big endian platforms."
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#pragma pack(2)
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
struct ChunkHeader {
|
|
|
|
|
uint32_t ID;
|
|
|
|
|
uint32_t Size;
|
|
|
|
|
};
|
2015-01-14 10:51:54 +00:00
|
|
|
static_assert(sizeof(ChunkHeader) == 8, "ChunkHeader size");
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
#pragma pack(2)
|
2018-11-08 12:16:11 +01:00
|
|
|
struct RiffHeader {
|
|
|
|
|
ChunkHeader header;
|
|
|
|
|
uint32_t Format;
|
|
|
|
|
};
|
2020-01-28 09:08:11 +01:00
|
|
|
static_assert(sizeof(RiffHeader) == sizeof(ChunkHeader) + 4, "RiffHeader size");
|
2018-11-08 12:16:11 +01:00
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
// We can't nest this definition in WavHeader, because VS2013 gives an error
|
|
|
|
|
// on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
|
2020-01-28 09:08:11 +01:00
|
|
|
#pragma pack(2)
|
|
|
|
|
struct FmtPcmSubchunk {
|
2014-10-31 21:51:03 +00:00
|
|
|
ChunkHeader header;
|
|
|
|
|
uint16_t AudioFormat;
|
|
|
|
|
uint16_t NumChannels;
|
|
|
|
|
uint32_t SampleRate;
|
|
|
|
|
uint32_t ByteRate;
|
|
|
|
|
uint16_t BlockAlign;
|
|
|
|
|
uint16_t BitsPerSample;
|
|
|
|
|
};
|
2020-01-28 09:08:11 +01:00
|
|
|
static_assert(sizeof(FmtPcmSubchunk) == 24, "FmtPcmSubchunk size");
|
|
|
|
|
const uint32_t kFmtPcmSubchunkSize =
|
|
|
|
|
sizeof(FmtPcmSubchunk) - sizeof(ChunkHeader);
|
|
|
|
|
|
|
|
|
|
// Pack struct to avoid additional padding bytes.
|
|
|
|
|
#pragma pack(2)
|
|
|
|
|
struct FmtIeeeFloatSubchunk {
|
|
|
|
|
ChunkHeader header;
|
|
|
|
|
uint16_t AudioFormat;
|
|
|
|
|
uint16_t NumChannels;
|
|
|
|
|
uint32_t SampleRate;
|
|
|
|
|
uint32_t ByteRate;
|
|
|
|
|
uint16_t BlockAlign;
|
|
|
|
|
uint16_t BitsPerSample;
|
|
|
|
|
uint16_t ExtensionSize;
|
|
|
|
|
};
|
|
|
|
|
static_assert(sizeof(FmtIeeeFloatSubchunk) == 26, "FmtIeeeFloatSubchunk size");
|
|
|
|
|
const uint32_t kFmtIeeeFloatSubchunkSize =
|
|
|
|
|
sizeof(FmtIeeeFloatSubchunk) - sizeof(ChunkHeader);
|
|
|
|
|
|
|
|
|
|
// Simple PCM wav header. It does not include chunks that are not essential to
|
|
|
|
|
// read audio samples.
|
|
|
|
|
#pragma pack(2)
|
|
|
|
|
struct WavHeaderPcm {
|
2018-11-08 12:16:11 +01:00
|
|
|
RiffHeader riff;
|
2020-01-28 09:08:11 +01:00
|
|
|
FmtPcmSubchunk fmt;
|
2014-10-31 21:51:03 +00:00
|
|
|
struct {
|
|
|
|
|
ChunkHeader header;
|
|
|
|
|
} data;
|
|
|
|
|
};
|
2020-01-28 09:08:11 +01:00
|
|
|
static_assert(sizeof(WavHeaderPcm) == kPcmWavHeaderSize,
|
|
|
|
|
"no padding in header");
|
|
|
|
|
|
|
|
|
|
// IEEE Float Wav header, includes extra chunks necessary for proper non-PCM
|
|
|
|
|
// WAV implementation.
|
|
|
|
|
#pragma pack(2)
|
|
|
|
|
struct WavHeaderIeeeFloat {
|
|
|
|
|
RiffHeader riff;
|
|
|
|
|
FmtIeeeFloatSubchunk fmt;
|
|
|
|
|
struct {
|
|
|
|
|
ChunkHeader header;
|
|
|
|
|
uint32_t SampleLength;
|
|
|
|
|
} fact;
|
|
|
|
|
struct {
|
|
|
|
|
ChunkHeader header;
|
|
|
|
|
} data;
|
|
|
|
|
};
|
|
|
|
|
static_assert(sizeof(WavHeaderIeeeFloat) == kIeeeFloatWavHeaderSize,
|
|
|
|
|
"no padding in header");
|
|
|
|
|
|
|
|
|
|
uint32_t PackFourCC(char a, char b, char c, char d) {
|
|
|
|
|
uint32_t packed_value =
|
|
|
|
|
static_cast<uint32_t>(a) | static_cast<uint32_t>(b) << 8 |
|
|
|
|
|
static_cast<uint32_t>(c) << 16 | static_cast<uint32_t>(d) << 24;
|
|
|
|
|
return packed_value;
|
2018-11-08 12:16:11 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
std::string ReadFourCC(uint32_t x) {
|
|
|
|
|
return std::string(reinterpret_cast<char*>(&x), 4);
|
2018-11-08 12:16:11 +01:00
|
|
|
}
|
2020-01-28 09:08:11 +01:00
|
|
|
|
|
|
|
|
uint16_t MapWavFormatToHeaderField(WavFormat format) {
|
|
|
|
|
switch (format) {
|
|
|
|
|
case WavFormat::kWavFormatPcm:
|
|
|
|
|
return 1;
|
|
|
|
|
case WavFormat::kWavFormatIeeeFloat:
|
|
|
|
|
return 3;
|
|
|
|
|
case WavFormat::kWavFormatALaw:
|
|
|
|
|
return 6;
|
|
|
|
|
case WavFormat::kWavFormatMuLaw:
|
|
|
|
|
return 7;
|
|
|
|
|
}
|
2020-11-08 00:49:37 +01:00
|
|
|
RTC_CHECK_NOTREACHED();
|
2018-11-08 12:16:11 +01:00
|
|
|
}
|
2020-01-28 09:08:11 +01:00
|
|
|
|
|
|
|
|
WavFormat MapHeaderFieldToWavFormat(uint16_t format_header_value) {
|
|
|
|
|
if (format_header_value == 1) {
|
|
|
|
|
return WavFormat::kWavFormatPcm;
|
|
|
|
|
}
|
|
|
|
|
if (format_header_value == 3) {
|
|
|
|
|
return WavFormat::kWavFormatIeeeFloat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RTC_CHECK(false) << "Unsupported WAV format";
|
2018-11-08 12:16:11 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
uint32_t RiffChunkSize(size_t bytes_in_payload, size_t header_size) {
|
|
|
|
|
return static_cast<uint32_t>(bytes_in_payload + header_size -
|
2018-11-08 12:16:11 +01:00
|
|
|
sizeof(ChunkHeader));
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
uint32_t ByteRate(size_t num_channels,
|
|
|
|
|
int sample_rate,
|
|
|
|
|
size_t bytes_per_sample) {
|
2018-11-08 12:16:11 +01:00
|
|
|
return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
uint16_t BlockAlign(size_t num_channels, size_t bytes_per_sample) {
|
2018-11-08 12:16:11 +01:00
|
|
|
return static_cast<uint16_t>(num_channels * bytes_per_sample);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 12:15:29 +02:00
|
|
|
// Finds a chunk having the sought ID. If found, then `readable` points to the
|
2018-11-08 12:16:11 +01:00
|
|
|
// first byte of the sought chunk data. If not found, the end of the file is
|
|
|
|
|
// reached.
|
2019-06-27 12:15:06 +02:00
|
|
|
bool FindWaveChunk(ChunkHeader* chunk_header,
|
2020-01-28 09:08:11 +01:00
|
|
|
WavHeaderReader* readable,
|
2018-11-08 12:16:11 +01:00
|
|
|
const std::string sought_chunk_id) {
|
|
|
|
|
RTC_DCHECK_EQ(sought_chunk_id.size(), 4);
|
2019-06-27 12:15:06 +02:00
|
|
|
while (true) {
|
2018-11-08 12:16:11 +01:00
|
|
|
if (readable->Read(chunk_header, sizeof(*chunk_header)) !=
|
|
|
|
|
sizeof(*chunk_header))
|
2019-06-27 12:15:06 +02:00
|
|
|
return false; // EOF.
|
2018-11-08 12:16:11 +01:00
|
|
|
if (ReadFourCC(chunk_header->ID) == sought_chunk_id)
|
2019-06-27 12:15:06 +02:00
|
|
|
return true; // Sought chunk found.
|
2018-11-08 12:16:11 +01:00
|
|
|
// Ignore current chunk by skipping its payload.
|
|
|
|
|
if (!readable->SeekForward(chunk_header->Size))
|
2019-06-27 12:15:06 +02:00
|
|
|
return false; // EOF or error.
|
2018-11-08 12:16:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
bool ReadFmtChunkData(FmtPcmSubchunk* fmt_subchunk, WavHeaderReader* readable) {
|
2018-11-08 12:16:11 +01:00
|
|
|
// Reads "fmt " chunk payload.
|
2020-01-28 09:08:11 +01:00
|
|
|
if (readable->Read(&(fmt_subchunk->AudioFormat), kFmtPcmSubchunkSize) !=
|
|
|
|
|
kFmtPcmSubchunkSize)
|
2018-11-08 12:16:11 +01:00
|
|
|
return false;
|
2020-01-28 09:08:11 +01:00
|
|
|
const uint32_t fmt_size = fmt_subchunk->header.Size;
|
|
|
|
|
if (fmt_size != kFmtPcmSubchunkSize) {
|
2018-11-08 12:16:11 +01:00
|
|
|
// There is an optional two-byte extension field permitted to be present
|
|
|
|
|
// with PCM, but which must be zero.
|
|
|
|
|
int16_t ext_size;
|
2020-01-28 09:08:11 +01:00
|
|
|
if (kFmtPcmSubchunkSize + sizeof(ext_size) != fmt_size)
|
2018-11-08 12:16:11 +01:00
|
|
|
return false;
|
|
|
|
|
if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size))
|
|
|
|
|
return false;
|
|
|
|
|
if (ext_size != 0)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
void WritePcmWavHeader(size_t num_channels,
|
|
|
|
|
int sample_rate,
|
|
|
|
|
size_t bytes_per_sample,
|
|
|
|
|
size_t num_samples,
|
|
|
|
|
uint8_t* buf,
|
|
|
|
|
size_t* header_size) {
|
|
|
|
|
RTC_CHECK(buf);
|
|
|
|
|
RTC_CHECK(header_size);
|
|
|
|
|
*header_size = kPcmWavHeaderSize;
|
|
|
|
|
auto header = rtc::MsanUninitialized<WavHeaderPcm>({});
|
|
|
|
|
const size_t bytes_in_payload = bytes_per_sample * num_samples;
|
|
|
|
|
|
|
|
|
|
header.riff.header.ID = PackFourCC('R', 'I', 'F', 'F');
|
|
|
|
|
header.riff.header.Size = RiffChunkSize(bytes_in_payload, *header_size);
|
|
|
|
|
header.riff.Format = PackFourCC('W', 'A', 'V', 'E');
|
|
|
|
|
header.fmt.header.ID = PackFourCC('f', 'm', 't', ' ');
|
|
|
|
|
header.fmt.header.Size = kFmtPcmSubchunkSize;
|
|
|
|
|
header.fmt.AudioFormat = MapWavFormatToHeaderField(WavFormat::kWavFormatPcm);
|
|
|
|
|
header.fmt.NumChannels = static_cast<uint16_t>(num_channels);
|
|
|
|
|
header.fmt.SampleRate = sample_rate;
|
|
|
|
|
header.fmt.ByteRate = ByteRate(num_channels, sample_rate, bytes_per_sample);
|
|
|
|
|
header.fmt.BlockAlign = BlockAlign(num_channels, bytes_per_sample);
|
|
|
|
|
header.fmt.BitsPerSample = static_cast<uint16_t>(8 * bytes_per_sample);
|
|
|
|
|
header.data.header.ID = PackFourCC('d', 'a', 't', 'a');
|
|
|
|
|
header.data.header.Size = static_cast<uint32_t>(bytes_in_payload);
|
|
|
|
|
|
|
|
|
|
// Do an extra copy rather than writing everything to buf directly, since buf
|
|
|
|
|
// might not be correctly aligned.
|
|
|
|
|
memcpy(buf, &header, *header_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteIeeeFloatWavHeader(size_t num_channels,
|
|
|
|
|
int sample_rate,
|
|
|
|
|
size_t bytes_per_sample,
|
|
|
|
|
size_t num_samples,
|
|
|
|
|
uint8_t* buf,
|
|
|
|
|
size_t* header_size) {
|
|
|
|
|
RTC_CHECK(buf);
|
|
|
|
|
RTC_CHECK(header_size);
|
|
|
|
|
*header_size = kIeeeFloatWavHeaderSize;
|
|
|
|
|
auto header = rtc::MsanUninitialized<WavHeaderIeeeFloat>({});
|
|
|
|
|
const size_t bytes_in_payload = bytes_per_sample * num_samples;
|
|
|
|
|
|
|
|
|
|
header.riff.header.ID = PackFourCC('R', 'I', 'F', 'F');
|
|
|
|
|
header.riff.header.Size = RiffChunkSize(bytes_in_payload, *header_size);
|
|
|
|
|
header.riff.Format = PackFourCC('W', 'A', 'V', 'E');
|
|
|
|
|
header.fmt.header.ID = PackFourCC('f', 'm', 't', ' ');
|
|
|
|
|
header.fmt.header.Size = kFmtIeeeFloatSubchunkSize;
|
|
|
|
|
header.fmt.AudioFormat =
|
|
|
|
|
MapWavFormatToHeaderField(WavFormat::kWavFormatIeeeFloat);
|
|
|
|
|
header.fmt.NumChannels = static_cast<uint16_t>(num_channels);
|
|
|
|
|
header.fmt.SampleRate = sample_rate;
|
|
|
|
|
header.fmt.ByteRate = ByteRate(num_channels, sample_rate, bytes_per_sample);
|
|
|
|
|
header.fmt.BlockAlign = BlockAlign(num_channels, bytes_per_sample);
|
|
|
|
|
header.fmt.BitsPerSample = static_cast<uint16_t>(8 * bytes_per_sample);
|
|
|
|
|
header.fmt.ExtensionSize = 0;
|
|
|
|
|
header.fact.header.ID = PackFourCC('f', 'a', 'c', 't');
|
|
|
|
|
header.fact.header.Size = 4;
|
|
|
|
|
header.fact.SampleLength = static_cast<uint32_t>(num_channels * num_samples);
|
|
|
|
|
header.data.header.ID = PackFourCC('d', 'a', 't', 'a');
|
|
|
|
|
header.data.header.Size = static_cast<uint32_t>(bytes_in_payload);
|
|
|
|
|
|
|
|
|
|
// Do an extra copy rather than writing everything to buf directly, since buf
|
|
|
|
|
// might not be correctly aligned.
|
|
|
|
|
memcpy(buf, &header, *header_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the number of bytes per sample for the format.
|
|
|
|
|
size_t GetFormatBytesPerSample(WavFormat format) {
|
|
|
|
|
switch (format) {
|
|
|
|
|
case WavFormat::kWavFormatPcm:
|
|
|
|
|
// Other values may be OK, but for now we're conservative.
|
|
|
|
|
return 2;
|
|
|
|
|
case WavFormat::kWavFormatALaw:
|
|
|
|
|
case WavFormat::kWavFormatMuLaw:
|
|
|
|
|
return 1;
|
|
|
|
|
case WavFormat::kWavFormatIeeeFloat:
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
2020-11-08 00:49:37 +01:00
|
|
|
RTC_CHECK_NOTREACHED();
|
2020-01-28 09:08:11 +01:00
|
|
|
}
|
2014-10-31 21:51:03 +00:00
|
|
|
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
bool CheckWavParameters(size_t num_channels,
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
int sample_rate,
|
|
|
|
|
WavFormat format,
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t bytes_per_sample,
|
|
|
|
|
size_t num_samples) {
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
// num_channels, sample_rate, and bytes_per_sample must be positive, must fit
|
|
|
|
|
// in their respective fields, and their product must fit in the 32-bit
|
|
|
|
|
// ByteRate field.
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
if (num_channels == 0 || sample_rate <= 0 || bytes_per_sample == 0)
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
return false;
|
|
|
|
|
if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
|
|
|
|
|
return false;
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
if (num_channels > std::numeric_limits<uint16_t>::max())
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
return false;
|
|
|
|
|
if (static_cast<uint64_t>(bytes_per_sample) * 8 >
|
|
|
|
|
std::numeric_limits<uint16_t>::max())
|
|
|
|
|
return false;
|
|
|
|
|
if (static_cast<uint64_t>(sample_rate) * num_channels * bytes_per_sample >
|
|
|
|
|
std::numeric_limits<uint32_t>::max())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// format and bytes_per_sample must agree.
|
|
|
|
|
switch (format) {
|
2020-01-28 09:08:11 +01:00
|
|
|
case WavFormat::kWavFormatPcm:
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
// Other values may be OK, but for now we're conservative:
|
|
|
|
|
if (bytes_per_sample != 1 && bytes_per_sample != 2)
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
2020-01-28 09:08:11 +01:00
|
|
|
case WavFormat::kWavFormatALaw:
|
|
|
|
|
case WavFormat::kWavFormatMuLaw:
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
if (bytes_per_sample != 1)
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
2020-01-28 09:08:11 +01:00
|
|
|
case WavFormat::kWavFormatIeeeFloat:
|
|
|
|
|
if (bytes_per_sample != 4)
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The number of bytes in the file, not counting the first ChunkHeader, must
|
|
|
|
|
// be less than 2^32; otherwise, the ChunkSize field overflows.
|
2020-01-28 09:08:11 +01:00
|
|
|
const size_t header_size = kPcmWavHeaderSize - sizeof(ChunkHeader);
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
const size_t max_samples =
|
|
|
|
|
(std::numeric_limits<uint32_t>::max() - header_size) / bytes_per_sample;
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
if (num_samples > max_samples)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Each channel must have the same number of samples.
|
|
|
|
|
if (num_samples % num_channels != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
bool CheckWavParameters(size_t num_channels,
|
|
|
|
|
int sample_rate,
|
|
|
|
|
WavFormat format,
|
|
|
|
|
size_t num_samples) {
|
|
|
|
|
return CheckWavParameters(num_channels, sample_rate, format,
|
|
|
|
|
GetFormatBytesPerSample(format), num_samples);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteWavHeader(size_t num_channels,
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
int sample_rate,
|
|
|
|
|
WavFormat format,
|
2020-01-28 09:08:11 +01:00
|
|
|
size_t num_samples,
|
|
|
|
|
uint8_t* buf,
|
|
|
|
|
size_t* header_size) {
|
|
|
|
|
RTC_CHECK(buf);
|
|
|
|
|
RTC_CHECK(header_size);
|
|
|
|
|
|
|
|
|
|
const size_t bytes_per_sample = GetFormatBytesPerSample(format);
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format,
|
|
|
|
|
bytes_per_sample, num_samples));
|
2020-01-28 09:08:11 +01:00
|
|
|
if (format == WavFormat::kWavFormatPcm) {
|
|
|
|
|
WritePcmWavHeader(num_channels, sample_rate, bytes_per_sample, num_samples,
|
|
|
|
|
buf, header_size);
|
|
|
|
|
} else {
|
|
|
|
|
RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat);
|
|
|
|
|
WriteIeeeFloatWavHeader(num_channels, sample_rate, bytes_per_sample,
|
|
|
|
|
num_samples, buf, header_size);
|
|
|
|
|
}
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
bool ReadWavHeader(WavHeaderReader* readable,
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t* num_channels,
|
2014-10-31 21:51:03 +00:00
|
|
|
int* sample_rate,
|
|
|
|
|
WavFormat* format,
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t* bytes_per_sample,
|
2020-01-28 09:08:11 +01:00
|
|
|
size_t* num_samples,
|
|
|
|
|
int64_t* data_start_pos) {
|
|
|
|
|
// Read using the PCM header, even though it might be float Wav file
|
|
|
|
|
auto header = rtc::MsanUninitialized<WavHeaderPcm>({});
|
2018-11-08 12:16:11 +01:00
|
|
|
|
|
|
|
|
// Read RIFF chunk.
|
|
|
|
|
if (readable->Read(&header.riff, sizeof(header.riff)) != sizeof(header.riff))
|
|
|
|
|
return false;
|
|
|
|
|
if (ReadFourCC(header.riff.header.ID) != "RIFF")
|
|
|
|
|
return false;
|
|
|
|
|
if (ReadFourCC(header.riff.Format) != "WAVE")
|
2014-12-16 20:17:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
2018-11-08 12:16:11 +01:00
|
|
|
// Find "fmt " and "data" chunks. While the official Wave file specification
|
|
|
|
|
// does not put requirements on the chunks order, it is uncommon to find the
|
|
|
|
|
// "data" chunk before the "fmt " one. The code below fails if this is not the
|
|
|
|
|
// case.
|
2019-06-27 12:15:06 +02:00
|
|
|
if (!FindWaveChunk(&header.fmt.header, readable, "fmt ")) {
|
2018-11-08 12:16:11 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Cannot find 'fmt ' chunk.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!ReadFmtChunkData(&header.fmt, readable)) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Cannot read 'fmt ' chunk.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-27 12:15:06 +02:00
|
|
|
if (!FindWaveChunk(&header.data.header, readable, "data")) {
|
2018-11-08 12:16:11 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Cannot find 'data' chunk.";
|
2014-12-16 20:17:21 +00:00
|
|
|
return false;
|
2018-11-08 12:16:11 +01:00
|
|
|
}
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
// Parse needed fields.
|
2020-01-28 09:08:11 +01:00
|
|
|
*format = MapHeaderFieldToWavFormat(header.fmt.AudioFormat);
|
|
|
|
|
*num_channels = header.fmt.NumChannels;
|
|
|
|
|
*sample_rate = header.fmt.SampleRate;
|
|
|
|
|
*bytes_per_sample = header.fmt.BitsPerSample / 8;
|
|
|
|
|
const size_t bytes_in_payload = header.data.header.Size;
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
if (*bytes_per_sample == 0)
|
2014-10-31 21:51:03 +00:00
|
|
|
return false;
|
|
|
|
|
*num_samples = bytes_in_payload / *bytes_per_sample;
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
const size_t header_size = *format == WavFormat::kWavFormatPcm
|
|
|
|
|
? kPcmWavHeaderSize
|
|
|
|
|
: kIeeeFloatWavHeaderSize;
|
|
|
|
|
|
|
|
|
|
if (header.riff.header.Size < RiffChunkSize(bytes_in_payload, header_size))
|
2014-10-31 21:51:03 +00:00
|
|
|
return false;
|
2020-01-28 09:08:11 +01:00
|
|
|
if (header.fmt.ByteRate !=
|
2014-10-31 21:51:03 +00:00
|
|
|
ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
|
|
|
|
|
return false;
|
2020-01-28 09:08:11 +01:00
|
|
|
if (header.fmt.BlockAlign != BlockAlign(*num_channels, *bytes_per_sample))
|
2014-10-31 21:51:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
2020-01-28 09:08:11 +01:00
|
|
|
if (!CheckWavParameters(*num_channels, *sample_rate, *format,
|
|
|
|
|
*bytes_per_sample, *num_samples)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*data_start_pos = readable->GetPosition();
|
|
|
|
|
return true;
|
2014-10-31 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
New utility class for easy debug dumping to WAV files
There are currently a number of places in the code where we dump audio
data in various stages of processing for debug purposes. Currently
these all write raw, uncompressed PCM files, which isn't supported by
the most common audio players, and requires the user to supply
metadata such as sample rate, sample size and endianness, etc.
This patch adds a simple class that makes it easy to write WAV files
instead. WAV files still contain the same uncompressed PCM data, but
they have a small header that contains all the requisite metadata, and
are supported by virtually all audio players.
Since some of the debug code that will be writing WAV files is written
in plain C, a C API is included as well.
R=andrew@webrtc.org, bjornv@webrtc.org, henrike@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/16809004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-08-20 07:42:46 +00:00
|
|
|
} // namespace webrtc
|