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 "media/base/audio_buffer.h" | 5 #include "media/base/audio_buffer.h" |
6 #include "media/base/audio_bus.h" | 6 #include "media/base/audio_bus.h" |
7 #include "media/base/test_helpers.h" | 7 #include "media/base/test_helpers.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
12 static const int kSampleRate = 48000; | 12 static const int kSampleRate = 48000; |
13 | 13 |
14 static void VerifyBus(AudioBus* bus, int frames, float start, float increment) { | 14 |
| 15 static void VerifyBusWithOffset(AudioBus* bus, |
| 16 int offset, |
| 17 int frames, |
| 18 float start, |
| 19 float increment) { |
15 for (int ch = 0; ch < bus->channels(); ++ch) { | 20 for (int ch = 0; ch < bus->channels(); ++ch) { |
16 const float v = start + ch * bus->frames() * increment; | 21 const float v = start + ch * bus->frames() * increment; |
17 for (int i = 0; i < frames; ++i) { | 22 for (int i = offset; i < frames; ++i) { |
18 ASSERT_FLOAT_EQ(v + i * increment, bus->channel(ch)[i]) << "i=" << i | 23 ASSERT_FLOAT_EQ(v + i * increment, bus->channel(ch)[i]) << "i=" << i |
19 << ", ch=" << ch; | 24 << ", ch=" << ch; |
20 } | 25 } |
21 } | 26 } |
22 } | 27 } |
23 | 28 |
| 29 static void VerifyBus(AudioBus* bus, int frames, float start, float increment) { |
| 30 VerifyBusWithOffset(bus, 0, frames, start, increment); |
| 31 } |
| 32 |
| 33 static void TrimRangeTest(SampleFormat sample_format) { |
| 34 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; |
| 35 const int channels = ChannelLayoutToChannelCount(channel_layout); |
| 36 const int frames = kSampleRate / 10; |
| 37 const base::TimeDelta timestamp = base::TimeDelta(); |
| 38 const base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100); |
| 39 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(sample_format, |
| 40 channel_layout, |
| 41 channels, |
| 42 kSampleRate, |
| 43 0, |
| 44 1, |
| 45 frames, |
| 46 timestamp); |
| 47 EXPECT_EQ(frames, buffer->frame_count()); |
| 48 EXPECT_EQ(timestamp, buffer->timestamp()); |
| 49 EXPECT_EQ(duration, buffer->duration()); |
| 50 |
| 51 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames); |
| 52 |
| 53 // Verify all frames before trimming. |
| 54 buffer->ReadFrames(frames, 0, 0, bus.get()); |
| 55 VerifyBus(bus.get(), frames, 0, 1); |
| 56 |
| 57 // Trim 10ms of frames from the middle of the buffer. |
| 58 int trim_start = frames / 2; |
| 59 const int trim_length = kSampleRate / 100; |
| 60 const base::TimeDelta trim_duration = base::TimeDelta::FromMilliseconds(10); |
| 61 buffer->TrimRange(trim_start, trim_start + trim_length); |
| 62 EXPECT_EQ(frames - trim_length, buffer->frame_count()); |
| 63 EXPECT_EQ(timestamp, buffer->timestamp()); |
| 64 EXPECT_EQ(duration - trim_duration, buffer->duration()); |
| 65 bus->Zero(); |
| 66 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 67 VerifyBus(bus.get(), trim_start, 0, 1); |
| 68 VerifyBusWithOffset( |
| 69 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); |
| 70 |
| 71 // Trim 10ms of frames from the start, which just adjusts the buffer's |
| 72 // internal start offset. |
| 73 buffer->TrimStart(trim_length); |
| 74 trim_start -= trim_length; |
| 75 EXPECT_EQ(frames - 2 * trim_length, buffer->frame_count()); |
| 76 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 77 EXPECT_EQ(duration - 2 * trim_duration, buffer->duration()); |
| 78 bus->Zero(); |
| 79 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 80 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 81 VerifyBusWithOffset( |
| 82 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); |
| 83 |
| 84 // Trim 10ms of frames from the end, which just adjusts the buffer's frame |
| 85 // count. |
| 86 buffer->TrimEnd(trim_length); |
| 87 EXPECT_EQ(frames - 3 * trim_length, buffer->frame_count()); |
| 88 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 89 EXPECT_EQ(duration - 3 * trim_duration, buffer->duration()); |
| 90 bus->Zero(); |
| 91 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 92 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 93 VerifyBusWithOffset( |
| 94 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); |
| 95 |
| 96 // Trim another 10ms from the inner portion of the buffer. |
| 97 buffer->TrimRange(trim_start, trim_start + trim_length); |
| 98 EXPECT_EQ(frames - 4 * trim_length, buffer->frame_count()); |
| 99 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 100 EXPECT_EQ(duration - 4 * trim_duration, buffer->duration()); |
| 101 bus->Zero(); |
| 102 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 103 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 104 VerifyBusWithOffset( |
| 105 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); |
| 106 |
| 107 // Trim off the end using TrimRange() to ensure end index is exclusive. |
| 108 buffer->TrimRange(buffer->frame_count() - trim_length, buffer->frame_count()); |
| 109 EXPECT_EQ(frames - 5 * trim_length, buffer->frame_count()); |
| 110 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 111 EXPECT_EQ(duration - 5 * trim_duration, buffer->duration()); |
| 112 bus->Zero(); |
| 113 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 114 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 115 VerifyBusWithOffset( |
| 116 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); |
| 117 |
| 118 // Trim off the start using TrimRange() to ensure start index is inclusive. |
| 119 buffer->TrimRange(0, trim_length); |
| 120 trim_start -= trim_length; |
| 121 EXPECT_EQ(frames - 6 * trim_length, buffer->frame_count()); |
| 122 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 123 EXPECT_EQ(duration - 6 * trim_duration, buffer->duration()); |
| 124 bus->Zero(); |
| 125 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 126 VerifyBus(bus.get(), trim_start, 2 * trim_length, 1); |
| 127 VerifyBusWithOffset( |
| 128 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); |
| 129 } |
| 130 |
24 TEST(AudioBufferTest, CopyFrom) { | 131 TEST(AudioBufferTest, CopyFrom) { |
25 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; | 132 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; |
26 scoped_refptr<AudioBuffer> original_buffer = | 133 scoped_refptr<AudioBuffer> original_buffer = |
27 MakeAudioBuffer<uint8>(kSampleFormatU8, | 134 MakeAudioBuffer<uint8>(kSampleFormatU8, |
28 kChannelLayout, | 135 kChannelLayout, |
29 ChannelLayoutToChannelCount(kChannelLayout), | 136 ChannelLayoutToChannelCount(kChannelLayout), |
30 kSampleRate, | 137 kSampleRate, |
31 1, | 138 1, |
32 1, | 139 1, |
33 kSampleRate / 100, | 140 kSampleRate / 100, |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 412 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
306 VerifyBus(bus.get(), buffer->frame_count(), 5 * ten_ms_of_frames, 1.0f); | 413 VerifyBus(bus.get(), buffer->frame_count(), 5 * ten_ms_of_frames, 1.0f); |
307 | 414 |
308 // Trim off the final 40ms from the end. | 415 // Trim off the final 40ms from the end. |
309 buffer->TrimEnd(4 * ten_ms_of_frames); | 416 buffer->TrimEnd(4 * ten_ms_of_frames); |
310 EXPECT_EQ(0, buffer->frame_count()); | 417 EXPECT_EQ(0, buffer->frame_count()); |
311 EXPECT_EQ(start_time + 5 * ten_ms, buffer->timestamp()); | 418 EXPECT_EQ(start_time + 5 * ten_ms, buffer->timestamp()); |
312 EXPECT_EQ(base::TimeDelta(), buffer->duration()); | 419 EXPECT_EQ(base::TimeDelta(), buffer->duration()); |
313 } | 420 } |
314 | 421 |
| 422 TEST(AudioBufferTest, TrimRangePlanar) { |
| 423 TrimRangeTest(kSampleFormatPlanarF32); |
| 424 } |
| 425 |
| 426 TEST(AudioBufferTest, TrimRangeInterleaved) { |
| 427 TrimRangeTest(kSampleFormatF32); |
| 428 } |
| 429 |
315 } // namespace media | 430 } // namespace media |
OLD | NEW |