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