webrtc_m130/modules/audio_mixer/audio_frame_manipulator.cc
Alex Loiko b4977de306 Receive-side ready for multiple channels.
Made path from NetEq to AudioTransport ready for many-channel audio.
If there is one stream, we can handle anything that fits in an
AudioFrame. For many streams, the current limit is 6.

Some multi-channel combinations are not supported: e.g. if we get
stereo audio and attempt to play out 6 channels.

Changes:
* AudioFrameOperations - replaced the MonoTo* and *ToMono methods by
  UpmixChannels & DownmixChannels.
* AudioMixer: removed DCHECKs for <= 2 channels and tweaked the mixing
  algorithm to handle many channels.

Bug: webrtc:8649
Change-Id: Ib83e16d463694e35658caa09c27849e853d508fb
Reviewed-on: https://webrtc-review.googlesource.com/c/106040
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Commit-Queue: Alex Loiko <aleloi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26446}
2019-01-29 12:43:23 +00:00

72 lines
2.5 KiB
C++

/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_mixer/audio_frame_manipulator.h"
#include "audio/utility/audio_frame_operations.h"
#include "rtc_base/checks.h"
namespace webrtc {
uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) {
if (audio_frame.muted()) {
return 0;
}
uint32_t energy = 0;
const int16_t* frame_data = audio_frame.data();
for (size_t position = 0;
position < audio_frame.samples_per_channel_ * audio_frame.num_channels_;
position++) {
// TODO(aleloi): This can overflow. Convert to floats.
energy += frame_data[position] * frame_data[position];
}
return energy;
}
void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) {
RTC_DCHECK(audio_frame);
RTC_DCHECK_GE(start_gain, 0.0f);
RTC_DCHECK_GE(target_gain, 0.0f);
if (start_gain == target_gain || audio_frame->muted()) {
return;
}
size_t samples = audio_frame->samples_per_channel_;
RTC_DCHECK_LT(0, samples);
float increment = (target_gain - start_gain) / samples;
float gain = start_gain;
int16_t* frame_data = audio_frame->mutable_data();
for (size_t i = 0; i < samples; ++i) {
// If the audio is interleaved of several channels, we want to
// apply the same gain change to the ith sample of every channel.
for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) {
frame_data[audio_frame->num_channels_ * i + ch] *= gain;
}
gain += increment;
}
}
void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) {
RTC_DCHECK_GE(target_number_of_channels, 1);
if (frame->num_channels_ == target_number_of_channels) {
return;
}
if (frame->num_channels_ > target_number_of_channels) {
AudioFrameOperations::DownmixChannels(target_number_of_channels, frame);
} else if (frame->num_channels_ < target_number_of_channels) {
AudioFrameOperations::UpmixChannels(target_number_of_channels, frame);
}
RTC_DCHECK_EQ(frame->num_channels_, target_number_of_channels)
<< "Wrong number of channels, " << frame->num_channels_ << " vs "
<< target_number_of_channels;
}
} // namespace webrtc