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" |
| 12 | #include "audio/utility/audio_frame_operations.h" |
| 13 | #include "modules/include/module_common_types.h" |
| 14 | #include "rtc_base/checks.h" |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 17 | |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 18 | uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 19 | if (audio_frame.muted()) { |
| 20 | return 0; |
| 21 | } |
| 22 | |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 23 | uint32_t energy = 0; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 24 | const int16_t* frame_data = audio_frame.data(); |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 25 | for (size_t position = 0; position < audio_frame.samples_per_channel_; |
| 26 | position++) { |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 27 | // TODO(aleloi): This can overflow. Convert to floats. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 28 | energy += frame_data[position] * frame_data[position]; |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 29 | } |
| 30 | return energy; |
| 31 | } |
| 32 | |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 33 | void 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); |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 37 | if (start_gain == target_gain || audio_frame->muted()) { |
aleloi | 4637b6a | 2017-02-01 03:43:31 -0800 | [diff] [blame] | 38 | return; |
| 39 | } |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 40 | |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 41 | size_t samples = audio_frame->samples_per_channel_; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 42 | RTC_DCHECK_LT(0, samples); |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 43 | float increment = (target_gain - start_gain) / samples; |
| 44 | float gain = start_gain; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 45 | int16_t* frame_data = audio_frame->mutable_data(); |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 46 | 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) { |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 50 | frame_data[audio_frame->num_channels_ * i + ch] *= gain; |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 51 | } |
| 52 | gain += increment; |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 53 | } |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 54 | } |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 55 | |
| 56 | void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 57 | RTC_DCHECK_GE(target_number_of_channels, 1); |
| 58 | RTC_DCHECK_LE(target_number_of_channels, 2); |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 59 | 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 | } |
aleloi | 5bcc00e | 2016-08-15 03:01:31 -0700 | [diff] [blame] | 65 | } // namespace webrtc |