niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/utility.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 13 | #include "webrtc/common_audio/resampler/include/push_resampler.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 14 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 15 | #include "webrtc/common_types.h" |
| 16 | #include "webrtc/modules/interface/module_common_types.h" |
| 17 | #include "webrtc/modules/utility/interface/audio_frame_operations.h" |
| 18 | #include "webrtc/system_wrappers/interface/logging.h" |
| 19 | #include "webrtc/voice_engine/voice_engine_defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 24 | // TODO(ajm): There is significant overlap between RemixAndResample and |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame^] | 25 | // ConvertToCodecFormat. Consolidate using AudioConverter. |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 26 | void RemixAndResample(const AudioFrame& src_frame, |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 27 | PushResampler<int16_t>* resampler, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 28 | AudioFrame* dst_frame) { |
| 29 | const int16_t* audio_ptr = src_frame.data_; |
| 30 | int audio_ptr_num_channels = src_frame.num_channels_; |
| 31 | int16_t mono_audio[AudioFrame::kMaxDataSizeSamples]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 33 | // Downmix before resampling. |
| 34 | if (src_frame.num_channels_ == 2 && dst_frame->num_channels_ == 1) { |
| 35 | AudioFrameOperations::StereoToMono(src_frame.data_, |
| 36 | src_frame.samples_per_channel_, |
| 37 | mono_audio); |
| 38 | audio_ptr = mono_audio; |
| 39 | audio_ptr_num_channels = 1; |
| 40 | } |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 +0000 | [diff] [blame] | 41 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 42 | if (resampler->InitializeIfNeeded(src_frame.sample_rate_hz_, |
| 43 | dst_frame->sample_rate_hz_, |
| 44 | audio_ptr_num_channels) == -1) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 45 | LOG_FERR3(LS_ERROR, InitializeIfNeeded, src_frame.sample_rate_hz_, |
| 46 | dst_frame->sample_rate_hz_, audio_ptr_num_channels); |
| 47 | assert(false); |
| 48 | } |
| 49 | |
| 50 | const int src_length = src_frame.samples_per_channel_ * |
| 51 | audio_ptr_num_channels; |
| 52 | int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_, |
| 53 | AudioFrame::kMaxDataSizeSamples); |
| 54 | if (out_length == -1) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 55 | LOG_FERR3(LS_ERROR, Resample, audio_ptr, src_length, dst_frame->data_); |
| 56 | assert(false); |
| 57 | } |
| 58 | dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels; |
| 59 | |
| 60 | // Upmix after resampling. |
| 61 | if (src_frame.num_channels_ == 1 && dst_frame->num_channels_ == 2) { |
| 62 | // The audio in dst_frame really is mono at this point; MonoToStereo will |
| 63 | // set this back to stereo. |
| 64 | dst_frame->num_channels_ = 1; |
| 65 | AudioFrameOperations::MonoToStereo(dst_frame); |
| 66 | } |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 67 | |
| 68 | dst_frame->timestamp_ = src_frame.timestamp_; |
| 69 | dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_; |
| 70 | dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 73 | void DownConvertToCodecFormat(const int16_t* src_data, |
| 74 | int samples_per_channel, |
| 75 | int num_channels, |
| 76 | int sample_rate_hz, |
| 77 | int codec_num_channels, |
| 78 | int codec_rate_hz, |
| 79 | int16_t* mono_buffer, |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 80 | PushResampler<int16_t>* resampler, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 81 | AudioFrame* dst_af) { |
| 82 | assert(samples_per_channel <= kMaxMonoDataSizeSamples); |
| 83 | assert(num_channels == 1 || num_channels == 2); |
| 84 | assert(codec_num_channels == 1 || codec_num_channels == 2); |
andrew@webrtc.org | 1fddd61 | 2014-05-30 17:28:50 +0000 | [diff] [blame] | 85 | dst_af->Reset(); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 86 | |
| 87 | // Never upsample the capture signal here. This should be done at the |
| 88 | // end of the send chain. |
| 89 | int destination_rate = std::min(codec_rate_hz, sample_rate_hz); |
| 90 | |
| 91 | // If no stereo codecs are in use, we downmix a stereo stream from the |
| 92 | // device early in the chain, before resampling. |
| 93 | if (num_channels == 2 && codec_num_channels == 1) { |
| 94 | AudioFrameOperations::StereoToMono(src_data, samples_per_channel, |
| 95 | mono_buffer); |
| 96 | src_data = mono_buffer; |
| 97 | num_channels = 1; |
| 98 | } |
| 99 | |
| 100 | if (resampler->InitializeIfNeeded( |
| 101 | sample_rate_hz, destination_rate, num_channels) != 0) { |
| 102 | LOG_FERR3(LS_ERROR, |
| 103 | InitializeIfNeeded, |
| 104 | sample_rate_hz, |
| 105 | destination_rate, |
| 106 | num_channels); |
| 107 | assert(false); |
| 108 | } |
| 109 | |
| 110 | const int in_length = samples_per_channel * num_channels; |
| 111 | int out_length = resampler->Resample( |
| 112 | src_data, in_length, dst_af->data_, AudioFrame::kMaxDataSizeSamples); |
| 113 | if (out_length == -1) { |
| 114 | LOG_FERR3(LS_ERROR, Resample, src_data, in_length, dst_af->data_); |
| 115 | assert(false); |
| 116 | } |
| 117 | |
| 118 | dst_af->samples_per_channel_ = out_length / num_channels; |
| 119 | dst_af->sample_rate_hz_ = destination_rate; |
| 120 | dst_af->num_channels_ = num_channels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 123 | void MixWithSat(int16_t target[], |
| 124 | int target_channel, |
| 125 | const int16_t source[], |
| 126 | int source_channel, |
| 127 | int source_len) { |
| 128 | assert(target_channel == 1 || target_channel == 2); |
| 129 | assert(source_channel == 1 || source_channel == 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 131 | if (target_channel == 2 && source_channel == 1) { |
| 132 | // Convert source from mono to stereo. |
| 133 | int32_t left = 0; |
| 134 | int32_t right = 0; |
| 135 | for (int i = 0; i < source_len; ++i) { |
| 136 | left = source[i] + target[i * 2]; |
| 137 | right = source[i] + target[i * 2 + 1]; |
| 138 | target[i * 2] = WebRtcSpl_SatW32ToW16(left); |
| 139 | target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 141 | } else if (target_channel == 1 && source_channel == 2) { |
| 142 | // Convert source from stereo to mono. |
| 143 | int32_t temp = 0; |
| 144 | for (int i = 0; i < source_len / 2; ++i) { |
| 145 | temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i]; |
| 146 | target[i] = WebRtcSpl_SatW32ToW16(temp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 148 | } else { |
| 149 | int32_t temp = 0; |
| 150 | for (int i = 0; i < source_len; ++i) { |
| 151 | temp = source[i] + target[i]; |
| 152 | target[i] = WebRtcSpl_SatW32ToW16(temp); |
| 153 | } |
| 154 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 157 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 158 | } // namespace webrtc |