blob: f394762c5294bc7fe4c7110cadea16271ab5c18e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
braveyao@webrtc.orgd7131432012-03-29 10:39:442 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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.org956aa7e2013-05-21 13:52:3211#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:2512
aleloi6321b492016-12-05 09:46:0913#include "webrtc/audio/utility/audio_frame_operations.h"
Tommi54e1c6a2016-05-26 20:03:0514#include "webrtc/base/checks.h"
pbosad856222015-11-27 17:48:3615#include "webrtc/base/logging.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:0116#include "webrtc/common_audio/resampler/include/push_resampler.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:3217#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:0118#include "webrtc/common_types.h"
Henrik Kjellanderff761fb2015-11-04 07:31:5219#include "webrtc/modules/include/module_common_types.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:0120#include "webrtc/voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:2521
andrew@webrtc.org40ee3d02014-04-03 21:56:0122namespace webrtc {
23namespace voe {
niklase@google.com470e71d2011-07-07 08:21:2524
andrew@webrtc.org40ee3d02014-04-03 21:56:0125void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:0726 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:0127 AudioFrame* dst_frame) {
Alejandro Luebscdfe20b2015-09-23 19:49:1228 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
36void RemixAndResample(const int16_t* src_data,
37 size_t samples_per_channel,
Peter Kasting69558702016-01-13 00:26:3538 size_t num_channels,
Alejandro Luebscdfe20b2015-09-23 19:49:1239 int sample_rate_hz,
40 PushResampler<int16_t>* resampler,
41 AudioFrame* dst_frame) {
42 const int16_t* audio_ptr = src_data;
Peter Kasting69558702016-01-13 00:26:3543 size_t audio_ptr_num_channels = num_channels;
henrik.lundinfb4f8b62017-03-02 12:10:5744 int16_t downsmixed_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:2545
andrew@webrtc.org40ee3d02014-04-03 21:56:0146 // Downmix before resampling.
jens.nielsen228c2682017-03-01 13:11:2247 if (num_channels > dst_frame->num_channels_) {
48 RTC_DCHECK(num_channels == 2 || num_channels == 4)
49 << "num_channels: " << num_channels;
50 RTC_DCHECK(dst_frame->num_channels_ == 1 || dst_frame->num_channels_ == 2)
51 << "dst_frame->num_channels_: " << dst_frame->num_channels_;
52
53 AudioFrameOperations::DownmixChannels(
54 src_data, num_channels, samples_per_channel, dst_frame->num_channels_,
henrik.lundinfb4f8b62017-03-02 12:10:5755 downsmixed_audio);
56 audio_ptr = downsmixed_audio;
jens.nielsen228c2682017-03-01 13:11:2257 audio_ptr_num_channels = dst_frame->num_channels_;
andrew@webrtc.org40ee3d02014-04-03 21:56:0158 }
braveyao@webrtc.orgd7131432012-03-29 10:39:4459
Alejandro Luebscdfe20b2015-09-23 19:49:1260 if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
andrew@webrtc.org40ee3d02014-04-03 21:56:0161 audio_ptr_num_channels) == -1) {
Tommi54e1c6a2016-05-26 20:03:0562 FATAL() << "InitializeIfNeeded failed: sample_rate_hz = " << sample_rate_hz
63 << ", dst_frame->sample_rate_hz_ = " << dst_frame->sample_rate_hz_
64 << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:0165 }
66
Alejandro Luebscdfe20b2015-09-23 19:49:1267 const size_t src_length = samples_per_channel * audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:0168 int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_,
69 AudioFrame::kMaxDataSizeSamples);
70 if (out_length == -1) {
Tommi54e1c6a2016-05-26 20:03:0571 FATAL() << "Resample failed: audio_ptr = " << audio_ptr
72 << ", src_length = " << src_length
73 << ", dst_frame->data_ = " << dst_frame->data_;
andrew@webrtc.org40ee3d02014-04-03 21:56:0174 }
Peter Kasting69558702016-01-13 00:26:3575 dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:0176
77 // Upmix after resampling.
Alejandro Luebscdfe20b2015-09-23 19:49:1278 if (num_channels == 1 && dst_frame->num_channels_ == 2) {
andrew@webrtc.org40ee3d02014-04-03 21:56:0179 // The audio in dst_frame really is mono at this point; MonoToStereo will
80 // set this back to stereo.
81 dst_frame->num_channels_ = 1;
82 AudioFrameOperations::MonoToStereo(dst_frame);
83 }
niklase@google.com470e71d2011-07-07 08:21:2584}
85
andrew@webrtc.org40ee3d02014-04-03 21:56:0186void MixWithSat(int16_t target[],
Peter Kasting69558702016-01-13 00:26:3587 size_t target_channel,
andrew@webrtc.org40ee3d02014-04-03 21:56:0188 const int16_t source[],
Peter Kasting69558702016-01-13 00:26:3589 size_t source_channel,
Peter Kastingdce40cf2015-08-24 21:52:2390 size_t source_len) {
kwibergaf476c72016-11-28 23:21:3991 RTC_DCHECK_GE(target_channel, 1);
92 RTC_DCHECK_LE(target_channel, 2);
93 RTC_DCHECK_GE(source_channel, 1);
94 RTC_DCHECK_LE(source_channel, 2);
niklase@google.com470e71d2011-07-07 08:21:2595
andrew@webrtc.org40ee3d02014-04-03 21:56:0196 if (target_channel == 2 && source_channel == 1) {
97 // Convert source from mono to stereo.
98 int32_t left = 0;
99 int32_t right = 0;
Peter Kastingdce40cf2015-08-24 21:52:23100 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01101 left = source[i] + target[i * 2];
102 right = source[i] + target[i * 2 + 1];
103 target[i * 2] = WebRtcSpl_SatW32ToW16(left);
104 target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right);
niklase@google.com470e71d2011-07-07 08:21:25105 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01106 } else if (target_channel == 1 && source_channel == 2) {
107 // Convert source from stereo to mono.
108 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 21:52:23109 for (size_t i = 0; i < source_len / 2; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01110 temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
111 target[i] = WebRtcSpl_SatW32ToW16(temp);
niklase@google.com470e71d2011-07-07 08:21:25112 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01113 } else {
114 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 21:52:23115 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01116 temp = source[i] + target[i];
117 target[i] = WebRtcSpl_SatW32ToW16(temp);
118 }
119 }
niklase@google.com470e71d2011-07-07 08:21:25120}
121
pbos@webrtc.orgd900e8b2013-07-03 15:12:26122} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26123} // namespace webrtc