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 |
49 // Overlap-and-add window size in milliseconds. | 55 // Overlap-and-add window size in milliseconds. |
50 static const int kOlaWindowSizeMs = 20; | 56 static const int kOlaWindowSizeMs = 20; |
51 | 57 |
52 // Size of search interval in milliseconds. The search interval is | 58 // Size of search interval in milliseconds. The search interval is |
53 // [-delta delta] around |output_index_| * |playback_rate_|. So the search | 59 // [-delta delta] around |output_index_| * |playback_rate_|. So the search |
54 // interval is 2 * delta. | 60 // interval is 2 * delta. |
55 static const int kWsolaSearchIntervalMs = 30; | 61 static const int kWsolaSearchIntervalMs = 30; |
56 | 62 |
57 // The maximum size in seconds for the |audio_buffer_|. Arbitrarily determined. | 63 // The maximum size in seconds for the |audio_buffer_|. Arbitrarily determined. |
58 static const int kMaxCapacityInSeconds = 3; | 64 static const int kMaxCapacityInSeconds = 3; |
59 | 65 |
60 // The starting size in frames for |audio_buffer_|. Previous usage maintained a | 66 // The starting size in frames for |audio_buffer_|. Previous usage maintained a |
61 // queue of 16 AudioBuffers, each of 512 frames. This worked well, so we | 67 // queue of 16 AudioBuffers, each of 512 frames. This worked well, so we |
62 // maintain this number of frames. | 68 // maintain this number of frames. |
63 static const int kStartingBufferSizeInFrames = 16 * 512; | 69 static const int kStartingBufferSizeInFrames = 16 * 512; |
64 | 70 |
65 COMPILE_ASSERT(kStartingBufferSizeInFrames < | 71 COMPILE_ASSERT(kStartingBufferSizeInFrames < |
66 (kMaxCapacityInSeconds * limits::kMinSampleRate), | 72 (kMaxCapacityInSeconds * limits::kMinSampleRate), |
67 max_capacity_smaller_than_starting_buffer_size); | 73 max_capacity_smaller_than_starting_buffer_size); |
68 | 74 |
69 AudioRendererAlgorithm::AudioRendererAlgorithm() | 75 AudioRendererAlgorithm::AudioRendererAlgorithm() |
70 : channels_(0), | 76 : channels_(0), |
71 samples_per_second_(0), | 77 samples_per_second_(0), |
72 playback_rate_(0), | 78 playback_rate_(0), |
| 79 muted_(false), |
| 80 muted_partial_frame_(0), |
73 capacity_(kStartingBufferSizeInFrames), | 81 capacity_(kStartingBufferSizeInFrames), |
74 output_time_(0.0), | 82 output_time_(0.0), |
75 search_block_center_offset_(0), | 83 search_block_center_offset_(0), |
76 search_block_index_(0), | 84 search_block_index_(0), |
77 num_candidate_blocks_(0), | 85 num_candidate_blocks_(0), |
78 target_block_index_(0), | 86 target_block_index_(0), |
79 ola_window_size_(0), | 87 ola_window_size_(0), |
80 ola_hop_size_(0), | 88 ola_hop_size_(0), |
81 num_complete_frames_(0) { | 89 num_complete_frames_(0) { |
82 } | 90 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 channels_, num_candidate_blocks_ + (ola_window_size_ - 1)); | 144 channels_, num_candidate_blocks_ + (ola_window_size_ - 1)); |
137 target_block_ = AudioBus::Create(channels_, ola_window_size_); | 145 target_block_ = AudioBus::Create(channels_, ola_window_size_); |
138 } | 146 } |
139 | 147 |
140 int AudioRendererAlgorithm::FillBuffer(AudioBus* dest, int requested_frames) { | 148 int AudioRendererAlgorithm::FillBuffer(AudioBus* dest, int requested_frames) { |
141 if (playback_rate_ == 0) | 149 if (playback_rate_ == 0) |
142 return 0; | 150 return 0; |
143 | 151 |
144 DCHECK_EQ(channels_, dest->channels()); | 152 DCHECK_EQ(channels_, dest->channels()); |
145 | 153 |
| 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 |
146 int slower_step = ceil(ola_window_size_ * playback_rate_); | 179 int slower_step = ceil(ola_window_size_ * playback_rate_); |
147 int faster_step = ceil(ola_window_size_ / playback_rate_); | 180 int faster_step = ceil(ola_window_size_ / playback_rate_); |
148 | 181 |
149 // Optimize the most common |playback_rate_| ~= 1 case to use a single copy | 182 // Optimize the most common |playback_rate_| ~= 1 case to use a single copy |
150 // instead of copying frame by frame. | 183 // instead of copying frame by frame. |
151 if (ola_window_size_ <= faster_step && slower_step >= ola_window_size_) { | 184 if (ola_window_size_ <= faster_step && slower_step >= ola_window_size_) { |
152 const int frames_to_copy = | 185 const int frames_to_copy = |
153 std::min(audio_buffer_.frames(), requested_frames); | 186 std::min(audio_buffer_.frames(), requested_frames); |
154 const int frames_read = audio_buffer_.ReadFrames(frames_to_copy, 0, dest); | 187 const int frames_read = audio_buffer_.ReadFrames(frames_to_copy, 0, dest); |
155 DCHECK_EQ(frames_read, frames_to_copy); | 188 DCHECK_EQ(frames_read, frames_to_copy); |
156 return frames_read; | 189 return frames_read; |
157 } | 190 } |
158 | 191 |
159 int rendered_frames = 0; | 192 int rendered_frames = 0; |
160 do { | 193 do { |
161 rendered_frames += WriteCompletedFramesTo( | 194 rendered_frames += WriteCompletedFramesTo( |
162 requested_frames - rendered_frames, rendered_frames, dest); | 195 requested_frames - rendered_frames, rendered_frames, dest); |
163 } while (rendered_frames < requested_frames && RunOneWsolaIteration()); | 196 } while (rendered_frames < requested_frames && RunOneWsolaIteration()); |
164 return rendered_frames; | 197 return rendered_frames; |
165 } | 198 } |
166 | 199 |
167 void AudioRendererAlgorithm::SetPlaybackRate(float new_rate) { | 200 void AudioRendererAlgorithm::SetPlaybackRate(float new_rate) { |
168 DCHECK_GE(new_rate, 0); | 201 DCHECK_GE(new_rate, 0); |
169 playback_rate_ = new_rate; | 202 playback_rate_ = new_rate; |
| 203 muted_ = |
| 204 playback_rate_ < kMinPlaybackRate || playback_rate_ > kMaxPlaybackRate; |
170 } | 205 } |
171 | 206 |
172 void AudioRendererAlgorithm::FlushBuffers() { | 207 void AudioRendererAlgorithm::FlushBuffers() { |
173 // Clear the queue of decoded packets (releasing the buffers). | 208 // Clear the queue of decoded packets (releasing the buffers). |
174 audio_buffer_.Clear(); | 209 audio_buffer_.Clear(); |
175 output_time_ = 0.0; | 210 output_time_ = 0.0; |
176 search_block_index_ = 0; | 211 search_block_index_ = 0; |
177 target_block_index_ = 0; | 212 target_block_index_ = 0; |
178 wsola_output_->Zero(); | 213 wsola_output_->Zero(); |
179 num_complete_frames_ = 0; | 214 num_complete_frames_ = 0; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 read_offset_frames = 0; | 387 read_offset_frames = 0; |
353 num_frames_to_read -= num_zero_frames_appended; | 388 num_frames_to_read -= num_zero_frames_appended; |
354 write_offset = num_zero_frames_appended; | 389 write_offset = num_zero_frames_appended; |
355 dest->ZeroFrames(num_zero_frames_appended); | 390 dest->ZeroFrames(num_zero_frames_appended); |
356 } | 391 } |
357 audio_buffer_.PeekFrames(num_frames_to_read, read_offset_frames, | 392 audio_buffer_.PeekFrames(num_frames_to_read, read_offset_frames, |
358 write_offset, dest); | 393 write_offset, dest); |
359 } | 394 } |
360 | 395 |
361 } // namespace media | 396 } // namespace media |
OLD | NEW |