| 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. We need to |
| 83 // index past the first bytes in the data, which contain the wav header. |
| 84 const int kFirstSampleIndex = 12 + 8 + 16 + 8; |
| 85 int16_t data[2]; |
| 86 data[0] = kTestAudioData[kFirstSampleIndex]; |
| 87 data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); |
| 88 data[1] = kTestAudioData[kFirstSampleIndex + 2]; |
| 89 data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); |
| 90 |
| 91 // The first frame should hold the WAV data. |
| 92 EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), |
| 93 audio_bus->channel(0)[0]); |
| 94 EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), |
| 95 audio_bus->channel(1)[0]); |
| 96 |
| 97 // All other frames should be zero-padded. This applies even when looping, as |
| 98 // the looping will restart on the next call to OnMoreData. |
| 99 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 100 for (int frame = 1; frame < audio_bus->frames(); ++frame) { |
| 101 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 102 } |
| 103 } |
| 104 } |
| 105 |
| 106 TEST(SimpleSources, FileSourceTestDataWithoutLooping) { |
| 82 const int kNumFrames = 10; | 107 const int kNumFrames = 10; |
| 83 | 108 |
| 84 // Create a temporary file filled with WAV data. | 109 // Create a temporary file filled with WAV data. |
| 85 base::FilePath temp_path; | 110 base::FilePath temp_path; |
| 86 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | 111 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| 87 base::File temp(temp_path, | 112 base::File temp(temp_path, |
| 88 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | 113 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| 89 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); | 114 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| 90 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 115 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 91 temp.Close(); | 116 temp.Close(); |
| 92 | 117 |
| 93 // Create AudioParameters which match those in the WAV data. | 118 // Create AudioParameters which match those in the WAV data. |
| 94 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 119 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 95 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 120 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 96 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 121 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 97 audio_bus->Zero(); | 122 audio_bus->Zero(); |
| 98 | 123 |
| 99 // Create a FileSource that reads this file. | 124 // Create a FileSource that reads this file. |
| 100 FileSource source(params, temp_path); | 125 bool loop = false; |
| 126 FileSource source(params, temp_path, loop); |
| 101 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); | 127 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); |
| 102 | 128 |
| 103 // Convert the test data (little-endian) into floats and compare. | 129 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 | 130 |
| 111 // The first frame should hold the WAV data. | 131 // 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), | 132 audio_bus->Zero(); |
| 113 audio_bus->channel(0)[0]); | 133 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) { | 134 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 119 for (int frame = 1; frame < audio_bus->frames(); ++frame) { | 135 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 120 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 136 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 121 } | 137 } |
| 122 } | 138 } |
| 123 } | 139 } |
| 124 | 140 |
| 141 TEST(SimpleSources, FileSourceTestDataWithLooping) { |
| 142 const int kNumFrames = 10; |
| 143 |
| 144 // Create a temporary file filled with WAV data. |
| 145 base::FilePath temp_path; |
| 146 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| 147 base::File temp(temp_path, |
| 148 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| 149 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| 150 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 151 temp.Close(); |
| 152 |
| 153 // Create AudioParameters which match those in the WAV data. |
| 154 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 155 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 156 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 157 audio_bus->Zero(); |
| 158 |
| 159 bool loop = true; |
| 160 FileSource source(params, temp_path, loop); |
| 161 |
| 162 // Verify that we keep reading in the file when looping. |
| 163 source.OnMoreData(audio_bus.get(), 0, 0); |
| 164 audio_bus->Zero(); |
| 165 source.OnMoreData(audio_bus.get(), 0, 0); |
| 166 |
| 167 VerifyContainsTestFile(audio_bus.get()); |
| 168 } |
| 169 |
| 125 TEST(SimpleSources, BadFilePathFails) { | 170 TEST(SimpleSources, BadFilePathFails) { |
| 126 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 171 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 127 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); | 172 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
| 128 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); | 173 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); |
| 129 audio_bus->Zero(); | 174 audio_bus->Zero(); |
| 130 | 175 |
| 131 // Create a FileSource that reads this file. | 176 // Create a FileSource that reads this file. |
| 132 base::FilePath path; | 177 base::FilePath path; |
| 133 path = path.Append(FILE_PATH_LITERAL("does")) | 178 path = path.Append(FILE_PATH_LITERAL("does")) |
| 134 .Append(FILE_PATH_LITERAL("not")) | 179 .Append(FILE_PATH_LITERAL("not")) |
| 135 .Append(FILE_PATH_LITERAL("exist")); | 180 .Append(FILE_PATH_LITERAL("exist")); |
| 136 FileSource source(params, path); | 181 bool loop = false; |
| 182 FileSource source(params, path, loop); |
| 137 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 183 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
| 138 | 184 |
| 139 // Confirm all frames are zero-padded. | 185 // Confirm all frames are zero-padded. |
| 140 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 186 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 141 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 187 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 142 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 188 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 143 } | 189 } |
| 144 } | 190 } |
| 145 } | 191 } |
| 146 | 192 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 160 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 206 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 161 temp.Close(); | 207 temp.Close(); |
| 162 | 208 |
| 163 // Create AudioParameters which match those in the WAV data. | 209 // Create AudioParameters which match those in the WAV data. |
| 164 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 210 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 165 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 211 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 166 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 212 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 167 audio_bus->Zero(); | 213 audio_bus->Zero(); |
| 168 | 214 |
| 169 // Create a FileSource that reads this file. | 215 // Create a FileSource that reads this file. |
| 170 FileSource source(params, temp_path); | 216 bool loop = false; |
| 217 FileSource source(params, temp_path, loop); |
| 171 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 218 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
| 172 | 219 |
| 173 // Confirm all frames are zero-padded. | 220 // Confirm all frames are zero-padded. |
| 174 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 221 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 175 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 222 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 176 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 223 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 177 } | 224 } |
| 178 } | 225 } |
| 179 } | 226 } |
| 180 | 227 |
| 181 } // namespace media | 228 } // namespace media |
| OLD | NEW |