blob: 01c920cdf118c84d50704f309fd7c1a521cbe90f [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 =
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
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) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000106 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000107 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-12 16:26:35 -0800108 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 13:50:27 -0800109 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000110 float samples[kNumSamples];
pkasting25702cb2016-01-08 13:50:27 -0800111 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000112 // A nice periodic beeping sound.
113 static const double kToneHz = 440;
114 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
115 const double x =
116 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
117 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
118 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
119 }
120 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000121 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000122 EXPECT_EQ(kSampleRate, w.sample_rate());
123 EXPECT_EQ(kNumChannels, w.num_channels());
124 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000125 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000126 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000127 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000128 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
129 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000130
131 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000132 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000133 EXPECT_EQ(kSampleRate, r.sample_rate());
134 EXPECT_EQ(kNumChannels, r.num_channels());
135 EXPECT_EQ(kNumSamples, r.num_samples());
136
137 float read_samples[kNumSamples];
138 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
139 for (size_t i = 0; i < kNumSamples; ++i)
140 EXPECT_NEAR(samples[i], read_samples[i], 1);
141
142 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
143 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000144}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000145
Artem Titov153056b2019-04-16 16:49:32 +0200146// Write a tiny WAV file with the C++ interface then read-reset-read.
147TEST(WavReaderTest, MAYBE_CPPReset) {
148 const std::string outfile = test::OutputPath() + "wavtest4.wav";
149 static const size_t kNumSamples = 3;
150 {
151 WavWriter w(outfile, 14099, 1);
152 EXPECT_EQ(14099, w.sample_rate());
153 EXPECT_EQ(1u, w.num_channels());
154 EXPECT_EQ(0u, w.num_samples());
155 w.WriteSamples(kSamples, kNumSamples);
156 EXPECT_EQ(kNumSamples, w.num_samples());
157 }
158 // Write some extra "metadata" to the file that should be silently ignored
159 // by WavReader. We don't use WavWriter directly for this because it doesn't
160 // support metadata.
161 static const uint8_t kMetadata[] = {101, 202};
162 {
163 FILE* f = fopen(outfile.c_str(), "ab");
164 ASSERT_TRUE(f);
165 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
166 fclose(f);
167 }
168 static const uint8_t kExpectedContents[] = {
169 // clang-format off
170 // clang formatting doesn't respect inline comments.
171 'R', 'I', 'F', 'F',
172 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
173 'W', 'A', 'V', 'E',
174 'f', 'm', 't', ' ',
175 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
176 1, 0, // format: PCM (1)
177 1, 0, // channels: 1
178 0x13, 0x37, 0, 0, // sample rate: 14099
179 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
180 2, 0, // block align: NumChannels * BytesPerSample
181 16, 0, // bits per sample: 2 * 8
182 'd', 'a', 't', 'a',
183 6, 0, 0, 0, // size of payload: 6
184 0, 0, // first sample: 0.0
185 10, 0, // second sample: 10.0
186 0xff, 0x7f, // third sample: 4e4 (saturated)
187 kMetadata[0], kMetadata[1],
188 // clang-format on
189 };
190 static const size_t kContentSize =
191 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
192 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
193 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
194 FILE* f = fopen(outfile.c_str(), "rb");
195 ASSERT_TRUE(f);
196 uint8_t contents[kContentSize];
197 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
198 EXPECT_EQ(0, fclose(f));
199 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
200
201 {
202 WavReader r(outfile);
203 EXPECT_EQ(14099, r.sample_rate());
204 EXPECT_EQ(1u, r.num_channels());
205 EXPECT_EQ(kNumSamples, r.num_samples());
206 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
207 float samples[kNumSamples];
208 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
209 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
210 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
211
212 r.Reset();
213 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
214 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
215 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
216 }
217}
218
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000219} // namespace webrtc