blob: 04f1f2c1ff5640e668498aae929b8919f3a66ad7 [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
25// ConvertToCodecFormat, but if we're to consolidate we should probably make a
26// real converter class.
27void RemixAndResample(const AudioFrame& src_frame,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000028 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000029 AudioFrame* dst_frame) {
30 const int16_t* audio_ptr = src_frame.data_;
31 int audio_ptr_num_channels = src_frame.num_channels_;
32 int16_t mono_audio[AudioFrame::kMaxDataSizeSamples];
niklase@google.com470e71d2011-07-07 08:21:25 +000033
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000034 // Downmix before resampling.
35 if (src_frame.num_channels_ == 2 && dst_frame->num_channels_ == 1) {
36 AudioFrameOperations::StereoToMono(src_frame.data_,
37 src_frame.samples_per_channel_,
38 mono_audio);
39 audio_ptr = mono_audio;
40 audio_ptr_num_channels = 1;
41 }
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000042
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000043 if (resampler->InitializeIfNeeded(src_frame.sample_rate_hz_,
44 dst_frame->sample_rate_hz_,
45 audio_ptr_num_channels) == -1) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000046 LOG_FERR3(LS_ERROR, InitializeIfNeeded, src_frame.sample_rate_hz_,
47 dst_frame->sample_rate_hz_, audio_ptr_num_channels);
48 assert(false);
49 }
50
51 const int src_length = src_frame.samples_per_channel_ *
52 audio_ptr_num_channels;
53 int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_,
54 AudioFrame::kMaxDataSizeSamples);
55 if (out_length == -1) {
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000056 LOG_FERR3(LS_ERROR, Resample, audio_ptr, src_length, dst_frame->data_);
57 assert(false);
58 }
59 dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
60
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 }
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000070void DownConvertToCodecFormat(const int16_t* src_data,
71 int samples_per_channel,
72 int num_channels,
73 int sample_rate_hz,
74 int codec_num_channels,
75 int codec_rate_hz,
76 int16_t* mono_buffer,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000077 PushResampler<int16_t>* resampler,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000078 AudioFrame* dst_af) {
79 assert(samples_per_channel <= kMaxMonoDataSizeSamples);
80 assert(num_channels == 1 || num_channels == 2);
81 assert(codec_num_channels == 1 || codec_num_channels == 2);
andrew@webrtc.org1fddd612014-05-30 17:28:50 +000082 dst_af->Reset();
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000083
84 // Never upsample the capture signal here. This should be done at the
85 // end of the send chain.
86 int destination_rate = std::min(codec_rate_hz, sample_rate_hz);
87
88 // If no stereo codecs are in use, we downmix a stereo stream from the
89 // device early in the chain, before resampling.
90 if (num_channels == 2 && codec_num_channels == 1) {
91 AudioFrameOperations::StereoToMono(src_data, samples_per_channel,
92 mono_buffer);
93 src_data = mono_buffer;
94 num_channels = 1;
95 }
96
97 if (resampler->InitializeIfNeeded(
98 sample_rate_hz, destination_rate, num_channels) != 0) {
99 LOG_FERR3(LS_ERROR,
100 InitializeIfNeeded,
101 sample_rate_hz,
102 destination_rate,
103 num_channels);
104 assert(false);
105 }
106
107 const int in_length = samples_per_channel * num_channels;
108 int out_length = resampler->Resample(
109 src_data, in_length, dst_af->data_, AudioFrame::kMaxDataSizeSamples);
110 if (out_length == -1) {
111 LOG_FERR3(LS_ERROR, Resample, src_data, in_length, dst_af->data_);
112 assert(false);
113 }
114
115 dst_af->samples_per_channel_ = out_length / num_channels;
116 dst_af->sample_rate_hz_ = destination_rate;
117 dst_af->num_channels_ = num_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000118}
119
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000120void MixWithSat(int16_t target[],
121 int target_channel,
122 const int16_t source[],
123 int source_channel,
124 int source_len) {
125 assert(target_channel == 1 || target_channel == 2);
126 assert(source_channel == 1 || source_channel == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000128 if (target_channel == 2 && source_channel == 1) {
129 // Convert source from mono to stereo.
130 int32_t left = 0;
131 int32_t right = 0;
132 for (int i = 0; i < source_len; ++i) {
133 left = source[i] + target[i * 2];
134 right = source[i] + target[i * 2 + 1];
135 target[i * 2] = WebRtcSpl_SatW32ToW16(left);
136 target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right);
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000138 } else if (target_channel == 1 && source_channel == 2) {
139 // Convert source from stereo to mono.
140 int32_t temp = 0;
141 for (int i = 0; i < source_len / 2; ++i) {
142 temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
143 target[i] = WebRtcSpl_SatW32ToW16(temp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000145 } else {
146 int32_t temp = 0;
147 for (int i = 0; i < source_len; ++i) {
148 temp = source[i] + target[i];
149 target[i] = WebRtcSpl_SatW32ToW16(temp);
150 }
151 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000154} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000155} // namespace webrtc