Line data Source code
1 : /*
2 : * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 : *
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 :
11 : #include "webrtc/voice_engine/utility.h"
12 :
13 : #include "webrtc/audio/utility/audio_frame_operations.h"
14 : #include "webrtc/base/checks.h"
15 : #include "webrtc/base/logging.h"
16 : #include "webrtc/common_audio/resampler/include/push_resampler.h"
17 : #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
18 : #include "webrtc/common_types.h"
19 : #include "webrtc/modules/include/module_common_types.h"
20 : #include "webrtc/voice_engine/voice_engine_defines.h"
21 :
22 : namespace webrtc {
23 : namespace voe {
24 :
25 0 : void RemixAndResample(const AudioFrame& src_frame,
26 : PushResampler<int16_t>* resampler,
27 : AudioFrame* dst_frame) {
28 0 : RemixAndResample(src_frame.data_, src_frame.samples_per_channel_,
29 0 : src_frame.num_channels_, src_frame.sample_rate_hz_,
30 0 : resampler, dst_frame);
31 0 : dst_frame->timestamp_ = src_frame.timestamp_;
32 0 : dst_frame->elapsed_time_ms_ = src_frame.elapsed_time_ms_;
33 0 : dst_frame->ntp_time_ms_ = src_frame.ntp_time_ms_;
34 0 : }
35 :
36 0 : void RemixAndResample(const int16_t* src_data,
37 : size_t samples_per_channel,
38 : size_t num_channels,
39 : int sample_rate_hz,
40 : PushResampler<int16_t>* resampler,
41 : AudioFrame* dst_frame) {
42 0 : const int16_t* audio_ptr = src_data;
43 0 : size_t audio_ptr_num_channels = num_channels;
44 : int16_t mono_audio[AudioFrame::kMaxDataSizeSamples];
45 :
46 : // Downmix before resampling.
47 0 : if (num_channels == 2 && dst_frame->num_channels_ == 1) {
48 : AudioFrameOperations::StereoToMono(src_data, samples_per_channel,
49 0 : mono_audio);
50 0 : audio_ptr = mono_audio;
51 0 : audio_ptr_num_channels = 1;
52 : }
53 :
54 0 : if (resampler->InitializeIfNeeded(sample_rate_hz, dst_frame->sample_rate_hz_,
55 : audio_ptr_num_channels) == -1) {
56 0 : FATAL() << "InitializeIfNeeded failed: sample_rate_hz = " << sample_rate_hz
57 0 : << ", dst_frame->sample_rate_hz_ = " << dst_frame->sample_rate_hz_
58 0 : << ", audio_ptr_num_channels = " << audio_ptr_num_channels;
59 : }
60 :
61 0 : const size_t src_length = samples_per_channel * audio_ptr_num_channels;
62 0 : int out_length = resampler->Resample(audio_ptr, src_length, dst_frame->data_,
63 0 : AudioFrame::kMaxDataSizeSamples);
64 0 : if (out_length == -1) {
65 0 : FATAL() << "Resample failed: audio_ptr = " << audio_ptr
66 0 : << ", src_length = " << src_length
67 0 : << ", dst_frame->data_ = " << dst_frame->data_;
68 : }
69 0 : dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
70 :
71 : // Upmix after resampling.
72 0 : if (num_channels == 1 && dst_frame->num_channels_ == 2) {
73 : // The audio in dst_frame really is mono at this point; MonoToStereo will
74 : // set this back to stereo.
75 0 : dst_frame->num_channels_ = 1;
76 0 : AudioFrameOperations::MonoToStereo(dst_frame);
77 : }
78 0 : }
79 :
80 0 : void MixWithSat(int16_t target[],
81 : size_t target_channel,
82 : const int16_t source[],
83 : size_t source_channel,
84 : size_t source_len) {
85 0 : RTC_DCHECK_GE(target_channel, 1);
86 0 : RTC_DCHECK_LE(target_channel, 2);
87 0 : RTC_DCHECK_GE(source_channel, 1);
88 0 : RTC_DCHECK_LE(source_channel, 2);
89 :
90 0 : if (target_channel == 2 && source_channel == 1) {
91 : // Convert source from mono to stereo.
92 0 : int32_t left = 0;
93 0 : int32_t right = 0;
94 0 : for (size_t i = 0; i < source_len; ++i) {
95 0 : left = source[i] + target[i * 2];
96 0 : right = source[i] + target[i * 2 + 1];
97 0 : target[i * 2] = WebRtcSpl_SatW32ToW16(left);
98 0 : target[i * 2 + 1] = WebRtcSpl_SatW32ToW16(right);
99 0 : }
100 0 : } else if (target_channel == 1 && source_channel == 2) {
101 : // Convert source from stereo to mono.
102 0 : int32_t temp = 0;
103 0 : for (size_t i = 0; i < source_len / 2; ++i) {
104 0 : temp = ((source[i * 2] + source[i * 2 + 1]) >> 1) + target[i];
105 0 : target[i] = WebRtcSpl_SatW32ToW16(temp);
106 0 : }
107 : } else {
108 0 : int32_t temp = 0;
109 0 : for (size_t i = 0; i < source_len; ++i) {
110 0 : temp = source[i] + target[i];
111 0 : target[i] = WebRtcSpl_SatW32ToW16(temp);
112 : }
113 : }
114 0 : }
115 :
116 : } // namespace voe
117 : } // namespace webrtc
|