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> |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/time/time.h" | |
| 15 #include "media/audio/sounds/test_data.h" | 16 #include "media/audio/sounds/test_data.h" |
| 16 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
| 17 #include "media/base/audio_parameters.h" | 18 #include "media/base/audio_parameters.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 | 22 |
| 22 // Validate that the SineWaveAudioSource writes the expected values. | 23 // Validate that the SineWaveAudioSource writes the expected values. |
| 23 TEST(SimpleSources, SineWaveAudioSource) { | 24 TEST(SimpleSources, SineWaveAudioSource) { |
| 24 static const uint32_t samples = 1024; | 25 static const uint32_t samples = 1024; |
| 25 static const uint32_t bytes_per_sample = 2; | 26 static const uint32_t bytes_per_sample = 2; |
| 26 static const int freq = 200; | 27 static const int freq = 200; |
| 27 | 28 |
| 28 AudioParameters params( | 29 AudioParameters params( |
| 29 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 30 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 30 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); | 31 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); |
| 31 | 32 |
| 32 SineWaveAudioSource source(1, freq, params.sample_rate()); | 33 SineWaveAudioSource source(1, freq, params.sample_rate()); |
| 33 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(params); | 34 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(params); |
| 34 source.OnMoreData(audio_bus.get(), 0, 0); | 35 source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()); |
|
chcunningham
2016/07/29 01:21:10
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 35 EXPECT_EQ(1, source.callbacks()); | 36 EXPECT_EQ(1, source.callbacks()); |
| 36 EXPECT_EQ(0, source.errors()); | 37 EXPECT_EQ(0, source.errors()); |
| 37 | 38 |
| 38 uint32_t half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); | 39 uint32_t half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); |
| 39 | 40 |
| 40 // Spot test positive incursion of sine wave. | 41 // Spot test positive incursion of sine wave. |
| 41 EXPECT_NEAR(0, audio_bus->channel(0)[0], | 42 EXPECT_NEAR(0, audio_bus->channel(0)[0], |
| 42 std::numeric_limits<float>::epsilon()); | 43 std::numeric_limits<float>::epsilon()); |
| 43 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); | 44 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); |
| 44 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); | 45 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); |
| 45 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); | 46 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); |
| 46 // Spot test negative incursion of sine wave. | 47 // Spot test negative incursion of sine wave. |
| 47 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], | 48 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], |
| 48 std::numeric_limits<float>::epsilon()); | 49 std::numeric_limits<float>::epsilon()); |
| 49 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); | 50 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); |
| 50 EXPECT_GT(audio_bus->channel(0)[half_period + 1], | 51 EXPECT_GT(audio_bus->channel(0)[half_period + 1], |
| 51 audio_bus->channel(0)[half_period + 2]); | 52 audio_bus->channel(0)[half_period + 2]); |
| 52 EXPECT_GT(audio_bus->channel(0)[half_period + 2], | 53 EXPECT_GT(audio_bus->channel(0)[half_period + 2], |
| 53 audio_bus->channel(0)[half_period + 3]); | 54 audio_bus->channel(0)[half_period + 3]); |
| 54 } | 55 } |
| 55 | 56 |
| 56 TEST(SimpleSources, SineWaveAudioCapped) { | 57 TEST(SimpleSources, SineWaveAudioCapped) { |
| 57 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 58 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
| 58 | 59 |
| 59 static const int kSampleCap = 100; | 60 static const int kSampleCap = 100; |
| 60 source.CapSamples(kSampleCap); | 61 source.CapSamples(kSampleCap); |
| 61 | 62 |
| 62 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); | 63 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); |
| 63 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); | 64 EXPECT_EQ(source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()), |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:47
Done.
| |
| 65 kSampleCap); | |
| 64 EXPECT_EQ(1, source.callbacks()); | 66 EXPECT_EQ(1, source.callbacks()); |
| 65 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), 0); | 67 EXPECT_EQ(source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()), 0); |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 66 EXPECT_EQ(2, source.callbacks()); | 68 EXPECT_EQ(2, source.callbacks()); |
| 67 source.Reset(); | 69 source.Reset(); |
| 68 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); | 70 EXPECT_EQ(source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()), |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 71 kSampleCap); | |
| 69 EXPECT_EQ(3, source.callbacks()); | 72 EXPECT_EQ(3, source.callbacks()); |
| 70 EXPECT_EQ(0, source.errors()); | 73 EXPECT_EQ(0, source.errors()); |
| 71 } | 74 } |
| 72 | 75 |
| 73 TEST(SimpleSources, OnError) { | 76 TEST(SimpleSources, OnError) { |
| 74 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 77 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
| 75 source.OnError(NULL); | 78 source.OnError(NULL); |
| 76 EXPECT_EQ(1, source.errors()); | 79 EXPECT_EQ(1, source.errors()); |
| 77 source.OnError(NULL); | 80 source.OnError(NULL); |
| 78 EXPECT_EQ(2, source.errors()); | 81 EXPECT_EQ(2, source.errors()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 | 120 |
| 118 // Create AudioParameters which match those in the WAV data. | 121 // Create AudioParameters which match those in the WAV data. |
| 119 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 122 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 120 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 123 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 121 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 124 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 122 audio_bus->Zero(); | 125 audio_bus->Zero(); |
| 123 | 126 |
| 124 // Create a FileSource that reads this file. | 127 // Create a FileSource that reads this file. |
| 125 bool loop = false; | 128 bool loop = false; |
| 126 FileSource source(params, temp_path, loop); | 129 FileSource source(params, temp_path, loop); |
| 127 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); | 130 EXPECT_EQ(kNumFrames, |
| 131 source.OnMoreData(base::TimeTicks(), 0, audio_bus.get())); | |
|
chcunningham
2016/07/29 01:21:10
Now?
jameswest
2016/08/26 02:08:47
Done.
| |
| 128 | 132 |
| 129 VerifyContainsTestFile(audio_bus.get()); | 133 VerifyContainsTestFile(audio_bus.get()); |
| 130 | 134 |
| 131 // We should not play any more audio after the file reaches its end. | 135 // We should not play any more audio after the file reaches its end. |
| 132 audio_bus->Zero(); | 136 audio_bus->Zero(); |
| 133 source.OnMoreData(audio_bus.get(), 0, 0); | 137 source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()); |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 134 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 138 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 135 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 139 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 136 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 140 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 137 } | 141 } |
| 138 } | 142 } |
| 139 } | 143 } |
| 140 | 144 |
| 141 TEST(SimpleSources, FileSourceTestDataWithLooping) { | 145 TEST(SimpleSources, FileSourceTestDataWithLooping) { |
| 142 const int kNumFrames = 10; | 146 const int kNumFrames = 10; |
| 143 | 147 |
| 144 // Create a temporary file filled with WAV data. | 148 // Create a temporary file filled with WAV data. |
| 145 base::FilePath temp_path; | 149 base::FilePath temp_path; |
| 146 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | 150 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| 147 base::File temp(temp_path, | 151 base::File temp(temp_path, |
| 148 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | 152 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| 149 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); | 153 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| 150 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 154 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 151 temp.Close(); | 155 temp.Close(); |
| 152 | 156 |
| 153 // Create AudioParameters which match those in the WAV data. | 157 // Create AudioParameters which match those in the WAV data. |
| 154 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 158 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 155 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 159 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 156 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 160 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 157 audio_bus->Zero(); | 161 audio_bus->Zero(); |
| 158 | 162 |
| 159 bool loop = true; | 163 bool loop = true; |
| 160 FileSource source(params, temp_path, loop); | 164 FileSource source(params, temp_path, loop); |
| 161 | 165 |
| 162 // Verify that we keep reading in the file when looping. | 166 // Verify that we keep reading in the file when looping. |
| 163 source.OnMoreData(audio_bus.get(), 0, 0); | 167 source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()); |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 164 audio_bus->Zero(); | 168 audio_bus->Zero(); |
| 165 source.OnMoreData(audio_bus.get(), 0, 0); | 169 source.OnMoreData(base::TimeTicks(), 0, audio_bus.get()); |
|
chcunningham
2016/07/29 01:21:10
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 166 | 170 |
| 167 VerifyContainsTestFile(audio_bus.get()); | 171 VerifyContainsTestFile(audio_bus.get()); |
| 168 } | 172 } |
| 169 | 173 |
| 170 TEST(SimpleSources, BadFilePathFails) { | 174 TEST(SimpleSources, BadFilePathFails) { |
| 171 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 175 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 172 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); | 176 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
| 173 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); | 177 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); |
| 174 audio_bus->Zero(); | 178 audio_bus->Zero(); |
| 175 | 179 |
| 176 // Create a FileSource that reads this file. | 180 // Create a FileSource that reads this file. |
| 177 base::FilePath path; | 181 base::FilePath path; |
| 178 path = path.Append(FILE_PATH_LITERAL("does")) | 182 path = path.Append(FILE_PATH_LITERAL("does")) |
| 179 .Append(FILE_PATH_LITERAL("not")) | 183 .Append(FILE_PATH_LITERAL("not")) |
| 180 .Append(FILE_PATH_LITERAL("exist")); | 184 .Append(FILE_PATH_LITERAL("exist")); |
| 181 bool loop = false; | 185 bool loop = false; |
| 182 FileSource source(params, path, loop); | 186 FileSource source(params, path, loop); |
| 183 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 187 EXPECT_EQ(0, source.OnMoreData(base::TimeTicks(), 0, audio_bus.get())); |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 184 | 188 |
| 185 // Confirm all frames are zero-padded. | 189 // Confirm all frames are zero-padded. |
| 186 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 190 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 187 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 191 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 188 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 192 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 189 } | 193 } |
| 190 } | 194 } |
| 191 } | 195 } |
| 192 | 196 |
| 193 TEST(SimpleSources, FileSourceCorruptTestDataFails) { | 197 TEST(SimpleSources, FileSourceCorruptTestDataFails) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 208 | 212 |
| 209 // Create AudioParameters which match those in the WAV data. | 213 // Create AudioParameters which match those in the WAV data. |
| 210 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 214 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 211 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 215 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 212 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 216 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 213 audio_bus->Zero(); | 217 audio_bus->Zero(); |
| 214 | 218 |
| 215 // Create a FileSource that reads this file. | 219 // Create a FileSource that reads this file. |
| 216 bool loop = false; | 220 bool loop = false; |
| 217 FileSource source(params, temp_path, loop); | 221 FileSource source(params, temp_path, loop); |
| 218 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 222 EXPECT_EQ(0, source.OnMoreData(base::TimeTicks(), 0, audio_bus.get())); |
|
chcunningham
2016/07/29 01:21:09
Now?
jameswest
2016/08/26 02:08:48
Done.
| |
| 219 | 223 |
| 220 // Confirm all frames are zero-padded. | 224 // Confirm all frames are zero-padded. |
| 221 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 225 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 222 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 226 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 223 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 227 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 224 } | 228 } |
| 225 } | 229 } |
| 226 } | 230 } |
| 227 | 231 |
| 228 } // namespace media | 232 } // namespace media |
| OLD | NEW |