blob: fef87c81691404c11acee37514f29bbfb6b16e24 [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
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 Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/wav_file.h"
18#include "common_audio/wav_header.h"
19#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000021
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020022// 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
Artem Titov153056b2019-04-16 16:49:32 +020027#define MAYBE_CPPReset DISABLED_CPPReset
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020028#else
29#define MAYBE_CPP CPP
30#define MAYBE_CPPFileDescriptor CPPFileDescriptor
Artem Titov153056b2019-04-16 16:49:32 +020031#define MAYBE_CPPReset CPPReset
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020032#endif
33
andrew@webrtc.org048c5022014-12-16 20:17:21 +000034namespace webrtc {
35
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000036static const float kSamples[] = {0.0, 10.0, 4e4, -1e9};
37
38// Write a tiny WAV file with the C++ interface and verify the result.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020039TEST(WavWriterTest, MAYBE_CPP) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000040 const std::string outfile = test::OutputPath() + "wavtest1.wav";
pkasting25702cb2016-01-08 13:50:27 -080041 static const size_t kNumSamples = 3;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000042 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000043 WavWriter w(outfile, 14099, 1);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000044 EXPECT_EQ(14099, w.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080045 EXPECT_EQ(1u, w.num_channels());
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000046 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000047 w.WriteSamples(kSamples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000048 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000049 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +000050 // Write some extra "metadata" to the file that should be silently ignored
51 // by WavReader. We don't use WavWriter directly for this because it doesn't
52 // support metadata.
53 static const uint8_t kMetadata[] = {101, 202};
54 {
55 FILE* f = fopen(outfile.c_str(), "ab");
56 ASSERT_TRUE(f);
57 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
58 fclose(f);
59 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000060 static const uint8_t kExpectedContents[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020061 // clang-format off
62 // clang formatting doesn't respect inline comments.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000063 'R', 'I', 'F', 'F',
64 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
65 'W', 'A', 'V', 'E',
66 'f', 'm', 't', ' ',
67 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
68 1, 0, // format: PCM (1)
69 1, 0, // channels: 1
70 0x13, 0x37, 0, 0, // sample rate: 14099
71 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
72 2, 0, // block align: NumChannels * BytesPerSample
73 16, 0, // bits per sample: 2 * 8
74 'd', 'a', 't', 'a',
75 6, 0, 0, 0, // size of payload: 6
76 0, 0, // first sample: 0.0
77 10, 0, // second sample: 10.0
78 0xff, 0x7f, // third sample: 4e4 (saturated)
andrew@webrtc.org048c5022014-12-16 20:17:21 +000079 kMetadata[0], kMetadata[1],
Yves Gerey665174f2018-06-19 15:03:05 +020080 // clang-format on
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000081 };
pkasting25702cb2016-01-08 13:50:27 -080082 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +000083 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000084 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -080085 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000086 FILE* f = fopen(outfile.c_str(), "rb");
87 ASSERT_TRUE(f);
88 uint8_t contents[kContentSize];
89 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
90 EXPECT_EQ(0, fclose(f));
91 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000092
93 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000094 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000095 EXPECT_EQ(14099, r.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080096 EXPECT_EQ(1u, r.num_channels());
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000097 EXPECT_EQ(kNumSamples, r.num_samples());
98 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
99 float samples[kNumSamples];
100 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
101 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
102 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
103 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000104}
105
106// Write a tiny WAV file with the C interface and verify the result.
107TEST(WavWriterTest, C) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000108 const std::string outfile = test::OutputPath() + "wavtest2.wav";
109 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000110 EXPECT_EQ(11904, rtc_WavSampleRate(w));
Peter Kasting69558702016-01-12 16:26:35 -0800111 EXPECT_EQ(2u, rtc_WavNumChannels(w));
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000112 EXPECT_EQ(0u, rtc_WavNumSamples(w));
pkasting25702cb2016-01-08 13:50:27 -0800113 static const size_t kNumSamples = 4;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000114 rtc_WavWriteSamples(w, &kSamples[0], 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000115 EXPECT_EQ(2u, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000116 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000117 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000118 rtc_WavClose(w);
119 static const uint8_t kExpectedContents[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200120 // clang-format off
121 // clang formatting doesn't respect inline comments.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000122 'R', 'I', 'F', 'F',
123 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8
124 'W', 'A', 'V', 'E',
125 'f', 'm', 't', ' ',
126 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
127 1, 0, // format: PCM (1)
128 2, 0, // channels: 2
129 0x80, 0x2e, 0, 0, // sample rate: 11904
130 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904
131 4, 0, // block align: NumChannels * BytesPerSample
132 16, 0, // bits per sample: 2 * 8
133 'd', 'a', 't', 'a',
134 8, 0, 0, 0, // size of payload: 8
135 0, 0, // first sample: 0.0
136 10, 0, // second sample: 10.0
137 0xff, 0x7f, // third sample: 4e4 (saturated)
138 0, 0x80, // fourth sample: -1e9 (saturated)
Yves Gerey665174f2018-06-19 15:03:05 +0200139 // clang-format on
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000140 };
pkasting25702cb2016-01-08 13:50:27 -0800141 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000142 kWavHeaderSize + kNumSamples * sizeof(int16_t);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000143 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -0800144 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000145 FILE* f = fopen(outfile.c_str(), "rb");
146 ASSERT_TRUE(f);
147 uint8_t contents[kContentSize];
148 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
149 EXPECT_EQ(0, fclose(f));
150 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
151}
152
153// Write a larger WAV file. You can listen to this file to sanity-check it.
154TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000155 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000156 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-12 16:26:35 -0800157 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 13:50:27 -0800158 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000159 float samples[kNumSamples];
pkasting25702cb2016-01-08 13:50:27 -0800160 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000161 // A nice periodic beeping sound.
162 static const double kToneHz = 440;
163 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
164 const double x =
165 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
166 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
167 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
168 }
169 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000170 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000171 EXPECT_EQ(kSampleRate, w.sample_rate());
172 EXPECT_EQ(kNumChannels, w.num_channels());
173 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000174 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000175 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000176 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000177 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
178 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000179
180 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000181 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000182 EXPECT_EQ(kSampleRate, r.sample_rate());
183 EXPECT_EQ(kNumChannels, r.num_channels());
184 EXPECT_EQ(kNumSamples, r.num_samples());
185
186 float read_samples[kNumSamples];
187 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
188 for (size_t i = 0; i < kNumSamples; ++i)
189 EXPECT_NEAR(samples[i], read_samples[i], 1);
190
191 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
192 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000193}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000194
Artem Titove62f6002018-03-19 15:40:00 +0100195// Write a tiny WAV file with the the std::FILE interface and verify the
196// result.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +0200197TEST(WavWriterTest, MAYBE_CPPFileDescriptor) {
Artem Titove62f6002018-03-19 15:40:00 +0100198 const std::string outfile = test::OutputPath() + "wavtest1.wav";
199 static constexpr size_t kNumSamples = 3;
200 {
201 WavWriter w(rtc::CreatePlatformFile(outfile), 14099, 1);
202 EXPECT_EQ(14099, w.sample_rate());
203 EXPECT_EQ(1u, w.num_channels());
204 EXPECT_EQ(0u, w.num_samples());
205 w.WriteSamples(kSamples, kNumSamples);
206 EXPECT_EQ(kNumSamples, w.num_samples());
207 }
208 // Write some extra "metadata" to the file that should be silently ignored
209 // by WavReader. We don't use WavWriter directly for this because it doesn't
210 // support metadata.
211 static constexpr uint8_t kMetadata[] = {101, 202};
212 {
213 FILE* f = fopen(outfile.c_str(), "ab");
214 ASSERT_TRUE(f);
215 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
216 fclose(f);
217 }
218 static const uint8_t kExpectedContents[] = {
219 // clang-format off
Yves Gerey665174f2018-06-19 15:03:05 +0200220 // clang formatting doesn't respect inline comments.
Artem Titove62f6002018-03-19 15:40:00 +0100221 'R', 'I', 'F', 'F',
222 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
223 'W', 'A', 'V', 'E',
224 'f', 'm', 't', ' ',
225 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
226 1, 0, // format: PCM (1)
227 1, 0, // channels: 1
228 0x13, 0x37, 0, 0, // sample rate: 14099
229 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
230 2, 0, // block align: NumChannels * BytesPerSample
231 16, 0, // bits per sample: 2 * 8
232 'd', 'a', 't', 'a',
233 6, 0, 0, 0, // size of payload: 6
234 0, 0, // first sample: 0.0
235 10, 0, // second sample: 10.0
236 0xff, 0x7f, // third sample: 4e4 (saturated)
237 kMetadata[0], kMetadata[1],
238 // clang-format on
239 };
240 static constexpr size_t kContentSize =
241 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
242 static_assert(sizeof(kExpectedContents) == kContentSize, "");
243 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
244 FILE* f = fopen(outfile.c_str(), "rb");
245 ASSERT_TRUE(f);
246 uint8_t contents[kContentSize];
247 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
248 EXPECT_EQ(0, fclose(f));
249 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
250
251 {
252 WavReader r(rtc::OpenPlatformFileReadOnly(outfile));
253 EXPECT_EQ(14099, r.sample_rate());
254 EXPECT_EQ(1u, r.num_channels());
255 EXPECT_EQ(kNumSamples, r.num_samples());
256 static constexpr float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
257 float samples[kNumSamples];
258 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
259 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
260 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
261 }
262}
263
Artem Titov153056b2019-04-16 16:49:32 +0200264// Write a tiny WAV file with the C++ interface then read-reset-read.
265TEST(WavReaderTest, MAYBE_CPPReset) {
266 const std::string outfile = test::OutputPath() + "wavtest4.wav";
267 static const size_t kNumSamples = 3;
268 {
269 WavWriter w(outfile, 14099, 1);
270 EXPECT_EQ(14099, w.sample_rate());
271 EXPECT_EQ(1u, w.num_channels());
272 EXPECT_EQ(0u, w.num_samples());
273 w.WriteSamples(kSamples, kNumSamples);
274 EXPECT_EQ(kNumSamples, w.num_samples());
275 }
276 // Write some extra "metadata" to the file that should be silently ignored
277 // by WavReader. We don't use WavWriter directly for this because it doesn't
278 // support metadata.
279 static const uint8_t kMetadata[] = {101, 202};
280 {
281 FILE* f = fopen(outfile.c_str(), "ab");
282 ASSERT_TRUE(f);
283 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
284 fclose(f);
285 }
286 static const uint8_t kExpectedContents[] = {
287 // clang-format off
288 // clang formatting doesn't respect inline comments.
289 'R', 'I', 'F', 'F',
290 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
291 'W', 'A', 'V', 'E',
292 'f', 'm', 't', ' ',
293 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
294 1, 0, // format: PCM (1)
295 1, 0, // channels: 1
296 0x13, 0x37, 0, 0, // sample rate: 14099
297 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
298 2, 0, // block align: NumChannels * BytesPerSample
299 16, 0, // bits per sample: 2 * 8
300 'd', 'a', 't', 'a',
301 6, 0, 0, 0, // size of payload: 6
302 0, 0, // first sample: 0.0
303 10, 0, // second sample: 10.0
304 0xff, 0x7f, // third sample: 4e4 (saturated)
305 kMetadata[0], kMetadata[1],
306 // clang-format on
307 };
308 static const size_t kContentSize =
309 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
310 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
311 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
312 FILE* f = fopen(outfile.c_str(), "rb");
313 ASSERT_TRUE(f);
314 uint8_t contents[kContentSize];
315 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
316 EXPECT_EQ(0, fclose(f));
317 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
318
319 {
320 WavReader r(outfile);
321 EXPECT_EQ(14099, r.sample_rate());
322 EXPECT_EQ(1u, r.num_channels());
323 EXPECT_EQ(kNumSamples, r.num_samples());
324 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
325 float samples[kNumSamples];
326 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
327 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
328 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
329
330 r.Reset();
331 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
332 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
333 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
334 }
335}
336
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000337} // namespace webrtc