OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/audio/audio_parameters.h" | |
6 #include "media/base/audio_bus.h" | |
7 #include "media/base/channel_layout.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 static const int kChannels = 6; | |
11 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_5_1; | |
12 // Use a buffer size which is intentionally not a multiple of 16. | |
scherkus (not reviewing)
2012/08/07 02:51:16
nit: "See kChannelAlignment in audio_bus.cc" ?
DaleCurtis
2012/08/07 18:29:45
Done.
| |
13 static const int kFrameCount = 1234; | |
14 | |
15 namespace media { | |
16 | |
17 class AudioBusTest : public testing::Test { | |
18 public: | |
19 AudioBusTest() {} | |
20 ~AudioBusTest() { | |
21 for (size_t i = 0; i < data_.size(); ++i) | |
22 base::AlignedFree(data_[i]); | |
23 } | |
24 | |
25 // Validate parameters returned by AudioBus v.s. the constructed parameters. | |
26 void VerifyParams(AudioBus* bus) { | |
27 EXPECT_EQ(kChannels, bus->channels()); | |
28 EXPECT_EQ(kFrameCount, bus->frames()); | |
29 } | |
30 | |
31 void VerifyValue(const float data[], int size, float value) { | |
32 for (int i = 0; i < size; ++i) | |
33 ASSERT_FLOAT_EQ(value, data[i]); | |
34 } | |
35 | |
36 // Read and write to the full extent of the allocated channel data. Also test | |
37 // the Zero() method and verify it does as advertised. Also test data if data | |
38 // is aligned properly. | |
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Can you be more specific than "properly"? Makes it
DaleCurtis
2012/08/07 18:29:45
Done.
| |
39 void VerifyChannelData(AudioBus* bus) { | |
40 for (int i = 0; i < bus->channels(); ++i) { | |
41 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(bus->channel(i)) & 0x0F); | |
42 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i); | |
43 } | |
44 | |
45 for (int i = 0; i < bus->channels(); ++i) | |
46 VerifyValue(bus->channel(i), bus->frames(), i); | |
47 | |
48 bus->Zero(); | |
49 for (int i = 0; i < bus->channels(); ++i) | |
50 VerifyValue(bus->channel(i), bus->frames(), 0); | |
51 } | |
52 | |
53 | |
54 protected: | |
55 std::vector<float*> data_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(AudioBusTest); | |
58 }; | |
59 | |
60 TEST_F(AudioBusTest, CreateAndSetLayout) { | |
61 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); | |
62 VerifyParams(bus.get()); | |
63 VerifyChannelData(bus.get()); | |
64 } | |
65 | |
66 TEST_F(AudioBusTest, CreateUsingAudioParameters) { | |
67 scoped_ptr<AudioBus> bus = AudioBus::Create(AudioParameters( | |
68 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, 48000, 32, | |
69 kFrameCount)); | |
70 VerifyParams(bus.get()); | |
71 VerifyChannelData(bus.get()); | |
72 } | |
73 | |
74 TEST_F(AudioBusTest, Wrap) { | |
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Short comment for this test would be nice.
DaleCurtis
2012/08/07 18:29:45
Done.
| |
75 data_.reserve(kChannels); | |
76 for (int i = 0; i < kChannels; ++i) { | |
77 data_.push_back(static_cast<float*>(base::AlignedAlloc( | |
78 sizeof(*data_[i]) * kFrameCount, 16))); | |
79 } | |
80 | |
81 scoped_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_); | |
82 VerifyParams(bus.get()); | |
83 VerifyChannelData(bus.get()); | |
84 } | |
85 | |
86 // Simulate a shared memory transfer and verify results. | |
87 TEST_F(AudioBusTest, AudioData) { | |
88 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); | |
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Nit, why not bus1 and bus2?
DaleCurtis
2012/08/07 18:29:45
Done.
| |
89 scoped_ptr<AudioBus> bus2 = AudioBus::Create(kChannels, kFrameCount); | |
90 | |
91 // Fill first bus with dummy data and zero out the second bus. | |
92 for (int i = 0; i < bus->channels(); ++i) | |
93 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i); | |
94 bus2->Zero(); | |
95 | |
96 // Transfer audio data from |bus| to |bus2|. | |
97 ASSERT_EQ(bus->data_size(), bus2->data_size()); | |
98 memcpy(bus2->data(), bus->data(), bus->data_size()); | |
99 | |
100 for (int i = 0; i < bus2->channels(); ++i) | |
101 VerifyValue(bus2->channel(i), bus2->frames(), i); | |
102 } | |
103 | |
104 TEST_F(AudioBusTest, Zero) { | |
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Comment?
DaleCurtis
2012/08/07 18:29:45
Done.
| |
105 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); | |
106 | |
107 // First fill the bus with dummy data. | |
108 for (int i = 0; i < bus->channels(); ++i) | |
109 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); | |
110 | |
111 // Zero half the frames of each channel. | |
112 bus->ZeroFrames(kFrameCount / 2); | |
113 for (int i = 0; i < bus->channels(); ++i) | |
114 VerifyValue(bus->channel(i), kFrameCount / 2, 0); | |
115 | |
116 // Zero all the frames of each channel. | |
117 bus->Zero(); | |
118 for (int i = 0; i < bus->channels(); ++i) | |
119 VerifyValue(bus->channel(i), bus->frames(), 0); | |
120 } | |
121 | |
122 } // namespace media | |
OLD | NEW |