(Or, in less flattering terms, fixing a performance issue introduced a few months ago by me). In GN release mode (is_debug = false), the version of the mixer code before this CL generated code that multiplied each sample (tens of thousands/second for each input stream) with a floating point number. This number is almost always exactly 1.0f. The only situation when it's not 1 is when an audio steam is added or removed. For one input stream early return leads to a 30% improvement of audio mixing time profiled on x86-64 under a release build (is_debug = false, enable_profiling, enable_full_stack_frames_for_profiling) with 16kHz and no APM limiter. There can be up to 3 streams. BUG=chromium:687502 Review-Url: https://codereview.webrtc.org/2659423002 Cr-Commit-Position: refs/heads/master@{#16396}
60 lines
2.1 KiB
C++
60 lines
2.1 KiB
C++
/*
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "webrtc/audio/utility/audio_frame_operations.h"
|
|
#include "webrtc/base/checks.h"
|
|
#include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
|
|
#include "webrtc/modules/include/module_common_types.h"
|
|
|
|
namespace webrtc {
|
|
|
|
uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
|
|
uint32_t energy = 0;
|
|
for (size_t position = 0; position < audio_frame.samples_per_channel_;
|
|
position++) {
|
|
// TODO(aleloi): This can overflow. Convert to floats.
|
|
energy += audio_frame.data_[position] * audio_frame.data_[position];
|
|
}
|
|
return energy;
|
|
}
|
|
|
|
void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) {
|
|
RTC_DCHECK(audio_frame);
|
|
RTC_DCHECK_GE(start_gain, 0.0f);
|
|
RTC_DCHECK_GE(target_gain, 0.0f);
|
|
if (start_gain == target_gain) {
|
|
return;
|
|
}
|
|
|
|
size_t samples = audio_frame->samples_per_channel_;
|
|
RTC_DCHECK_LT(0, samples);
|
|
float increment = (target_gain - start_gain) / samples;
|
|
float gain = start_gain;
|
|
for (size_t i = 0; i < samples; ++i) {
|
|
// If the audio is interleaved of several channels, we want to
|
|
// apply the same gain change to the ith sample of every channel.
|
|
for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) {
|
|
audio_frame->data_[audio_frame->num_channels_ * i + ch] *= gain;
|
|
}
|
|
gain += increment;
|
|
}
|
|
}
|
|
|
|
void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
|
|
RTC_DCHECK_GE(target_number_of_channels, 1);
|
|
RTC_DCHECK_LE(target_number_of_channels, 2);
|
|
if (frame->num_channels_ == 1 && target_number_of_channels == 2) {
|
|
AudioFrameOperations::MonoToStereo(frame);
|
|
} else if (frame->num_channels_ == 2 && target_number_of_channels == 1) {
|
|
AudioFrameOperations::StereoToMono(frame);
|
|
}
|
|
}
|
|
} // namespace webrtc
|