blob: 18dba52b651771a3e5fd882cee776fd889958781 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org02d71742012-04-24 19:47:00 +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.org8b062002013-07-12 08:28:10 +000011#include "webrtc/modules/interface/module_common_types.h"
12#include "webrtc/modules/utility/interface/audio_frame_operations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000013
14namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000015
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000016void AudioFrameOperations::MonoToStereo(const int16_t* src_audio,
17 int samples_per_channel,
18 int16_t* dst_audio) {
19 for (int 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
25int AudioFrameOperations::MonoToStereo(AudioFrame* frame) {
26 if (frame->num_channels_ != 1) {
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000027 return -1;
28 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000029 if ((frame->samples_per_channel_ * 2) >= AudioFrame::kMaxDataSizeSamples) {
30 // Not enough memory to expand from mono to stereo.
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000031 return -1;
32 }
niklase@google.com470e71d2011-07-07 08:21:25 +000033
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000034 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;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000040 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000043void AudioFrameOperations::StereoToMono(const int16_t* src_audio,
44 int samples_per_channel,
45 int16_t* dst_audio) {
46 for (int i = 0; i < samples_per_channel; i++) {
47 dst_audio[i] = (src_audio[2 * i] + src_audio[2 * i + 1]) >> 1;
48 }
49}
50
51int AudioFrameOperations::StereoToMono(AudioFrame* frame) {
52 if (frame->num_channels_ != 2) {
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000053 return -1;
54 }
niklase@google.com470e71d2011-07-07 08:21:25 +000055
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000056 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_);
57 frame->num_channels_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000058
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000059 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
andrew@webrtc.org02d71742012-04-24 19:47:00 +000062void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000063 if (frame->num_channels_ != 2) return;
andrew@webrtc.org1c7bfe02012-04-26 00:20:28 +000064
andrew@webrtc.org63a50982012-05-02 23:56:37 +000065 for (int 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;
andrew@webrtc.org02d71742012-04-24 19:47:00 +000069 }
70}
71
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000072void AudioFrameOperations::Mute(AudioFrame& frame) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000073 memset(frame.data_, 0, sizeof(int16_t) *
74 frame.samples_per_channel_ * frame.num_channels_);
75 frame.energy_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000076}
77
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000078int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000079 if (frame.num_channels_ != 2) {
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000080 return -1;
81 }
niklase@google.com470e71d2011-07-07 08:21:25 +000082
andrew@webrtc.org63a50982012-05-02 23:56:37 +000083 for (int i = 0; i < frame.samples_per_channel_; i++) {
84 frame.data_[2 * i] =
85 static_cast<int16_t>(left * frame.data_[2 * i]);
86 frame.data_[2 * i + 1] =
87 static_cast<int16_t>(right * frame.data_[2 * i + 1]);
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000088 }
89 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000092int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) {
93 int32_t temp_data = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000095 // Ensure that the output result is saturated [-32768, +32767].
andrew@webrtc.org63a50982012-05-02 23:56:37 +000096 for (int i = 0; i < frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000097 i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000098 temp_data = static_cast<int32_t>(scale * frame.data_[i]);
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000099 if (temp_data < -32768) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000100 frame.data_[i] = -32768;
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +0000101 } else if (temp_data > 32767) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000102 frame.data_[i] = 32767;
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +0000103 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000104 frame.data_[i] = static_cast<int16_t>(temp_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 }
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +0000106 }
107 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
109
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000110} // namespace webrtc