Line data Source code
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 :
11 : #include "webrtc/audio/utility/audio_frame_operations.h"
12 : #include "webrtc/base/checks.h"
13 : #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
14 : #include "webrtc/modules/include/module_common_types.h"
15 :
16 : namespace webrtc {
17 :
18 0 : uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
19 0 : uint32_t energy = 0;
20 0 : for (size_t position = 0; position < audio_frame.samples_per_channel_;
21 : position++) {
22 : // TODO(aleloi): This can overflow. Convert to floats.
23 0 : energy += audio_frame.data_[position] * audio_frame.data_[position];
24 : }
25 0 : return energy;
26 : }
27 :
28 0 : void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) {
29 0 : RTC_DCHECK(audio_frame);
30 0 : RTC_DCHECK_GE(start_gain, 0.0f);
31 0 : RTC_DCHECK_GE(target_gain, 0.0f);
32 0 : if (start_gain == target_gain) {
33 0 : return;
34 : }
35 :
36 0 : size_t samples = audio_frame->samples_per_channel_;
37 0 : RTC_DCHECK_LT(0, samples);
38 0 : float increment = (target_gain - start_gain) / samples;
39 0 : float gain = start_gain;
40 0 : for (size_t i = 0; i < samples; ++i) {
41 : // If the audio is interleaved of several channels, we want to
42 : // apply the same gain change to the ith sample of every channel.
43 0 : for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) {
44 0 : audio_frame->data_[audio_frame->num_channels_ * i + ch] *= gain;
45 : }
46 0 : gain += increment;
47 : }
48 : }
49 :
50 0 : void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
51 0 : RTC_DCHECK_GE(target_number_of_channels, 1);
52 0 : RTC_DCHECK_LE(target_number_of_channels, 2);
53 0 : if (frame->num_channels_ == 1 && target_number_of_channels == 2) {
54 0 : AudioFrameOperations::MonoToStereo(frame);
55 0 : } else if (frame->num_channels_ == 2 && target_number_of_channels == 1) {
56 0 : AudioFrameOperations::StereoToMono(frame);
57 : }
58 0 : }
59 : } // namespace webrtc
|