kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [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 | |
| 14 | #include <cmath> |
| 15 | #include <limits> |
| 16 | |
kwiberg | 77eab70 | 2016-09-29 00:42:01 | [diff] [blame] | 17 | #include "webrtc/test/gtest.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 18 | #include "webrtc/common_audio/wav_header.h" |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 | [diff] [blame] | 19 | #include "webrtc/common_audio/wav_file.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 20 | #include "webrtc/test/testsupport/fileutils.h" |
| 21 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 24 | static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; |
| 25 | |
| 26 | // Write a tiny WAV file with the C++ interface and verify the result. |
| 27 | TEST(WavWriterTest, CPP) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 28 | const std::string outfile = test::OutputPath() + "wavtest1.wav"; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 29 | static const size_t kNumSamples = 3; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 30 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 31 | WavWriter w(outfile, 14099, 1); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 32 | EXPECT_EQ(14099, w.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 33 | EXPECT_EQ(1u, w.num_channels()); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 34 | EXPECT_EQ(0u, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 35 | w.WriteSamples(kSamples, kNumSamples); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 36 | EXPECT_EQ(kNumSamples, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 37 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 38 | // Write some extra "metadata" to the file that should be silently ignored |
| 39 | // by WavReader. We don't use WavWriter directly for this because it doesn't |
| 40 | // support metadata. |
| 41 | static const uint8_t kMetadata[] = {101, 202}; |
| 42 | { |
| 43 | FILE* f = fopen(outfile.c_str(), "ab"); |
| 44 | ASSERT_TRUE(f); |
| 45 | ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f)); |
| 46 | fclose(f); |
| 47 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 48 | static const uint8_t kExpectedContents[] = { |
| 49 | 'R', 'I', 'F', 'F', |
| 50 | 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8 |
| 51 | 'W', 'A', 'V', 'E', |
| 52 | 'f', 'm', 't', ' ', |
| 53 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 54 | 1, 0, // format: PCM (1) |
| 55 | 1, 0, // channels: 1 |
| 56 | 0x13, 0x37, 0, 0, // sample rate: 14099 |
| 57 | 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099 |
| 58 | 2, 0, // block align: NumChannels * BytesPerSample |
| 59 | 16, 0, // bits per sample: 2 * 8 |
| 60 | 'd', 'a', 't', 'a', |
| 61 | 6, 0, 0, 0, // size of payload: 6 |
| 62 | 0, 0, // first sample: 0.0 |
| 63 | 10, 0, // second sample: 10.0 |
| 64 | 0xff, 0x7f, // third sample: 4e4 (saturated) |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 65 | kMetadata[0], kMetadata[1], |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 66 | }; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 67 | static const size_t kContentSize = |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 68 | kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 | [diff] [blame] | 69 | static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 70 | EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 71 | FILE* f = fopen(outfile.c_str(), "rb"); |
| 72 | ASSERT_TRUE(f); |
| 73 | uint8_t contents[kContentSize]; |
| 74 | ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 75 | EXPECT_EQ(0, fclose(f)); |
| 76 | EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 | [diff] [blame] | 77 | |
| 78 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 79 | WavReader r(outfile); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 | [diff] [blame] | 80 | EXPECT_EQ(14099, r.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 81 | EXPECT_EQ(1u, r.num_channels()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 | [diff] [blame] | 82 | EXPECT_EQ(kNumSamples, r.num_samples()); |
| 83 | static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0}; |
| 84 | float samples[kNumSamples]; |
| 85 | EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples)); |
| 86 | EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples))); |
| 87 | EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples)); |
| 88 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Write a tiny WAV file with the C interface and verify the result. |
| 92 | TEST(WavWriterTest, C) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 93 | const std::string outfile = test::OutputPath() + "wavtest2.wav"; |
| 94 | rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 95 | EXPECT_EQ(11904, rtc_WavSampleRate(w)); |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 96 | EXPECT_EQ(2u, rtc_WavNumChannels(w)); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 97 | EXPECT_EQ(0u, rtc_WavNumSamples(w)); |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 98 | static const size_t kNumSamples = 4; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 99 | rtc_WavWriteSamples(w, &kSamples[0], 2); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 100 | EXPECT_EQ(2u, rtc_WavNumSamples(w)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 101 | rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 102 | EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 103 | rtc_WavClose(w); |
| 104 | static const uint8_t kExpectedContents[] = { |
| 105 | 'R', 'I', 'F', 'F', |
| 106 | 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8 |
| 107 | 'W', 'A', 'V', 'E', |
| 108 | 'f', 'm', 't', ' ', |
| 109 | 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 110 | 1, 0, // format: PCM (1) |
| 111 | 2, 0, // channels: 2 |
| 112 | 0x80, 0x2e, 0, 0, // sample rate: 11904 |
| 113 | 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904 |
| 114 | 4, 0, // block align: NumChannels * BytesPerSample |
| 115 | 16, 0, // bits per sample: 2 * 8 |
| 116 | 'd', 'a', 't', 'a', |
| 117 | 8, 0, 0, 0, // size of payload: 8 |
| 118 | 0, 0, // first sample: 0.0 |
| 119 | 10, 0, // second sample: 10.0 |
| 120 | 0xff, 0x7f, // third sample: 4e4 (saturated) |
| 121 | 0, 0x80, // fourth sample: -1e9 (saturated) |
| 122 | }; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 123 | static const size_t kContentSize = |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 124 | kWavHeaderSize + kNumSamples * sizeof(int16_t); |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 | [diff] [blame] | 125 | static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 126 | EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 127 | FILE* f = fopen(outfile.c_str(), "rb"); |
| 128 | ASSERT_TRUE(f); |
| 129 | uint8_t contents[kContentSize]; |
| 130 | ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 131 | EXPECT_EQ(0, fclose(f)); |
| 132 | EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
| 133 | } |
| 134 | |
| 135 | // Write a larger WAV file. You can listen to this file to sanity-check it. |
| 136 | TEST(WavWriterTest, LargeFile) { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 137 | std::string outfile = test::OutputPath() + "wavtest3.wav"; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 138 | static const int kSampleRate = 8000; |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 139 | static const size_t kNumChannels = 2; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 140 | static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 141 | float samples[kNumSamples]; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 142 | for (size_t i = 0; i < kNumSamples; i += kNumChannels) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 143 | // A nice periodic beeping sound. |
| 144 | static const double kToneHz = 440; |
| 145 | const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); |
| 146 | const double x = |
| 147 | std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI); |
| 148 | samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x; |
| 149 | samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x; |
| 150 | } |
| 151 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 152 | WavWriter w(outfile, kSampleRate, kNumChannels); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 153 | EXPECT_EQ(kSampleRate, w.sample_rate()); |
| 154 | EXPECT_EQ(kNumChannels, w.num_channels()); |
| 155 | EXPECT_EQ(0u, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 156 | w.WriteSamples(samples, kNumSamples); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 | [diff] [blame] | 157 | EXPECT_EQ(kNumSamples, w.num_samples()); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 158 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 159 | EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize, |
| 160 | test::GetFileSize(outfile)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 | [diff] [blame] | 161 | |
| 162 | { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 163 | WavReader r(outfile); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 | [diff] [blame] | 164 | EXPECT_EQ(kSampleRate, r.sample_rate()); |
| 165 | EXPECT_EQ(kNumChannels, r.num_channels()); |
| 166 | EXPECT_EQ(kNumSamples, r.num_samples()); |
| 167 | |
| 168 | float read_samples[kNumSamples]; |
| 169 | EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples)); |
| 170 | for (size_t i = 0; i < kNumSamples; ++i) |
| 171 | EXPECT_NEAR(samples[i], read_samples[i], 1); |
| 172 | |
| 173 | EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples)); |
| 174 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 | [diff] [blame] | 175 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 | [diff] [blame] | 176 | |
| 177 | } // namespace webrtc |