OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 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 #include <algorithm> |
| 12 #include <array> |
| 13 |
| 14 #include "webrtc/common_audio/wav_file.h" |
| 15 #include "webrtc/common_audio/wav_header.h" |
| 16 #include "webrtc/test/fake_audio_device.h" |
| 17 #include "webrtc/test/gtest.h" |
| 18 #include "webrtc/test/testsupport/fileutils.h" |
| 19 |
| 20 namespace webrtc { |
| 21 namespace test { |
| 22 |
| 23 namespace { |
| 24 void RunTest(const std::vector<int16_t>& input_samples, |
| 25 const std::vector<int16_t>& expected_samples, |
| 26 size_t samples_per_frame) { |
| 27 const std::string output_filename = test::OutputPath() + "boundedwavtest.wav"; |
| 28 |
| 29 static const size_t kSamplesPerFrame = 8; |
| 30 static const int kSampleRate = kSamplesPerFrame * 100; |
| 31 EXPECT_EQ(FakeAudioDevice::SamplesPerFrame(kSampleRate), kSamplesPerFrame); |
| 32 |
| 33 { |
| 34 std::unique_ptr<FakeAudioDevice::Renderer> writer = |
| 35 FakeAudioDevice::CreateBoundedWavFileWriter(output_filename, 800); |
| 36 |
| 37 for (size_t i = 0; i < input_samples.size(); i += kSamplesPerFrame) { |
| 38 EXPECT_TRUE(writer->Render(rtc::ArrayView<const int16_t>( |
| 39 &input_samples[i], |
| 40 std::min(kSamplesPerFrame, input_samples.size() - i)))); |
| 41 } |
| 42 } |
| 43 |
| 44 { |
| 45 WavReader reader(output_filename); |
| 46 std::vector<int16_t> read_samples(expected_samples.size()); |
| 47 EXPECT_EQ(expected_samples.size(), |
| 48 reader.ReadSamples(read_samples.size(), read_samples.data())); |
| 49 EXPECT_EQ(0u, reader.ReadSamples(read_samples.size(), read_samples.data())); |
| 50 |
| 51 EXPECT_EQ(expected_samples, read_samples); |
| 52 } |
| 53 |
| 54 remove(output_filename.c_str()); |
| 55 } |
| 56 } // namespace |
| 57 |
| 58 TEST(BoundedWavFileWriterTest, NoSilence) { |
| 59 static const std::vector<int16_t> kInputSamples = { |
| 60 75, 1234, 243, -1231, -22222, 0, 3, 88, |
| 61 1222, -1213, -13222, -7, -3525, 5787, -25247, 8 |
| 62 }; |
| 63 static const std::vector<int16_t> kExpectedSamples = kInputSamples; |
| 64 RunTest(kInputSamples, kExpectedSamples, 8); |
| 65 } |
| 66 |
| 67 TEST(BoundedWavFileWriterTest, SomeStartSilence) { |
| 68 static const std::vector<int16_t> kInputSamples = { |
| 69 0, 0, 0, 0, 3, 0, 0, 0, |
| 70 0, 3, -13222, -7, -3525, 5787, -25247, 8 |
| 71 }; |
| 72 static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin() + 10, |
| 73 kInputSamples.end()); |
| 74 RunTest(kInputSamples, kExpectedSamples, 8); |
| 75 } |
| 76 |
| 77 TEST(BoundedWavFileWriterTest, NegativeStartSilence) { |
| 78 static const std::vector<int16_t> kInputSamples = { |
| 79 0, -4, -6, 0, 3, 0, 0, 0, |
| 80 0, 3, -13222, -7, -3525, 5787, -25247, 8 |
| 81 }; |
| 82 static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin() + 2, |
| 83 kInputSamples.end()); |
| 84 RunTest(kInputSamples, kExpectedSamples, 8); |
| 85 } |
| 86 |
| 87 TEST(BoundedWavFileWriterTest, SomeEndSilence) { |
| 88 static const std::vector<int16_t> kInputSamples = { |
| 89 75, 1234, 243, -1231, -22222, 0, 1, 0, |
| 90 0, 0, 0, 0, 0, 0, 0, 0 |
| 91 }; |
| 92 static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin(), |
| 93 kInputSamples.end() - 9); |
| 94 RunTest(kInputSamples, kExpectedSamples, 8); |
| 95 } |
| 96 |
| 97 TEST(BoundedWavFileWriterTest, DoubleEndSilence) { |
| 98 static const std::vector<int16_t> kInputSamples = { |
| 99 75, 1234, 243, -1231, -22222, 0, 0, 0, |
| 100 0, -1213, -13222, -7, -3525, 5787, 0, 0 |
| 101 }; |
| 102 static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin(), |
| 103 kInputSamples.end() - 2); |
| 104 RunTest(kInputSamples, kExpectedSamples, 8); |
| 105 } |
| 106 |
| 107 TEST(BoundedWavFileWriterTest, DoubleSilence) { |
| 108 static const std::vector<int16_t> kInputSamples = { |
| 109 0, -1213, -13222, -7, -3525, 5787, 0, 0 |
| 110 }; |
| 111 static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin() + 1, |
| 112 kInputSamples.end() - 2); |
| 113 RunTest(kInputSamples, kExpectedSamples, 8); |
| 114 } |
| 115 |
| 116 TEST(BoundedWavFileWriterTest, EndSilenceCutoff) { |
| 117 static const std::vector<int16_t> kInputSamples = { |
| 118 75, 1234, 243, -1231, -22222, 0, 1, 0, |
| 119 0, 0, 0 |
| 120 }; |
| 121 static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin(), |
| 122 kInputSamples.end() - 4); |
| 123 RunTest(kInputSamples, kExpectedSamples, 8); |
| 124 } |
| 125 |
| 126 } // namespace test |
| 127 } // namespace webrtc |
OLD | NEW |