blob: b7e5d3fa3e10cfd779afc0850d4b5993adab60bf [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
27#else
28#define MAYBE_CPP CPP
29#define MAYBE_CPPFileDescriptor CPPFileDescriptor
30#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 =
andrew@webrtc.org048c5022014-12-16 20:17:21 +000081 kWavHeaderSize + 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
104// Write a tiny WAV file with the C interface and verify the result.
105TEST(WavWriterTest, C) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000106 const std::string outfile = test::OutputPath() + "wavtest2.wav";
107 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000108 EXPECT_EQ(11904, rtc_WavSampleRate(w));
Peter Kasting69558702016-01-12 16:26:35 -0800109 EXPECT_EQ(2u, rtc_WavNumChannels(w));
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000110 EXPECT_EQ(0u, rtc_WavNumSamples(w));
pkasting25702cb2016-01-08 13:50:27 -0800111 static const size_t kNumSamples = 4;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000112 rtc_WavWriteSamples(w, &kSamples[0], 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000113 EXPECT_EQ(2u, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000114 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000115 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000116 rtc_WavClose(w);
117 static const uint8_t kExpectedContents[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200118 // clang-format off
119 // clang formatting doesn't respect inline comments.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000120 '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 Gerey665174f2018-06-19 15:03:05 +0200137 // clang-format on
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000138 };
pkasting25702cb2016-01-08 13:50:27 -0800139 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000140 kWavHeaderSize + kNumSamples * sizeof(int16_t);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000141 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -0800142 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000143 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.
152TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000153 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000154 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-12 16:26:35 -0800155 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 13:50:27 -0800156 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000157 float samples[kNumSamples];
pkasting25702cb2016-01-08 13:50:27 -0800158 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000159 // 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.org048c5022014-12-16 20:17:21 +0000168 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000169 EXPECT_EQ(kSampleRate, w.sample_rate());
170 EXPECT_EQ(kNumChannels, w.num_channels());
171 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000172 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000173 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000174 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000175 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
176 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000177
178 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000179 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000180 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.org877083c2014-08-20 07:42:46 +0000191}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000192
Artem Titove62f6002018-03-19 15:40:00 +0100193// Write a tiny WAV file with the the std::FILE interface and verify the
194// result.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +0200195TEST(WavWriterTest, MAYBE_CPPFileDescriptor) {
Artem Titove62f6002018-03-19 15:40:00 +0100196 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 Gerey665174f2018-06-19 15:03:05 +0200218 // clang formatting doesn't respect inline comments.
Artem Titove62f6002018-03-19 15:40:00 +0100219 '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.org048c5022014-12-16 20:17:21 +0000262} // namespace webrtc