blob: 3100271cfb6d3b6f278973096acc8dfbb67f08d7 [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"
Alex Loikob4977de2019-01-28 16:38:38 +010012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "audio/utility/audio_frame_operations.h"
henrikad0679bd2019-07-09 15:37:45 +020014#include "audio/utility/channel_mixer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
aleloi5bcc00e2016-08-15 03:01:31 -070016
17namespace webrtc {
aleloi5bcc00e2016-08-15 03:01:31 -070018
aleloi36542512016-10-07 05:28:32 -070019uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
yujo36b1a5f2017-06-12 12:45:32 -070020 if (audio_frame.muted()) {
21 return 0;
22 }
23
aleloi5bcc00e2016-08-15 03:01:31 -070024 uint32_t energy = 0;
yujo36b1a5f2017-06-12 12:45:32 -070025 const int16_t* frame_data = audio_frame.data();
Piasy Xudeee55b2018-06-11 19:39:50 +080026 for (size_t position = 0;
27 position < audio_frame.samples_per_channel_ * audio_frame.num_channels_;
aleloi5bcc00e2016-08-15 03:01:31 -070028 position++) {
aleloi36542512016-10-07 05:28:32 -070029 // TODO(aleloi): This can overflow. Convert to floats.
yujo36b1a5f2017-06-12 12:45:32 -070030 energy += frame_data[position] * frame_data[position];
aleloi5bcc00e2016-08-15 03:01:31 -070031 }
32 return energy;
33}
34
aleloi4b8bfb82016-10-12 02:14:59 -070035void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) {
36 RTC_DCHECK(audio_frame);
37 RTC_DCHECK_GE(start_gain, 0.0f);
38 RTC_DCHECK_GE(target_gain, 0.0f);
yujo36b1a5f2017-06-12 12:45:32 -070039 if (start_gain == target_gain || audio_frame->muted()) {
aleloi4637b6a2017-02-01 03:43:31 -080040 return;
41 }
aleloi5bcc00e2016-08-15 03:01:31 -070042
aleloi4b8bfb82016-10-12 02:14:59 -070043 size_t samples = audio_frame->samples_per_channel_;
kwibergaf476c72016-11-28 15:21:39 -080044 RTC_DCHECK_LT(0, samples);
aleloi4b8bfb82016-10-12 02:14:59 -070045 float increment = (target_gain - start_gain) / samples;
46 float gain = start_gain;
yujo36b1a5f2017-06-12 12:45:32 -070047 int16_t* frame_data = audio_frame->mutable_data();
aleloi4b8bfb82016-10-12 02:14:59 -070048 for (size_t i = 0; i < samples; ++i) {
49 // If the audio is interleaved of several channels, we want to
50 // apply the same gain change to the ith sample of every channel.
51 for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) {
yujo36b1a5f2017-06-12 12:45:32 -070052 frame_data[audio_frame->num_channels_ * i + ch] *= gain;
aleloi4b8bfb82016-10-12 02:14:59 -070053 }
54 gain += increment;
aleloi5bcc00e2016-08-15 03:01:31 -070055 }
aleloi5bcc00e2016-08-15 03:01:31 -070056}
aleloie8914152016-10-11 06:18:31 -070057
58void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
kwibergaf476c72016-11-28 15:21:39 -080059 RTC_DCHECK_GE(target_number_of_channels, 1);
henrikad0679bd2019-07-09 15:37:45 +020060 // TODO(bugs.webrtc.org/10783): take channel layout into account as well.
61 if (frame->num_channels() == target_number_of_channels) {
Alex Loikob4977de2019-01-28 16:38:38 +010062 return;
aleloie8914152016-10-11 06:18:31 -070063 }
henrikad0679bd2019-07-09 15:37:45 +020064
65 // Use legacy components for the most simple cases (mono <-> stereo) to ensure
66 // that native WebRTC clients are not affected when support for multi-channel
67 // audio is added to Chrome.
68 // TODO(bugs.webrtc.org/10783): utilize channel mixer for mono/stereo as well.
69 if (target_number_of_channels < 3 && frame->num_channels() < 3) {
70 if (frame->num_channels() > target_number_of_channels) {
71 AudioFrameOperations::DownmixChannels(target_number_of_channels, frame);
72 } else {
73 AudioFrameOperations::UpmixChannels(target_number_of_channels, frame);
74 }
75 } else {
76 // Use generic channel mixer when the number of channels for input our
77 // output is larger than two. E.g. stereo -> 5.1 channel up-mixing.
78 // TODO(bugs.webrtc.org/10783): ensure that actual channel layouts are used
79 // instead of guessing based on number of channels.
80 const ChannelLayout output_layout(
81 GuessChannelLayout(target_number_of_channels));
82 ChannelMixer mixer(GuessChannelLayout(frame->num_channels()),
83 output_layout);
84 mixer.Transform(frame);
85 RTC_DCHECK_EQ(frame->channel_layout(), output_layout);
Alex Loikob4977de2019-01-28 16:38:38 +010086 }
henrikad0679bd2019-07-09 15:37:45 +020087 RTC_DCHECK_EQ(frame->num_channels(), target_number_of_channels)
88 << "Wrong number of channels, " << frame->num_channels() << " vs "
Alex Loikob4977de2019-01-28 16:38:38 +010089 << target_number_of_channels;
aleloie8914152016-10-11 06:18:31 -070090}
henrikad0679bd2019-07-09 15:37:45 +020091
aleloi5bcc00e2016-08-15 03:01:31 -070092} // namespace webrtc