| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // MSVC++ requires this to be set before any other includes to get M_PI. | 11 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 12 #define _USE_MATH_DEFINES | 12 #define _USE_MATH_DEFINES |
| 13 | 13 |
| 14 #include <cmath> | 14 #include <cmath> |
| 15 #include <limits> | 15 #include <limits> |
| 16 | 16 |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "webrtc/common_audio/wav_header.h" | 18 #include "webrtc/common_audio/wav_header.h" |
| 19 #include "webrtc/common_audio/wav_file.h" | 19 #include "webrtc/common_audio/wav_file.h" |
| 20 #include "webrtc/test/testsupport/fileutils.h" | 20 #include "webrtc/test/testsupport/fileutils.h" |
| 21 | 21 |
| 22 namespace webrtc { | 22 namespace webrtc { |
| 23 | 23 |
| 24 static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; | 24 static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; |
| 25 | 25 |
| 26 // Write a tiny WAV file with the C++ interface and verify the result. | 26 // Write a tiny WAV file with the C++ interface and verify the result. |
| 27 TEST(WavWriterTest, CPP) { | 27 TEST(WavWriterTest, CPP) { |
| 28 const std::string outfile = test::OutputPath() + "wavtest1.wav"; | 28 const std::string outfile = test::OutputPath() + "wavtest1.wav"; |
| 29 static const uint32_t kNumSamples = 3; | 29 static const size_t kNumSamples = 3; |
| 30 { | 30 { |
| 31 WavWriter w(outfile, 14099, 1); | 31 WavWriter w(outfile, 14099, 1); |
| 32 EXPECT_EQ(14099, w.sample_rate()); | 32 EXPECT_EQ(14099, w.sample_rate()); |
| 33 EXPECT_EQ(1, w.num_channels()); | 33 EXPECT_EQ(1, w.num_channels()); |
| 34 EXPECT_EQ(0u, w.num_samples()); | 34 EXPECT_EQ(0u, w.num_samples()); |
| 35 w.WriteSamples(kSamples, kNumSamples); | 35 w.WriteSamples(kSamples, kNumSamples); |
| 36 EXPECT_EQ(kNumSamples, w.num_samples()); | 36 EXPECT_EQ(kNumSamples, w.num_samples()); |
| 37 } | 37 } |
| 38 // Write some extra "metadata" to the file that should be silently ignored | 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 | 39 // by WavReader. We don't use WavWriter directly for this because it doesn't |
| (...skipping 17 matching lines...) Expand all Loading... |
| 57 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099 | 57 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099 |
| 58 2, 0, // block align: NumChannels * BytesPerSample | 58 2, 0, // block align: NumChannels * BytesPerSample |
| 59 16, 0, // bits per sample: 2 * 8 | 59 16, 0, // bits per sample: 2 * 8 |
| 60 'd', 'a', 't', 'a', | 60 'd', 'a', 't', 'a', |
| 61 6, 0, 0, 0, // size of payload: 6 | 61 6, 0, 0, 0, // size of payload: 6 |
| 62 0, 0, // first sample: 0.0 | 62 0, 0, // first sample: 0.0 |
| 63 10, 0, // second sample: 10.0 | 63 10, 0, // second sample: 10.0 |
| 64 0xff, 0x7f, // third sample: 4e4 (saturated) | 64 0xff, 0x7f, // third sample: 4e4 (saturated) |
| 65 kMetadata[0], kMetadata[1], | 65 kMetadata[0], kMetadata[1], |
| 66 }; | 66 }; |
| 67 static const int kContentSize = | 67 static const size_t kContentSize = |
| 68 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); | 68 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); |
| 69 static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); | 69 static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
| 70 EXPECT_EQ(size_t(kContentSize), test::GetFileSize(outfile)); | 70 EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
| 71 FILE* f = fopen(outfile.c_str(), "rb"); | 71 FILE* f = fopen(outfile.c_str(), "rb"); |
| 72 ASSERT_TRUE(f); | 72 ASSERT_TRUE(f); |
| 73 uint8_t contents[kContentSize]; | 73 uint8_t contents[kContentSize]; |
| 74 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); | 74 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 75 EXPECT_EQ(0, fclose(f)); | 75 EXPECT_EQ(0, fclose(f)); |
| 76 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); | 76 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
| 77 | 77 |
| 78 { | 78 { |
| 79 WavReader r(outfile); | 79 WavReader r(outfile); |
| 80 EXPECT_EQ(14099, r.sample_rate()); | 80 EXPECT_EQ(14099, r.sample_rate()); |
| 81 EXPECT_EQ(1, r.num_channels()); | 81 EXPECT_EQ(1, r.num_channels()); |
| 82 EXPECT_EQ(kNumSamples, r.num_samples()); | 82 EXPECT_EQ(kNumSamples, r.num_samples()); |
| 83 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0}; | 83 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0}; |
| 84 float samples[kNumSamples]; | 84 float samples[kNumSamples]; |
| 85 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples)); | 85 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples)); |
| 86 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples))); | 86 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples))); |
| 87 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples)); | 87 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples)); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Write a tiny WAV file with the C interface and verify the result. | 91 // Write a tiny WAV file with the C interface and verify the result. |
| 92 TEST(WavWriterTest, C) { | 92 TEST(WavWriterTest, C) { |
| 93 const std::string outfile = test::OutputPath() + "wavtest2.wav"; | 93 const std::string outfile = test::OutputPath() + "wavtest2.wav"; |
| 94 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2); | 94 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2); |
| 95 EXPECT_EQ(11904, rtc_WavSampleRate(w)); | 95 EXPECT_EQ(11904, rtc_WavSampleRate(w)); |
| 96 EXPECT_EQ(2, rtc_WavNumChannels(w)); | 96 EXPECT_EQ(2, rtc_WavNumChannels(w)); |
| 97 EXPECT_EQ(0u, rtc_WavNumSamples(w)); | 97 EXPECT_EQ(0u, rtc_WavNumSamples(w)); |
| 98 static const uint32_t kNumSamples = 4; | 98 static const size_t kNumSamples = 4; |
| 99 rtc_WavWriteSamples(w, &kSamples[0], 2); | 99 rtc_WavWriteSamples(w, &kSamples[0], 2); |
| 100 EXPECT_EQ(2u, rtc_WavNumSamples(w)); | 100 EXPECT_EQ(2u, rtc_WavNumSamples(w)); |
| 101 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); | 101 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); |
| 102 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); | 102 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); |
| 103 rtc_WavClose(w); | 103 rtc_WavClose(w); |
| 104 static const uint8_t kExpectedContents[] = { | 104 static const uint8_t kExpectedContents[] = { |
| 105 'R', 'I', 'F', 'F', | 105 'R', 'I', 'F', 'F', |
| 106 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8 | 106 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8 |
| 107 'W', 'A', 'V', 'E', | 107 'W', 'A', 'V', 'E', |
| 108 'f', 'm', 't', ' ', | 108 'f', 'm', 't', ' ', |
| 109 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 | 109 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 110 1, 0, // format: PCM (1) | 110 1, 0, // format: PCM (1) |
| 111 2, 0, // channels: 2 | 111 2, 0, // channels: 2 |
| 112 0x80, 0x2e, 0, 0, // sample rate: 11904 | 112 0x80, 0x2e, 0, 0, // sample rate: 11904 |
| 113 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904 | 113 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904 |
| 114 4, 0, // block align: NumChannels * BytesPerSample | 114 4, 0, // block align: NumChannels * BytesPerSample |
| 115 16, 0, // bits per sample: 2 * 8 | 115 16, 0, // bits per sample: 2 * 8 |
| 116 'd', 'a', 't', 'a', | 116 'd', 'a', 't', 'a', |
| 117 8, 0, 0, 0, // size of payload: 8 | 117 8, 0, 0, 0, // size of payload: 8 |
| 118 0, 0, // first sample: 0.0 | 118 0, 0, // first sample: 0.0 |
| 119 10, 0, // second sample: 10.0 | 119 10, 0, // second sample: 10.0 |
| 120 0xff, 0x7f, // third sample: 4e4 (saturated) | 120 0xff, 0x7f, // third sample: 4e4 (saturated) |
| 121 0, 0x80, // fourth sample: -1e9 (saturated) | 121 0, 0x80, // fourth sample: -1e9 (saturated) |
| 122 }; | 122 }; |
| 123 static const int kContentSize = | 123 static const size_t kContentSize = |
| 124 kWavHeaderSize + kNumSamples * sizeof(int16_t); | 124 kWavHeaderSize + kNumSamples * sizeof(int16_t); |
| 125 static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); | 125 static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); |
| 126 EXPECT_EQ(size_t(kContentSize), test::GetFileSize(outfile)); | 126 EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); |
| 127 FILE* f = fopen(outfile.c_str(), "rb"); | 127 FILE* f = fopen(outfile.c_str(), "rb"); |
| 128 ASSERT_TRUE(f); | 128 ASSERT_TRUE(f); |
| 129 uint8_t contents[kContentSize]; | 129 uint8_t contents[kContentSize]; |
| 130 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); | 130 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); |
| 131 EXPECT_EQ(0, fclose(f)); | 131 EXPECT_EQ(0, fclose(f)); |
| 132 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); | 132 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Write a larger WAV file. You can listen to this file to sanity-check it. | 135 // Write a larger WAV file. You can listen to this file to sanity-check it. |
| 136 TEST(WavWriterTest, LargeFile) { | 136 TEST(WavWriterTest, LargeFile) { |
| 137 std::string outfile = test::OutputPath() + "wavtest3.wav"; | 137 std::string outfile = test::OutputPath() + "wavtest3.wav"; |
| 138 static const int kSampleRate = 8000; | 138 static const int kSampleRate = 8000; |
| 139 static const int kNumChannels = 2; | 139 static const int kNumChannels = 2; |
| 140 static const uint32_t kNumSamples = 3 * kSampleRate * kNumChannels; | 140 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; |
| 141 float samples[kNumSamples]; | 141 float samples[kNumSamples]; |
| 142 for (uint32_t i = 0; i < kNumSamples; i += kNumChannels) { | 142 for (size_t i = 0; i < kNumSamples; i += kNumChannels) { |
| 143 // A nice periodic beeping sound. | 143 // A nice periodic beeping sound. |
| 144 static const double kToneHz = 440; | 144 static const double kToneHz = 440; |
| 145 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); | 145 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); |
| 146 const double x = | 146 const double x = |
| 147 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI); | 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; | 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; | 149 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x; |
| 150 } | 150 } |
| 151 { | 151 { |
| 152 WavWriter w(outfile, kSampleRate, kNumChannels); | 152 WavWriter w(outfile, kSampleRate, kNumChannels); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 168 float read_samples[kNumSamples]; | 168 float read_samples[kNumSamples]; |
| 169 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples)); | 169 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples)); |
| 170 for (size_t i = 0; i < kNumSamples; ++i) | 170 for (size_t i = 0; i < kNumSamples; ++i) |
| 171 EXPECT_NEAR(samples[i], read_samples[i], 1); | 171 EXPECT_NEAR(samples[i], read_samples[i], 1); |
| 172 | 172 |
| 173 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples)); | 173 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples)); |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 | 176 |
| 177 } // namespace webrtc | 177 } // namespace webrtc |
| OLD | NEW |