kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 1 | /* |
| 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 Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | #include "common_audio/wav_file.h" |
| 15 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 16 | #include <cmath> |
| 17 | #include <limits> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "common_audio/wav_header.h" |
| 20 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "test/testsupport/file_utils.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 22 | |
Niels Möller | 825aad1 | 2019-06-27 10:52:14 +0200 | [diff] [blame] | 23 | // WavWriterTest.CPP flaky on Mac. See webrtc:9247. |
Alessio Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 24 | #if defined(WEBRTC_MAC) |
| 25 | #define MAYBE_CPP DISABLED_CPP |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 26 | #define MAYBE_CPPReset DISABLED_CPPReset |
Alessio Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 27 | #else |
| 28 | #define MAYBE_CPP CPP |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 29 | #define MAYBE_CPPReset CPPReset |
Alessio Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 32 | namespace webrtc { |
| 33 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 34 | static 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 Bazzica | d5ef6ff | 2018-05-07 16:33:36 +0200 | [diff] [blame] | 37 | TEST(WavWriterTest, MAYBE_CPP) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 38 | const std::string outfile = test::OutputPath() + "wavtest1.wav"; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 39 | static const size_t kNumSamples = 3; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 40 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 41 | WavWriter w(outfile, 14099, 1); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 42 | EXPECT_EQ(14099, w.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 43 | EXPECT_EQ(1u, w.num_channels()); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 44 | EXPECT_EQ(0u, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 45 | w.WriteSamples(kSamples, kNumSamples); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 46 | EXPECT_EQ(kNumSamples, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 47 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 48 | // 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.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 58 | static const uint8_t kExpectedContents[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 59 | // clang-format off |
| 60 | // clang formatting doesn't respect inline comments. |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 61 | '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.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 77 | kMetadata[0], kMetadata[1], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | // clang-format on |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 79 | }; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 80 | static const size_t kContentSize = |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 81 | kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 82 | static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 83 | EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 84 | 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.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 90 | |
| 91 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 92 | WavReader r(outfile); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 93 | EXPECT_EQ(14099, r.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 94 | EXPECT_EQ(1u, r.num_channels()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 95 | 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.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 104 | // Write a larger WAV file. You can listen to this file to sanity-check it. |
| 105 | TEST(WavWriterTest, LargeFile) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 106 | std::string outfile = test::OutputPath() + "wavtest3.wav"; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 107 | static const int kSampleRate = 8000; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 108 | static const size_t kNumChannels = 2; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 109 | static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 110 | float samples[kNumSamples]; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 111 | for (size_t i = 0; i < kNumSamples; i += kNumChannels) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 112 | // 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.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 121 | WavWriter w(outfile, kSampleRate, kNumChannels); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 122 | EXPECT_EQ(kSampleRate, w.sample_rate()); |
| 123 | EXPECT_EQ(kNumChannels, w.num_channels()); |
| 124 | EXPECT_EQ(0u, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 125 | w.WriteSamples(samples, kNumSamples); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 126 | EXPECT_EQ(kNumSamples, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 127 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 128 | EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize, |
| 129 | test::GetFileSize(outfile)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 130 | |
| 131 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 132 | WavReader r(outfile); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 133 | 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.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 144 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 145 | |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 146 | // Write a tiny WAV file with the C++ interface then read-reset-read. |
| 147 | TEST(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.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 219 | } // namespace webrtc |