andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 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/base/checks.h" |
| 12 | #include "webrtc/common_audio/audio_converter.h" |
| 13 | #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | namespace { |
| 17 | |
| 18 | void DownmixToMono(const float* const* src, |
| 19 | int src_channels, |
| 20 | int frames, |
| 21 | float* dst) { |
| 22 | DCHECK_GT(src_channels, 0); |
| 23 | for (int i = 0; i < frames; ++i) { |
| 24 | float sum = 0; |
| 25 | for (int j = 0; j < src_channels; ++j) |
| 26 | sum += src[j][i]; |
| 27 | dst[i] = sum / src_channels; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void UpmixFromMono(const float* src, |
| 32 | int dst_channels, |
| 33 | int frames, |
| 34 | float* const* dst) { |
| 35 | DCHECK_GT(dst_channels, 0); |
| 36 | for (int i = 0; i < frames; ++i) { |
| 37 | float value = src[i]; |
| 38 | for (int j = 0; j < dst_channels; ++j) |
| 39 | dst[j][i] = value; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | AudioConverter::AudioConverter(int src_channels, int src_frames, |
| 46 | int dst_channels, int dst_frames) { |
| 47 | CHECK(dst_channels == src_channels || dst_channels == 1 || src_channels == 1); |
| 48 | const int resample_channels = src_channels < dst_channels ? src_channels : |
| 49 | dst_channels; |
| 50 | |
| 51 | // Prepare buffers as needed for intermediate stages. |
| 52 | if (dst_channels < src_channels) |
| 53 | downmix_buffer_.reset(new ChannelBuffer<float>(src_frames, |
| 54 | resample_channels)); |
| 55 | |
| 56 | if (src_frames != dst_frames) { |
| 57 | resamplers_.reserve(resample_channels); |
| 58 | for (int i = 0; i < resample_channels; ++i) |
| 59 | resamplers_.push_back(new PushSincResampler(src_frames, dst_frames)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void AudioConverter::Convert(const float* const* src, |
| 64 | int src_channels, |
| 65 | int src_frames, |
| 66 | int dst_channels, |
| 67 | int dst_frames, |
| 68 | float* const* dst) { |
| 69 | DCHECK(dst_channels == src_channels || dst_channels == 1 || |
| 70 | src_channels == 1); |
| 71 | if (src_channels == dst_channels && src_frames == dst_frames) { |
| 72 | // Shortcut copy. |
| 73 | if (src != dst) { |
| 74 | for (int i = 0; i < src_channels; ++i) |
| 75 | memcpy(dst[i], src[i], dst_frames * sizeof(*dst[i])); |
| 76 | } |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | const float* const* src_ptr = src; |
| 81 | if (dst_channels < src_channels) { |
| 82 | float* const* dst_ptr = dst; |
| 83 | if (src_frames != dst_frames) { |
| 84 | // Downmix to a buffer for subsequent resampling. |
| 85 | DCHECK_EQ(downmix_buffer_->num_channels(), dst_channels); |
| 86 | DCHECK_EQ(downmix_buffer_->samples_per_channel(), src_frames); |
| 87 | dst_ptr = downmix_buffer_->channels(); |
| 88 | } |
| 89 | |
| 90 | DownmixToMono(src, src_channels, src_frames, dst_ptr[0]); |
| 91 | src_ptr = dst_ptr; |
| 92 | } |
| 93 | |
| 94 | if (src_frames != dst_frames) { |
| 95 | for (size_t i = 0; i < resamplers_.size(); ++i) |
| 96 | resamplers_[i]->Resample(src_ptr[i], src_frames, dst[i], dst_frames); |
| 97 | src_ptr = dst; |
| 98 | } |
| 99 | |
| 100 | if (dst_channels > src_channels) |
| 101 | UpmixFromMono(src_ptr[0], dst_channels, dst_frames, dst); |
| 102 | } |
| 103 | |
| 104 | } // namespace webrtc |