1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11471ae72f18e7b23a96b245dbd508386fe139449cpbos@webrtc.org#include "webrtc/voice_engine/utility.h"
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
13f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org#include "webrtc/common_audio/resampler/include/push_resampler.h"
14471ae72f18e7b23a96b245dbd508386fe139449cpbos@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
15f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org#include "webrtc/common_types.h"
16f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org#include "webrtc/modules/interface/module_common_types.h"
17f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org#include "webrtc/modules/utility/interface/audio_frame_operations.h"
18f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org#include "webrtc/system_wrappers/interface/logging.h"
19f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org#include "webrtc/voice_engine/voice_engine_defines.h"
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
21f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.orgnamespace webrtc {
22f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.orgnamespace voe {
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
24f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org// TODO(ajm): There is significant overlap between RemixAndResample and
25f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org// ConvertToCodecFormat, but if we're to consolidate we should probably make a
26f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org// real converter class.
27f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.orgvoid RemixAndResample(const AudioFrame& src_frame,
28708ff4d93770d863cdea26fd496ae71414eb8a53andrew@webrtc.org                      PushResampler<int16_t>* resampler,
29f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                      AudioFrame* dst_frame) {
30f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  const int16_t* audio_ptr = src_frame.data_;
31f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  int audio_ptr_num_channels = src_frame.num_channels_;
32f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  int16_t mono_audio[AudioFrame::kMaxDataSizeSamples];
33f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
34f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  // Downmix before resampling.
35f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (src_frame.num_channels_ == 2 && dst_frame->num_channels_ == 1) {
36f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    AudioFrameOperations::StereoToMono(src_frame.data_,
37f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                                       src_frame.samples_per_channel_,
38f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                                       mono_audio);
39f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    audio_ptr = mono_audio;
40f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    audio_ptr_num_channels = 1;
41f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
42f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
43f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (resampler->InitializeIfNeeded(src_frame.sample_rate_hz_,
44f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                                    dst_frame->sample_rate_hz_,
45f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                                    audio_ptr_num_channels) == -1) {
46f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    LOG_FERR3(LS_ERROR, InitializeIfNeeded, src_frame.sample_rate_hz_,
47f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org              dst_frame->sample_rate_hz_, audio_ptr_num_channels);
48f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    assert(false);
49f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
50f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
51f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  const int src_length = src_frame.samples_per_channel_ *
52f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                         audio_ptr_num_channels;
53f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_,
54f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                                       AudioFrame::kMaxDataSizeSamples);
55f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (out_length == -1) {
56f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    LOG_FERR3(LS_ERROR, Resample, audio_ptr, src_length, dst_frame->data_);
57f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    assert(false);
58f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
59f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
60f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
61f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  // Upmix after resampling.
62f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (src_frame.num_channels_ == 1 && dst_frame->num_channels_ == 2) {
63f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    // The audio in dst_frame really is mono at this point; MonoToStereo will
64f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    // set this back to stereo.
65f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    dst_frame->num_channels_ = 1;
66f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    AudioFrameOperations::MonoToStereo(dst_frame);
67f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
6881f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org
6981f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  dst_frame->timestamp_ = src_frame.timestamp_;
7081f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
7181f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
72b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
73b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
74f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.orgvoid DownConvertToCodecFormat(const int16_t* src_data,
75f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              int samples_per_channel,
76f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              int num_channels,
77f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              int sample_rate_hz,
78f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              int codec_num_channels,
79f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              int codec_rate_hz,
80f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              int16_t* mono_buffer,
81708ff4d93770d863cdea26fd496ae71414eb8a53andrew@webrtc.org                              PushResampler<int16_t>* resampler,
82f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                              AudioFrame* dst_af) {
83f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  assert(samples_per_channel <= kMaxMonoDataSizeSamples);
84f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  assert(num_channels == 1 || num_channels == 2);
85f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  assert(codec_num_channels == 1 || codec_num_channels == 2);
8635af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  dst_af->Reset();
87f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
88f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  // Never upsample the capture signal here. This should be done at the
89f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  // end of the send chain.
90f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  int destination_rate = std::min(codec_rate_hz, sample_rate_hz);
91f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
92f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  // If no stereo codecs are in use, we downmix a stereo stream from the
93f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  // device early in the chain, before resampling.
94f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (num_channels == 2 && codec_num_channels == 1) {
95f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    AudioFrameOperations::StereoToMono(src_data, samples_per_channel,
96f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                                       mono_buffer);
97f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    src_data = mono_buffer;
98f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    num_channels = 1;
99f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
100f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
101f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (resampler->InitializeIfNeeded(
102f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org          sample_rate_hz, destination_rate, num_channels) != 0) {
103f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    LOG_FERR3(LS_ERROR,
104f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org              InitializeIfNeeded,
105f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org              sample_rate_hz,
106f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org              destination_rate,
107f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org              num_channels);
108f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    assert(false);
109f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
110f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
111f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  const int in_length = samples_per_channel * num_channels;
112f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  int out_length = resampler->Resample(
113f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      src_data, in_length, dst_af->data_, AudioFrame::kMaxDataSizeSamples);
114f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (out_length == -1) {
115f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    LOG_FERR3(LS_ERROR, Resample, src_data, in_length, dst_af->data_);
116f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    assert(false);
117f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
118f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
119f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  dst_af->samples_per_channel_ = out_length / num_channels;
120f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  dst_af->sample_rate_hz_ = destination_rate;
121f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  dst_af->num_channels_ = num_channels;
122b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
123b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
124f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.orgvoid MixWithSat(int16_t target[],
125f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                int target_channel,
126f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                const int16_t source[],
127f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                int source_channel,
128f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org                int source_len) {
129f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  assert(target_channel == 1 || target_channel == 2);
130f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  assert(source_channel == 1 || source_channel == 2);
131f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org
132f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  if (target_channel == 2 && source_channel == 1) {
133f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    // Convert source from mono to stereo.
134f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    int32_t left = 0;
135f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    int32_t right = 0;
136f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    for (int i = 0; i < source_len; ++i) {
137f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      left = source[i] + target[i * 2];
138f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      right = source[i] + target[i * 2 + 1];
139f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      target[i * 2] = WebRtcSpl_SatW32ToW16(left);
140f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right);
141f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    }
142f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  } else if (target_channel == 1 && source_channel == 2) {
143f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    // Convert source from stereo to mono.
144f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    int32_t temp = 0;
145f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    for (int i = 0; i < source_len / 2; ++i) {
146f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
147f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      target[i] = WebRtcSpl_SatW32ToW16(temp);
148b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
149f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  } else {
150f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    int32_t temp = 0;
151f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    for (int i = 0; i < source_len; ++i) {
152f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      temp = source[i] + target[i];
153f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org      target[i] = WebRtcSpl_SatW32ToW16(temp);
154f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org    }
155f7c73b531c9f2aca2adb87044613a7b7fa94de84andrew@webrtc.org  }
156b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
157b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
1583b89e10f31160da35b408fd00cb8f89d2b08862dpbos@webrtc.org}  // namespace voe
1593b89e10f31160da35b408fd00cb8f89d2b08862dpbos@webrtc.org}  // namespace webrtc
160