blob: bf03b1cfb3183b361427e5d0a61c67b1a12b2cb7 [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
Niels Möller825aad12019-06-27 10:52:14 +020022// WavWriterTest.CPP flaky on Mac. See webrtc:9247.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020023#if defined(WEBRTC_MAC)
24#define MAYBE_CPP DISABLED_CPP
Artem Titov153056b2019-04-16 16:49:32 +020025#define MAYBE_CPPReset DISABLED_CPPReset
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020026#else
27#define MAYBE_CPP CPP
Artem Titov153056b2019-04-16 16:49:32 +020028#define MAYBE_CPPReset CPPReset
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020029#endif
30
andrew@webrtc.org048c5022014-12-16 20:17:21 +000031namespace webrtc {
32
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000033static const float kSamples[] = {0.0, 10.0, 4e4, -1e9};
34
35// Write a tiny WAV file with the C++ interface and verify the result.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +020036TEST(WavWriterTest, MAYBE_CPP) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000037 const std::string outfile = test::OutputPath() + "wavtest1.wav";
pkasting25702cb2016-01-08 13:50:27 -080038 static const size_t kNumSamples = 3;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000039 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000040 WavWriter w(outfile, 14099, 1);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000041 EXPECT_EQ(14099, w.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080042 EXPECT_EQ(1u, w.num_channels());
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000043 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000044 w.WriteSamples(kSamples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000045 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000046 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +000047 // Write some extra "metadata" to the file that should be silently ignored
48 // by WavReader. We don't use WavWriter directly for this because it doesn't
49 // support metadata.
50 static const uint8_t kMetadata[] = {101, 202};
51 {
52 FILE* f = fopen(outfile.c_str(), "ab");
53 ASSERT_TRUE(f);
54 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
55 fclose(f);
56 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000057 static const uint8_t kExpectedContents[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020058 // clang-format off
59 // clang formatting doesn't respect inline comments.
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000060 'R', 'I', 'F', 'F',
61 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
62 'W', 'A', 'V', 'E',
63 'f', 'm', 't', ' ',
64 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
65 1, 0, // format: PCM (1)
66 1, 0, // channels: 1
67 0x13, 0x37, 0, 0, // sample rate: 14099
68 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
69 2, 0, // block align: NumChannels * BytesPerSample
70 16, 0, // bits per sample: 2 * 8
71 'd', 'a', 't', 'a',
72 6, 0, 0, 0, // size of payload: 6
73 0, 0, // first sample: 0.0
74 10, 0, // second sample: 10.0
75 0xff, 0x7f, // third sample: 4e4 (saturated)
andrew@webrtc.org048c5022014-12-16 20:17:21 +000076 kMetadata[0], kMetadata[1],
Yves Gerey665174f2018-06-19 15:03:05 +020077 // clang-format on
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000078 };
pkasting25702cb2016-01-08 13:50:27 -080079 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +000080 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000081 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -080082 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000083 FILE* f = fopen(outfile.c_str(), "rb");
84 ASSERT_TRUE(f);
85 uint8_t contents[kContentSize];
86 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
87 EXPECT_EQ(0, fclose(f));
88 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000089
90 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000091 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000092 EXPECT_EQ(14099, r.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080093 EXPECT_EQ(1u, r.num_channels());
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000094 EXPECT_EQ(kNumSamples, r.num_samples());
95 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
96 float samples[kNumSamples];
97 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
98 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
99 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
100 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000101}
102
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000103// Write a larger WAV file. You can listen to this file to sanity-check it.
104TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000105 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000106 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-12 16:26:35 -0800107 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 13:50:27 -0800108 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000109 float samples[kNumSamples];
pkasting25702cb2016-01-08 13:50:27 -0800110 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000111 // A nice periodic beeping sound.
112 static const double kToneHz = 440;
113 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
114 const double x =
115 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
116 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
117 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
118 }
119 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000120 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000121 EXPECT_EQ(kSampleRate, w.sample_rate());
122 EXPECT_EQ(kNumChannels, w.num_channels());
123 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000124 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000125 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000126 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000127 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
128 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000129
130 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000131 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000132 EXPECT_EQ(kSampleRate, r.sample_rate());
133 EXPECT_EQ(kNumChannels, r.num_channels());
134 EXPECT_EQ(kNumSamples, r.num_samples());
135
136 float read_samples[kNumSamples];
137 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
138 for (size_t i = 0; i < kNumSamples; ++i)
139 EXPECT_NEAR(samples[i], read_samples[i], 1);
140
141 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
142 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000143}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000144
Artem Titov153056b2019-04-16 16:49:32 +0200145// Write a tiny WAV file with the C++ interface then read-reset-read.
146TEST(WavReaderTest, MAYBE_CPPReset) {
147 const std::string outfile = test::OutputPath() + "wavtest4.wav";
148 static const size_t kNumSamples = 3;
149 {
150 WavWriter w(outfile, 14099, 1);
151 EXPECT_EQ(14099, w.sample_rate());
152 EXPECT_EQ(1u, w.num_channels());
153 EXPECT_EQ(0u, w.num_samples());
154 w.WriteSamples(kSamples, kNumSamples);
155 EXPECT_EQ(kNumSamples, w.num_samples());
156 }
157 // Write some extra "metadata" to the file that should be silently ignored
158 // by WavReader. We don't use WavWriter directly for this because it doesn't
159 // support metadata.
160 static const uint8_t kMetadata[] = {101, 202};
161 {
162 FILE* f = fopen(outfile.c_str(), "ab");
163 ASSERT_TRUE(f);
164 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
165 fclose(f);
166 }
167 static const uint8_t kExpectedContents[] = {
168 // clang-format off
169 // clang formatting doesn't respect inline comments.
170 'R', 'I', 'F', 'F',
171 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
172 'W', 'A', 'V', 'E',
173 'f', 'm', 't', ' ',
174 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
175 1, 0, // format: PCM (1)
176 1, 0, // channels: 1
177 0x13, 0x37, 0, 0, // sample rate: 14099
178 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
179 2, 0, // block align: NumChannels * BytesPerSample
180 16, 0, // bits per sample: 2 * 8
181 'd', 'a', 't', 'a',
182 6, 0, 0, 0, // size of payload: 6
183 0, 0, // first sample: 0.0
184 10, 0, // second sample: 10.0
185 0xff, 0x7f, // third sample: 4e4 (saturated)
186 kMetadata[0], kMetadata[1],
187 // clang-format on
188 };
189 static const size_t kContentSize =
190 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
191 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
192 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
193 FILE* f = fopen(outfile.c_str(), "rb");
194 ASSERT_TRUE(f);
195 uint8_t contents[kContentSize];
196 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
197 EXPECT_EQ(0, fclose(f));
198 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
199
200 {
201 WavReader r(outfile);
202 EXPECT_EQ(14099, r.sample_rate());
203 EXPECT_EQ(1u, r.num_channels());
204 EXPECT_EQ(kNumSamples, r.num_samples());
205 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
206 float samples[kNumSamples];
207 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
208 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
209 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
210
211 r.Reset();
212 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
213 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
214 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
215 }
216}
217
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000218} // namespace webrtc