OLD | NEW |
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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "media/base/audio_buffer.h" | 10 #include "media/base/audio_buffer.h" |
11 #include "media/base/audio_bus.h" | 11 #include "media/base/audio_bus.h" |
12 #include "media/base/test_helpers.h" | 12 #include "media/base/test_helpers.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace media { | 15 namespace media { |
16 | 16 |
17 static const int kSampleRate = 4800; | 17 static const int kSampleRate = 4800; |
18 | 18 |
19 static void VerifyBusWithOffset(AudioBus* bus, | 19 static void VerifyBusWithOffset(AudioBus* bus, |
20 int offset, | 20 int offset, |
21 int frames, | 21 int frames, |
22 float start, | 22 float start, |
23 float start_offset, | 23 float start_offset, |
24 float increment) { | 24 float increment) { |
25 for (int ch = 0; ch < bus->channels(); ++ch) { | 25 for (int ch = 0; ch < bus->channels(); ++ch) { |
26 const float v = start_offset + start + ch * bus->frames() * increment; | 26 const float v = start_offset + start + ch * bus->frames() * increment; |
27 | |
28 for (int i = offset; i < offset + frames; ++i) { | 27 for (int i = offset; i < offset + frames; ++i) { |
29 ASSERT_FLOAT_EQ(v + i * increment, bus->channel(ch)[i]) << "i=" << i | 28 ASSERT_FLOAT_EQ(v + i * increment, bus->channel(ch)[i]) << "i=" << i |
30 << ", ch=" << ch; | 29 << ", ch=" << ch; |
31 } | 30 } |
32 } | 31 } |
33 } | 32 } |
34 | 33 |
35 static void VerifyBus(AudioBus* bus, int frames, float start, float increment) { | 34 static void VerifyBus(AudioBus* bus, int frames, float start, float increment) { |
36 VerifyBusWithOffset(bus, 0, frames, start, 0, increment); | 35 VerifyBusWithOffset(bus, 0, frames, start, 0, increment); |
37 } | 36 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 150 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
152 VerifyBus(bus.get(), trim_start, 2 * trim_length, 1); | 151 VerifyBus(bus.get(), trim_start, 2 * trim_length, 1); |
153 VerifyBusWithOffset(bus.get(), | 152 VerifyBusWithOffset(bus.get(), |
154 trim_start, | 153 trim_start, |
155 buffer->frame_count() - trim_start, | 154 buffer->frame_count() - trim_start, |
156 trim_length * 2, | 155 trim_length * 2, |
157 trim_length * 2, | 156 trim_length * 2, |
158 1); | 157 1); |
159 } | 158 } |
160 | 159 |
161 void PadStartTest(SampleFormat sample_format) { | |
162 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; | |
163 const int channels = ChannelLayoutToChannelCount(channel_layout); | |
164 const int frames = 100; | |
165 const int silence_frames = frames / 5; | |
166 const base::TimeDelta start_time; | |
167 scoped_refptr<AudioBuffer> buffer = | |
168 MakeAudioBuffer<float>(sample_format, channel_layout, channels, | |
169 kSampleRate, 0.0f, 1.0f, frames, start_time); | |
170 | |
171 // Read all 100 frames from the buffer. | |
172 std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, frames); | |
173 buffer->ReadFrames(frames, 0, 0, bus.get()); | |
174 VerifyBus(bus.get(), frames, 0, 1); | |
175 | |
176 // Now trim off some frames and add in leading silence. | |
177 buffer->TrimStart(silence_frames); | |
178 buffer->PadStart(silence_frames); | |
179 | |
180 // Re-read. Verify first 20 frames of silence, next 80 frames same as before. | |
181 bus->Zero(); | |
182 buffer->ReadFrames(frames, 0, 0, bus.get()); | |
183 VerifyBus(bus.get(), silence_frames, 0, 0); | |
184 VerifyBusWithOffset(bus.get(), silence_frames, frames - silence_frames, 0, 0, | |
185 1); | |
186 | |
187 // Now trim off the silence. Verify silence is gone. | |
188 buffer->TrimStart(silence_frames); | |
189 bus->Zero(); | |
190 buffer->ReadFrames(frames - silence_frames, 0, 0, bus.get()); | |
191 VerifyBusWithOffset(bus.get(), 0, frames - silence_frames, silence_frames, 0, | |
192 1); | |
193 } | |
194 | |
195 TEST(AudioBufferTest, CopyFrom) { | 160 TEST(AudioBufferTest, CopyFrom) { |
196 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; | 161 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; |
197 scoped_refptr<AudioBuffer> original_buffer = MakeAudioBuffer<uint8_t>( | 162 scoped_refptr<AudioBuffer> original_buffer = MakeAudioBuffer<uint8_t>( |
198 kSampleFormatU8, kChannelLayout, | 163 kSampleFormatU8, kChannelLayout, |
199 ChannelLayoutToChannelCount(kChannelLayout), kSampleRate, 1, 1, | 164 ChannelLayoutToChannelCount(kChannelLayout), kSampleRate, 1, 1, |
200 kSampleRate / 100, base::TimeDelta()); | 165 kSampleRate / 100, base::TimeDelta()); |
201 scoped_refptr<AudioBuffer> new_buffer = | 166 scoped_refptr<AudioBuffer> new_buffer = |
202 AudioBuffer::CopyFrom(kSampleFormatU8, | 167 AudioBuffer::CopyFrom(kSampleFormatU8, |
203 original_buffer->channel_layout(), | 168 original_buffer->channel_layout(), |
204 original_buffer->channel_count(), | 169 original_buffer->channel_count(), |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 } | 427 } |
463 | 428 |
464 TEST(AudioBufferTest, TrimRangePlanar) { | 429 TEST(AudioBufferTest, TrimRangePlanar) { |
465 TrimRangeTest(kSampleFormatPlanarF32); | 430 TrimRangeTest(kSampleFormatPlanarF32); |
466 } | 431 } |
467 | 432 |
468 TEST(AudioBufferTest, TrimRangeInterleaved) { | 433 TEST(AudioBufferTest, TrimRangeInterleaved) { |
469 TrimRangeTest(kSampleFormatF32); | 434 TrimRangeTest(kSampleFormatF32); |
470 } | 435 } |
471 | 436 |
472 TEST(AudioBufferTest, PadStartPlanar) { | |
473 PadStartTest(kSampleFormatPlanarF32); | |
474 } | |
475 | |
476 TEST(AudioBufferTest, PadStartInterleaved) { | |
477 PadStartTest(kSampleFormatF32); | |
478 } | |
479 | |
480 TEST(AudioBufferTest, PadStartEmptyBuffer) { | |
481 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; | |
482 const int channels = ChannelLayoutToChannelCount(channel_layout); | |
483 const int frames = kSampleRate / 10; | |
484 const base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100); | |
485 const base::TimeDelta start_time; | |
486 scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEmptyBuffer( | |
487 channel_layout, channels, kSampleRate, frames, start_time); | |
488 | |
489 // Empty buffer should zero data size with non-zero frame count and duration. | |
490 EXPECT_EQ(std::size_t{0}, buffer->data_size()); | |
491 EXPECT_EQ(frames, buffer->frame_count()); | |
492 EXPECT_EQ(duration, buffer->duration()); | |
493 | |
494 // Read all frames from the buffer. All data should be 0. | |
495 std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, frames); | |
496 buffer->ReadFrames(frames, 0, 0, bus.get()); | |
497 VerifyBus(bus.get(), frames, 0, 0); | |
498 | |
499 // Double the number of frames by padding the start of the buffer with | |
500 // |frames| of silence. | |
501 buffer->PadStart(frames); | |
502 const int new_frame_count = frames * 2; | |
503 const base::TimeDelta new_duration = base::TimeDelta::FromMilliseconds(200); | |
504 | |
505 // Adding silence should not trigger an allocation, but the frame count and | |
506 // duration should be increased. | |
507 EXPECT_EQ(std::size_t{0}, buffer->data_size()); | |
508 EXPECT_EQ(new_frame_count, buffer->frame_count()); | |
509 EXPECT_EQ(new_duration, buffer->duration()); | |
510 | |
511 // Read all frames from the buffer. All data should be 0. | |
512 bus = AudioBus::Create(channels, new_frame_count); | |
513 buffer->ReadFrames(new_frame_count, 0, 0, bus.get()); | |
514 VerifyBus(bus.get(), new_frame_count, 0, 0); | |
515 } | |
516 | |
517 } // namespace media | 437 } // namespace media |
OLD | NEW |