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 17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "webrtc/common_audio/wav_header.h" 19 #include "webrtc/common_audio/wav_file.h" 20 #include "webrtc/test/testsupport/fileutils.h" 21 22 namespace webrtc { 23 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) { 28 const std::string outfile = test::OutputPath() + "wavtest1.wav"; 29 static const size_t kNumSamples = 3; 30 { 31 WavWriter w(outfile, 14099, 1); 32 EXPECT_EQ(14099, w.sample_rate()); 33 EXPECT_EQ(1u, w.num_channels()); 34 EXPECT_EQ(0u, w.num_samples()); 35 w.WriteSamples(kSamples, kNumSamples); 36 EXPECT_EQ(kNumSamples, w.num_samples()); 37 } 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 } 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) 65 kMetadata[0], kMetadata[1], 66 }; 67 static const size_t kContentSize = 68 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); 69 static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); 70 EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); 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)); 77 78 { 79 WavReader r(outfile); 80 EXPECT_EQ(14099, r.sample_rate()); 81 EXPECT_EQ(1u, r.num_channels()); 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 } 89 } 90 91 // Write a tiny WAV file with the C interface and verify the result. 92 TEST(WavWriterTest, C) { 93 const std::string outfile = test::OutputPath() + "wavtest2.wav"; 94 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2); 95 EXPECT_EQ(11904, rtc_WavSampleRate(w)); 96 EXPECT_EQ(2u, rtc_WavNumChannels(w)); 97 EXPECT_EQ(0u, rtc_WavNumSamples(w)); 98 static const size_t kNumSamples = 4; 99 rtc_WavWriteSamples(w, &kSamples[0], 2); 100 EXPECT_EQ(2u, rtc_WavNumSamples(w)); 101 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); 102 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); 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 }; 123 static const size_t kContentSize = 124 kWavHeaderSize + kNumSamples * sizeof(int16_t); 125 static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); 126 EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); 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) { 137 std::string outfile = test::OutputPath() + "wavtest3.wav"; 138 static const int kSampleRate = 8000; 139 static const size_t kNumChannels = 2; 140 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; 141 float samples[kNumSamples]; 142 for (size_t i = 0; i < kNumSamples; i += kNumChannels) { 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 { 152 WavWriter w(outfile, kSampleRate, kNumChannels); 153 EXPECT_EQ(kSampleRate, w.sample_rate()); 154 EXPECT_EQ(kNumChannels, w.num_channels()); 155 EXPECT_EQ(0u, w.num_samples()); 156 w.WriteSamples(samples, kNumSamples); 157 EXPECT_EQ(kNumSamples, w.num_samples()); 158 } 159 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize, 160 test::GetFileSize(outfile)); 161 162 { 163 WavReader r(outfile); 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 } 175 } 176 177 } // namespace webrtc 178