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/filters/audio_renderer_algorithm.h" | 5 #include "media/filters/audio_renderer_algorithm.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // | 39 // |
40 // 5) Overlap-and-add |optimal_block_| to the |wsola_output_|. | 40 // 5) Overlap-and-add |optimal_block_| to the |wsola_output_|. |
41 // | 41 // |
42 // 6) Update: | 42 // 6) Update: |
43 // |target_block_| = |optimal_index| + |ola_window_size_| / 2. | 43 // |target_block_| = |optimal_index| + |ola_window_size_| / 2. |
44 // |output_index_| = |output_index_| + |ola_window_size_| / 2, | 44 // |output_index_| = |output_index_| + |ola_window_size_| / 2, |
45 // |search_block_center_offset_| = |output_index_| * |playback_rate_|, and | 45 // |search_block_center_offset_| = |output_index_| * |playback_rate_|, and |
46 // |search_block_index_| = |search_block_center_offset_| - | 46 // |search_block_index_| = |search_block_center_offset_| - |
47 // |search_block_center_offset_|. | 47 // |search_block_center_offset_|. |
48 | 48 |
49 // Max/min supported playback rates for fast/slow audio. Audio outside of these | |
50 // ranges are muted. | |
51 // Audio at these speeds would sound better under a frequency domain algorithm. | |
52 static const float kMinPlaybackRate = 0.5f; | |
53 static const float kMaxPlaybackRate = 4.0f; | |
54 | |
55 // Overlap-and-add window size in milliseconds. | 49 // Overlap-and-add window size in milliseconds. |
56 static const int kOlaWindowSizeMs = 20; | 50 static const int kOlaWindowSizeMs = 20; |
57 | 51 |
58 // Size of search interval in milliseconds. The search interval is | 52 // Size of search interval in milliseconds. The search interval is |
59 // [-delta delta] around |output_index_| * |playback_rate_|. So the search | 53 // [-delta delta] around |output_index_| * |playback_rate_|. So the search |
60 // interval is 2 * delta. | 54 // interval is 2 * delta. |
61 static const int kWsolaSearchIntervalMs = 30; | 55 static const int kWsolaSearchIntervalMs = 30; |
62 | 56 |
63 // The maximum size in seconds for the |audio_buffer_|. Arbitrarily determined. | 57 // The maximum size in seconds for the |audio_buffer_|. Arbitrarily determined. |
64 static const int kMaxCapacityInSeconds = 3; | 58 static const int kMaxCapacityInSeconds = 3; |
65 | 59 |
66 // The starting size in frames for |audio_buffer_|. Previous usage maintained a | 60 // The starting size in frames for |audio_buffer_|. Previous usage maintained a |
67 // queue of 16 AudioBuffers, each of 512 frames. This worked well, so we | 61 // queue of 16 AudioBuffers, each of 512 frames. This worked well, so we |
68 // maintain this number of frames. | 62 // maintain this number of frames. |
69 static const int kStartingBufferSizeInFrames = 16 * 512; | 63 static const int kStartingBufferSizeInFrames = 16 * 512; |
70 | 64 |
71 COMPILE_ASSERT(kStartingBufferSizeInFrames < | 65 COMPILE_ASSERT(kStartingBufferSizeInFrames < |
72 (kMaxCapacityInSeconds * limits::kMinSampleRate), | 66 (kMaxCapacityInSeconds * limits::kMinSampleRate), |
73 max_capacity_smaller_than_starting_buffer_size); | 67 max_capacity_smaller_than_starting_buffer_size); |
74 | 68 |
75 AudioRendererAlgorithm::AudioRendererAlgorithm() | 69 AudioRendererAlgorithm::AudioRendererAlgorithm() |
76 : channels_(0), | 70 : channels_(0), |
77 samples_per_second_(0), | 71 samples_per_second_(0), |
78 playback_rate_(0), | 72 playback_rate_(0), |
79 muted_(false), | |
80 muted_partial_frame_(0), | |
81 capacity_(kStartingBufferSizeInFrames), | 73 capacity_(kStartingBufferSizeInFrames), |
82 output_time_(0.0), | 74 output_time_(0.0), |
83 search_block_center_offset_(0), | 75 search_block_center_offset_(0), |
84 search_block_index_(0), | 76 search_block_index_(0), |
85 num_candidate_blocks_(0), | 77 num_candidate_blocks_(0), |
86 target_block_index_(0), | 78 target_block_index_(0), |
87 ola_window_size_(0), | 79 ola_window_size_(0), |
88 ola_hop_size_(0), | 80 ola_hop_size_(0), |
89 num_complete_frames_(0) { | 81 num_complete_frames_(0) { |
90 } | 82 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 channels_, num_candidate_blocks_ + (ola_window_size_ - 1)); | 136 channels_, num_candidate_blocks_ + (ola_window_size_ - 1)); |
145 target_block_ = AudioBus::Create(channels_, ola_window_size_); | 137 target_block_ = AudioBus::Create(channels_, ola_window_size_); |
146 } | 138 } |
147 | 139 |
148 int AudioRendererAlgorithm::FillBuffer(AudioBus* dest, int requested_frames) { | 140 int AudioRendererAlgorithm::FillBuffer(AudioBus* dest, int requested_frames) { |
149 if (playback_rate_ == 0) | 141 if (playback_rate_ == 0) |
150 return 0; | 142 return 0; |
151 | 143 |
152 DCHECK_EQ(channels_, dest->channels()); | 144 DCHECK_EQ(channels_, dest->channels()); |
153 | 145 |
154 // Optimize the |muted_| case to issue a single clear instead of performing | |
155 // the full crossfade and clearing each crossfaded frame. | |
156 if (muted_) { | |
157 int frames_to_render = | |
158 std::min(static_cast<int>(audio_buffer_.frames() / playback_rate_), | |
159 requested_frames); | |
160 | |
161 // Compute accurate number of frames to actually skip in the source data. | |
162 // Includes the leftover partial frame from last request. However, we can | |
163 // only skip over complete frames, so a partial frame may remain for next | |
164 // time. | |
165 muted_partial_frame_ += frames_to_render * playback_rate_; | |
166 int seek_frames = static_cast<int>(muted_partial_frame_); | |
167 dest->ZeroFrames(frames_to_render); | |
168 audio_buffer_.SeekFrames(seek_frames); | |
169 | |
170 // Determine the partial frame that remains to be skipped for next call. If | |
171 // the user switches back to playing, it may be off time by this partial | |
172 // frame, which would be undetectable. If they subsequently switch to | |
173 // another playback rate that mutes, the code will attempt to line up the | |
174 // frames again. | |
175 muted_partial_frame_ -= seek_frames; | |
176 return frames_to_render; | |
177 } | |
178 | |
179 int slower_step = ceil(ola_window_size_ * playback_rate_); | 146 int slower_step = ceil(ola_window_size_ * playback_rate_); |
180 int faster_step = ceil(ola_window_size_ / playback_rate_); | 147 int faster_step = ceil(ola_window_size_ / playback_rate_); |
181 | 148 |
182 // Optimize the most common |playback_rate_| ~= 1 case to use a single copy | 149 // Optimize the most common |playback_rate_| ~= 1 case to use a single copy |
183 // instead of copying frame by frame. | 150 // instead of copying frame by frame. |
184 if (ola_window_size_ <= faster_step && slower_step >= ola_window_size_) { | 151 if (ola_window_size_ <= faster_step && slower_step >= ola_window_size_) { |
185 const int frames_to_copy = | 152 const int frames_to_copy = |
186 std::min(audio_buffer_.frames(), requested_frames); | 153 std::min(audio_buffer_.frames(), requested_frames); |
187 const int frames_read = audio_buffer_.ReadFrames(frames_to_copy, 0, dest); | 154 const int frames_read = audio_buffer_.ReadFrames(frames_to_copy, 0, dest); |
188 DCHECK_EQ(frames_read, frames_to_copy); | 155 DCHECK_EQ(frames_read, frames_to_copy); |
189 return frames_read; | 156 return frames_read; |
190 } | 157 } |
191 | 158 |
192 int rendered_frames = 0; | 159 int rendered_frames = 0; |
193 do { | 160 do { |
194 rendered_frames += WriteCompletedFramesTo( | 161 rendered_frames += WriteCompletedFramesTo( |
195 requested_frames - rendered_frames, rendered_frames, dest); | 162 requested_frames - rendered_frames, rendered_frames, dest); |
196 } while (rendered_frames < requested_frames && RunOneWsolaIteration()); | 163 } while (rendered_frames < requested_frames && RunOneWsolaIteration()); |
197 return rendered_frames; | 164 return rendered_frames; |
198 } | 165 } |
199 | 166 |
200 void AudioRendererAlgorithm::SetPlaybackRate(float new_rate) { | 167 void AudioRendererAlgorithm::SetPlaybackRate(float new_rate) { |
201 DCHECK_GE(new_rate, 0); | 168 DCHECK_GE(new_rate, 0); |
202 playback_rate_ = new_rate; | 169 playback_rate_ = new_rate; |
203 muted_ = | |
204 playback_rate_ < kMinPlaybackRate || playback_rate_ > kMaxPlaybackRate; | |
205 } | 170 } |
206 | 171 |
207 void AudioRendererAlgorithm::FlushBuffers() { | 172 void AudioRendererAlgorithm::FlushBuffers() { |
208 // Clear the queue of decoded packets (releasing the buffers). | 173 // Clear the queue of decoded packets (releasing the buffers). |
209 audio_buffer_.Clear(); | 174 audio_buffer_.Clear(); |
210 output_time_ = 0.0; | 175 output_time_ = 0.0; |
211 search_block_index_ = 0; | 176 search_block_index_ = 0; |
212 target_block_index_ = 0; | 177 target_block_index_ = 0; |
213 wsola_output_->Zero(); | 178 wsola_output_->Zero(); |
214 num_complete_frames_ = 0; | 179 num_complete_frames_ = 0; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 read_offset_frames = 0; | 352 read_offset_frames = 0; |
388 num_frames_to_read -= num_zero_frames_appended; | 353 num_frames_to_read -= num_zero_frames_appended; |
389 write_offset = num_zero_frames_appended; | 354 write_offset = num_zero_frames_appended; |
390 dest->ZeroFrames(num_zero_frames_appended); | 355 dest->ZeroFrames(num_zero_frames_appended); |
391 } | 356 } |
392 audio_buffer_.PeekFrames(num_frames_to_read, read_offset_frames, | 357 audio_buffer_.PeekFrames(num_frames_to_read, read_offset_frames, |
393 write_offset, dest); | 358 write_offset, dest); |
394 } | 359 } |
395 | 360 |
396 } // namespace media | 361 } // namespace media |
OLD | NEW |