Chromium Code Reviews| 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 | |
| 15 static void VerifyBusWithOffset(AudioBus* bus, | 14 static void VerifyBusWithOffset(AudioBus* bus, |
| 16 int offset, | 15 int offset, |
| 17 int frames, | 16 int frames, |
| 18 float start, | 17 float start, |
| 18 float start_offset, | |
|
wolenetz
2014/05/05 20:28:45
Is there really a difference between start and sta
DaleCurtis
2014/05/05 21:28:50
Yes. |start| is used to calculate a new |v| at th
wolenetz
2014/05/05 22:16:29
nit: Hmm. Then maybe lift the constant 'start_offs
DaleCurtis
2014/05/05 22:54:44
Done.
| |
| 19 float increment) { | 19 float increment) { |
| 20 for (int ch = 0; ch < bus->channels(); ++ch) { | 20 for (int ch = 0; ch < bus->channels(); ++ch) { |
| 21 const float v = start + ch * bus->frames() * increment; | 21 const float v = start + ch * bus->frames() * increment; |
| 22 for (int i = offset; i < frames; ++i) { | 22 for (int i = offset; i < offset + frames; ++i) { |
| 23 ASSERT_FLOAT_EQ(v + i * increment, bus->channel(ch)[i]) << "i=" << i | 23 ASSERT_FLOAT_EQ(start_offset + v + i * increment, bus->channel(ch)[i]) |
| 24 << ", ch=" << ch; | 24 << "i=" << i << ", ch=" << ch; |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 static void VerifyBus(AudioBus* bus, int frames, float start, float increment) { | 29 static void VerifyBus(AudioBus* bus, int frames, float start, float increment) { |
| 30 VerifyBusWithOffset(bus, 0, frames, start, increment); | 30 VerifyBusWithOffset(bus, 0, frames, start, 0, increment); |
| 31 } | 31 } |
| 32 | 32 |
| 33 static void TrimRangeTest(SampleFormat sample_format) { | 33 static void TrimRangeTest(SampleFormat sample_format) { |
| 34 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; | 34 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; |
| 35 const int channels = ChannelLayoutToChannelCount(channel_layout); | 35 const int channels = ChannelLayoutToChannelCount(channel_layout); |
| 36 const int frames = kSampleRate / 10; | 36 const int frames = kSampleRate / 10; |
| 37 const base::TimeDelta timestamp = base::TimeDelta(); | 37 const base::TimeDelta timestamp = base::TimeDelta(); |
| 38 const base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100); | 38 const base::TimeDelta duration = base::TimeDelta::FromMilliseconds(100); |
| 39 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(sample_format, | 39 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(sample_format, |
| 40 channel_layout, | 40 channel_layout, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 58 int trim_start = frames / 2; | 58 int trim_start = frames / 2; |
| 59 const int trim_length = kSampleRate / 100; | 59 const int trim_length = kSampleRate / 100; |
| 60 const base::TimeDelta trim_duration = base::TimeDelta::FromMilliseconds(10); | 60 const base::TimeDelta trim_duration = base::TimeDelta::FromMilliseconds(10); |
| 61 buffer->TrimRange(trim_start, trim_start + trim_length); | 61 buffer->TrimRange(trim_start, trim_start + trim_length); |
| 62 EXPECT_EQ(frames - trim_length, buffer->frame_count()); | 62 EXPECT_EQ(frames - trim_length, buffer->frame_count()); |
| 63 EXPECT_EQ(timestamp, buffer->timestamp()); | 63 EXPECT_EQ(timestamp, buffer->timestamp()); |
| 64 EXPECT_EQ(duration - trim_duration, buffer->duration()); | 64 EXPECT_EQ(duration - trim_duration, buffer->duration()); |
| 65 bus->Zero(); | 65 bus->Zero(); |
| 66 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 66 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 67 VerifyBus(bus.get(), trim_start, 0, 1); | 67 VerifyBus(bus.get(), trim_start, 0, 1); |
| 68 VerifyBusWithOffset( | 68 VerifyBusWithOffset(bus.get(), |
| 69 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); | 69 trim_start, |
| 70 buffer->frame_count() - trim_start, | |
| 71 0, | |
| 72 trim_length, | |
| 73 1); | |
| 70 | 74 |
| 71 // Trim 10ms of frames from the start, which just adjusts the buffer's | 75 // Trim 10ms of frames from the start, which just adjusts the buffer's |
| 72 // internal start offset. | 76 // internal start offset. |
| 73 buffer->TrimStart(trim_length); | 77 buffer->TrimStart(trim_length); |
| 74 trim_start -= trim_length; | 78 trim_start -= trim_length; |
| 75 EXPECT_EQ(frames - 2 * trim_length, buffer->frame_count()); | 79 EXPECT_EQ(frames - 2 * trim_length, buffer->frame_count()); |
| 76 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); | 80 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 77 EXPECT_EQ(duration - 2 * trim_duration, buffer->duration()); | 81 EXPECT_EQ(duration - 2 * trim_duration, buffer->duration()); |
| 78 bus->Zero(); | 82 bus->Zero(); |
| 79 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 83 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 80 VerifyBus(bus.get(), trim_start, trim_length, 1); | 84 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 81 VerifyBusWithOffset( | 85 VerifyBusWithOffset(bus.get(), |
| 82 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); | 86 trim_start, |
| 87 buffer->frame_count() - trim_start, | |
| 88 trim_length, | |
| 89 trim_length, | |
| 90 1); | |
| 83 | 91 |
| 84 // Trim 10ms of frames from the end, which just adjusts the buffer's frame | 92 // Trim 10ms of frames from the end, which just adjusts the buffer's frame |
| 85 // count. | 93 // count. |
| 86 buffer->TrimEnd(trim_length); | 94 buffer->TrimEnd(trim_length); |
| 87 EXPECT_EQ(frames - 3 * trim_length, buffer->frame_count()); | 95 EXPECT_EQ(frames - 3 * trim_length, buffer->frame_count()); |
| 88 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); | 96 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 89 EXPECT_EQ(duration - 3 * trim_duration, buffer->duration()); | 97 EXPECT_EQ(duration - 3 * trim_duration, buffer->duration()); |
| 90 bus->Zero(); | 98 bus->Zero(); |
| 91 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 99 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 92 VerifyBus(bus.get(), trim_start, trim_length, 1); | 100 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 93 VerifyBusWithOffset( | 101 VerifyBusWithOffset(bus.get(), |
| 94 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); | 102 trim_start, |
| 103 buffer->frame_count() - trim_start, | |
| 104 trim_length, | |
| 105 trim_length, | |
| 106 1); | |
| 95 | 107 |
| 96 // Trim another 10ms from the inner portion of the buffer. | 108 // Trim another 10ms from the inner portion of the buffer. |
| 97 buffer->TrimRange(trim_start, trim_start + trim_length); | 109 buffer->TrimRange(trim_start, trim_start + trim_length); |
| 98 EXPECT_EQ(frames - 4 * trim_length, buffer->frame_count()); | 110 EXPECT_EQ(frames - 4 * trim_length, buffer->frame_count()); |
| 99 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); | 111 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 100 EXPECT_EQ(duration - 4 * trim_duration, buffer->duration()); | 112 EXPECT_EQ(duration - 4 * trim_duration, buffer->duration()); |
| 101 bus->Zero(); | 113 bus->Zero(); |
| 102 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 114 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 103 VerifyBus(bus.get(), trim_start, trim_length, 1); | 115 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 104 VerifyBusWithOffset( | 116 VerifyBusWithOffset(bus.get(), |
| 105 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); | 117 trim_start, |
| 118 buffer->frame_count() - trim_start, | |
| 119 trim_length, | |
| 120 trim_length * 2, | |
| 121 1); | |
| 106 | 122 |
| 107 // Trim off the end using TrimRange() to ensure end index is exclusive. | 123 // Trim off the end using TrimRange() to ensure end index is exclusive. |
| 108 buffer->TrimRange(buffer->frame_count() - trim_length, buffer->frame_count()); | 124 buffer->TrimRange(buffer->frame_count() - trim_length, buffer->frame_count()); |
| 109 EXPECT_EQ(frames - 5 * trim_length, buffer->frame_count()); | 125 EXPECT_EQ(frames - 5 * trim_length, buffer->frame_count()); |
| 110 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); | 126 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
| 111 EXPECT_EQ(duration - 5 * trim_duration, buffer->duration()); | 127 EXPECT_EQ(duration - 5 * trim_duration, buffer->duration()); |
| 112 bus->Zero(); | 128 bus->Zero(); |
| 113 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 129 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 114 VerifyBus(bus.get(), trim_start, trim_length, 1); | 130 VerifyBus(bus.get(), trim_start, trim_length, 1); |
| 115 VerifyBusWithOffset( | 131 VerifyBusWithOffset(bus.get(), |
| 116 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); | 132 trim_start, |
| 133 buffer->frame_count() - trim_start, | |
| 134 trim_length, | |
| 135 trim_length * 2, | |
| 136 1); | |
| 117 | 137 |
| 118 // Trim off the start using TrimRange() to ensure start index is inclusive. | 138 // Trim off the start using TrimRange() to ensure start index is inclusive. |
| 119 buffer->TrimRange(0, trim_length); | 139 buffer->TrimRange(0, trim_length); |
| 120 trim_start -= trim_length; | 140 trim_start -= trim_length; |
| 121 EXPECT_EQ(frames - 6 * trim_length, buffer->frame_count()); | 141 EXPECT_EQ(frames - 6 * trim_length, buffer->frame_count()); |
| 122 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); | 142 EXPECT_EQ(timestamp + trim_duration, buffer->timestamp()); |
|
wolenetz
2014/05/05 20:28:45
nit: versus TrimStart, TrimRange(0,...) does not u
DaleCurtis
2014/05/05 21:28:50
No, callers can call TrimStart() if they want the
| |
| 123 EXPECT_EQ(duration - 6 * trim_duration, buffer->duration()); | 143 EXPECT_EQ(duration - 6 * trim_duration, buffer->duration()); |
| 124 bus->Zero(); | 144 bus->Zero(); |
| 125 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); | 145 buffer->ReadFrames(buffer->frame_count(), 0, 0, bus.get()); |
| 126 VerifyBus(bus.get(), trim_start, 2 * trim_length, 1); | 146 VerifyBus(bus.get(), trim_start, 2 * trim_length, 1); |
| 127 VerifyBusWithOffset( | 147 VerifyBusWithOffset(bus.get(), |
| 128 bus.get(), trim_start, buffer->frame_count() - trim_start, 0, 1); | 148 trim_start, |
| 149 buffer->frame_count() - trim_start, | |
| 150 trim_length * 2, | |
| 151 trim_length * 2, | |
| 152 1); | |
| 129 } | 153 } |
| 130 | 154 |
| 131 TEST(AudioBufferTest, CopyFrom) { | 155 TEST(AudioBufferTest, CopyFrom) { |
| 132 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; | 156 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; |
| 133 scoped_refptr<AudioBuffer> original_buffer = | 157 scoped_refptr<AudioBuffer> original_buffer = |
| 134 MakeAudioBuffer<uint8>(kSampleFormatU8, | 158 MakeAudioBuffer<uint8>(kSampleFormatU8, |
| 135 kChannelLayout, | 159 kChannelLayout, |
| 136 ChannelLayoutToChannelCount(kChannelLayout), | 160 ChannelLayoutToChannelCount(kChannelLayout), |
| 137 kSampleRate, | 161 kSampleRate, |
| 138 1, | 162 1, |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 421 | 445 |
| 422 TEST(AudioBufferTest, TrimRangePlanar) { | 446 TEST(AudioBufferTest, TrimRangePlanar) { |
| 423 TrimRangeTest(kSampleFormatPlanarF32); | 447 TrimRangeTest(kSampleFormatPlanarF32); |
| 424 } | 448 } |
| 425 | 449 |
| 426 TEST(AudioBufferTest, TrimRangeInterleaved) { | 450 TEST(AudioBufferTest, TrimRangeInterleaved) { |
| 427 TrimRangeTest(kSampleFormatF32); | 451 TrimRangeTest(kSampleFormatF32); |
| 428 } | 452 } |
| 429 | 453 |
| 430 } // namespace media | 454 } // namespace media |
| OLD | NEW |