aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_mixer/audio_frame_manipulator.h" |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "audio/utility/audio_frame_operations.h" |
henrika | d0679bd | 2019-07-09 15:37:45 +0200 | [diff] [blame] | 14 | #include "audio/utility/channel_mixer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 18 | |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 19 | uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 20 | if (audio_frame.muted()) { |
| 21 | return 0; |
| 22 | } |
| 23 | |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 24 | uint32_t energy = 0; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 25 | const int16_t* frame_data = audio_frame.data(); |
Piasy Xu | deee55b | 2018-06-11 19:39:50 +0800 | [diff] [blame] | 26 | for (size_t position = 0; |
| 27 | position < audio_frame.samples_per_channel_ * audio_frame.num_channels_; |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 28 | position++) { |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 29 | // TODO(aleloi): This can overflow. Convert to floats. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 30 | energy += frame_data[position] * frame_data[position]; |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 31 | } |
| 32 | return energy; |
| 33 | } |
| 34 | |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 35 | void 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); |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 39 | if (start_gain == target_gain || audio_frame->muted()) { |
aleloi | 4637b6a | 2017-02-01 03:43:31 -0800 | [diff] [blame] | 40 | return; |
| 41 | } |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 42 | |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 43 | size_t samples = audio_frame->samples_per_channel_; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 44 | RTC_DCHECK_LT(0, samples); |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 45 | float increment = (target_gain - start_gain) / samples; |
| 46 | float gain = start_gain; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 47 | int16_t* frame_data = audio_frame->mutable_data(); |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 48 | 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) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 52 | frame_data[audio_frame->num_channels_ * i + ch] *= gain; |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 53 | } |
| 54 | gain += increment; |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 55 | } |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 56 | } |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 57 | |
| 58 | void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 59 | RTC_DCHECK_GE(target_number_of_channels, 1); |
henrika | d0679bd | 2019-07-09 15:37:45 +0200 | [diff] [blame] | 60 | // TODO(bugs.webrtc.org/10783): take channel layout into account as well. |
| 61 | if (frame->num_channels() == target_number_of_channels) { |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 62 | return; |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 63 | } |
henrika | d0679bd | 2019-07-09 15:37:45 +0200 | [diff] [blame] | 64 | |
| 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 Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 86 | } |
henrika | d0679bd | 2019-07-09 15:37:45 +0200 | [diff] [blame] | 87 | RTC_DCHECK_EQ(frame->num_channels(), target_number_of_channels) |
| 88 | << "Wrong number of channels, " << frame->num_channels() << " vs " |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 89 | << target_number_of_channels; |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 90 | } |
henrika | d0679bd | 2019-07-09 15:37:45 +0200 | [diff] [blame] | 91 | |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 92 | } // namespace webrtc |