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