blob: 97cecc345ff5299e194ac0e744b4c15e78b1f7ed [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
Jonas Olssona4d87372019-07-05 19:08:33 +020014#include "common_audio/wav_file.h"
15
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000016#include <cmath>
17#include <limits>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_audio/wav_header.h"
20#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "test/testsupport/file_utils.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000022
Niels Möller825aad12019-06-27 10:52:14 +020023// WavWriterTest.CPP flaky on Mac. See webrtc:9247.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020024#if defined(WEBRTC_MAC)
25#define MAYBE_CPP DISABLED_CPP
Artem Titov153056b2019-04-16 16:49:32 +020026#define MAYBE_CPPReset DISABLED_CPPReset
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020027#else
28#define MAYBE_CPP CPP
Artem Titov153056b2019-04-16 16:49:32 +020029#define MAYBE_CPPReset CPPReset
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020030#endif
31
andrew@webrtc.org048c5022014-12-16 20:17:21 +000032namespace webrtc {
33
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000034static 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 Bazzicad5ef6ff2018-05-07 16:33:36 +020037TEST(WavWriterTest, MAYBE_CPP) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000038 const std::string outfile = test::OutputPath() + "wavtest1.wav";
pkasting25702cb2016-01-08 13:50:27 -080039 static const size_t kNumSamples = 3;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000040 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000041 WavWriter w(outfile, 14099, 1);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000042 EXPECT_EQ(14099, w.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080043 EXPECT_EQ(1u, w.num_channels());
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000044 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000045 w.WriteSamples(kSamples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000046 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000047 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +000048 // 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.org877083c2014-08-20 07:42:46 +000058 static const uint8_t kExpectedContents[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020059 // clang-format off
60 // clang formatting doesn't respect inline comments.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000061 '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.org048c5022014-12-16 20:17:21 +000077 kMetadata[0], kMetadata[1],
Yves Gerey665174f2018-06-19 15:03:05 +020078 // clang-format on
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000079 };
pkasting25702cb2016-01-08 13:50:27 -080080 static const size_t kContentSize =
Per Åhgren5dca3f12020-01-28 09:08:11 +010081 kPcmWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000082 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -080083 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000084 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.orga3ed7132014-10-31 21:51:03 +000090
91 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000092 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000093 EXPECT_EQ(14099, r.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080094 EXPECT_EQ(1u, r.num_channels());
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000095 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.org877083c2014-08-20 07:42:46 +0000102}
103
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000104// Write a larger WAV file. You can listen to this file to sanity-check it.
105TEST(WavWriterTest, LargeFile) {
Per Åhgren5dca3f12020-01-28 09:08:11 +0100106 constexpr int kSampleRate = 8000;
107 constexpr size_t kNumChannels = 2;
108 constexpr size_t kNumSamples = 3 * kSampleRate * kNumChannels;
109 for (WavFile::SampleFormat wav_format :
110 {WavFile::SampleFormat::kInt16, WavFile::SampleFormat::kFloat}) {
111 for (WavFile::SampleFormat write_format :
112 {WavFile::SampleFormat::kInt16, WavFile::SampleFormat::kFloat}) {
113 for (WavFile::SampleFormat read_format :
114 {WavFile::SampleFormat::kInt16, WavFile::SampleFormat::kFloat}) {
115 std::string outfile = test::OutputPath() + "wavtest3.wav";
116 float samples[kNumSamples];
117 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
118 // A nice periodic beeping sound.
119 static const double kToneHz = 440;
120 const double t =
121 static_cast<double>(i) / (kNumChannels * kSampleRate);
122 const double x = std::numeric_limits<int16_t>::max() *
123 std::sin(t * kToneHz * 2 * M_PI);
124 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
125 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
126 }
127 {
128 WavWriter w(outfile, kSampleRate, kNumChannels, wav_format);
129 EXPECT_EQ(kSampleRate, w.sample_rate());
130 EXPECT_EQ(kNumChannels, w.num_channels());
131 EXPECT_EQ(0u, w.num_samples());
132 if (write_format == WavFile::SampleFormat::kFloat) {
133 float truncated_samples[kNumSamples];
134 for (size_t k = 0; k < kNumSamples; ++k) {
135 truncated_samples[k] = static_cast<int16_t>(samples[k]);
136 }
137 w.WriteSamples(truncated_samples, kNumSamples);
138 } else {
139 w.WriteSamples(samples, kNumSamples);
140 }
141 EXPECT_EQ(kNumSamples, w.num_samples());
142 }
143 if (wav_format == WavFile::SampleFormat::kFloat) {
144 EXPECT_EQ(sizeof(float) * kNumSamples + kIeeeFloatWavHeaderSize,
145 test::GetFileSize(outfile));
146 } else {
147 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kPcmWavHeaderSize,
148 test::GetFileSize(outfile));
149 }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000150
Per Åhgren5dca3f12020-01-28 09:08:11 +0100151 {
152 WavReader r(outfile);
153 EXPECT_EQ(kSampleRate, r.sample_rate());
154 EXPECT_EQ(kNumChannels, r.num_channels());
155 EXPECT_EQ(kNumSamples, r.num_samples());
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000156
Per Åhgren5dca3f12020-01-28 09:08:11 +0100157 if (read_format == WavFile::SampleFormat::kFloat) {
158 float read_samples[kNumSamples];
159 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
160 for (size_t i = 0; i < kNumSamples; ++i) {
161 EXPECT_NEAR(samples[i], read_samples[i], 1);
162 }
163 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
164 } else {
165 int16_t read_samples[kNumSamples];
166 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
167 for (size_t i = 0; i < kNumSamples; ++i) {
168 EXPECT_NEAR(samples[i], static_cast<float>(read_samples[i]), 1);
169 }
170 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
171 }
172 }
173 }
174 }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000175 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000176}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000177
Artem Titov153056b2019-04-16 16:49:32 +0200178// Write a tiny WAV file with the C++ interface then read-reset-read.
179TEST(WavReaderTest, MAYBE_CPPReset) {
180 const std::string outfile = test::OutputPath() + "wavtest4.wav";
181 static const size_t kNumSamples = 3;
182 {
183 WavWriter w(outfile, 14099, 1);
184 EXPECT_EQ(14099, w.sample_rate());
185 EXPECT_EQ(1u, w.num_channels());
186 EXPECT_EQ(0u, w.num_samples());
187 w.WriteSamples(kSamples, kNumSamples);
188 EXPECT_EQ(kNumSamples, w.num_samples());
189 }
190 // Write some extra "metadata" to the file that should be silently ignored
191 // by WavReader. We don't use WavWriter directly for this because it doesn't
192 // support metadata.
193 static const uint8_t kMetadata[] = {101, 202};
194 {
195 FILE* f = fopen(outfile.c_str(), "ab");
196 ASSERT_TRUE(f);
197 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
198 fclose(f);
199 }
200 static const uint8_t kExpectedContents[] = {
201 // clang-format off
202 // clang formatting doesn't respect inline comments.
203 'R', 'I', 'F', 'F',
204 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
205 'W', 'A', 'V', 'E',
206 'f', 'm', 't', ' ',
207 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
208 1, 0, // format: PCM (1)
209 1, 0, // channels: 1
210 0x13, 0x37, 0, 0, // sample rate: 14099
211 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
212 2, 0, // block align: NumChannels * BytesPerSample
213 16, 0, // bits per sample: 2 * 8
214 'd', 'a', 't', 'a',
215 6, 0, 0, 0, // size of payload: 6
216 0, 0, // first sample: 0.0
217 10, 0, // second sample: 10.0
218 0xff, 0x7f, // third sample: 4e4 (saturated)
219 kMetadata[0], kMetadata[1],
220 // clang-format on
221 };
222 static const size_t kContentSize =
Per Åhgren5dca3f12020-01-28 09:08:11 +0100223 kPcmWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
Artem Titov153056b2019-04-16 16:49:32 +0200224 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
225 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
226 FILE* f = fopen(outfile.c_str(), "rb");
227 ASSERT_TRUE(f);
228 uint8_t contents[kContentSize];
229 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
230 EXPECT_EQ(0, fclose(f));
231 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
232
233 {
234 WavReader r(outfile);
235 EXPECT_EQ(14099, r.sample_rate());
236 EXPECT_EQ(1u, r.num_channels());
237 EXPECT_EQ(kNumSamples, r.num_samples());
238 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
239 float samples[kNumSamples];
240 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
241 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
242 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
243
244 r.Reset();
245 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
246 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
247 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
248 }
249}
250
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000251} // namespace webrtc