Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/simple_sources.h" | 5 #include "media/audio/simple_sources.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 } | 71 } |
| 72 | 72 |
| 73 TEST(SimpleSources, OnError) { | 73 TEST(SimpleSources, OnError) { |
| 74 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 74 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
| 75 source.OnError(NULL); | 75 source.OnError(NULL); |
| 76 EXPECT_EQ(1, source.errors()); | 76 EXPECT_EQ(1, source.errors()); |
| 77 source.OnError(NULL); | 77 source.OnError(NULL); |
| 78 EXPECT_EQ(2, source.errors()); | 78 EXPECT_EQ(2, source.errors()); |
| 79 } | 79 } |
| 80 | 80 |
| 81 TEST(SimpleSources, FileSourceTestData) { | 81 void VerifyContainsTestFile(const AudioBus* audio_bus) { |
| 82 // Convert the test data (little-endian) into floats and compare. | |
|
tommi (sloooow) - chröme
2016/06/08 14:47:33
can you update the comment to explain the value th
phoglund_chromium
2016/06/09 12:08:31
Done.
| |
| 83 const int kFirstSampleIndex = 12 + 8 + 16 + 8; | |
| 84 int16_t data[2]; | |
| 85 data[0] = kTestAudioData[kFirstSampleIndex]; | |
| 86 data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); | |
| 87 data[1] = kTestAudioData[kFirstSampleIndex + 2]; | |
| 88 data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); | |
| 89 | |
| 90 // The first frame should hold the WAV data. | |
| 91 EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), | |
| 92 audio_bus->channel(0)[0]); | |
| 93 EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), | |
| 94 audio_bus->channel(1)[0]); | |
| 95 | |
| 96 // All other frames should be zero-padded. This applies even when looping, as | |
| 97 // the looping will restart on the next call to OnMoreData. | |
| 98 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | |
| 99 for (int frame = 1; frame < audio_bus->frames(); ++frame) { | |
| 100 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 TEST(SimpleSources, FileSourceTestDataWithoutLooping) { | |
| 82 const int kNumFrames = 10; | 106 const int kNumFrames = 10; |
| 83 | 107 |
| 84 // Create a temporary file filled with WAV data. | 108 // Create a temporary file filled with WAV data. |
| 85 base::FilePath temp_path; | 109 base::FilePath temp_path; |
| 86 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | 110 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| 87 base::File temp(temp_path, | 111 base::File temp(temp_path, |
| 88 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | 112 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| 89 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); | 113 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| 90 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 114 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 91 temp.Close(); | 115 temp.Close(); |
| 92 | 116 |
| 93 // Create AudioParameters which match those in the WAV data. | 117 // Create AudioParameters which match those in the WAV data. |
| 94 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 118 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 95 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 119 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 96 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 120 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 97 audio_bus->Zero(); | 121 audio_bus->Zero(); |
| 98 | 122 |
| 99 // Create a FileSource that reads this file. | 123 // Create a FileSource that reads this file. |
| 100 FileSource source(params, temp_path); | 124 bool looping = false; |
|
tommi (sloooow) - chröme
2016/06/08 14:47:33
nit: should this variable/param name be 'loop' rat
phoglund_chromium
2016/06/09 12:08:31
Done.
| |
| 125 FileSource source(params, temp_path, looping); | |
| 101 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); | 126 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); |
| 102 | 127 |
| 103 // Convert the test data (little-endian) into floats and compare. | 128 VerifyContainsTestFile(audio_bus.get()); |
| 104 const int kFirstSampleIndex = 12 + 8 + 16 + 8; | |
| 105 int16_t data[2]; | |
| 106 data[0] = kTestAudioData[kFirstSampleIndex]; | |
| 107 data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); | |
| 108 data[1] = kTestAudioData[kFirstSampleIndex + 2]; | |
| 109 data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); | |
| 110 | 129 |
| 111 // The first frame should hold the WAV data. | 130 // We should not play any more audio after the file reaches its end. |
| 112 EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), | 131 audio_bus->Zero(); |
| 113 audio_bus->channel(0)[0]); | 132 source.OnMoreData(audio_bus.get(), 0, 0); |
| 114 EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), | |
| 115 audio_bus->channel(1)[0]); | |
| 116 | |
| 117 // All other frames should be zero-padded. | |
| 118 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 133 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 119 for (int frame = 1; frame < audio_bus->frames(); ++frame) { | 134 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 120 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 135 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 121 } | 136 } |
| 122 } | 137 } |
| 123 } | 138 } |
| 124 | 139 |
| 140 TEST(SimpleSources, FileSourceTestDataWithLooping) { | |
| 141 const int kNumFrames = 10; | |
| 142 | |
| 143 // Create a temporary file filled with WAV data. | |
| 144 base::FilePath temp_path; | |
| 145 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | |
| 146 base::File temp(temp_path, | |
| 147 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | |
| 148 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); | |
| 149 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | |
| 150 temp.Close(); | |
| 151 | |
| 152 // Create AudioParameters which match those in the WAV data. | |
| 153 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | |
| 154 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | |
| 155 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | |
| 156 audio_bus->Zero(); | |
| 157 | |
| 158 bool looping = true; | |
| 159 FileSource source(params, temp_path, looping); | |
| 160 | |
| 161 // Verify that we keep reading in the file when looping. | |
| 162 source.OnMoreData(audio_bus.get(), 0, 0); | |
| 163 audio_bus->Zero(); | |
| 164 source.OnMoreData(audio_bus.get(), 0, 0); | |
| 165 | |
| 166 VerifyContainsTestFile(audio_bus.get()); | |
| 167 } | |
| 168 | |
| 125 TEST(SimpleSources, BadFilePathFails) { | 169 TEST(SimpleSources, BadFilePathFails) { |
| 126 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 170 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 127 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); | 171 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
| 128 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); | 172 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); |
| 129 audio_bus->Zero(); | 173 audio_bus->Zero(); |
| 130 | 174 |
| 131 // Create a FileSource that reads this file. | 175 // Create a FileSource that reads this file. |
| 132 base::FilePath path; | 176 base::FilePath path; |
| 133 path = path.Append(FILE_PATH_LITERAL("does")) | 177 path = path.Append(FILE_PATH_LITERAL("does")) |
| 134 .Append(FILE_PATH_LITERAL("not")) | 178 .Append(FILE_PATH_LITERAL("not")) |
| 135 .Append(FILE_PATH_LITERAL("exist")); | 179 .Append(FILE_PATH_LITERAL("exist")); |
| 136 FileSource source(params, path); | 180 bool looping = false; |
| 181 FileSource source(params, path, looping); | |
| 137 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 182 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
| 138 | 183 |
| 139 // Confirm all frames are zero-padded. | 184 // Confirm all frames are zero-padded. |
| 140 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 185 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 141 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 186 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 142 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 187 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 143 } | 188 } |
| 144 } | 189 } |
| 145 } | 190 } |
| 146 | 191 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 160 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 205 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 161 temp.Close(); | 206 temp.Close(); |
| 162 | 207 |
| 163 // Create AudioParameters which match those in the WAV data. | 208 // Create AudioParameters which match those in the WAV data. |
| 164 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 209 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 165 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 210 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 166 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 211 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 167 audio_bus->Zero(); | 212 audio_bus->Zero(); |
| 168 | 213 |
| 169 // Create a FileSource that reads this file. | 214 // Create a FileSource that reads this file. |
| 170 FileSource source(params, temp_path); | 215 bool looping = false; |
| 216 FileSource source(params, temp_path, looping); | |
| 171 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 217 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
| 172 | 218 |
| 173 // Confirm all frames are zero-padded. | 219 // Confirm all frames are zero-padded. |
| 174 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 220 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 175 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 221 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 176 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 222 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 177 } | 223 } |
| 178 } | 224 } |
| 179 } | 225 } |
| 180 | 226 |
| 181 } // namespace media | 227 } // namespace media |
| OLD | NEW |