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 <limits> | 5 #include <limits> |
6 | 6 |
7 #include "base/files/file_util.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "media/audio/audio_parameters.h" | 11 #include "media/audio/audio_parameters.h" |
11 #include "media/audio/simple_sources.h" | 12 #include "media/audio/simple_sources.h" |
13 #include "media/audio/sounds/test_data.h" | |
12 #include "media/base/audio_bus.h" | 14 #include "media/base/audio_bus.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 namespace media { | 17 namespace media { |
16 | 18 |
17 // Validate that the SineWaveAudioSource writes the expected values. | 19 // Validate that the SineWaveAudioSource writes the expected values. |
18 TEST(SimpleSources, SineWaveAudioSource) { | 20 TEST(SimpleSources, SineWaveAudioSource) { |
19 static const uint32 samples = 1024; | 21 static const uint32 samples = 1024; |
20 static const uint32 bytes_per_sample = 2; | 22 static const uint32 bytes_per_sample = 2; |
21 static const int freq = 200; | 23 static const int freq = 200; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 } | 70 } |
69 | 71 |
70 TEST(SimpleSources, OnError) { | 72 TEST(SimpleSources, OnError) { |
71 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 73 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
72 source.OnError(NULL); | 74 source.OnError(NULL); |
73 EXPECT_EQ(1, source.errors()); | 75 EXPECT_EQ(1, source.errors()); |
74 source.OnError(NULL); | 76 source.OnError(NULL); |
75 EXPECT_EQ(2, source.errors()); | 77 EXPECT_EQ(2, source.errors()); |
76 } | 78 } |
77 | 79 |
80 TEST(SimpleSources, FileSourceTestData) { | |
81 const int kNumFrames = 10; | |
82 | |
83 // Create a temporary file filled with WAV data. | |
84 base::FilePath temp_path; | |
85 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | |
86 base::File temp(temp_path, | |
87 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | |
88 temp.WriteAtCurrentPos(kTestAudioData, arraysize(kTestAudioData) - 1); | |
89 ASSERT_EQ(arraysize(kTestAudioData) - 1, (size_t)temp.GetLength()); | |
tommi (sloooow) - chröme
2015/11/20 09:45:23
static_cast
slan
2015/11/20 18:09:36
Done, here and below.
| |
90 temp.Close(); | |
91 | |
92 // Create AudioParameters which match those in the WAV data. | |
93 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | |
94 CHANNEL_LAYOUT_STEREO, | |
95 48000, | |
96 16, | |
97 kNumFrames); | |
98 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | |
99 audio_bus->Zero(); | |
100 | |
101 // Create a FileSource that reads this file. | |
102 FileSource source(params, temp_path); | |
103 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0)); | |
104 | |
105 // Convert the test data (little-endian) into floats and compare. | |
106 const int kFirstSampleIndex = 12 + 8 + 16 + 8; | |
107 int16_t data[2]; | |
108 data[0] = kTestAudioData[kFirstSampleIndex]; | |
109 data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); | |
110 data[1] = kTestAudioData[kFirstSampleIndex + 2]; | |
111 data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); | |
112 | |
113 // The first frame should hold the WAV data. | |
114 EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), | |
115 audio_bus->channel(0)[0]); | |
116 EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), | |
117 audio_bus->channel(1)[0]); | |
118 | |
119 // All other frames should be zero-padded. | |
120 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | |
121 for (int frame = 1; frame < audio_bus->frames(); ++frame) { | |
122 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | |
123 } | |
124 } | |
125 } | |
126 | |
127 TEST(SimpleSources, BadFilePathFails) { | |
128 AudioParameters params( | |
129 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 48000, 16, 10); | |
130 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); | |
131 audio_bus->Zero(); | |
132 | |
133 // Create a FileSource that reads this file. | |
134 FileSource source(params, base::FilePath("/does/not/exist")); | |
135 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0)); | |
136 | |
137 // Confirm all frames are zero-padded. | |
138 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | |
139 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | |
140 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | |
141 } | |
142 } | |
143 } | |
144 | |
145 TEST(SimpleSources, FileSourceCorruptTestDataFails) { | |
146 const int kNumFrames = 10; | |
147 | |
148 // Create a temporary file filled with WAV data. | |
149 base::FilePath temp_path; | |
150 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | |
151 base::File temp(temp_path, | |
152 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | |
153 temp.WriteAtCurrentPos(kTestAudioData, arraysize(kTestAudioData) - 1); | |
154 | |
155 // Corrupt the header. | |
156 temp.Write(3, "0x00", 1); | |
157 | |
158 ASSERT_EQ(arraysize(kTestAudioData) - 1, (size_t)temp.GetLength()); | |
159 temp.Close(); | |
160 | |
161 // Create AudioParameters which match those in the WAV data. | |
162 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | |
163 CHANNEL_LAYOUT_STEREO, | |
164 48000, | |
165 16, | |
166 kNumFrames); | |
167 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | |
168 audio_bus->Zero(); | |
169 | |
170 // Create a FileSource that reads this file. | |
171 FileSource source(params, temp_path); | |
172 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0)); | |
173 | |
174 // Confirm all frames are zero-padded. | |
175 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | |
176 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | |
177 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | |
178 } | |
179 } | |
180 } | |
181 | |
78 } // namespace media | 182 } // namespace media |
OLD | NEW |