diff options
author | kwiberg@webrtc.org <kwiberg@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d> | 2014-08-20 07:42:46 +0000 |
---|---|---|
committer | kwiberg@webrtc.org <kwiberg@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d> | 2014-08-20 07:42:46 +0000 |
commit | ddd80b5eacf50958c4335eb5a535e07aa8b05369 (patch) | |
tree | be2ffcbff221826488472f8a807e8385566a67ee /common_audio/wav_header_unittest.cc | |
parent | 39377b82ddae9a3eb0b679a20100b9bdf98759e1 (diff) | |
download | webrtc-ddd80b5eacf50958c4335eb5a535e07aa8b05369.tar.gz |
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/webrtc@6932 4adac7df-926f-26a2-2b94-8c16560cd09d
Diffstat (limited to 'common_audio/wav_header_unittest.cc')
-rw-r--r-- | common_audio/wav_header_unittest.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/common_audio/wav_header_unittest.cc b/common_audio/wav_header_unittest.cc new file mode 100644 index 00000000..f05160ea --- /dev/null +++ b/common_audio/wav_header_unittest.cc @@ -0,0 +1,77 @@ +/* + * 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. + */ + +#include <limits> + +#include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/common_audio/wav_header.h" +#include "webrtc/system_wrappers/interface/compile_assert.h" + +// Try various choices of WAV header parameters, and make sure that the good +// ones are accepted and the bad ones rejected. +TEST(WavHeaderTest, CheckWavParameters) { + // Try some really stupid values for one parameter at a time. + EXPECT_TRUE(webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 1, 0)); + EXPECT_FALSE( + webrtc::CheckWavParameters(0, 8000, webrtc::kWavFormatPcm, 1, 0)); + EXPECT_FALSE( + webrtc::CheckWavParameters(-1, 8000, webrtc::kWavFormatPcm, 1, 0)); + EXPECT_FALSE(webrtc::CheckWavParameters(1, 0, webrtc::kWavFormatPcm, 1, 0)); + EXPECT_FALSE(webrtc::CheckWavParameters(1, 8000, webrtc::WavFormat(0), 1, 0)); + EXPECT_FALSE( + webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 0, 0)); + + // Try invalid format/bytes-per-sample combinations. + EXPECT_TRUE(webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 2, 0)); + EXPECT_FALSE( + webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatPcm, 4, 0)); + EXPECT_FALSE( + webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatALaw, 2, 0)); + EXPECT_FALSE( + webrtc::CheckWavParameters(1, 8000, webrtc::kWavFormatMuLaw, 2, 0)); + + // Too large values. + EXPECT_FALSE(webrtc::CheckWavParameters( + 1 << 20, 1 << 20, webrtc::kWavFormatPcm, 1, 0)); + EXPECT_FALSE(webrtc::CheckWavParameters( + 1, 8000, webrtc::kWavFormatPcm, 1, std::numeric_limits<uint32_t>::max())); + + // Not the same number of samples for each channel. + EXPECT_FALSE( + webrtc::CheckWavParameters(3, 8000, webrtc::kWavFormatPcm, 1, 5)); +} + +// Try writing a WAV header and make sure it looks OK. +TEST(WavHeaderTest, WriteWavHeader) { + static const int kSize = 4 + webrtc::kWavHeaderSize + 4; + uint8_t buf[kSize]; + memset(buf, 0xa4, sizeof(buf)); + webrtc::WriteWavHeader( + buf + 4, 17, 12345, webrtc::kWavFormatALaw, 1, 123457689); + static const uint8_t kExpectedBuf[] = { + 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes before header + 'R', 'I', 'F', 'F', + 0xbd, 0xd0, 0x5b, 0x07, // size of whole file - 8: 123457689 + 44 - 8 + 'W', 'A', 'V', 'E', + 'f', 'm', 't', ' ', + 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 + 6, 0, // format: A-law (6) + 17, 0, // channels: 17 + 0x39, 0x30, 0, 0, // sample rate: 12345 + 0xc9, 0x33, 0x03, 0, // byte rate: 1 * 17 * 12345 + 17, 0, // block align: NumChannels * BytesPerSample + 8, 0, // bits per sample: 1 * 8 + 'd', 'a', 't', 'a', + 0x99, 0xd0, 0x5b, 0x07, // size of payload: 123457689 + 0xa4, 0xa4, 0xa4, 0xa4, // untouched bytes after header + }; + COMPILE_ASSERT(sizeof(kExpectedBuf) == kSize, buf_size); + EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize)); +} |