This source file includes following definitions.
- num_complete_frames_
- Initialize
- FillBuffer
- SetPlaybackRate
- FlushBuffers
- GetTime
- EnqueueBuffer
- IsQueueFull
- IncreaseQueueCapacity
- CanPerformWsola
- RunOneWsolaIteration
- UpdateOutputTime
- RemoveOldInputFrames
- WriteCompletedFramesTo
- TargetIsWithinSearchRegion
- GetOptimalBlock
- PeekAudioWithZeroPrepend
#include "media/filters/audio_renderer_algorithm.h"
#include <algorithm>
#include <cmath>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_bus.h"
#include "media/base/limits.h"
#include "media/filters/wsola_internals.h"
namespace media {
static const int kOlaWindowSizeMs = 20;
static const int kWsolaSearchIntervalMs = 30;
static const int kMaxCapacityInSeconds = 3;
static const int kStartingBufferSizeInFrames = 16 * 512;
COMPILE_ASSERT(kStartingBufferSizeInFrames <
(kMaxCapacityInSeconds * limits::kMinSampleRate),
max_capacity_smaller_than_starting_buffer_size);
AudioRendererAlgorithm::AudioRendererAlgorithm()
: channels_(0),
samples_per_second_(0),
playback_rate_(0),
capacity_(kStartingBufferSizeInFrames),
output_time_(0.0),
search_block_center_offset_(0),
search_block_index_(0),
num_candidate_blocks_(0),
target_block_index_(0),
ola_window_size_(0),
ola_hop_size_(0),
num_complete_frames_(0) {
}
AudioRendererAlgorithm::~AudioRendererAlgorithm() {}
void AudioRendererAlgorithm::Initialize(float initial_playback_rate,
const AudioParameters& params) {
CHECK(params.IsValid());
channels_ = params.channels();
samples_per_second_ = params.sample_rate();
SetPlaybackRate(initial_playback_rate);
num_candidate_blocks_ = (kWsolaSearchIntervalMs * samples_per_second_) / 1000;
ola_window_size_ = kOlaWindowSizeMs * samples_per_second_ / 1000;
ola_window_size_ += ola_window_size_ & 1;
ola_hop_size_ = ola_window_size_ / 2;
search_block_center_offset_ = num_candidate_blocks_ / 2 +
(ola_window_size_ / 2 - 1);
ola_window_.reset(new float[ola_window_size_]);
internal::GetSymmetricHanningWindow(ola_window_size_, ola_window_.get());
transition_window_.reset(new float[ola_window_size_ * 2]);
internal::GetSymmetricHanningWindow(2 * ola_window_size_,
transition_window_.get());
wsola_output_ = AudioBus::Create(channels_, ola_window_size_ + ola_hop_size_);
wsola_output_->Zero();
optimal_block_ = AudioBus::Create(channels_, ola_window_size_);
search_block_ = AudioBus::Create(
channels_, num_candidate_blocks_ + (ola_window_size_ - 1));
target_block_ = AudioBus::Create(channels_, ola_window_size_);
}
int AudioRendererAlgorithm::FillBuffer(AudioBus* dest, int requested_frames) {
if (playback_rate_ == 0)
return 0;
DCHECK_EQ(channels_, dest->channels());
int slower_step = ceil(ola_window_size_ * playback_rate_);
int faster_step = ceil(ola_window_size_ / playback_rate_);
if (ola_window_size_ <= faster_step && slower_step >= ola_window_size_) {
const int frames_to_copy =
std::min(audio_buffer_.frames(), requested_frames);
const int frames_read = audio_buffer_.ReadFrames(frames_to_copy, 0, dest);
DCHECK_EQ(frames_read, frames_to_copy);
return frames_read;
}
int rendered_frames = 0;
do {
rendered_frames += WriteCompletedFramesTo(
requested_frames - rendered_frames, rendered_frames, dest);
} while (rendered_frames < requested_frames && RunOneWsolaIteration());
return rendered_frames;
}
void AudioRendererAlgorithm::SetPlaybackRate(float new_rate) {
DCHECK_GE(new_rate, 0);
playback_rate_ = new_rate;
}
void AudioRendererAlgorithm::FlushBuffers() {
audio_buffer_.Clear();
output_time_ = 0.0;
search_block_index_ = 0;
target_block_index_ = 0;
wsola_output_->Zero();
num_complete_frames_ = 0;
capacity_ = kStartingBufferSizeInFrames;
}
base::TimeDelta AudioRendererAlgorithm::GetTime() {
return audio_buffer_.current_time();
}
void AudioRendererAlgorithm::EnqueueBuffer(
const scoped_refptr<AudioBuffer>& buffer_in) {
DCHECK(!buffer_in->end_of_stream());
audio_buffer_.Append(buffer_in);
}
bool AudioRendererAlgorithm::IsQueueFull() {
return audio_buffer_.frames() >= capacity_;
}
void AudioRendererAlgorithm::IncreaseQueueCapacity() {
int max_capacity = kMaxCapacityInSeconds * samples_per_second_;
DCHECK_LE(capacity_, max_capacity);
capacity_ = std::min(2 * capacity_, max_capacity);
}
bool AudioRendererAlgorithm::CanPerformWsola() const {
const int search_block_size = num_candidate_blocks_ + (ola_window_size_ - 1);
const int frames = audio_buffer_.frames();
return target_block_index_ + ola_window_size_ <= frames &&
search_block_index_ + search_block_size <= frames;
}
bool AudioRendererAlgorithm::RunOneWsolaIteration() {
if (!CanPerformWsola())
return false;
GetOptimalBlock();
for (int k = 0; k < channels_; ++k) {
const float* const ch_opt_frame = optimal_block_->channel(k);
float* ch_output = wsola_output_->channel(k) + num_complete_frames_;
for (int n = 0; n < ola_hop_size_; ++n) {
ch_output[n] = ch_output[n] * ola_window_[ola_hop_size_ + n] +
ch_opt_frame[n] * ola_window_[n];
}
memcpy(&ch_output[ola_hop_size_], &ch_opt_frame[ola_hop_size_],
sizeof(*ch_opt_frame) * ola_hop_size_);
}
num_complete_frames_ += ola_hop_size_;
UpdateOutputTime(ola_hop_size_);
RemoveOldInputFrames();
return true;
}
void AudioRendererAlgorithm::UpdateOutputTime(double time_change) {
output_time_ += time_change;
const int search_block_center_index = static_cast<int>(
output_time_ * playback_rate_ + 0.5);
search_block_index_ = search_block_center_index - search_block_center_offset_;
}
void AudioRendererAlgorithm::RemoveOldInputFrames() {
const int earliest_used_index = std::min(target_block_index_,
search_block_index_);
if (earliest_used_index <= 0)
return;
audio_buffer_.SeekFrames(earliest_used_index);
target_block_index_ -= earliest_used_index;
double output_time_change = static_cast<double>(earliest_used_index) /
playback_rate_;
CHECK_GE(output_time_, output_time_change);
UpdateOutputTime(-output_time_change);
}
int AudioRendererAlgorithm::WriteCompletedFramesTo(
int requested_frames, int dest_offset, AudioBus* dest) {
int rendered_frames = std::min(num_complete_frames_, requested_frames);
if (rendered_frames == 0)
return 0;
wsola_output_->CopyPartialFramesTo(0, rendered_frames, dest_offset, dest);
int frames_to_move = wsola_output_->frames() - rendered_frames;
for (int k = 0; k < channels_; ++k) {
float* ch = wsola_output_->channel(k);
memmove(ch, &ch[rendered_frames], sizeof(*ch) * frames_to_move);
}
num_complete_frames_ -= rendered_frames;
return rendered_frames;
}
bool AudioRendererAlgorithm::TargetIsWithinSearchRegion() const {
const int search_block_size = num_candidate_blocks_ + (ola_window_size_ - 1);
return target_block_index_ >= search_block_index_ &&
target_block_index_ + ola_window_size_ <=
search_block_index_ + search_block_size;
}
void AudioRendererAlgorithm::GetOptimalBlock() {
int optimal_index = 0;
const int kExcludeIntervalLengthFrames = 160;
if (TargetIsWithinSearchRegion()) {
optimal_index = target_block_index_;
PeekAudioWithZeroPrepend(optimal_index, optimal_block_.get());
} else {
PeekAudioWithZeroPrepend(target_block_index_, target_block_.get());
PeekAudioWithZeroPrepend(search_block_index_, search_block_.get());
int last_optimal = target_block_index_ - ola_hop_size_ -
search_block_index_;
internal::Interval exclude_iterval = std::make_pair(
last_optimal - kExcludeIntervalLengthFrames / 2,
last_optimal + kExcludeIntervalLengthFrames / 2);
optimal_index = internal::OptimalIndex(
search_block_.get(), target_block_.get(), exclude_iterval);
optimal_index += search_block_index_;
PeekAudioWithZeroPrepend(optimal_index, optimal_block_.get());
for (int k = 0; k < channels_; ++k) {
float* ch_opt = optimal_block_->channel(k);
const float* const ch_target = target_block_->channel(k);
for (int n = 0; n < ola_window_size_; ++n) {
ch_opt[n] = ch_opt[n] * transition_window_[n] + ch_target[n] *
transition_window_[ola_window_size_ + n];
}
}
}
target_block_index_ = optimal_index + ola_hop_size_;
}
void AudioRendererAlgorithm::PeekAudioWithZeroPrepend(
int read_offset_frames, AudioBus* dest) {
CHECK_LE(read_offset_frames + dest->frames(), audio_buffer_.frames());
int write_offset = 0;
int num_frames_to_read = dest->frames();
if (read_offset_frames < 0) {
int num_zero_frames_appended = std::min(-read_offset_frames,
num_frames_to_read);
read_offset_frames = 0;
num_frames_to_read -= num_zero_frames_appended;
write_offset = num_zero_frames_appended;
dest->ZeroFrames(num_zero_frames_appended);
}
audio_buffer_.PeekFrames(num_frames_to_read, read_offset_frames,
write_offset, dest);
}
}