blob: 0f5a83fed7184b30d482bf14778bec4948907e42 [file] [log] [blame]
aleloi5bcc00e2016-08-15 03:01:31 -07001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_mixer/audio_frame_manipulator.h"
12#include "audio/utility/audio_frame_operations.h"
13#include "modules/include/module_common_types.h"
14#include "rtc_base/checks.h"
aleloi5bcc00e2016-08-15 03:01:31 -070015
16namespace webrtc {
aleloi5bcc00e2016-08-15 03:01:31 -070017
aleloi36542512016-10-07 05:28:32 -070018uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
yujo36b1a5f2017-06-12 12:45:32 -070019 if (audio_frame.muted()) {
20 return 0;
21 }
22
aleloi5bcc00e2016-08-15 03:01:31 -070023 uint32_t energy = 0;
yujo36b1a5f2017-06-12 12:45:32 -070024 const int16_t* frame_data = audio_frame.data();
aleloi5bcc00e2016-08-15 03:01:31 -070025 for (size_t position = 0; position < audio_frame.samples_per_channel_;
26 position++) {
aleloi36542512016-10-07 05:28:32 -070027 // TODO(aleloi): This can overflow. Convert to floats.
yujo36b1a5f2017-06-12 12:45:32 -070028 energy += frame_data[position] * frame_data[position];
aleloi5bcc00e2016-08-15 03:01:31 -070029 }
30 return energy;
31}
32
aleloi4b8bfb82016-10-12 02:14:59 -070033void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) {
34 RTC_DCHECK(audio_frame);
35 RTC_DCHECK_GE(start_gain, 0.0f);
36 RTC_DCHECK_GE(target_gain, 0.0f);
yujo36b1a5f2017-06-12 12:45:32 -070037 if (start_gain == target_gain || audio_frame->muted()) {
aleloi4637b6a2017-02-01 03:43:31 -080038 return;
39 }
aleloi5bcc00e2016-08-15 03:01:31 -070040
aleloi4b8bfb82016-10-12 02:14:59 -070041 size_t samples = audio_frame->samples_per_channel_;
kwibergaf476c72016-11-28 15:21:39 -080042 RTC_DCHECK_LT(0, samples);
aleloi4b8bfb82016-10-12 02:14:59 -070043 float increment = (target_gain - start_gain) / samples;
44 float gain = start_gain;
yujo36b1a5f2017-06-12 12:45:32 -070045 int16_t* frame_data = audio_frame->mutable_data();
aleloi4b8bfb82016-10-12 02:14:59 -070046 for (size_t i = 0; i < samples; ++i) {
47 // If the audio is interleaved of several channels, we want to
48 // apply the same gain change to the ith sample of every channel.
49 for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) {
yujo36b1a5f2017-06-12 12:45:32 -070050 frame_data[audio_frame->num_channels_ * i + ch] *= gain;
aleloi4b8bfb82016-10-12 02:14:59 -070051 }
52 gain += increment;
aleloi5bcc00e2016-08-15 03:01:31 -070053 }
aleloi5bcc00e2016-08-15 03:01:31 -070054}
aleloie8914152016-10-11 06:18:31 -070055
56void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
kwibergaf476c72016-11-28 15:21:39 -080057 RTC_DCHECK_GE(target_number_of_channels, 1);
58 RTC_DCHECK_LE(target_number_of_channels, 2);
aleloie8914152016-10-11 06:18:31 -070059 if (frame->num_channels_ == 1 && target_number_of_channels == 2) {
60 AudioFrameOperations::MonoToStereo(frame);
61 } else if (frame->num_channels_ == 2 && target_number_of_channels == 1) {
62 AudioFrameOperations::StereoToMono(frame);
63 }
64}
aleloi5bcc00e2016-08-15 03:01:31 -070065} // namespace webrtc