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