OLD | NEW |
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 "media/base/audio_splicer.h" |
| 6 |
| 7 #include <memory> |
| 8 |
5 #include "base/macros.h" | 9 #include "base/macros.h" |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "media/base/audio_buffer.h" | 10 #include "media/base/audio_buffer.h" |
8 #include "media/base/audio_bus.h" | 11 #include "media/base/audio_bus.h" |
9 #include "media/base/audio_splicer.h" | |
10 #include "media/base/audio_timestamp_helper.h" | 12 #include "media/base/audio_timestamp_helper.h" |
11 #include "media/base/test_helpers.h" | 13 #include "media/base/test_helpers.h" |
12 #include "media/base/timestamp_constants.h" | 14 #include "media/base/timestamp_constants.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 namespace media { | 17 namespace media { |
16 | 18 |
17 // Do not change this format. AddInput() and GetValue() only work with float. | 19 // Do not change this format. AddInput() and GetValue() only work with float. |
18 static const SampleFormat kSampleFormat = kSampleFormatF32; | 20 static const SampleFormat kSampleFormat = kSampleFormatF32; |
19 static_assert(kSampleFormat == kSampleFormatF32, "invalid splice format"); | 21 static_assert(kSampleFormat == kSampleFormatF32, "invalid splice format"); |
(...skipping 28 matching lines...) Expand all Loading... |
48 input_timestamp_helper_.AddFrames(frame_size); | 50 input_timestamp_helper_.AddFrames(frame_size); |
49 return buffer; | 51 return buffer; |
50 } | 52 } |
51 | 53 |
52 float GetValue(const scoped_refptr<AudioBuffer>& buffer) { | 54 float GetValue(const scoped_refptr<AudioBuffer>& buffer) { |
53 return reinterpret_cast<const float*>(buffer->channel_data()[0])[0]; | 55 return reinterpret_cast<const float*>(buffer->channel_data()[0])[0]; |
54 } | 56 } |
55 | 57 |
56 bool VerifyData(const scoped_refptr<AudioBuffer>& buffer, float value) { | 58 bool VerifyData(const scoped_refptr<AudioBuffer>& buffer, float value) { |
57 int frames = buffer->frame_count(); | 59 int frames = buffer->frame_count(); |
58 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames); | 60 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames); |
59 buffer->ReadFrames(frames, 0, 0, bus.get()); | 61 buffer->ReadFrames(frames, 0, 0, bus.get()); |
60 for (int ch = 0; ch < buffer->channel_count(); ++ch) { | 62 for (int ch = 0; ch < buffer->channel_count(); ++ch) { |
61 for (int i = 0; i < frames; ++i) { | 63 for (int i = 0; i < frames; ++i) { |
62 if (bus->channel(ch)[i] != value) | 64 if (bus->channel(ch)[i] != value) |
63 return false; | 65 return false; |
64 } | 66 } |
65 } | 67 } |
66 return true; | 68 return true; |
67 } | 69 } |
68 | 70 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 105 |
104 // The splice timestamp may be adjusted by a microsecond. | 106 // The splice timestamp may be adjusted by a microsecond. |
105 EXPECT_NEAR(overlapping_buffer->timestamp().InMicroseconds(), | 107 EXPECT_NEAR(overlapping_buffer->timestamp().InMicroseconds(), |
106 crossfade_output->timestamp().InMicroseconds(), | 108 crossfade_output->timestamp().InMicroseconds(), |
107 1); | 109 1); |
108 | 110 |
109 // Verify the actual crossfade. | 111 // Verify the actual crossfade. |
110 const int frames = crossfade_output->frame_count(); | 112 const int frames = crossfade_output->frame_count(); |
111 float overlapped_value = GetValue(overlapped_buffer_1); | 113 float overlapped_value = GetValue(overlapped_buffer_1); |
112 const float overlapping_value = GetValue(overlapping_buffer); | 114 const float overlapping_value = GetValue(overlapping_buffer); |
113 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames); | 115 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames); |
114 crossfade_output->ReadFrames(frames, 0, 0, bus.get()); | 116 crossfade_output->ReadFrames(frames, 0, 0, bus.get()); |
115 for (int ch = 0; ch < crossfade_output->channel_count(); ++ch) { | 117 for (int ch = 0; ch < crossfade_output->channel_count(); ++ch) { |
116 float cf_ratio = 0; | 118 float cf_ratio = 0; |
117 const float cf_increment = 1.0f / frames; | 119 const float cf_increment = 1.0f / frames; |
118 for (int i = 0; i < frames; ++i, cf_ratio += cf_increment) { | 120 for (int i = 0; i < frames; ++i, cf_ratio += cf_increment) { |
119 if (overlapped_buffer_2.get() && i >= second_overlap_index) | 121 if (overlapped_buffer_2.get() && i >= second_overlap_index) |
120 overlapped_value = GetValue(overlapped_buffer_2); | 122 overlapped_value = GetValue(overlapped_buffer_2); |
121 const float actual = bus->channel(ch)[i]; | 123 const float actual = bus->channel(ch)[i]; |
122 const float expected = | 124 const float expected = |
123 (1.0f - cf_ratio) * overlapped_value + cf_ratio * overlapping_value; | 125 (1.0f - cf_ratio) * overlapped_value + cf_ratio * overlapping_value; |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 second_buffer->timestamp() + second_buffer->duration(); | 761 second_buffer->timestamp() + second_buffer->duration(); |
760 scoped_refptr<AudioBuffer> output = splicer_.GetNextBuffer(); | 762 scoped_refptr<AudioBuffer> output = splicer_.GetNextBuffer(); |
761 EXPECT_EQ(second_buffer_end_ts, output->timestamp()); | 763 EXPECT_EQ(second_buffer_end_ts, output->timestamp()); |
762 EXPECT_EQ(third_buffer->duration() - | 764 EXPECT_EQ(third_buffer->duration() - |
763 (second_buffer_end_ts - third_buffer->timestamp()), | 765 (second_buffer_end_ts - third_buffer->timestamp()), |
764 output->duration()); | 766 output->duration()); |
765 EXPECT_TRUE(VerifyData(output, GetValue(third_buffer))); | 767 EXPECT_TRUE(VerifyData(output, GetValue(third_buffer))); |
766 } | 768 } |
767 | 769 |
768 } // namespace media | 770 } // namespace media |
OLD | NEW |