andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [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 | |
| 11 | #include "output_mixer_internal.h" |
| 12 | |
| 13 | #include "audio_frame_operations.h" |
| 14 | #include "common_audio/resampler/include/resampler.h" |
| 15 | #include "module_common_types.h" |
| 16 | #include "trace.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace voe { |
| 20 | |
| 21 | int RemixAndResample(const AudioFrame& src_frame, |
| 22 | Resampler* resampler, |
| 23 | AudioFrame* dst_frame) { |
| 24 | const int16_t* audio_ptr = src_frame.data_; |
| 25 | int audio_ptr_num_channels = src_frame.num_channels_; |
| 26 | int16_t mono_audio[AudioFrame::kMaxDataSizeSamples]; |
| 27 | |
| 28 | // Downmix before resampling. |
| 29 | if (src_frame.num_channels_ == 2 && dst_frame->num_channels_ == 1) { |
| 30 | AudioFrameOperations::StereoToMono(src_frame.data_, |
| 31 | src_frame.samples_per_channel_, |
| 32 | mono_audio); |
| 33 | audio_ptr = mono_audio; |
| 34 | audio_ptr_num_channels = 1; |
| 35 | } |
| 36 | |
| 37 | const ResamplerType resampler_type = audio_ptr_num_channels == 1 ? |
| 38 | kResamplerSynchronous : kResamplerSynchronousStereo; |
| 39 | if (resampler->ResetIfNeeded(src_frame.sample_rate_hz_, |
| 40 | dst_frame->sample_rate_hz_, |
| 41 | resampler_type) == -1) { |
| 42 | *dst_frame = src_frame; |
| 43 | WEBRTC_TRACE(kTraceError, kTraceVoice, -1, |
| 44 | "%s ResetIfNeeded failed", __FUNCTION__); |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | int out_length = 0; |
| 49 | if (resampler->Push(audio_ptr, |
| 50 | src_frame.samples_per_channel_* audio_ptr_num_channels, |
| 51 | dst_frame->data_, |
| 52 | AudioFrame::kMaxDataSizeSamples, |
| 53 | out_length) == 0) { |
| 54 | dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels; |
| 55 | } else { |
| 56 | *dst_frame = src_frame; |
| 57 | WEBRTC_TRACE(kTraceError, kTraceVoice, -1, |
| 58 | "%s resampling failed", __FUNCTION__); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | // Upmix after resampling. |
| 63 | if (src_frame.num_channels_ == 1 && dst_frame->num_channels_ == 2) { |
| 64 | // The audio in dst_frame really is mono at this point; MonoToStereo will |
| 65 | // set this back to stereo. |
| 66 | dst_frame->num_channels_ = 1; |
| 67 | AudioFrameOperations::MonoToStereo(dst_frame); |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | } // namespace voe |
| 73 | } // namespace webrtc |