blob: c07ca1fdf6053edc679b66e44c081d7624dcca4c [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,
Peter Kastingdce40cf2015-08-24 14:52:23 -070017 size_t samples_per_channel,
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000018 int16_t* dst_audio) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070019 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000020 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,
Peter Kastingdce40cf2015-08-24 14:52:23 -070044 size_t samples_per_channel,
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000045 int16_t* dst_audio) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000047 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
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000066 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_);
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000077int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000078 if (frame.num_channels_ != 2) {
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000079 return -1;
80 }
niklase@google.com470e71d2011-07-07 08:21:25 +000081
Peter Kastingdce40cf2015-08-24 14:52:23 -070082 for (size_t i = 0; i < frame.samples_per_channel_; i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000083 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]);
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000087 }
88 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000091int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) {
92 int32_t temp_data = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000093
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000094 // Ensure that the output result is saturated [-32768, +32767].
Peter Kastingdce40cf2015-08-24 14:52:23 -070095 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000096 i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000097 temp_data = static_cast<int32_t>(scale * frame.data_[i]);
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +000098 if (temp_data < -32768) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +000099 frame.data_[i] = -32768;
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +0000100 } else if (temp_data > 32767) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000101 frame.data_[i] = 32767;
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +0000102 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000103 frame.data_[i] = static_cast<int16_t>(temp_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 }
andrew@webrtc.org9c4f6a52012-04-26 22:32:03 +0000105 }
106 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000109} // namespace webrtc