2016-08-15 03:01:31 -07:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_mixer/audio_frame_manipulator.h"
|
|
|
|
|
#include "audio/utility/audio_frame_operations.h"
|
|
|
|
|
#include "modules/include/module_common_types.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
2016-08-15 03:01:31 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-10-07 05:28:32 -07:00
|
|
|
uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
|
2017-06-12 12:45:32 -07:00
|
|
|
if (audio_frame.muted()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-15 03:01:31 -07:00
|
|
|
uint32_t energy = 0;
|
2017-06-12 12:45:32 -07:00
|
|
|
const int16_t* frame_data = audio_frame.data();
|
2016-08-15 03:01:31 -07:00
|
|
|
for (size_t position = 0; position < audio_frame.samples_per_channel_;
|
|
|
|
|
position++) {
|
2016-10-07 05:28:32 -07:00
|
|
|
// TODO(aleloi): This can overflow. Convert to floats.
|
2017-06-12 12:45:32 -07:00
|
|
|
energy += frame_data[position] * frame_data[position];
|
2016-08-15 03:01:31 -07:00
|
|
|
}
|
|
|
|
|
return energy;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-12 02:14:59 -07:00
|
|
|
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);
|
2017-06-12 12:45:32 -07:00
|
|
|
if (start_gain == target_gain || audio_frame->muted()) {
|
2017-02-01 03:43:31 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-15 03:01:31 -07:00
|
|
|
|
2016-10-12 02:14:59 -07:00
|
|
|
size_t samples = audio_frame->samples_per_channel_;
|
2016-11-28 15:21:39 -08:00
|
|
|
RTC_DCHECK_LT(0, samples);
|
2016-10-12 02:14:59 -07:00
|
|
|
float increment = (target_gain - start_gain) / samples;
|
|
|
|
|
float gain = start_gain;
|
2017-06-12 12:45:32 -07:00
|
|
|
int16_t* frame_data = audio_frame->mutable_data();
|
2016-10-12 02:14:59 -07:00
|
|
|
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) {
|
2017-06-12 12:45:32 -07:00
|
|
|
frame_data[audio_frame->num_channels_ * i + ch] *= gain;
|
2016-10-12 02:14:59 -07:00
|
|
|
}
|
|
|
|
|
gain += increment;
|
2016-08-15 03:01:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-10-11 06:18:31 -07:00
|
|
|
|
|
|
|
|
void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
|
2016-11-28 15:21:39 -08:00
|
|
|
RTC_DCHECK_GE(target_number_of_channels, 1);
|
|
|
|
|
RTC_DCHECK_LE(target_number_of_channels, 2);
|
2016-10-11 06:18:31 -07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-15 03:01:31 -07:00
|
|
|
} // namespace webrtc
|