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 | void RemixAndResample(const AudioFrame& src_frame, |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 25 | PushResampler<int16_t>* resampler, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 26 | AudioFrame* dst_frame) { |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame^] | 27 | RemixAndResample(src_frame.data_, src_frame.samples_per_channel_, |
| 28 | src_frame.num_channels_, src_frame.sample_rate_hz_, |
| 29 | resampler, dst_frame); |
| 30 | dst_frame->timestamp_ = src_frame.timestamp_; |
| 31 | dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_; |
| 32 | dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_; |
| 33 | } |
| 34 | |
| 35 | void RemixAndResample(const int16_t* src_data, |
| 36 | size_t samples_per_channel, |
| 37 | int num_channels, |
| 38 | int sample_rate_hz, |
| 39 | PushResampler<int16_t>* resampler, |
| 40 | AudioFrame* dst_frame) { |
| 41 | const int16_t* audio_ptr = src_data; |
| 42 | int audio_ptr_num_channels = num_channels; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 43 | int16_t mono_audio[AudioFrame::kMaxDataSizeSamples]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 45 | // Downmix before resampling. |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame^] | 46 | if (num_channels == 2 && dst_frame->num_channels_ == 1) { |
| 47 | AudioFrameOperations::StereoToMono(src_data, samples_per_channel, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 48 | mono_audio); |
| 49 | audio_ptr = mono_audio; |
| 50 | audio_ptr_num_channels = 1; |
| 51 | } |
braveyao@webrtc.org | d713143 | 2012-03-29 10:39:44 +0000 | [diff] [blame] | 52 | |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame^] | 53 | if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 54 | audio_ptr_num_channels) == -1) { |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame^] | 55 | LOG_FERR3(LS_ERROR, InitializeIfNeeded, sample_rate_hz, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 56 | dst_frame->sample_rate_hz_, audio_ptr_num_channels); |
| 57 | assert(false); |
| 58 | } |
| 59 | |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame^] | 60 | const size_t src_length = samples_per_channel * audio_ptr_num_channels; |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 61 | int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_, |
| 62 | AudioFrame::kMaxDataSizeSamples); |
| 63 | if (out_length == -1) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 64 | LOG_FERR3(LS_ERROR, Resample, audio_ptr, src_length, dst_frame->data_); |
| 65 | assert(false); |
| 66 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 67 | dst_frame->samples_per_channel_ = |
| 68 | static_cast<size_t>(out_length / audio_ptr_num_channels); |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 69 | |
| 70 | // Upmix after resampling. |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame^] | 71 | if (num_channels == 1 && dst_frame->num_channels_ == 2) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 72 | // The audio in dst_frame really is mono at this point; MonoToStereo will |
| 73 | // set this back to stereo. |
| 74 | dst_frame->num_channels_ = 1; |
| 75 | AudioFrameOperations::MonoToStereo(dst_frame); |
| 76 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | } |
| 78 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 79 | void MixWithSat(int16_t target[], |
| 80 | int target_channel, |
| 81 | const int16_t source[], |
| 82 | int source_channel, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 83 | size_t source_len) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 84 | assert(target_channel == 1 || target_channel == 2); |
| 85 | assert(source_channel == 1 || source_channel == 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 87 | if (target_channel == 2 && source_channel == 1) { |
| 88 | // Convert source from mono to stereo. |
| 89 | int32_t left = 0; |
| 90 | int32_t right = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 91 | for (size_t i = 0; i < source_len; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 92 | left = source[i] + target[i * 2]; |
| 93 | right = source[i] + target[i * 2 + 1]; |
| 94 | target[i * 2] = WebRtcSpl_SatW32ToW16(left); |
| 95 | target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 97 | } else if (target_channel == 1 && source_channel == 2) { |
| 98 | // Convert source from stereo to mono. |
| 99 | int32_t temp = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 100 | for (size_t i = 0; i < source_len / 2; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 101 | temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i]; |
| 102 | target[i] = WebRtcSpl_SatW32ToW16(temp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 104 | } else { |
| 105 | int32_t temp = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 106 | for (size_t i = 0; i < source_len; ++i) { |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 107 | temp = source[i] + target[i]; |
| 108 | target[i] = WebRtcSpl_SatW32ToW16(temp); |
| 109 | } |
| 110 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | } |
| 112 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 113 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 114 | } // namespace webrtc |