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" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 #include <stdint.h> | 8 #include <stdint.h> |
7 | 9 |
8 #include <limits> | 10 #include <limits> |
| 11 #include <memory> |
9 | 12 |
10 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "media/audio/simple_sources.h" | |
14 #include "media/audio/sounds/test_data.h" | 15 #include "media/audio/sounds/test_data.h" |
15 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
16 #include "media/base/audio_parameters.h" | 17 #include "media/base/audio_parameters.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 | 21 |
21 // Validate that the SineWaveAudioSource writes the expected values. | 22 // Validate that the SineWaveAudioSource writes the expected values. |
22 TEST(SimpleSources, SineWaveAudioSource) { | 23 TEST(SimpleSources, SineWaveAudioSource) { |
23 static const uint32_t samples = 1024; | 24 static const uint32_t samples = 1024; |
24 static const uint32_t bytes_per_sample = 2; | 25 static const uint32_t bytes_per_sample = 2; |
25 static const int freq = 200; | 26 static const int freq = 200; |
26 | 27 |
27 AudioParameters params( | 28 AudioParameters params( |
28 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 29 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
29 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); | 30 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); |
30 | 31 |
31 SineWaveAudioSource source(1, freq, params.sample_rate()); | 32 SineWaveAudioSource source(1, freq, params.sample_rate()); |
32 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(params); | 33 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(params); |
33 source.OnMoreData(audio_bus.get(), 0, 0); | 34 source.OnMoreData(audio_bus.get(), 0, 0); |
34 EXPECT_EQ(1, source.callbacks()); | 35 EXPECT_EQ(1, source.callbacks()); |
35 EXPECT_EQ(0, source.errors()); | 36 EXPECT_EQ(0, source.errors()); |
36 | 37 |
37 uint32_t half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); | 38 uint32_t half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); |
38 | 39 |
39 // Spot test positive incursion of sine wave. | 40 // Spot test positive incursion of sine wave. |
40 EXPECT_NEAR(0, audio_bus->channel(0)[0], | 41 EXPECT_NEAR(0, audio_bus->channel(0)[0], |
41 std::numeric_limits<float>::epsilon()); | 42 std::numeric_limits<float>::epsilon()); |
42 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); | 43 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); |
43 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); | 44 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); |
44 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); | 45 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); |
45 // Spot test negative incursion of sine wave. | 46 // Spot test negative incursion of sine wave. |
46 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], | 47 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], |
47 std::numeric_limits<float>::epsilon()); | 48 std::numeric_limits<float>::epsilon()); |
48 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); | 49 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); |
49 EXPECT_GT(audio_bus->channel(0)[half_period + 1], | 50 EXPECT_GT(audio_bus->channel(0)[half_period + 1], |
50 audio_bus->channel(0)[half_period + 2]); | 51 audio_bus->channel(0)[half_period + 2]); |
51 EXPECT_GT(audio_bus->channel(0)[half_period + 2], | 52 EXPECT_GT(audio_bus->channel(0)[half_period + 2], |
52 audio_bus->channel(0)[half_period + 3]); | 53 audio_bus->channel(0)[half_period + 3]); |
53 } | 54 } |
54 | 55 |
55 TEST(SimpleSources, SineWaveAudioCapped) { | 56 TEST(SimpleSources, SineWaveAudioCapped) { |
56 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 57 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
57 | 58 |
58 static const int kSampleCap = 100; | 59 static const int kSampleCap = 100; |
59 source.CapSamples(kSampleCap); | 60 source.CapSamples(kSampleCap); |
60 | 61 |
61 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); | 62 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); |
62 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); | 63 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); |
63 EXPECT_EQ(1, source.callbacks()); | 64 EXPECT_EQ(1, source.callbacks()); |
64 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), 0); | 65 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), 0); |
65 EXPECT_EQ(2, source.callbacks()); | 66 EXPECT_EQ(2, source.callbacks()); |
66 source.Reset(); | 67 source.Reset(); |
67 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); | 68 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); |
68 EXPECT_EQ(3, source.callbacks()); | 69 EXPECT_EQ(3, source.callbacks()); |
69 EXPECT_EQ(0, source.errors()); | 70 EXPECT_EQ(0, source.errors()); |
70 } | 71 } |
71 | 72 |
(...skipping 13 matching lines...) Expand all Loading... |
85 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | 86 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
86 base::File temp(temp_path, | 87 base::File temp(temp_path, |
87 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | 88 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
88 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); | 89 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
89 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 90 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
90 temp.Close(); | 91 temp.Close(); |
91 | 92 |
92 // Create AudioParameters which match those in the WAV data. | 93 // Create AudioParameters which match those in the WAV data. |
93 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 94 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
94 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 95 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
95 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 96 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
96 audio_bus->Zero(); | 97 audio_bus->Zero(); |
97 | 98 |
98 // Create a FileSource that reads this file. | 99 // Create a FileSource that reads this file. |
99 FileSource source(params, temp_path); | 100 FileSource source(params, temp_path); |
100 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); | 101 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); |
101 | 102 |
102 // Convert the test data (little-endian) into floats and compare. | 103 // Convert the test data (little-endian) into floats and compare. |
103 const int kFirstSampleIndex = 12 + 8 + 16 + 8; | 104 const int kFirstSampleIndex = 12 + 8 + 16 + 8; |
104 int16_t data[2]; | 105 int16_t data[2]; |
105 data[0] = kTestAudioData[kFirstSampleIndex]; | 106 data[0] = kTestAudioData[kFirstSampleIndex]; |
(...skipping 11 matching lines...) Expand all Loading... |
117 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 118 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
118 for (int frame = 1; frame < audio_bus->frames(); ++frame) { | 119 for (int frame = 1; frame < audio_bus->frames(); ++frame) { |
119 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 120 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
120 } | 121 } |
121 } | 122 } |
122 } | 123 } |
123 | 124 |
124 TEST(SimpleSources, BadFilePathFails) { | 125 TEST(SimpleSources, BadFilePathFails) { |
125 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 126 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
126 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); | 127 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
127 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); | 128 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); |
128 audio_bus->Zero(); | 129 audio_bus->Zero(); |
129 | 130 |
130 // Create a FileSource that reads this file. | 131 // Create a FileSource that reads this file. |
131 base::FilePath path; | 132 base::FilePath path; |
132 path = path.Append(FILE_PATH_LITERAL("does")) | 133 path = path.Append(FILE_PATH_LITERAL("does")) |
133 .Append(FILE_PATH_LITERAL("not")) | 134 .Append(FILE_PATH_LITERAL("not")) |
134 .Append(FILE_PATH_LITERAL("exist")); | 135 .Append(FILE_PATH_LITERAL("exist")); |
135 FileSource source(params, path); | 136 FileSource source(params, path); |
136 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 137 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
137 | 138 |
(...skipping 17 matching lines...) Expand all Loading... |
155 | 156 |
156 // Corrupt the header. | 157 // Corrupt the header. |
157 temp.Write(3, "0x00", 1); | 158 temp.Write(3, "0x00", 1); |
158 | 159 |
159 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 160 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
160 temp.Close(); | 161 temp.Close(); |
161 | 162 |
162 // Create AudioParameters which match those in the WAV data. | 163 // Create AudioParameters which match those in the WAV data. |
163 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 164 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
164 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 165 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
165 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 166 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
166 audio_bus->Zero(); | 167 audio_bus->Zero(); |
167 | 168 |
168 // Create a FileSource that reads this file. | 169 // Create a FileSource that reads this file. |
169 FileSource source(params, temp_path); | 170 FileSource source(params, temp_path); |
170 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 171 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
171 | 172 |
172 // Confirm all frames are zero-padded. | 173 // Confirm all frames are zero-padded. |
173 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 174 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
174 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 175 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
175 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 176 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
176 } | 177 } |
177 } | 178 } |
178 } | 179 } |
179 | 180 |
180 } // namespace media | 181 } // namespace media |
OLD | NEW |