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/modules/include/module_common_types.h"
12 #include "webrtc/modules/utility/include/audio_frame_operations.h"
13
14 namespace webrtc {
15
MonoToStereo(const int16_t * src_audio,size_t samples_per_channel,int16_t * dst_audio)16 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio,
17 size_t samples_per_channel,
18 int16_t* dst_audio) {
19 for (size_t i = 0; i < samples_per_channel; i++) {
20 dst_audio[2 * i] = src_audio[i];
21 dst_audio[2 * i + 1] = src_audio[i];
22 }
23 }
24
MonoToStereo(AudioFrame * frame)25 int AudioFrameOperations::MonoToStereo(AudioFrame* frame) {
26 if (frame->num_channels_ != 1) {
27 return -1;
28 }
29 if ((frame->samples_per_channel_ * 2) >= AudioFrame::kMaxDataSizeSamples) {
30 // Not enough memory to expand from mono to stereo.
31 return -1;
32 }
33
34 int16_t data_copy[AudioFrame::kMaxDataSizeSamples];
35 memcpy(data_copy, frame->data_,
36 sizeof(int16_t) * frame->samples_per_channel_);
37 MonoToStereo(data_copy, frame->samples_per_channel_, frame->data_);
38 frame->num_channels_ = 2;
39
40 return 0;
41 }
42
StereoToMono(const int16_t * src_audio,size_t samples_per_channel,int16_t * dst_audio)43 void AudioFrameOperations::StereoToMono(const int16_t* src_audio,
44 size_t samples_per_channel,
45 int16_t* dst_audio) {
46 for (size_t i = 0; i < samples_per_channel; i++) {
47 dst_audio[i] = (src_audio[2 * i] + src_audio[2 * i + 1]) >> 1;
48 }
49 }
50
StereoToMono(AudioFrame * frame)51 int AudioFrameOperations::StereoToMono(AudioFrame* frame) {
52 if (frame->num_channels_ != 2) {
53 return -1;
54 }
55
56 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_);
57 frame->num_channels_ = 1;
58
59 return 0;
60 }
61
SwapStereoChannels(AudioFrame * frame)62 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) {
63 if (frame->num_channels_ != 2) return;
64
65 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
66 int16_t temp_data = frame->data_[i];
67 frame->data_[i] = frame->data_[i + 1];
68 frame->data_[i + 1] = temp_data;
69 }
70 }
71
Mute(AudioFrame & frame)72 void AudioFrameOperations::Mute(AudioFrame& frame) {
73 memset(frame.data_, 0, sizeof(int16_t) *
74 frame.samples_per_channel_ * frame.num_channels_);
75 }
76
Scale(float left,float right,AudioFrame & frame)77 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
78 if (frame.num_channels_ != 2) {
79 return -1;
80 }
81
82 for (size_t i = 0; i < frame.samples_per_channel_; i++) {
83 frame.data_[2 * i] =
84 static_cast<int16_t>(left * frame.data_[2 * i]);
85 frame.data_[2 * i + 1] =
86 static_cast<int16_t>(right * frame.data_[2 * i + 1]);
87 }
88 return 0;
89 }
90
ScaleWithSat(float scale,AudioFrame & frame)91 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) {
92 int32_t temp_data = 0;
93
94 // Ensure that the output result is saturated [-32768, +32767].
95 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_;
96 i++) {
97 temp_data = static_cast<int32_t>(scale * frame.data_[i]);
98 if (temp_data < -32768) {
99 frame.data_[i] = -32768;
100 } else if (temp_data > 32767) {
101 frame.data_[i] = 32767;
102 } else {
103 frame.data_[i] = static_cast<int16_t>(temp_data);
104 }
105 }
106 return 0;
107 }
108
109 } // namespace webrtc
110