Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: media/base/audio_bus_unittest.cc

Issue 10871051: Convert WebAudio file handlers to use AudioBus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/stringprintf.h" 7 #include "base/stringprintf.h"
8 #include "base/time.h" 8 #include "base/time.h"
9 #include "media/audio/audio_parameters.h" 9 #include "media/audio/audio_parameters.h"
10 #include "media/base/audio_bus.h" 10 #include "media/base/audio_bus.h"
11 #include "media/base/channel_layout.h" 11 #include "media/base/channel_layout.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 static const int kChannels = 6; 14 static const int kChannels = 6;
15 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_5_1; 15 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_5_1;
16 // Use a buffer size which is intentionally not a multiple of kChannelAlignment. 16 // Use a buffer size which is intentionally not a multiple of kChannelAlignment.
17 static const int kFrameCount = media::AudioBus::kChannelAlignment * 128 - 1; 17 static const int kFrameCount = media::AudioBus::kChannelAlignment * 32 - 1;
18 static const int kSampleRate = 48000; 18 static const int kSampleRate = 48000;
19 19
20 namespace media { 20 namespace media {
21 21
22 class AudioBusTest : public testing::Test { 22 class AudioBusTest : public testing::Test {
23 public: 23 public:
24 AudioBusTest() {} 24 AudioBusTest() {}
25 ~AudioBusTest() { 25 ~AudioBusTest() {
26 for (size_t i = 0; i < data_.size(); ++i) 26 for (size_t i = 0; i < data_.size(); ++i)
27 base::AlignedFree(data_[i]); 27 base::AlignedFree(data_[i]);
28 } 28 }
29 29
30 // Validate parameters returned by AudioBus v.s. the constructed parameters. 30 // Validate parameters returned by AudioBus v.s. the constructed parameters.
31 void VerifyParams(AudioBus* bus) { 31 void VerifyParams(AudioBus* bus) {
32 EXPECT_EQ(kChannels, bus->channels()); 32 EXPECT_EQ(kChannels, bus->channels());
33 EXPECT_EQ(kFrameCount, bus->frames()); 33 EXPECT_EQ(kFrameCount, bus->frames());
34 } 34 }
35 35
36 void VerifyValue(const float data[], int size, float value) { 36 void VerifyValue(const float data[], int size, float value) {
37 for (int i = 0; i < size; ++i) 37 for (int i = 0; i < size; ++i)
38 ASSERT_FLOAT_EQ(value, data[i]); 38 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i;
39 } 39 }
40 40
41 // Verify values for each channel in |result| against |expected|. 41 // Verify values for each channel in |result| against |expected|.
42 void VerifyBus(const AudioBus* result, const AudioBus* expected) { 42 void VerifyBus(const AudioBus* result, const AudioBus* expected) {
43 ASSERT_EQ(expected->channels(), result->channels()); 43 ASSERT_EQ(expected->channels(), result->channels());
44 ASSERT_EQ(expected->frames(), result->frames()); 44 ASSERT_EQ(expected->frames(), result->frames());
45 for (int ch = 0; ch < result->channels(); ++ch) { 45 for (int ch = 0; ch < result->channels(); ++ch) {
46 for (int i = 0; i < result->frames(); ++i) { 46 for (int i = 0; i < result->frames(); ++i) {
47 SCOPED_TRACE(base::StringPrintf("ch=%d, i=%d", ch, i)); 47 SCOPED_TRACE(base::StringPrintf("ch=%d, i=%d", ch, i));
48 ASSERT_FLOAT_EQ(expected->channel(ch)[i], result->channel(ch)[i]); 48 ASSERT_FLOAT_EQ(expected->channel(ch)[i], result->channel(ch)[i]);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 183
184 bus2 = AudioBus::WrapMemory(params, data.get()); 184 bus2 = AudioBus::WrapMemory(params, data.get());
185 CopyTest(bus1.get(), bus2.get()); 185 CopyTest(bus1.get(), bus2.get());
186 } 186 }
187 } 187 }
188 188
189 // Verify Zero() and ZeroFrames(...) utility methods work as advertised. 189 // Verify Zero() and ZeroFrames(...) utility methods work as advertised.
190 TEST_F(AudioBusTest, Zero) { 190 TEST_F(AudioBusTest, Zero) {
191 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 191 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
192 192
193 // First fill the bus with dummy data. 193 // Fill the bus with dummy data.
194 for (int i = 0; i < bus->channels(); ++i) 194 for (int i = 0; i < bus->channels(); ++i)
195 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); 195 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
196 196
197 // Zero half the frames of each channel. 197 // Zero first half the frames of each channel.
198 bus->ZeroFrames(kFrameCount / 2); 198 bus->ZeroFrames(kFrameCount / 2);
199 for (int i = 0; i < bus->channels(); ++i) {
200 SCOPED_TRACE("First Half Zero");
201 VerifyValue(bus->channel(i), kFrameCount / 2, 0);
202 VerifyValue(bus->channel(i) + kFrameCount / 2,
203 kFrameCount - kFrameCount / 2, i + 1);
204 }
205
206 // Fill the bus with dummy data.
199 for (int i = 0; i < bus->channels(); ++i) 207 for (int i = 0; i < bus->channels(); ++i)
200 VerifyValue(bus->channel(i), kFrameCount / 2, 0); 208 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
209
210 // Zero the last half of the frames.
211 bus->ZeroFramesPartial(kFrameCount / 2, kFrameCount - kFrameCount / 2);
212 for (int i = 0; i < bus->channels(); ++i) {
213 SCOPED_TRACE("Last Half Zero");
214 VerifyValue(bus->channel(i) + kFrameCount / 2,
215 kFrameCount - kFrameCount / 2, 0);
216 VerifyValue(bus->channel(i), kFrameCount / 2, i + 1);
217 }
218
219 // Fill the bus with dummy data.
220 for (int i = 0; i < bus->channels(); ++i)
221 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
201 222
202 // Zero all the frames of each channel. 223 // Zero all the frames of each channel.
203 bus->Zero(); 224 bus->Zero();
204 for (int i = 0; i < bus->channels(); ++i) 225 for (int i = 0; i < bus->channels(); ++i) {
226 SCOPED_TRACE("All Zero");
205 VerifyValue(bus->channel(i), bus->frames(), 0); 227 VerifyValue(bus->channel(i), bus->frames(), 0);
228 }
206 } 229 }
207 230
208 // Each test vector represents two channels of data in the following arbitrary 231 // Each test vector represents two channels of data in the following arbitrary
209 // layout: <min, zero, max, min, zero, max, zero, zero>. 232 // layout: <min, zero, max, min, zero, max, zero, zero>.
210 static const int kTestVectorSize = 8; 233 static const int kTestVectorSize = 8;
211 static const uint8 kTestVectorUint8[kTestVectorSize] = { 234 static const uint8 kTestVectorUint8[kTestVectorSize] = {
212 0, -kint8min, kuint8max, 0, -kint8min, kuint8max, -kint8min, -kint8min }; 235 0, -kint8min, kuint8max, 0, -kint8min, kuint8max, -kint8min, -kint8min };
213 static const int16 kTestVectorInt16[kTestVectorSize] = { 236 static const int16 kTestVectorInt16[kTestVectorSize] = {
214 kint16min, 0, kint16max, kint16min, 0, kint16max, 0, 0 }; 237 kint16min, 0, kint16max, kint16min, 0, kint16max, 0, 0 };
215 static const int32 kTestVectorInt32[kTestVectorSize] = { 238 static const int32 kTestVectorInt32[kTestVectorSize] = {
(...skipping 10 matching lines...) Expand all
226 scoped_ptr<AudioBus> bus = AudioBus::Create( 249 scoped_ptr<AudioBus> bus = AudioBus::Create(
227 kTestVectorChannels, kTestVectorFrames); 250 kTestVectorChannels, kTestVectorFrames);
228 scoped_ptr<AudioBus> expected = AudioBus::Create( 251 scoped_ptr<AudioBus> expected = AudioBus::Create(
229 kTestVectorChannels, kTestVectorFrames); 252 kTestVectorChannels, kTestVectorFrames);
230 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 253 for (int ch = 0; ch < kTestVectorChannels; ++ch) {
231 memcpy(expected->channel(ch), kTestVectorResult[ch], 254 memcpy(expected->channel(ch), kTestVectorResult[ch],
232 kTestVectorFrames * sizeof(*expected->channel(ch))); 255 kTestVectorFrames * sizeof(*expected->channel(ch)));
233 } 256 }
234 { 257 {
235 SCOPED_TRACE("uint8"); 258 SCOPED_TRACE("uint8");
259 bus->Zero();
236 bus->FromInterleaved( 260 bus->FromInterleaved(
237 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8)); 261 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8));
238 VerifyBus(bus.get(), expected.get()); 262 VerifyBus(bus.get(), expected.get());
239 } 263 }
240 { 264 {
241 SCOPED_TRACE("int16"); 265 SCOPED_TRACE("int16");
266 bus->Zero();
242 bus->FromInterleaved( 267 bus->FromInterleaved(
243 kTestVectorInt16, kTestVectorFrames, sizeof(*kTestVectorInt16)); 268 kTestVectorInt16, kTestVectorFrames, sizeof(*kTestVectorInt16));
244 VerifyBus(bus.get(), expected.get()); 269 VerifyBus(bus.get(), expected.get());
245 } 270 }
246 { 271 {
247 SCOPED_TRACE("int32"); 272 SCOPED_TRACE("int32");
273 bus->Zero();
248 bus->FromInterleaved( 274 bus->FromInterleaved(
249 kTestVectorInt32, kTestVectorFrames, sizeof(*kTestVectorInt32)); 275 kTestVectorInt32, kTestVectorFrames, sizeof(*kTestVectorInt32));
250 VerifyBus(bus.get(), expected.get()); 276 VerifyBus(bus.get(), expected.get());
251 } 277 }
252 } 278 }
253 279
280 // Verify FromInterleavedPartial() deinterleaves audio correctly.
281 TEST_F(AudioBusTest, FromInterleavedPartial) {
282 // Only deinterleave the middle two frames in each channel.
283 static const int kPartialStart = 1;
284 static const int kPartialFrames = 2;
285 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames);
286
287 scoped_ptr<AudioBus> bus = AudioBus::Create(
288 kTestVectorChannels, kTestVectorFrames);
289 scoped_ptr<AudioBus> expected = AudioBus::Create(
290 kTestVectorChannels, kTestVectorFrames);
291 expected->Zero();
292 for (int ch = 0; ch < kTestVectorChannels; ++ch) {
293 memcpy(expected->channel(ch) + kPartialStart,
294 kTestVectorResult[ch] + kPartialStart,
295 kPartialFrames * sizeof(*expected->channel(ch)));
296 }
297
298 bus->Zero();
299 bus->FromInterleavedPartial(
300 kTestVectorInt32 + kPartialStart * bus->channels(), kPartialStart,
301 kPartialFrames, sizeof(*kTestVectorInt32));
302 VerifyBus(bus.get(), expected.get());
303 }
304
254 // Verify ToInterleaved() interleaves audio in suported formats correctly. 305 // Verify ToInterleaved() interleaves audio in suported formats correctly.
255 TEST_F(AudioBusTest, ToInterleaved) { 306 TEST_F(AudioBusTest, ToInterleaved) {
256 scoped_ptr<AudioBus> bus = AudioBus::Create( 307 scoped_ptr<AudioBus> bus = AudioBus::Create(
257 kTestVectorChannels, kTestVectorFrames); 308 kTestVectorChannels, kTestVectorFrames);
258 // Fill the bus with our test vector. 309 // Fill the bus with our test vector.
259 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 310 for (int ch = 0; ch < kTestVectorChannels; ++ch) {
260 memcpy(bus->channel(ch), kTestVectorResult[ch], 311 memcpy(bus->channel(ch), kTestVectorResult[ch],
261 kTestVectorFrames * sizeof(*bus->channel(ch))); 312 kTestVectorFrames * sizeof(*bus->channel(ch)));
262 } 313 }
263 { 314 {
(...skipping 13 matching lines...) Expand all
277 { 328 {
278 SCOPED_TRACE("int32"); 329 SCOPED_TRACE("int32");
279 int32 test_array[arraysize(kTestVectorInt32)]; 330 int32 test_array[arraysize(kTestVectorInt32)];
280 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt32), test_array); 331 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt32), test_array);
281 ASSERT_EQ(memcmp( 332 ASSERT_EQ(memcmp(
282 test_array, kTestVectorInt32, arraysize(kTestVectorInt32)), 0); 333 test_array, kTestVectorInt32, arraysize(kTestVectorInt32)), 0);
283 } 334 }
284 } 335 }
285 336
286 } // namespace media 337 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698