blob: d7b09c11933ef8256fdb01c873ea1869333567f6 [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
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000106// Write a larger WAV file. You can listen to this file to sanity-check it.
107TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000108 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000109 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-12 16:26:35 -0800110 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 13:50:27 -0800111 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000112 float samples[kNumSamples];
pkasting25702cb2016-01-08 13:50:27 -0800113 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000114 // A nice periodic beeping sound.
115 static const double kToneHz = 440;
116 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
117 const double x =
118 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
119 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
120 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
121 }
122 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000123 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000124 EXPECT_EQ(kSampleRate, w.sample_rate());
125 EXPECT_EQ(kNumChannels, w.num_channels());
126 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000127 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000128 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000129 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000130 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
131 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000132
133 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000134 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000135 EXPECT_EQ(kSampleRate, r.sample_rate());
136 EXPECT_EQ(kNumChannels, r.num_channels());
137 EXPECT_EQ(kNumSamples, r.num_samples());
138
139 float read_samples[kNumSamples];
140 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
141 for (size_t i = 0; i < kNumSamples; ++i)
142 EXPECT_NEAR(samples[i], read_samples[i], 1);
143
144 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
145 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000146}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000147
Artem Titove62f6002018-03-19 15:40:00 +0100148// Write a tiny WAV file with the the std::FILE interface and verify the
149// result.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +0200150TEST(WavWriterTest, MAYBE_CPPFileDescriptor) {
Artem Titove62f6002018-03-19 15:40:00 +0100151 const std::string outfile = test::OutputPath() + "wavtest1.wav";
152 static constexpr size_t kNumSamples = 3;
153 {
Niels Möller19a1d502019-06-13 14:08:24 +0200154 WavWriter w(FileWrapper::OpenWriteOnly(outfile), 14099, 1);
Artem Titove62f6002018-03-19 15:40:00 +0100155 EXPECT_EQ(14099, w.sample_rate());
156 EXPECT_EQ(1u, w.num_channels());
157 EXPECT_EQ(0u, w.num_samples());
158 w.WriteSamples(kSamples, kNumSamples);
159 EXPECT_EQ(kNumSamples, w.num_samples());
160 }
161 // Write some extra "metadata" to the file that should be silently ignored
162 // by WavReader. We don't use WavWriter directly for this because it doesn't
163 // support metadata.
164 static constexpr uint8_t kMetadata[] = {101, 202};
165 {
166 FILE* f = fopen(outfile.c_str(), "ab");
167 ASSERT_TRUE(f);
168 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
169 fclose(f);
170 }
171 static const uint8_t kExpectedContents[] = {
172 // clang-format off
Yves Gerey665174f2018-06-19 15:03:05 +0200173 // clang formatting doesn't respect inline comments.
Artem Titove62f6002018-03-19 15:40:00 +0100174 'R', 'I', 'F', 'F',
175 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
176 'W', 'A', 'V', 'E',
177 'f', 'm', 't', ' ',
178 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
179 1, 0, // format: PCM (1)
180 1, 0, // channels: 1
181 0x13, 0x37, 0, 0, // sample rate: 14099
182 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
183 2, 0, // block align: NumChannels * BytesPerSample
184 16, 0, // bits per sample: 2 * 8
185 'd', 'a', 't', 'a',
186 6, 0, 0, 0, // size of payload: 6
187 0, 0, // first sample: 0.0
188 10, 0, // second sample: 10.0
189 0xff, 0x7f, // third sample: 4e4 (saturated)
190 kMetadata[0], kMetadata[1],
191 // clang-format on
192 };
193 static constexpr size_t kContentSize =
194 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
195 static_assert(sizeof(kExpectedContents) == kContentSize, "");
196 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
197 FILE* f = fopen(outfile.c_str(), "rb");
198 ASSERT_TRUE(f);
199 uint8_t contents[kContentSize];
200 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
201 EXPECT_EQ(0, fclose(f));
202 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
203
204 {
205 WavReader r(rtc::OpenPlatformFileReadOnly(outfile));
206 EXPECT_EQ(14099, r.sample_rate());
207 EXPECT_EQ(1u, r.num_channels());
208 EXPECT_EQ(kNumSamples, r.num_samples());
209 static constexpr float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
210 float samples[kNumSamples];
211 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
212 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
213 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
214 }
215}
216
Artem Titov153056b2019-04-16 16:49:32 +0200217// Write a tiny WAV file with the C++ interface then read-reset-read.
218TEST(WavReaderTest, MAYBE_CPPReset) {
219 const std::string outfile = test::OutputPath() + "wavtest4.wav";
220 static const size_t kNumSamples = 3;
221 {
222 WavWriter w(outfile, 14099, 1);
223 EXPECT_EQ(14099, w.sample_rate());
224 EXPECT_EQ(1u, w.num_channels());
225 EXPECT_EQ(0u, w.num_samples());
226 w.WriteSamples(kSamples, kNumSamples);
227 EXPECT_EQ(kNumSamples, w.num_samples());
228 }
229 // Write some extra "metadata" to the file that should be silently ignored
230 // by WavReader. We don't use WavWriter directly for this because it doesn't
231 // support metadata.
232 static const uint8_t kMetadata[] = {101, 202};
233 {
234 FILE* f = fopen(outfile.c_str(), "ab");
235 ASSERT_TRUE(f);
236 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
237 fclose(f);
238 }
239 static const uint8_t kExpectedContents[] = {
240 // clang-format off
241 // clang formatting doesn't respect inline comments.
242 'R', 'I', 'F', 'F',
243 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
244 'W', 'A', 'V', 'E',
245 'f', 'm', 't', ' ',
246 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
247 1, 0, // format: PCM (1)
248 1, 0, // channels: 1
249 0x13, 0x37, 0, 0, // sample rate: 14099
250 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
251 2, 0, // block align: NumChannels * BytesPerSample
252 16, 0, // bits per sample: 2 * 8
253 'd', 'a', 't', 'a',
254 6, 0, 0, 0, // size of payload: 6
255 0, 0, // first sample: 0.0
256 10, 0, // second sample: 10.0
257 0xff, 0x7f, // third sample: 4e4 (saturated)
258 kMetadata[0], kMetadata[1],
259 // clang-format on
260 };
261 static const size_t kContentSize =
262 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
263 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
264 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
265 FILE* f = fopen(outfile.c_str(), "rb");
266 ASSERT_TRUE(f);
267 uint8_t contents[kContentSize];
268 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
269 EXPECT_EQ(0, fclose(f));
270 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
271
272 {
273 WavReader r(outfile);
274 EXPECT_EQ(14099, r.sample_rate());
275 EXPECT_EQ(1u, r.num_channels());
276 EXPECT_EQ(kNumSamples, r.num_samples());
277 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
278 float samples[kNumSamples];
279 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
280 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
281 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
282
283 r.Reset();
284 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
285 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
286 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
287 }
288}
289
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000290} // namespace webrtc