blob: 605e55369e3d4a31666e17bf69d3b04ccd054630 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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:32 +000011#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbosad856222015-11-27 09:48:36 -080013#include "webrtc/base/logging.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000014#include "webrtc/common_audio/resampler/include/push_resampler.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000016#include "webrtc/common_types.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/include/module_common_types.h"
18#include "webrtc/modules/utility/include/audio_frame_operations.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000019#include "webrtc/voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000021namespace webrtc {
22namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000024void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000025 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000026 AudioFrame* dst_frame) {
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070027 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
35void RemixAndResample(const int16_t* src_data,
36 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080037 size_t num_channels,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070038 int sample_rate_hz,
39 PushResampler<int16_t>* resampler,
40 AudioFrame* dst_frame) {
41 const int16_t* audio_ptr = src_data;
Peter Kasting69558702016-01-12 16:26:35 -080042 size_t audio_ptr_num_channels = num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000043 int16_t mono_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000045 // Downmix before resampling.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070046 if (num_channels == 2 && dst_frame->num_channels_ == 1) {
47 AudioFrameOperations::StereoToMono(src_data, samples_per_channel,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000048 mono_audio);
49 audio_ptr = mono_audio;
50 audio_ptr_num_channels = 1;
51 }
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000052
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070053 if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000054 audio_ptr_num_channels) == -1) {
tommib4ff7a72016-05-26 08:27:35 -070055 LOG(LS_ERROR) << "InitializeIfNeeded failed: sample_rate_hz = "
56 << sample_rate_hz << ", dst_frame->sample_rate_hz_ = "
57 << dst_frame->sample_rate_hz_
58 << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
59 assert(false);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000060 }
61
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070062 const size_t src_length = samples_per_channel * audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000063 int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_,
64 AudioFrame::kMaxDataSizeSamples);
65 if (out_length == -1) {
tommib4ff7a72016-05-26 08:27:35 -070066 LOG(LS_ERROR) << "Resample failed: audio_ptr = " << audio_ptr
67 << ", src_length = " << src_length
68 << ", dst_frame->data_ = " << dst_frame->data_;
69 assert(false);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000070 }
Peter Kasting69558702016-01-12 16:26:35 -080071 dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000072
73 // Upmix after resampling.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070074 if (num_channels == 1 && dst_frame->num_channels_ == 2) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000075 // The audio in dst_frame really is mono at this point; MonoToStereo will
76 // set this back to stereo.
77 dst_frame->num_channels_ = 1;
78 AudioFrameOperations::MonoToStereo(dst_frame);
79 }
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000082void MixWithSat(int16_t target[],
Peter Kasting69558702016-01-12 16:26:35 -080083 size_t target_channel,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000084 const int16_t source[],
Peter Kasting69558702016-01-12 16:26:35 -080085 size_t source_channel,
Peter Kastingdce40cf2015-08-24 14:52:23 -070086 size_t source_len) {
tommib4ff7a72016-05-26 08:27:35 -070087 assert(target_channel == 1 || target_channel == 2);
88 assert(source_channel == 1 || source_channel == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000090 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 Kastingdce40cf2015-08-24 14:52:23 -070094 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000095 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.com470e71d2011-07-07 08:21:25 +000099 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000100 } else if (target_channel == 1 && source_channel == 2) {
101 // Convert source from stereo to mono.
102 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700103 for (size_t i = 0; i < source_len / 2; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000104 temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
105 target[i] = WebRtcSpl_SatW32ToW16(temp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000107 } else {
108 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700109 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000110 temp = source[i] + target[i];
111 target[i] = WebRtcSpl_SatW32ToW16(temp);
112 }
113 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000114}
115
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000116} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000117} // namespace webrtc