Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / third_party / libwebrtc / modules / audio_mixer / audio_frame_manipulator.cc
blob3100271cfb6d3b6f278973096acc8dfbb67f08d7
1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 */
11 #include "modules/audio_mixer/audio_frame_manipulator.h"
13 #include "audio/utility/audio_frame_operations.h"
14 #include "audio/utility/channel_mixer.h"
15 #include "rtc_base/checks.h"
17 namespace webrtc {
19 uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
20 if (audio_frame.muted()) {
21 return 0;
24 uint32_t energy = 0;
25 const int16_t* frame_data = audio_frame.data();
26 for (size_t position = 0;
27 position < audio_frame.samples_per_channel_ * audio_frame.num_channels_;
28 position++) {
29 // TODO(aleloi): This can overflow. Convert to floats.
30 energy += frame_data[position] * frame_data[position];
32 return energy;
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);
39 if (start_gain == target_gain || audio_frame->muted()) {
40 return;
43 size_t samples = audio_frame->samples_per_channel_;
44 RTC_DCHECK_LT(0, samples);
45 float increment = (target_gain - start_gain) / samples;
46 float gain = start_gain;
47 int16_t* frame_data = audio_frame->mutable_data();
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) {
52 frame_data[audio_frame->num_channels_ * i + ch] *= gain;
54 gain += increment;
58 void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
59 RTC_DCHECK_GE(target_number_of_channels, 1);
60 // TODO(bugs.webrtc.org/10783): take channel layout into account as well.
61 if (frame->num_channels() == target_number_of_channels) {
62 return;
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);
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);
87 RTC_DCHECK_EQ(frame->num_channels(), target_number_of_channels)
88 << "Wrong number of channels, " << frame->num_channels() << " vs "
89 << target_number_of_channels;
92 } // namespace webrtc