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