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

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

Issue 17112016: Add new class AudioBufferQueue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 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
« no previous file with comments | « media/base/audio_buffer_queue_unittest.cc ('k') | media/base/test_helpers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/string_util.h" 5 #include "base/string_util.h"
6 #include "base/strings/stringprintf.h" 6 #include "base/strings/stringprintf.h"
7 #include "media/base/audio_buffer.h" 7 #include "media/base/audio_buffer.h"
8 #include "media/base/audio_bus.h" 8 #include "media/base/audio_bus.h"
9 #include "media/base/test_helpers.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 namespace media { 12 namespace media {
12 13
13 template <class T>
14 static scoped_refptr<AudioBuffer> MakeInterleavedBuffer(
15 SampleFormat format,
16 int channels,
17 T start,
18 T increment,
19 int frames,
20 const base::TimeDelta start_time) {
21 DCHECK(format == kSampleFormatU8 || format == kSampleFormatS16 ||
22 format == kSampleFormatS32 || format == kSampleFormatF32);
23
24 // Create a block of memory with values:
25 // start
26 // start + increment
27 // start + 2 * increment, ...
28 // Since this is interleaved data, channel 0 data will be:
29 // start
30 // start + channels * increment
31 // start + 2 * channels * increment, ...
32 int buffer_size = frames * channels * sizeof(T);
33 scoped_ptr<uint8[]> memory(new uint8[buffer_size]);
34 uint8* data[] = { memory.get() };
35 T* buffer = reinterpret_cast<T*>(memory.get());
36 for (int i = 0; i < frames * channels; ++i) {
37 buffer[i] = start;
38 start += increment;
39 }
40 // Duration is 1 second per frame (for simplicity).
41 base::TimeDelta duration = base::TimeDelta::FromSeconds(frames);
42 return AudioBuffer::CopyFrom(
43 format, channels, frames, data, start_time, duration);
44 }
45
46 template <class T>
47 static scoped_refptr<AudioBuffer> MakePlanarBuffer(
48 SampleFormat format,
49 int channels,
50 T start,
51 T increment,
52 int frames,
53 const base::TimeDelta start_time) {
54 DCHECK(format == kSampleFormatPlanarF32 || format == kSampleFormatPlanarS16);
55
56 // Create multiple blocks of data, once for each channel.
57 // Values in channel 0 will be:
58 // start
59 // start + increment
60 // start + 2 * increment, ...
61 // Values in channel 1 will be:
62 // start + frames * increment
63 // start + (frames + 1) * increment
64 // start + (frames + 2) * increment, ...
65 int buffer_size = frames * sizeof(T);
66 scoped_ptr<uint8*[]> data(new uint8*[channels]);
67 scoped_ptr<uint8[]> memory(new uint8[channels * buffer_size]);
68 for (int i = 0; i < channels; ++i) {
69 data.get()[i] = memory.get() + i * buffer_size;
70 T* buffer = reinterpret_cast<T*>(data.get()[i]);
71 for (int j = 0; j < frames; ++j) {
72 buffer[j] = start;
73 start += increment;
74 }
75 }
76 // Duration is 1 second per frame (for simplicity).
77 base::TimeDelta duration = base::TimeDelta::FromSeconds(frames);
78 return AudioBuffer::CopyFrom(
79 format, channels, frames, data.get(), start_time, duration);
80 }
81
82 static void VerifyResult(float* channel_data, 14 static void VerifyResult(float* channel_data,
83 int frames, 15 int frames,
84 float start, 16 float start,
85 float increment) { 17 float increment) {
86 for (int i = 0; i < frames; ++i) { 18 for (int i = 0; i < frames; ++i) {
87 SCOPED_TRACE(base::StringPrintf( 19 SCOPED_TRACE(base::StringPrintf(
88 "i=%d/%d start=%f, increment=%f", i, frames, start, increment)); 20 "i=%d/%d start=%f, increment=%f", i, frames, start, increment));
89 ASSERT_EQ(channel_data[i], start); 21 ASSERT_EQ(channel_data[i], start);
90 start += increment; 22 start += increment;
91 } 23 }
92 } 24 }
93 25
94 TEST(AudioBufferTest, CopyFrom) { 26 TEST(AudioBufferTest, CopyFrom) {
95 const int channels = 1; 27 const int channels = 1;
96 const int frames = 8; 28 const int frames = 8;
97 const base::TimeDelta start_time; 29 const base::TimeDelta start_time;
98 scoped_refptr<AudioBuffer> buffer = MakeInterleavedBuffer<uint8>( 30 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<uint8>(
99 kSampleFormatU8, channels, 1, 1, frames, start_time); 31 kSampleFormatU8, channels, 1, 1, frames, start_time);
100 EXPECT_EQ(frames, buffer->frame_count()); 32 EXPECT_EQ(frames, buffer->frame_count());
101 EXPECT_EQ(buffer->timestamp(), start_time); 33 EXPECT_EQ(buffer->timestamp(), start_time);
102 EXPECT_EQ(buffer->duration().InSeconds(), frames); 34 EXPECT_EQ(buffer->duration().InSeconds(), frames);
103 EXPECT_FALSE(buffer->end_of_stream()); 35 EXPECT_FALSE(buffer->end_of_stream());
104 } 36 }
105 37
106 TEST(AudioBufferTest, CreateEOSBuffer) { 38 TEST(AudioBufferTest, CreateEOSBuffer) {
107 scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEOSBuffer(); 39 scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEOSBuffer();
108 EXPECT_TRUE(buffer->end_of_stream()); 40 EXPECT_TRUE(buffer->end_of_stream());
(...skipping 13 matching lines...) Expand all
122 54
123 buffer = AudioBuffer::CopyFrom( 55 buffer = AudioBuffer::CopyFrom(
124 kSampleFormatF32, 4, 2, data, kTimestampA, kTimestampB); 56 kSampleFormatF32, 4, 2, data, kTimestampA, kTimestampB);
125 EXPECT_EQ(2, buffer->frame_count()); // now 4 channels of 32-bit data 57 EXPECT_EQ(2, buffer->frame_count()); // now 4 channels of 32-bit data
126 } 58 }
127 59
128 TEST(AudioBufferTest, ReadU8) { 60 TEST(AudioBufferTest, ReadU8) {
129 const int channels = 4; 61 const int channels = 4;
130 const int frames = 4; 62 const int frames = 4;
131 const base::TimeDelta start_time; 63 const base::TimeDelta start_time;
132 scoped_refptr<AudioBuffer> buffer = MakeInterleavedBuffer<uint8>( 64 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<uint8>(
133 kSampleFormatU8, channels, 128, 1, frames, start_time); 65 kSampleFormatU8, channels, 128, 1, frames, start_time);
134 66
135 // Read all 4 frames from the buffer. Data is interleaved, so ch[0] should be 67 // Read all 4 frames from the buffer. Data is interleaved, so ch[0] should be
136 // 128, 132, 136, 140, other channels similar. However, values are converted 68 // 128, 132, 136, 140, other channels similar. However, values are converted
137 // from [0, 255] to [-1.0, 1.0] with a bias of 128. Thus the first buffer 69 // from [0, 255] to [-1.0, 1.0] with a bias of 128. Thus the first buffer
138 // value should be 0.0, then 1/127, 2/127, etc. 70 // value should be 0.0, then 1/127, 2/127, etc.
139 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 71 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
140 buffer->ReadFrames(frames, 0, 0, bus.get()); 72 buffer->ReadFrames(frames, 0, 0, bus.get());
141 VerifyResult(bus->channel(0), frames, 0.0f, 4.0f / 127.0f); 73 VerifyResult(bus->channel(0), frames, 0.0f, 4.0f / 127.0f);
142 VerifyResult(bus->channel(1), frames, 1.0f / 127.0f, 4.0f / 127.0f); 74 VerifyResult(bus->channel(1), frames, 1.0f / 127.0f, 4.0f / 127.0f);
143 VerifyResult(bus->channel(2), frames, 2.0f / 127.0f, 4.0f / 127.0f); 75 VerifyResult(bus->channel(2), frames, 2.0f / 127.0f, 4.0f / 127.0f);
144 VerifyResult(bus->channel(3), frames, 3.0f / 127.0f, 4.0f / 127.0f); 76 VerifyResult(bus->channel(3), frames, 3.0f / 127.0f, 4.0f / 127.0f);
145 } 77 }
146 78
147 TEST(AudioBufferTest, ReadS16) { 79 TEST(AudioBufferTest, ReadS16) {
148 const int channels = 2; 80 const int channels = 2;
149 const int frames = 10; 81 const int frames = 10;
150 const base::TimeDelta start_time; 82 const base::TimeDelta start_time;
151 scoped_refptr<AudioBuffer> buffer = MakeInterleavedBuffer<int16>( 83 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<int16>(
152 kSampleFormatS16, channels, 1, 1, frames, start_time); 84 kSampleFormatS16, channels, 1, 1, frames, start_time);
153 85
154 // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 1, 86 // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 1,
155 // 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. Data is converted 87 // 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. Data is converted
156 // to float from -1.0 to 1.0 based on int16 range. 88 // to float from -1.0 to 1.0 based on int16 range.
157 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 89 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
158 buffer->ReadFrames(6, 0, 0, bus.get()); 90 buffer->ReadFrames(6, 0, 0, bus.get());
159 VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 2.0f / kint16max); 91 VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 2.0f / kint16max);
160 VerifyResult(bus->channel(1), 6, 2.0f / kint16max, 2.0f / kint16max); 92 VerifyResult(bus->channel(1), 6, 2.0f / kint16max, 2.0f / kint16max);
161 93
162 // Now read the same data one frame at a time. 94 // Now read the same data one frame at a time.
163 bus = AudioBus::Create(channels, 100); 95 bus = AudioBus::Create(channels, 100);
164 for (int i = 0; i < frames; ++i) { 96 for (int i = 0; i < frames; ++i) {
165 buffer->ReadFrames(1, i, i, bus.get()); 97 buffer->ReadFrames(1, i, i, bus.get());
166 } 98 }
167 VerifyResult(bus->channel(0), frames, 1.0f / kint16max, 2.0f / kint16max); 99 VerifyResult(bus->channel(0), frames, 1.0f / kint16max, 2.0f / kint16max);
168 VerifyResult(bus->channel(1), frames, 2.0f / kint16max, 2.0f / kint16max); 100 VerifyResult(bus->channel(1), frames, 2.0f / kint16max, 2.0f / kint16max);
169 } 101 }
170 102
171 TEST(AudioBufferTest, ReadS32) { 103 TEST(AudioBufferTest, ReadS32) {
172 const int channels = 2; 104 const int channels = 2;
173 const int frames = 6; 105 const int frames = 6;
174 const base::TimeDelta start_time; 106 const base::TimeDelta start_time;
175 scoped_refptr<AudioBuffer> buffer = MakeInterleavedBuffer<int32>( 107 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<int32>(
176 kSampleFormatS32, channels, 1, 1, frames, start_time); 108 kSampleFormatS32, channels, 1, 1, frames, start_time);
177 109
178 // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 1, 110 // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 1,
179 // 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. Data is converted 111 // 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. Data is converted
180 // to float from -1.0 to 1.0 based on int32 range. 112 // to float from -1.0 to 1.0 based on int32 range.
181 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 113 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
182 buffer->ReadFrames(frames, 0, 0, bus.get()); 114 buffer->ReadFrames(frames, 0, 0, bus.get());
183 VerifyResult(bus->channel(0), frames, 1.0f / kint32max, 2.0f / kint32max); 115 VerifyResult(bus->channel(0), frames, 1.0f / kint32max, 2.0f / kint32max);
184 VerifyResult(bus->channel(1), frames, 2.0f / kint32max, 2.0f / kint32max); 116 VerifyResult(bus->channel(1), frames, 2.0f / kint32max, 2.0f / kint32max);
185 117
186 // Now read 2 frames starting at frame offset 3. ch[0] should be 7, 9, and 118 // Now read 2 frames starting at frame offset 3. ch[0] should be 7, 9, and
187 // ch[1] should be 8, 10. 119 // ch[1] should be 8, 10.
188 buffer->ReadFrames(2, 3, 0, bus.get()); 120 buffer->ReadFrames(2, 3, 0, bus.get());
189 VerifyResult(bus->channel(0), 2, 7.0f / kint32max, 2.0f / kint32max); 121 VerifyResult(bus->channel(0), 2, 7.0f / kint32max, 2.0f / kint32max);
190 VerifyResult(bus->channel(1), 2, 8.0f / kint32max, 2.0f / kint32max); 122 VerifyResult(bus->channel(1), 2, 8.0f / kint32max, 2.0f / kint32max);
191 } 123 }
192 124
193 TEST(AudioBufferTest, ReadF32) { 125 TEST(AudioBufferTest, ReadF32) {
194 const int channels = 2; 126 const int channels = 2;
195 const int frames = 20; 127 const int frames = 20;
196 const base::TimeDelta start_time; 128 const base::TimeDelta start_time;
197 scoped_refptr<AudioBuffer> buffer = MakeInterleavedBuffer<float>( 129 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<float>(
198 kSampleFormatF32, channels, 1.0f, 1.0f, frames, start_time); 130 kSampleFormatF32, channels, 1.0f, 1.0f, frames, start_time);
199 131
200 // Read first 10 frames from the buffer. F32 is interleaved, so ch[0] should 132 // Read first 10 frames from the buffer. F32 is interleaved, so ch[0] should
201 // be 1, 3, 5, ... and ch[1] should be 2, 4, 6, ... 133 // be 1, 3, 5, ... and ch[1] should be 2, 4, 6, ...
202 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 134 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
203 buffer->ReadFrames(10, 0, 0, bus.get()); 135 buffer->ReadFrames(10, 0, 0, bus.get());
204 VerifyResult(bus->channel(0), 10, 1.0f, 2.0f); 136 VerifyResult(bus->channel(0), 10, 1.0f, 2.0f);
205 VerifyResult(bus->channel(1), 10, 2.0f, 2.0f); 137 VerifyResult(bus->channel(1), 10, 2.0f, 2.0f);
206 138
207 // Read second 10 frames. 139 // Read second 10 frames.
208 bus = AudioBus::Create(channels, 100); 140 bus = AudioBus::Create(channels, 100);
209 buffer->ReadFrames(10, 10, 0, bus.get()); 141 buffer->ReadFrames(10, 10, 0, bus.get());
210 VerifyResult(bus->channel(0), 10, 21.0f, 2.0f); 142 VerifyResult(bus->channel(0), 10, 21.0f, 2.0f);
211 VerifyResult(bus->channel(1), 10, 22.0f, 2.0f); 143 VerifyResult(bus->channel(1), 10, 22.0f, 2.0f);
212 } 144 }
213 145
214 TEST(AudioBufferTest, ReadS16Planar) { 146 TEST(AudioBufferTest, ReadS16Planar) {
215 const int channels = 2; 147 const int channels = 2;
216 const int frames = 20; 148 const int frames = 20;
217 const base::TimeDelta start_time; 149 const base::TimeDelta start_time;
218 scoped_refptr<AudioBuffer> buffer = MakePlanarBuffer<int16>( 150 scoped_refptr<AudioBuffer> buffer = MakePlanarAudioBuffer<int16>(
219 kSampleFormatPlanarS16, channels, 1, 1, frames, start_time); 151 kSampleFormatPlanarS16, channels, 1, 1, frames, start_time);
220 152
221 // Read 6 frames from the buffer. Data is planar, so ch[0] should be 1, 2, 3, 153 // Read 6 frames from the buffer. Data is planar, so ch[0] should be 1, 2, 3,
222 // 4, 5, 6, and ch[1] should be 21, 22, 23, 24, 25, 26. Data is converted to 154 // 4, 5, 6, and ch[1] should be 21, 22, 23, 24, 25, 26. Data is converted to
223 // float from -1.0 to 1.0 based on int16 range. 155 // float from -1.0 to 1.0 based on int16 range.
224 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 156 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
225 buffer->ReadFrames(6, 0, 0, bus.get()); 157 buffer->ReadFrames(6, 0, 0, bus.get());
226 VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 1.0f / kint16max); 158 VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 1.0f / kint16max);
227 VerifyResult(bus->channel(1), 6, 21.0f / kint16max, 1.0f / kint16max); 159 VerifyResult(bus->channel(1), 6, 21.0f / kint16max, 1.0f / kint16max);
228 160
(...skipping 11 matching lines...) Expand all
240 buffer->ReadFrames(0, 0, 10, bus.get()); 172 buffer->ReadFrames(0, 0, 10, bus.get());
241 buffer->ReadFrames(0, 10, 0, bus.get()); 173 buffer->ReadFrames(0, 10, 0, bus.get());
242 VerifyResult(bus->channel(0), frames, 20.0f / kint16max, -1.0f / kint16max); 174 VerifyResult(bus->channel(0), frames, 20.0f / kint16max, -1.0f / kint16max);
243 VerifyResult(bus->channel(1), frames, 40.0f / kint16max, -1.0f / kint16max); 175 VerifyResult(bus->channel(1), frames, 40.0f / kint16max, -1.0f / kint16max);
244 } 176 }
245 177
246 TEST(AudioBufferTest, ReadF32Planar) { 178 TEST(AudioBufferTest, ReadF32Planar) {
247 const int channels = 4; 179 const int channels = 4;
248 const int frames = 100; 180 const int frames = 100;
249 const base::TimeDelta start_time; 181 const base::TimeDelta start_time;
250 scoped_refptr<AudioBuffer> buffer = MakePlanarBuffer<float>( 182 scoped_refptr<AudioBuffer> buffer = MakePlanarAudioBuffer<float>(
251 kSampleFormatPlanarF32, channels, 1.0f, 1.0f, frames, start_time); 183 kSampleFormatPlanarF32, channels, 1.0f, 1.0f, frames, start_time);
252 184
253 // Read all 100 frames from the buffer. F32 is planar, so ch[0] should be 1, 185 // Read all 100 frames from the buffer. F32 is planar, so ch[0] should be 1,
254 // 2, 3, 4, ..., ch[1] should be 101, 102, 103, ..., and so on for all 4 186 // 2, 3, 4, ..., ch[1] should be 101, 102, 103, ..., and so on for all 4
255 // channels. 187 // channels.
256 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 188 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
257 buffer->ReadFrames(frames, 0, 0, bus.get()); 189 buffer->ReadFrames(frames, 0, 0, bus.get());
258 VerifyResult(bus->channel(0), frames, 1.0f, 1.0f); 190 VerifyResult(bus->channel(0), frames, 1.0f, 1.0f);
259 VerifyResult(bus->channel(1), frames, 101.0f, 1.0f); 191 VerifyResult(bus->channel(1), frames, 101.0f, 1.0f);
260 VerifyResult(bus->channel(2), frames, 201.0f, 1.0f); 192 VerifyResult(bus->channel(2), frames, 201.0f, 1.0f);
261 VerifyResult(bus->channel(3), frames, 301.0f, 1.0f); 193 VerifyResult(bus->channel(3), frames, 301.0f, 1.0f);
262 194
263 // Now read 20 frames from the middle of the buffer. 195 // Now read 20 frames from the middle of the buffer.
264 bus = AudioBus::Create(channels, 100); 196 bus = AudioBus::Create(channels, 100);
265 buffer->ReadFrames(20, 50, 0, bus.get()); 197 buffer->ReadFrames(20, 50, 0, bus.get());
266 VerifyResult(bus->channel(0), 20, 51.0f, 1.0f); 198 VerifyResult(bus->channel(0), 20, 51.0f, 1.0f);
267 VerifyResult(bus->channel(1), 20, 151.0f, 1.0f); 199 VerifyResult(bus->channel(1), 20, 151.0f, 1.0f);
268 VerifyResult(bus->channel(2), 20, 251.0f, 1.0f); 200 VerifyResult(bus->channel(2), 20, 251.0f, 1.0f);
269 VerifyResult(bus->channel(3), 20, 351.0f, 1.0f); 201 VerifyResult(bus->channel(3), 20, 351.0f, 1.0f);
270 } 202 }
271 203
272 } // namespace media 204 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_buffer_queue_unittest.cc ('k') | media/base/test_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698