blob: 39e8968e9331b0d6f5c7ba5ec618dbf26561fb49 [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"
20#include "test/testsupport/fileutils.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[] = {
59 'R', 'I', 'F', 'F',
60 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
61 'W', 'A', 'V', 'E',
62 'f', 'm', 't', ' ',
63 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
64 1, 0, // format: PCM (1)
65 1, 0, // channels: 1
66 0x13, 0x37, 0, 0, // sample rate: 14099
67 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
68 2, 0, // block align: NumChannels * BytesPerSample
69 16, 0, // bits per sample: 2 * 8
70 'd', 'a', 't', 'a',
71 6, 0, 0, 0, // size of payload: 6
72 0, 0, // first sample: 0.0
73 10, 0, // second sample: 10.0
74 0xff, 0x7f, // third sample: 4e4 (saturated)
andrew@webrtc.org048c5022014-12-16 20:17:21 +000075 kMetadata[0], kMetadata[1],
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000076 };
pkasting25702cb2016-01-08 13:50:27 -080077 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +000078 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000079 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -080080 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000081 FILE* f = fopen(outfile.c_str(), "rb");
82 ASSERT_TRUE(f);
83 uint8_t contents[kContentSize];
84 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
85 EXPECT_EQ(0, fclose(f));
86 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000087
88 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000089 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000090 EXPECT_EQ(14099, r.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -080091 EXPECT_EQ(1u, r.num_channels());
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000092 EXPECT_EQ(kNumSamples, r.num_samples());
93 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
94 float samples[kNumSamples];
95 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
96 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
97 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
98 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000099}
100
101// Write a tiny WAV file with the C interface and verify the result.
102TEST(WavWriterTest, C) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000103 const std::string outfile = test::OutputPath() + "wavtest2.wav";
104 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000105 EXPECT_EQ(11904, rtc_WavSampleRate(w));
Peter Kasting69558702016-01-12 16:26:35 -0800106 EXPECT_EQ(2u, rtc_WavNumChannels(w));
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000107 EXPECT_EQ(0u, rtc_WavNumSamples(w));
pkasting25702cb2016-01-08 13:50:27 -0800108 static const size_t kNumSamples = 4;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000109 rtc_WavWriteSamples(w, &kSamples[0], 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000110 EXPECT_EQ(2u, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000111 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000112 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000113 rtc_WavClose(w);
114 static const uint8_t kExpectedContents[] = {
115 'R', 'I', 'F', 'F',
116 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8
117 'W', 'A', 'V', 'E',
118 'f', 'm', 't', ' ',
119 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
120 1, 0, // format: PCM (1)
121 2, 0, // channels: 2
122 0x80, 0x2e, 0, 0, // sample rate: 11904
123 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904
124 4, 0, // block align: NumChannels * BytesPerSample
125 16, 0, // bits per sample: 2 * 8
126 'd', 'a', 't', 'a',
127 8, 0, 0, 0, // size of payload: 8
128 0, 0, // first sample: 0.0
129 10, 0, // second sample: 10.0
130 0xff, 0x7f, // third sample: 4e4 (saturated)
131 0, 0x80, // fourth sample: -1e9 (saturated)
132 };
pkasting25702cb2016-01-08 13:50:27 -0800133 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000134 kWavHeaderSize + kNumSamples * sizeof(int16_t);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000135 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 13:50:27 -0800136 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000137 FILE* f = fopen(outfile.c_str(), "rb");
138 ASSERT_TRUE(f);
139 uint8_t contents[kContentSize];
140 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
141 EXPECT_EQ(0, fclose(f));
142 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
143}
144
145// Write a larger WAV file. You can listen to this file to sanity-check it.
146TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000147 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000148 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-12 16:26:35 -0800149 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 13:50:27 -0800150 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000151 float samples[kNumSamples];
pkasting25702cb2016-01-08 13:50:27 -0800152 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000153 // A nice periodic beeping sound.
154 static const double kToneHz = 440;
155 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
156 const double x =
157 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
158 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
159 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
160 }
161 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000162 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000163 EXPECT_EQ(kSampleRate, w.sample_rate());
164 EXPECT_EQ(kNumChannels, w.num_channels());
165 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000166 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000167 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000168 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000169 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
170 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000171
172 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000173 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000174 EXPECT_EQ(kSampleRate, r.sample_rate());
175 EXPECT_EQ(kNumChannels, r.num_channels());
176 EXPECT_EQ(kNumSamples, r.num_samples());
177
178 float read_samples[kNumSamples];
179 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
180 for (size_t i = 0; i < kNumSamples; ++i)
181 EXPECT_NEAR(samples[i], read_samples[i], 1);
182
183 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
184 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000185}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000186
Artem Titove62f6002018-03-19 15:40:00 +0100187// Write a tiny WAV file with the the std::FILE interface and verify the
188// result.
Alessio Bazzicad5ef6ff2018-05-07 16:33:36 +0200189TEST(WavWriterTest, MAYBE_CPPFileDescriptor) {
Artem Titove62f6002018-03-19 15:40:00 +0100190 const std::string outfile = test::OutputPath() + "wavtest1.wav";
191 static constexpr size_t kNumSamples = 3;
192 {
193 WavWriter w(rtc::CreatePlatformFile(outfile), 14099, 1);
194 EXPECT_EQ(14099, w.sample_rate());
195 EXPECT_EQ(1u, w.num_channels());
196 EXPECT_EQ(0u, w.num_samples());
197 w.WriteSamples(kSamples, kNumSamples);
198 EXPECT_EQ(kNumSamples, w.num_samples());
199 }
200 // Write some extra "metadata" to the file that should be silently ignored
201 // by WavReader. We don't use WavWriter directly for this because it doesn't
202 // support metadata.
203 static constexpr uint8_t kMetadata[] = {101, 202};
204 {
205 FILE* f = fopen(outfile.c_str(), "ab");
206 ASSERT_TRUE(f);
207 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
208 fclose(f);
209 }
210 static const uint8_t kExpectedContents[] = {
211 // clang-format off
212 'R', 'I', 'F', 'F',
213 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
214 'W', 'A', 'V', 'E',
215 'f', 'm', 't', ' ',
216 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
217 1, 0, // format: PCM (1)
218 1, 0, // channels: 1
219 0x13, 0x37, 0, 0, // sample rate: 14099
220 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
221 2, 0, // block align: NumChannels * BytesPerSample
222 16, 0, // bits per sample: 2 * 8
223 'd', 'a', 't', 'a',
224 6, 0, 0, 0, // size of payload: 6
225 0, 0, // first sample: 0.0
226 10, 0, // second sample: 10.0
227 0xff, 0x7f, // third sample: 4e4 (saturated)
228 kMetadata[0], kMetadata[1],
229 // clang-format on
230 };
231 static constexpr size_t kContentSize =
232 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
233 static_assert(sizeof(kExpectedContents) == kContentSize, "");
234 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
235 FILE* f = fopen(outfile.c_str(), "rb");
236 ASSERT_TRUE(f);
237 uint8_t contents[kContentSize];
238 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
239 EXPECT_EQ(0, fclose(f));
240 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
241
242 {
243 WavReader r(rtc::OpenPlatformFileReadOnly(outfile));
244 EXPECT_EQ(14099, r.sample_rate());
245 EXPECT_EQ(1u, r.num_channels());
246 EXPECT_EQ(kNumSamples, r.num_samples());
247 static constexpr float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
248 float samples[kNumSamples];
249 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
250 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
251 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
252 }
253}
254
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000255} // namespace webrtc