kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 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 | // MSVC++ requires this to be set before any other includes to get M_PI. |
| 12 | #define _USE_MATH_DEFINES |
| 13 | |
| 14 | #include <cmath> |
| 15 | #include <limits> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "common_audio/wav_file.h" |
| 18 | #include "common_audio/wav_header.h" |
| 19 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "test/testsupport/file_utils.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 21 | |
Alessio Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 22 | // WavWriterTest.CPPFileDescriptor and WavWriterTest.CPP flaky on Mac. |
| 23 | // See webrtc:9247. |
| 24 | #if defined(WEBRTC_MAC) |
| 25 | #define MAYBE_CPP DISABLED_CPP |
| 26 | #define MAYBE_CPPFileDescriptor DISABLED_CPPFileDescriptor |
| 27 | #else |
| 28 | #define MAYBE_CPP CPP |
| 29 | #define MAYBE_CPPFileDescriptor CPPFileDescriptor |
| 30 | #endif |
| 31 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 32 | namespace webrtc { |
| 33 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 34 | static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; |
| 35 | |
| 36 | // Write a tiny WAV file with the C++ interface and verify the result. |
Alessio Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 37 | TEST(WavWriterTest, MAYBE_CPP) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 38 | const std::string outfile = test::OutputPath() + "wavtest1.wav"; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 39 | static const size_t kNumSamples = 3; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 40 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 41 | WavWriter w(outfile, 14099, 1); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 42 | EXPECT_EQ(14099, w.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 43 | EXPECT_EQ(1u, w.num_channels()); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 44 | EXPECT_EQ(0u, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 45 | w.WriteSamples(kSamples, kNumSamples); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 46 | EXPECT_EQ(kNumSamples, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 47 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 48 | // Write some extra "metadata" to the file that should be silently ignored |
| 49 | // by WavReader. We don't use WavWriter directly for this because it doesn't |
| 50 | // support metadata. |
| 51 | static const uint8_t kMetadata[] = {101, 202}; |
| 52 | { |
| 53 | FILE* f = fopen(outfile.c_str(), "ab"); |
| 54 | ASSERT_TRUE(f); |
| 55 | ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f)); |
| 56 | fclose(f); |
| 57 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 58 | static const uint8_t kExpectedContents[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 59 | // clang-format off |
| 60 | // clang formatting doesn't respect inline comments. |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 61 | 'R', 'I', 'F', 'F', |
| 62 | 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8 |
| 63 | 'W', 'A', 'V', 'E', |
| 64 | 'f', 'm', 't', ' ', |
| 65 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 66 | 1, 0, // format: PCM (1) |
| 67 | 1, 0, // channels: 1 |
| 68 | 0x13, 0x37, 0, 0, // sample rate: 14099 |
| 69 | 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099 |
| 70 | 2, 0, // block align: NumChannels * BytesPerSample |
| 71 | 16, 0, // bits per sample: 2 * 8 |
| 72 | 'd', 'a', 't', 'a', |
| 73 | 6, 0, 0, 0, // size of payload: 6 |
| 74 | 0, 0, // first sample: 0.0 |
| 75 | 10, 0, // second sample: 10.0 |
| 76 | 0xff, 0x7f, // third sample: 4e4 (saturated) |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 77 | kMetadata[0], kMetadata[1], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | // clang-format on |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 79 | }; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 80 | static const size_t kContentSize = |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 81 | kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 82 | static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 83 | EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 84 | FILE* f = fopen(outfile.c_str(), "rb"); |
| 85 | ASSERT_TRUE(f); |
| 86 | uint8_t contents[kContentSize]; |
| 87 | ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 88 | EXPECT_EQ(0, fclose(f)); |
| 89 | EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 90 | |
| 91 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 92 | WavReader r(outfile); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 93 | EXPECT_EQ(14099, r.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 94 | EXPECT_EQ(1u, r.num_channels()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 95 | EXPECT_EQ(kNumSamples, r.num_samples()); |
| 96 | static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0}; |
| 97 | float samples[kNumSamples]; |
| 98 | EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples)); |
| 99 | EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples))); |
| 100 | EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples)); |
| 101 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Write a tiny WAV file with the C interface and verify the result. |
| 105 | TEST(WavWriterTest, C) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 106 | const std::string outfile = test::OutputPath() + "wavtest2.wav"; |
| 107 | rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 108 | EXPECT_EQ(11904, rtc_WavSampleRate(w)); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 109 | EXPECT_EQ(2u, rtc_WavNumChannels(w)); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 110 | EXPECT_EQ(0u, rtc_WavNumSamples(w)); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 111 | static const size_t kNumSamples = 4; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 112 | rtc_WavWriteSamples(w, &kSamples[0], 2); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 113 | EXPECT_EQ(2u, rtc_WavNumSamples(w)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 114 | rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 115 | EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 116 | rtc_WavClose(w); |
| 117 | static const uint8_t kExpectedContents[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | // clang-format off |
| 119 | // clang formatting doesn't respect inline comments. |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 120 | 'R', 'I', 'F', 'F', |
| 121 | 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8 |
| 122 | 'W', 'A', 'V', 'E', |
| 123 | 'f', 'm', 't', ' ', |
| 124 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 125 | 1, 0, // format: PCM (1) |
| 126 | 2, 0, // channels: 2 |
| 127 | 0x80, 0x2e, 0, 0, // sample rate: 11904 |
| 128 | 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904 |
| 129 | 4, 0, // block align: NumChannels * BytesPerSample |
| 130 | 16, 0, // bits per sample: 2 * 8 |
| 131 | 'd', 'a', 't', 'a', |
| 132 | 8, 0, 0, 0, // size of payload: 8 |
| 133 | 0, 0, // first sample: 0.0 |
| 134 | 10, 0, // second sample: 10.0 |
| 135 | 0xff, 0x7f, // third sample: 4e4 (saturated) |
| 136 | 0, 0x80, // fourth sample: -1e9 (saturated) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 137 | // clang-format on |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 138 | }; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 139 | static const size_t kContentSize = |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 140 | kWavHeaderSize + kNumSamples * sizeof(int16_t); |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 141 | static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 142 | EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 143 | FILE* f = fopen(outfile.c_str(), "rb"); |
| 144 | ASSERT_TRUE(f); |
| 145 | uint8_t contents[kContentSize]; |
| 146 | ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 147 | EXPECT_EQ(0, fclose(f)); |
| 148 | EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
| 149 | } |
| 150 | |
| 151 | // Write a larger WAV file. You can listen to this file to sanity-check it. |
| 152 | TEST(WavWriterTest, LargeFile) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 153 | std::string outfile = test::OutputPath() + "wavtest3.wav"; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 154 | static const int kSampleRate = 8000; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 155 | static const size_t kNumChannels = 2; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 156 | static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 157 | float samples[kNumSamples]; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 158 | for (size_t i = 0; i < kNumSamples; i += kNumChannels) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 159 | // A nice periodic beeping sound. |
| 160 | static const double kToneHz = 440; |
| 161 | const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); |
| 162 | const double x = |
| 163 | std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI); |
| 164 | samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x; |
| 165 | samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x; |
| 166 | } |
| 167 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 168 | WavWriter w(outfile, kSampleRate, kNumChannels); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 169 | EXPECT_EQ(kSampleRate, w.sample_rate()); |
| 170 | EXPECT_EQ(kNumChannels, w.num_channels()); |
| 171 | EXPECT_EQ(0u, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 172 | w.WriteSamples(samples, kNumSamples); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 173 | EXPECT_EQ(kNumSamples, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 174 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 175 | EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize, |
| 176 | test::GetFileSize(outfile)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 177 | |
| 178 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 179 | WavReader r(outfile); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 180 | EXPECT_EQ(kSampleRate, r.sample_rate()); |
| 181 | EXPECT_EQ(kNumChannels, r.num_channels()); |
| 182 | EXPECT_EQ(kNumSamples, r.num_samples()); |
| 183 | |
| 184 | float read_samples[kNumSamples]; |
| 185 | EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples)); |
| 186 | for (size_t i = 0; i < kNumSamples; ++i) |
| 187 | EXPECT_NEAR(samples[i], read_samples[i], 1); |
| 188 | |
| 189 | EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples)); |
| 190 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 191 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 192 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 193 | // Write a tiny WAV file with the the std::FILE interface and verify the |
| 194 | // result. |
Alessio Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 195 | TEST(WavWriterTest, MAYBE_CPPFileDescriptor) { |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 196 | const std::string outfile = test::OutputPath() + "wavtest1.wav"; |
| 197 | static constexpr size_t kNumSamples = 3; |
| 198 | { |
| 199 | WavWriter w(rtc::CreatePlatformFile(outfile), 14099, 1); |
| 200 | EXPECT_EQ(14099, w.sample_rate()); |
| 201 | EXPECT_EQ(1u, w.num_channels()); |
| 202 | EXPECT_EQ(0u, w.num_samples()); |
| 203 | w.WriteSamples(kSamples, kNumSamples); |
| 204 | EXPECT_EQ(kNumSamples, w.num_samples()); |
| 205 | } |
| 206 | // Write some extra "metadata" to the file that should be silently ignored |
| 207 | // by WavReader. We don't use WavWriter directly for this because it doesn't |
| 208 | // support metadata. |
| 209 | static constexpr uint8_t kMetadata[] = {101, 202}; |
| 210 | { |
| 211 | FILE* f = fopen(outfile.c_str(), "ab"); |
| 212 | ASSERT_TRUE(f); |
| 213 | ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f)); |
| 214 | fclose(f); |
| 215 | } |
| 216 | static const uint8_t kExpectedContents[] = { |
| 217 | // clang-format off |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 218 | // clang formatting doesn't respect inline comments. |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 219 | 'R', 'I', 'F', 'F', |
| 220 | 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8 |
| 221 | 'W', 'A', 'V', 'E', |
| 222 | 'f', 'm', 't', ' ', |
| 223 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 224 | 1, 0, // format: PCM (1) |
| 225 | 1, 0, // channels: 1 |
| 226 | 0x13, 0x37, 0, 0, // sample rate: 14099 |
| 227 | 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099 |
| 228 | 2, 0, // block align: NumChannels * BytesPerSample |
| 229 | 16, 0, // bits per sample: 2 * 8 |
| 230 | 'd', 'a', 't', 'a', |
| 231 | 6, 0, 0, 0, // size of payload: 6 |
| 232 | 0, 0, // first sample: 0.0 |
| 233 | 10, 0, // second sample: 10.0 |
| 234 | 0xff, 0x7f, // third sample: 4e4 (saturated) |
| 235 | kMetadata[0], kMetadata[1], |
| 236 | // clang-format on |
| 237 | }; |
| 238 | static constexpr size_t kContentSize = |
| 239 | kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); |
| 240 | static_assert(sizeof(kExpectedContents) == kContentSize, ""); |
| 241 | EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
| 242 | FILE* f = fopen(outfile.c_str(), "rb"); |
| 243 | ASSERT_TRUE(f); |
| 244 | uint8_t contents[kContentSize]; |
| 245 | ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 246 | EXPECT_EQ(0, fclose(f)); |
| 247 | EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
| 248 | |
| 249 | { |
| 250 | WavReader r(rtc::OpenPlatformFileReadOnly(outfile)); |
| 251 | EXPECT_EQ(14099, r.sample_rate()); |
| 252 | EXPECT_EQ(1u, r.num_channels()); |
| 253 | EXPECT_EQ(kNumSamples, r.num_samples()); |
| 254 | static constexpr float kTruncatedSamples[] = {0.0, 10.0, 32767.0}; |
| 255 | float samples[kNumSamples]; |
| 256 | EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples)); |
| 257 | EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples))); |
| 258 | EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples)); |
| 259 | } |
| 260 | } |
| 261 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 262 | } // namespace webrtc |