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

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

Issue 2024993004: AudioBus: Add a ToInterleavedFloat() method to AudioBus (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revise comments/names in code Created 4 years, 6 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
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <limits> 8 #include <limits>
9 #include <memory> 9 #include <memory>
10 10
(...skipping 16 matching lines...) Expand all
27 static const int kSampleRate = 48000; 27 static const int kSampleRate = 48000;
28 28
29 class AudioBusTest : public testing::Test { 29 class AudioBusTest : public testing::Test {
30 public: 30 public:
31 AudioBusTest() {} 31 AudioBusTest() {}
32 ~AudioBusTest() override { 32 ~AudioBusTest() override {
33 for (size_t i = 0; i < data_.size(); ++i) 33 for (size_t i = 0; i < data_.size(); ++i)
34 base::AlignedFree(data_[i]); 34 base::AlignedFree(data_[i]);
35 } 35 }
36 36
37 // Validate parameters returned by AudioBus v.s. the constructed parameters. 37 void VerifyChannelAndFrameCount(AudioBus* bus) {
38 void VerifyParams(AudioBus* bus) {
39 EXPECT_EQ(kChannels, bus->channels()); 38 EXPECT_EQ(kChannels, bus->channels());
40 EXPECT_EQ(kFrameCount, bus->frames()); 39 EXPECT_EQ(kFrameCount, bus->frames());
41 } 40 }
42 41
43 void VerifyValue(const float data[], int size, float value) { 42 void VerifyArrayIsFilledWithValue(const float data[], int size, float value) {
44 for (int i = 0; i < size; ++i) 43 for (int i = 0; i < size; ++i)
45 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; 44 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i;
46 } 45 }
47 46
48 // Verify values for each channel in |result| are within |epsilon| of 47 // Verify values for each channel in |result| are within |epsilon| of
49 // |expected|. If |epsilon| exactly equals 0, uses FLOAT_EQ macro. 48 // |expected|. If |epsilon| exactly equals 0, uses FLOAT_EQ macro.
50 void VerifyBusWithEpsilon(const AudioBus* result, const AudioBus* expected, 49 void VerifyAreEqualWithEpsilon(const AudioBus* result,
51 float epsilon) { 50 const AudioBus* expected,
51 float epsilon) {
52 ASSERT_EQ(expected->channels(), result->channels()); 52 ASSERT_EQ(expected->channels(), result->channels());
53 ASSERT_EQ(expected->frames(), result->frames()); 53 ASSERT_EQ(expected->frames(), result->frames());
54 for (int ch = 0; ch < result->channels(); ++ch) { 54 for (int ch = 0; ch < result->channels(); ++ch) {
55 for (int i = 0; i < result->frames(); ++i) { 55 for (int i = 0; i < result->frames(); ++i) {
56 SCOPED_TRACE(base::StringPrintf("ch=%d, i=%d", ch, i)); 56 SCOPED_TRACE(base::StringPrintf("ch=%d, i=%d", ch, i));
57 if (epsilon == 0) { 57 if (epsilon == 0) {
58 ASSERT_FLOAT_EQ(expected->channel(ch)[i], result->channel(ch)[i]); 58 ASSERT_FLOAT_EQ(expected->channel(ch)[i], result->channel(ch)[i]);
59 } else { 59 } else {
60 ASSERT_NEAR(expected->channel(ch)[i], result->channel(ch)[i], 60 ASSERT_NEAR(expected->channel(ch)[i], result->channel(ch)[i],
61 epsilon); 61 epsilon);
62 } 62 }
63 } 63 }
64 } 64 }
65 } 65 }
66 66
67 // Verify values for each channel in |result| against |expected|. 67 // Verify values for each channel in |result| against |expected|.
68 void VerifyBus(const AudioBus* result, const AudioBus* expected) { 68 void VerifyAreEqual(const AudioBus* result, const AudioBus* expected) {
69 VerifyBusWithEpsilon(result, expected, 0); 69 VerifyAreEqualWithEpsilon(result, expected, 0);
70 } 70 }
71 71
72 // Read and write to the full extent of the allocated channel data. Also test 72 // Read and write to the full extent of the allocated channel data. Also test
73 // the Zero() method and verify it does as advertised. Also test data if data 73 // the Zero() method and verify it does as advertised. Also test data if data
74 // is 16-byte aligned as advertised (see kChannelAlignment in audio_bus.h). 74 // is 16-byte aligned as advertised (see kChannelAlignment in audio_bus.h).
75 void VerifyChannelData(AudioBus* bus) { 75 void VerifyReadWriteAndAlignment(AudioBus* bus) {
76 for (int i = 0; i < bus->channels(); ++i) { 76 for (int i = 0; i < bus->channels(); ++i) {
77 // Verify that the address returned by channel(i) is a multiple of
78 // AudioBus::kChannelAlignment.
77 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>( 79 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(
78 bus->channel(i)) & (AudioBus::kChannelAlignment - 1)); 80 bus->channel(i)) & (AudioBus::kChannelAlignment - 1));
81
82 // Write into the channel buffer.
79 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i); 83 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i);
80 } 84 }
81 85
82 for (int i = 0; i < bus->channels(); ++i) 86 for (int i = 0; i < bus->channels(); ++i)
83 VerifyValue(bus->channel(i), bus->frames(), i); 87 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), i);
84 88
85 bus->Zero(); 89 bus->Zero();
86 for (int i = 0; i < bus->channels(); ++i) 90 for (int i = 0; i < bus->channels(); ++i)
87 VerifyValue(bus->channel(i), bus->frames(), 0); 91 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), 0);
88 } 92 }
89 93
90 // Verify copying to and from |bus1| and |bus2|. 94 // Verify copying to and from |bus1| and |bus2|.
91 void CopyTest(AudioBus* bus1, AudioBus* bus2) { 95 void CopyTest(AudioBus* bus1, AudioBus* bus2) {
92 // Fill |bus1| with dummy data. 96 // Fill |bus1| with dummy data.
93 for (int i = 0; i < bus1->channels(); ++i) 97 for (int i = 0; i < bus1->channels(); ++i)
94 std::fill(bus1->channel(i), bus1->channel(i) + bus1->frames(), i); 98 std::fill(bus1->channel(i), bus1->channel(i) + bus1->frames(), i);
95 99
96 // Verify copy from |bus1| to |bus2|. 100 // Verify copy from |bus1| to |bus2|.
97 bus2->Zero(); 101 bus2->Zero();
98 bus1->CopyTo(bus2); 102 bus1->CopyTo(bus2);
99 VerifyBus(bus1, bus2); 103 VerifyAreEqual(bus1, bus2);
100 104
101 // Verify copy from |bus2| to |bus1|. 105 // Verify copy from |bus2| to |bus1|.
102 bus1->Zero(); 106 bus1->Zero();
103 bus2->CopyTo(bus1); 107 bus2->CopyTo(bus1);
104 VerifyBus(bus2, bus1); 108 VerifyAreEqual(bus2, bus1);
105 } 109 }
106 110
107 protected: 111 protected:
108 std::vector<float*> data_; 112 std::vector<float*> data_;
109 113
110 DISALLOW_COPY_AND_ASSIGN(AudioBusTest); 114 DISALLOW_COPY_AND_ASSIGN(AudioBusTest);
111 }; 115 };
112 116
113 // Verify basic Create(...) method works as advertised. 117 // Verify basic Create(...) method works as advertised.
114 TEST_F(AudioBusTest, Create) { 118 TEST_F(AudioBusTest, Create) {
115 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 119 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
116 VerifyParams(bus.get()); 120 VerifyChannelAndFrameCount(bus.get());
117 VerifyChannelData(bus.get()); 121 VerifyReadWriteAndAlignment(bus.get());
118 } 122 }
119 123
120 // Verify Create(...) using AudioParameters works as advertised. 124 // Verify Create(...) using AudioParameters works as advertised.
121 TEST_F(AudioBusTest, CreateUsingAudioParameters) { 125 TEST_F(AudioBusTest, CreateUsingAudioParameters) {
122 std::unique_ptr<AudioBus> bus = AudioBus::Create( 126 std::unique_ptr<AudioBus> bus = AudioBus::Create(
123 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, 127 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
124 kSampleRate, 32, kFrameCount)); 128 kSampleRate, 32, kFrameCount));
125 VerifyParams(bus.get()); 129 VerifyChannelAndFrameCount(bus.get());
126 VerifyChannelData(bus.get()); 130 VerifyReadWriteAndAlignment(bus.get());
127 } 131 }
128 132
129 // Verify an AudioBus created via wrapping a vector works as advertised. 133 // Verify an AudioBus created via wrapping a vector works as advertised.
130 TEST_F(AudioBusTest, WrapVector) { 134 TEST_F(AudioBusTest, WrapVector) {
131 data_.reserve(kChannels); 135 data_.reserve(kChannels);
132 for (int i = 0; i < kChannels; ++i) { 136 for (int i = 0; i < kChannels; ++i) {
133 data_.push_back(static_cast<float*>(base::AlignedAlloc( 137 data_.push_back(static_cast<float*>(base::AlignedAlloc(
134 sizeof(*data_[i]) * kFrameCount, AudioBus::kChannelAlignment))); 138 sizeof(*data_[i]) * kFrameCount, AudioBus::kChannelAlignment)));
135 } 139 }
136 140
137 std::unique_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_); 141 std::unique_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_);
138 VerifyParams(bus.get()); 142 VerifyChannelAndFrameCount(bus.get());
139 VerifyChannelData(bus.get()); 143 VerifyReadWriteAndAlignment(bus.get());
140 } 144 }
141 145
142 // Verify an AudioBus created via wrapping a memory block works as advertised. 146 // Verify an AudioBus created via wrapping a memory block works as advertised.
143 TEST_F(AudioBusTest, WrapMemory) { 147 TEST_F(AudioBusTest, WrapMemory) {
144 AudioParameters params( 148 AudioParameters params(
145 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32, 149 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32,
146 kFrameCount); 150 kFrameCount);
147 int data_size = AudioBus::CalculateMemorySize(params); 151 int data_size = AudioBus::CalculateMemorySize(params);
148 std::unique_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>( 152 std::unique_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>(
149 base::AlignedAlloc(data_size, AudioBus::kChannelAlignment))); 153 base::AlignedAlloc(data_size, AudioBus::kChannelAlignment)));
150 154
151 // Fill the memory with a test value we can check for after wrapping. 155 // Fill the memory with a test value we can check for after wrapping.
152 static const float kTestValue = 3; 156 static const float kTestValue = 3;
153 std::fill( 157 std::fill(
154 data.get(), data.get() + data_size / sizeof(*data.get()), kTestValue); 158 data.get(), data.get() + data_size / sizeof(*data.get()), kTestValue);
155 159
156 std::unique_ptr<AudioBus> bus = AudioBus::WrapMemory(params, data.get()); 160 std::unique_ptr<AudioBus> bus = AudioBus::WrapMemory(params, data.get());
157 // Verify the test value we filled prior to wrapping. 161 // Verify the test value we filled prior to wrapping.
158 for (int i = 0; i < bus->channels(); ++i) 162 for (int i = 0; i < bus->channels(); ++i)
159 VerifyValue(bus->channel(i), bus->frames(), kTestValue); 163 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), kTestValue);
160 VerifyParams(bus.get()); 164 VerifyChannelAndFrameCount(bus.get());
161 VerifyChannelData(bus.get()); 165 VerifyReadWriteAndAlignment(bus.get());
162 166
163 // Verify the channel vectors lie within the provided memory block. 167 // Verify the channel vectors lie within the provided memory block.
164 EXPECT_GE(bus->channel(0), data.get()); 168 EXPECT_GE(bus->channel(0), data.get());
165 EXPECT_LT(bus->channel(bus->channels() - 1) + bus->frames(), 169 EXPECT_LT(bus->channel(bus->channels() - 1) + bus->frames(),
166 data.get() + data_size / sizeof(*data.get())); 170 data.get() + data_size / sizeof(*data.get()));
167 } 171 }
168 172
169 // Simulate a shared memory transfer and verify results. 173 // Simulate a shared memory transfer and verify results.
170 TEST_F(AudioBusTest, CopyTo) { 174 TEST_F(AudioBusTest, CopyTo) {
171 // Create one bus with AudioParameters and the other through direct values to 175 // Create one bus with AudioParameters and the other through direct values to
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 214
211 // Fill the bus with dummy data. 215 // Fill the bus with dummy data.
212 for (int i = 0; i < bus->channels(); ++i) 216 for (int i = 0; i < bus->channels(); ++i)
213 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); 217 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
214 EXPECT_FALSE(bus->AreFramesZero()); 218 EXPECT_FALSE(bus->AreFramesZero());
215 219
216 // Zero first half the frames of each channel. 220 // Zero first half the frames of each channel.
217 bus->ZeroFrames(kFrameCount / 2); 221 bus->ZeroFrames(kFrameCount / 2);
218 for (int i = 0; i < bus->channels(); ++i) { 222 for (int i = 0; i < bus->channels(); ++i) {
219 SCOPED_TRACE("First Half Zero"); 223 SCOPED_TRACE("First Half Zero");
220 VerifyValue(bus->channel(i), kFrameCount / 2, 0); 224 VerifyArrayIsFilledWithValue(bus->channel(i), kFrameCount / 2, 0);
221 VerifyValue(bus->channel(i) + kFrameCount / 2, 225 VerifyArrayIsFilledWithValue(bus->channel(i) + kFrameCount / 2,
222 kFrameCount - kFrameCount / 2, i + 1); 226 kFrameCount - kFrameCount / 2, i + 1);
223 } 227 }
224 EXPECT_FALSE(bus->AreFramesZero()); 228 EXPECT_FALSE(bus->AreFramesZero());
225 229
226 // Fill the bus with dummy data. 230 // Fill the bus with dummy data.
227 for (int i = 0; i < bus->channels(); ++i) 231 for (int i = 0; i < bus->channels(); ++i)
228 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); 232 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
229 233
230 // Zero the last half of the frames. 234 // Zero the last half of the frames.
231 bus->ZeroFramesPartial(kFrameCount / 2, kFrameCount - kFrameCount / 2); 235 bus->ZeroFramesPartial(kFrameCount / 2, kFrameCount - kFrameCount / 2);
232 for (int i = 0; i < bus->channels(); ++i) { 236 for (int i = 0; i < bus->channels(); ++i) {
233 SCOPED_TRACE("Last Half Zero"); 237 SCOPED_TRACE("Last Half Zero");
234 VerifyValue(bus->channel(i) + kFrameCount / 2, 238 VerifyArrayIsFilledWithValue(bus->channel(i) + kFrameCount / 2,
235 kFrameCount - kFrameCount / 2, 0); 239 kFrameCount - kFrameCount / 2, 0);
236 VerifyValue(bus->channel(i), kFrameCount / 2, i + 1); 240 VerifyArrayIsFilledWithValue(bus->channel(i), kFrameCount / 2, i + 1);
237 } 241 }
238 EXPECT_FALSE(bus->AreFramesZero()); 242 EXPECT_FALSE(bus->AreFramesZero());
239 243
240 // Fill the bus with dummy data. 244 // Fill the bus with dummy data.
241 for (int i = 0; i < bus->channels(); ++i) 245 for (int i = 0; i < bus->channels(); ++i)
242 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); 246 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
243 247
244 // Zero all the frames of each channel. 248 // Zero all the frames of each channel.
245 bus->Zero(); 249 bus->Zero();
246 for (int i = 0; i < bus->channels(); ++i) { 250 for (int i = 0; i < bus->channels(); ++i) {
247 SCOPED_TRACE("All Zero"); 251 SCOPED_TRACE("All Zero");
248 VerifyValue(bus->channel(i), bus->frames(), 0); 252 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), 0);
249 } 253 }
250 EXPECT_TRUE(bus->AreFramesZero()); 254 EXPECT_TRUE(bus->AreFramesZero());
251 } 255 }
252 256
253 // Each test vector represents two channels of data in the following arbitrary 257 // Each test vector represents two channels of data in the following arbitrary
254 // layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>. 258 // layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>.
255 static const int kTestVectorSize = 10; 259 static const int kTestVectorSize = 10;
256 static const uint8_t kTestVectorUint8[kTestVectorSize] = { 260 static const uint8_t kTestVectorUint8[kTestVectorSize] = {
257 0, -INT8_MIN, UINT8_MAX, 261 0, -INT8_MIN, UINT8_MAX,
258 0, INT8_MAX / 2 + 128, INT8_MIN / 2 + 128, 262 0, INT8_MAX / 2 + 128, INT8_MIN / 2 + 128,
259 -INT8_MIN, UINT8_MAX, -INT8_MIN, 263 -INT8_MIN, UINT8_MAX, -INT8_MIN,
260 -INT8_MIN}; 264 -INT8_MIN};
261 static const int16_t kTestVectorInt16[kTestVectorSize] = { 265 static const int16_t kTestVectorInt16[kTestVectorSize] = {
262 INT16_MIN, 0, INT16_MAX, INT16_MIN, INT16_MAX / 2, 266 INT16_MIN, 0, INT16_MAX, INT16_MIN, INT16_MAX / 2,
263 INT16_MIN / 2, 0, INT16_MAX, 0, 0}; 267 INT16_MIN / 2, 0, INT16_MAX, 0, 0};
264 static const int32_t kTestVectorInt32[kTestVectorSize] = { 268 static const int32_t kTestVectorInt32[kTestVectorSize] = {
265 INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2, 269 INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2,
266 INT32_MIN / 2, 0, INT32_MAX, 0, 0}; 270 INT32_MIN / 2, 0, INT32_MAX, 0, 0};
271 static const float kTestVectorFloat[kTestVectorSize] = {
272 -1.0f, 0.0f, 1.0f, -1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f};
267 273
268 // Expected results. 274 // Expected results.
269 static const int kTestVectorFrames = kTestVectorSize / 2; 275 static const int kTestVectorFrameCount = kTestVectorSize / 2;
270 static const float kTestVectorResult[][kTestVectorFrames] = { 276 static const float kTestVectorResult[][kTestVectorFrameCount] = {
271 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }}; 277 {-1.0f, 1.0f, 0.5f, 0.0f, 0.0f},
272 static const int kTestVectorChannels = arraysize(kTestVectorResult); 278 {0.0f, -1.0f, -0.5f, 1.0f, 0.0f}};
279 static const int kTestVectorChannelCount = arraysize(kTestVectorResult);
273 280
274 // Verify FromInterleaved() deinterleaves audio in supported formats correctly. 281 // Verify FromInterleaved() deinterleaves audio in supported formats correctly.
275 TEST_F(AudioBusTest, FromInterleaved) { 282 TEST_F(AudioBusTest, FromInterleaved) {
276 std::unique_ptr<AudioBus> bus = 283 std::unique_ptr<AudioBus> bus =
277 AudioBus::Create(kTestVectorChannels, kTestVectorFrames); 284 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
278 std::unique_ptr<AudioBus> expected = 285 std::unique_ptr<AudioBus> expected =
279 AudioBus::Create(kTestVectorChannels, kTestVectorFrames); 286 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
280 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 287 for (int ch = 0; ch < kTestVectorChannelCount; ++ch) {
281 memcpy(expected->channel(ch), kTestVectorResult[ch], 288 memcpy(expected->channel(ch), kTestVectorResult[ch],
282 kTestVectorFrames * sizeof(*expected->channel(ch))); 289 kTestVectorFrameCount * sizeof(*expected->channel(ch)));
283 } 290 }
284 { 291 {
285 SCOPED_TRACE("uint8_t"); 292 SCOPED_TRACE("uint8_t");
286 bus->Zero(); 293 bus->Zero();
287 bus->FromInterleaved( 294 bus->FromInterleaved(kTestVectorUint8, kTestVectorFrameCount,
288 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8)); 295 sizeof(*kTestVectorUint8));
289 // Biased uint8_t calculations have poor precision, so the epsilon here is 296 // Biased uint8_t calculations have poor precision, so the epsilon here is
290 // slightly more permissive than int16_t and int32_t calculations. 297 // slightly more permissive than int16_t and int32_t calculations.
291 VerifyBusWithEpsilon(bus.get(), expected.get(), 298 VerifyAreEqualWithEpsilon(bus.get(), expected.get(),
292 1.0f / (std::numeric_limits<uint8_t>::max() - 1)); 299 1.0f / (std::numeric_limits<uint8_t>::max() - 1));
293 } 300 }
294 { 301 {
295 SCOPED_TRACE("int16_t"); 302 SCOPED_TRACE("int16_t");
296 bus->Zero(); 303 bus->Zero();
297 bus->FromInterleaved( 304 bus->FromInterleaved(kTestVectorInt16, kTestVectorFrameCount,
298 kTestVectorInt16, kTestVectorFrames, sizeof(*kTestVectorInt16)); 305 sizeof(*kTestVectorInt16));
299 VerifyBusWithEpsilon(bus.get(), expected.get(), 306 VerifyAreEqualWithEpsilon(
300 1.0f / (std::numeric_limits<uint16_t>::max() + 1.0f)); 307 bus.get(), expected.get(),
308 1.0f / (std::numeric_limits<uint16_t>::max() + 1.0f));
301 } 309 }
302 { 310 {
303 SCOPED_TRACE("int32_t"); 311 SCOPED_TRACE("int32_t");
304 bus->Zero(); 312 bus->Zero();
305 bus->FromInterleaved( 313 bus->FromInterleaved(kTestVectorInt32, kTestVectorFrameCount,
306 kTestVectorInt32, kTestVectorFrames, sizeof(*kTestVectorInt32)); 314 sizeof(*kTestVectorInt32));
307 VerifyBusWithEpsilon(bus.get(), expected.get(), 315 VerifyAreEqualWithEpsilon(
308 1.0f / (std::numeric_limits<uint32_t>::max() + 1.0f)); 316 bus.get(), expected.get(),
317 1.0f / (std::numeric_limits<uint32_t>::max() + 1.0f));
309 } 318 }
310 } 319 }
311 320
312 // Verify FromInterleavedPartial() deinterleaves audio correctly. 321 // Verify FromInterleavedPartial() deinterleaves audio correctly.
313 TEST_F(AudioBusTest, FromInterleavedPartial) { 322 TEST_F(AudioBusTest, FromInterleavedPartial) {
314 // Only deinterleave the middle two frames in each channel. 323 // Only deinterleave the middle two frames in each channel.
315 static const int kPartialStart = 1; 324 static const int kPartialStart = 1;
316 static const int kPartialFrames = 2; 325 static const int kPartialFrames = 2;
317 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); 326 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrameCount);
318 327
319 std::unique_ptr<AudioBus> bus = 328 std::unique_ptr<AudioBus> bus =
320 AudioBus::Create(kTestVectorChannels, kTestVectorFrames); 329 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
321 std::unique_ptr<AudioBus> expected = 330 std::unique_ptr<AudioBus> expected =
322 AudioBus::Create(kTestVectorChannels, kTestVectorFrames); 331 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
323 expected->Zero(); 332 expected->Zero();
324 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 333 for (int ch = 0; ch < kTestVectorChannelCount; ++ch) {
325 memcpy(expected->channel(ch) + kPartialStart, 334 memcpy(expected->channel(ch) + kPartialStart,
326 kTestVectorResult[ch] + kPartialStart, 335 kTestVectorResult[ch] + kPartialStart,
327 kPartialFrames * sizeof(*expected->channel(ch))); 336 kPartialFrames * sizeof(*expected->channel(ch)));
328 } 337 }
329 338
330 bus->Zero(); 339 bus->Zero();
331 bus->FromInterleavedPartial( 340 bus->FromInterleavedPartial(
332 kTestVectorInt32 + kPartialStart * bus->channels(), kPartialStart, 341 kTestVectorInt32 + kPartialStart * bus->channels(), kPartialStart,
333 kPartialFrames, sizeof(*kTestVectorInt32)); 342 kPartialFrames, sizeof(*kTestVectorInt32));
334 VerifyBus(bus.get(), expected.get()); 343 VerifyAreEqual(bus.get(), expected.get());
335 } 344 }
336 345
337 // Verify ToInterleaved() interleaves audio in suported formats correctly. 346 // Verify ToInterleaved() interleaves audio in suported formats correctly.
338 TEST_F(AudioBusTest, ToInterleaved) { 347 TEST_F(AudioBusTest, ToInterleaved) {
339 std::unique_ptr<AudioBus> bus = 348 std::unique_ptr<AudioBus> bus =
340 AudioBus::Create(kTestVectorChannels, kTestVectorFrames); 349 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
341 // Fill the bus with our test vector. 350 // Fill the bus with our test vector.
342 for (int ch = 0; ch < bus->channels(); ++ch) { 351 for (int ch = 0; ch < bus->channels(); ++ch) {
343 memcpy(bus->channel(ch), kTestVectorResult[ch], 352 memcpy(bus->channel(ch), kTestVectorResult[ch],
344 kTestVectorFrames * sizeof(*bus->channel(ch))); 353 kTestVectorFrameCount * sizeof(*bus->channel(ch)));
345 } 354 }
346 { 355 {
347 SCOPED_TRACE("uint8_t"); 356 SCOPED_TRACE("uint8_t");
348 uint8_t test_array[arraysize(kTestVectorUint8)]; 357 uint8_t test_array[arraysize(kTestVectorUint8)];
349 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorUint8), test_array); 358 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorUint8), test_array);
350 ASSERT_EQ(memcmp( 359 ASSERT_EQ(memcmp(
351 test_array, kTestVectorUint8, sizeof(kTestVectorUint8)), 0); 360 test_array, kTestVectorUint8, sizeof(kTestVectorUint8)), 0);
352 } 361 }
353 { 362 {
354 SCOPED_TRACE("int16_t"); 363 SCOPED_TRACE("int16_t");
(...skipping 13 matching lines...) Expand all
368 memcpy(fixed_test_array, kTestVectorInt32, sizeof(kTestVectorInt32)); 377 memcpy(fixed_test_array, kTestVectorInt32, sizeof(kTestVectorInt32));
369 ASSERT_EQ(fixed_test_array[4], std::numeric_limits<int32_t>::max() / 2); 378 ASSERT_EQ(fixed_test_array[4], std::numeric_limits<int32_t>::max() / 2);
370 fixed_test_array[4]++; 379 fixed_test_array[4]++;
371 380
372 ASSERT_TRUE( 381 ASSERT_TRUE(
373 memcmp(test_array, kTestVectorInt32, sizeof(kTestVectorInt32)) == 0 || 382 memcmp(test_array, kTestVectorInt32, sizeof(kTestVectorInt32)) == 0 ||
374 memcmp(test_array, fixed_test_array, sizeof(fixed_test_array)) == 0); 383 memcmp(test_array, fixed_test_array, sizeof(fixed_test_array)) == 0);
375 } 384 }
376 } 385 }
377 386
387 // Verify ToInterleavedFloat interleaves |AudioBus| channels of float
388 // into a single linear output buffer correctly
389 TEST_F(AudioBusTest, ToInterleavedFloat) {
390 SCOPED_TRACE("float");
391 std::unique_ptr<AudioBus> bus =
392 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
393 for (int ch = 0; ch < bus->channels(); ++ch) {
394 memcpy(bus->channel(ch), kTestVectorResult[ch],
395 kTestVectorFrameCount * sizeof(*bus->channel(ch)));
396 }
397
398 float test_array[arraysize(kTestVectorFloat)];
399 bus->ToInterleavedFloat(0, 0, bus->frames(), test_array);
400 ASSERT_EQ(memcmp(test_array, kTestVectorFloat,
401 kTestVectorFrameCount * sizeof(*kTestVectorFloat)),
402 0);
403 }
404
378 // Verify ToInterleavedPartial() interleaves audio correctly. 405 // Verify ToInterleavedPartial() interleaves audio correctly.
379 TEST_F(AudioBusTest, ToInterleavedPartial) { 406 TEST_F(AudioBusTest, ToInterleavedPartial) {
380 // Only interleave the middle two frames in each channel. 407 // Only interleave the middle two frames in each channel.
381 static const int kPartialStart = 1; 408 static const int kPartialStart = 1;
382 static const int kPartialFrames = 2; 409 static const int kPartialFrames = 2;
383 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); 410 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrameCount);
384 411
385 std::unique_ptr<AudioBus> expected = 412 std::unique_ptr<AudioBus> expected =
386 AudioBus::Create(kTestVectorChannels, kTestVectorFrames); 413 AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
387 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 414 for (int ch = 0; ch < kTestVectorChannelCount; ++ch) {
388 memcpy(expected->channel(ch), kTestVectorResult[ch], 415 memcpy(expected->channel(ch), kTestVectorResult[ch],
389 kTestVectorFrames * sizeof(*expected->channel(ch))); 416 kTestVectorFrameCount * sizeof(*expected->channel(ch)));
390 } 417 }
391 418
392 int16_t test_array[arraysize(kTestVectorInt16)]; 419 int16_t test_array[arraysize(kTestVectorInt16)];
393 expected->ToInterleavedPartial( 420 expected->ToInterleavedPartial(
394 kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array); 421 kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array);
395 ASSERT_EQ(memcmp( 422 ASSERT_EQ(memcmp(test_array,
396 test_array, kTestVectorInt16 + kPartialStart * kTestVectorChannels, 423 kTestVectorInt16 + kPartialStart * kTestVectorChannelCount,
397 kPartialFrames * sizeof(*kTestVectorInt16) * kTestVectorChannels), 0); 424 kPartialFrames * sizeof(*kTestVectorInt16) *
425 kTestVectorChannelCount),
426 0);
398 } 427 }
399 428
400 TEST_F(AudioBusTest, Scale) { 429 TEST_F(AudioBusTest, Scale) {
401 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 430 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
402 431
403 // Fill the bus with dummy data. 432 // Fill the bus with dummy data.
404 static const float kFillValue = 1; 433 static const float kFillValue = 1;
405 for (int i = 0; i < bus->channels(); ++i) 434 for (int i = 0; i < bus->channels(); ++i)
406 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), kFillValue); 435 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), kFillValue);
407 436
408 // Adjust by an invalid volume and ensure volume is unchanged. 437 // Adjust by an invalid volume and ensure volume is unchanged.
409 bus->Scale(-1); 438 bus->Scale(-1);
410 for (int i = 0; i < bus->channels(); ++i) { 439 for (int i = 0; i < bus->channels(); ++i) {
411 SCOPED_TRACE("Invalid Scale"); 440 SCOPED_TRACE("Invalid Scale");
412 VerifyValue(bus->channel(i), bus->frames(), kFillValue); 441 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), kFillValue);
413 } 442 }
414 443
415 // Verify correct volume adjustment. 444 // Verify correct volume adjustment.
416 static const float kVolume = 0.5; 445 static const float kVolume = 0.5;
417 bus->Scale(kVolume); 446 bus->Scale(kVolume);
418 for (int i = 0; i < bus->channels(); ++i) { 447 for (int i = 0; i < bus->channels(); ++i) {
419 SCOPED_TRACE("Half Scale"); 448 SCOPED_TRACE("Half Scale");
420 VerifyValue(bus->channel(i), bus->frames(), kFillValue * kVolume); 449 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(),
450 kFillValue * kVolume);
421 } 451 }
422 452
423 // Verify zero volume case. 453 // Verify zero volume case.
424 bus->Scale(0); 454 bus->Scale(0);
425 for (int i = 0; i < bus->channels(); ++i) { 455 for (int i = 0; i < bus->channels(); ++i) {
426 SCOPED_TRACE("Zero Scale"); 456 SCOPED_TRACE("Zero Scale");
427 VerifyValue(bus->channel(i), bus->frames(), 0); 457 VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), 0);
428 } 458 }
429 } 459 }
430 460
431 } // namespace media 461 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698