blob: 82ef076d41b01d70af7a60f55d4d22d2c2baaf41 [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
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000013#include "webrtc/common_audio/resampler/include/push_resampler.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000014#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000015#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.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 +000024// TODO(ajm): There is significant overlap between RemixAndResample and
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000025// ConvertToCodecFormat. Consolidate using AudioConverter.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000026void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000027 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000028 AudioFrame* dst_frame) {
29 const int16_t* audio_ptr = src_frame.data_;
30 int audio_ptr_num_channels = src_frame.num_channels_;
31 int16_t mono_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:25 +000032
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000033 // Downmix before resampling.
34 if (src_frame.num_channels_ == 2 && dst_frame->num_channels_ == 1) {
35 AudioFrameOperations::StereoToMono(src_frame.data_,
36 src_frame.samples_per_channel_,
37 mono_audio);
38 audio_ptr = mono_audio;
39 audio_ptr_num_channels = 1;
40 }
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000041
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000042 if (resampler->InitializeIfNeeded(src_frame.sample_rate_hz_,
43 dst_frame->sample_rate_hz_,
44 audio_ptr_num_channels) == -1) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000045 LOG_FERR3(LS_ERROR, InitializeIfNeeded, src_frame.sample_rate_hz_,
46 dst_frame->sample_rate_hz_, audio_ptr_num_channels);
47 assert(false);
48 }
49
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 const size_t src_length = src_frame.samples_per_channel_ *
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000051 audio_ptr_num_channels;
52 int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_,
53 AudioFrame::kMaxDataSizeSamples);
54 if (out_length == -1) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000055 LOG_FERR3(LS_ERROR, Resample, audio_ptr, src_length, dst_frame->data_);
56 assert(false);
57 }
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 dst_frame->samples_per_channel_ =
59 static_cast<size_t>(out_length / audio_ptr_num_channels);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000060
61 // Upmix after resampling.
62 if (src_frame.num_channels_ == 1 && dst_frame->num_channels_ == 2) {
63 // The audio in dst_frame really is mono at this point; MonoToStereo will
64 // set this back to stereo.
65 dst_frame->num_channels_ = 1;
66 AudioFrameOperations::MonoToStereo(dst_frame);
67 }
wu@webrtc.org94454b72014-06-05 20:34:08 +000068
69 dst_frame->timestamp_ = src_frame.timestamp_;
70 dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
71 dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000074void DownConvertToCodecFormat(const int16_t* src_data,
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 size_t samples_per_channel,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000076 int num_channels,
77 int sample_rate_hz,
78 int codec_num_channels,
79 int codec_rate_hz,
80 int16_t* mono_buffer,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000081 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000082 AudioFrame* dst_af) {
83 assert(samples_per_channel <= kMaxMonoDataSizeSamples);
84 assert(num_channels == 1 || num_channels == 2);
85 assert(codec_num_channels == 1 || codec_num_channels == 2);
andrew@webrtc.org1fddd612014-05-30 17:28:50 +000086 dst_af->Reset();
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000087
88 // Never upsample the capture signal here. This should be done at the
89 // end of the send chain.
90 int destination_rate = std::min(codec_rate_hz, sample_rate_hz);
91
92 // If no stereo codecs are in use, we downmix a stereo stream from the
93 // device early in the chain, before resampling.
94 if (num_channels == 2 && codec_num_channels == 1) {
95 AudioFrameOperations::StereoToMono(src_data, samples_per_channel,
96 mono_buffer);
97 src_data = mono_buffer;
98 num_channels = 1;
99 }
100
101 if (resampler->InitializeIfNeeded(
102 sample_rate_hz, destination_rate, num_channels) != 0) {
103 LOG_FERR3(LS_ERROR,
104 InitializeIfNeeded,
105 sample_rate_hz,
106 destination_rate,
107 num_channels);
108 assert(false);
109 }
110
Peter Kastingdce40cf2015-08-24 14:52:23 -0700111 const size_t in_length = samples_per_channel * num_channels;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000112 int out_length = resampler->Resample(
113 src_data, in_length, dst_af->data_, AudioFrame::kMaxDataSizeSamples);
114 if (out_length == -1) {
115 LOG_FERR3(LS_ERROR, Resample, src_data, in_length, dst_af->data_);
116 assert(false);
117 }
118
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 dst_af->samples_per_channel_ = static_cast<size_t>(out_length / num_channels);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000120 dst_af->sample_rate_hz_ = destination_rate;
121 dst_af->num_channels_ = num_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122}
123
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000124void MixWithSat(int16_t target[],
125 int target_channel,
126 const int16_t source[],
127 int source_channel,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700128 size_t source_len) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000129 assert(target_channel == 1 || target_channel == 2);
130 assert(source_channel == 1 || source_channel == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000132 if (target_channel == 2 && source_channel == 1) {
133 // Convert source from mono to stereo.
134 int32_t left = 0;
135 int32_t right = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000137 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.com470e71d2011-07-07 08:21:25 +0000141 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000142 } else if (target_channel == 1 && source_channel == 2) {
143 // Convert source from stereo to mono.
144 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 for (size_t i = 0; i < source_len / 2; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000146 temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
147 target[i] = WebRtcSpl_SatW32ToW16(temp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000149 } else {
150 int32_t temp = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0; i < source_len; ++i) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000152 temp = source[i] + target[i];
153 target[i] = WebRtcSpl_SatW32ToW16(temp);
154 }
155 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000156}
157
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000158} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000159} // namespace webrtc