niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame^] | 11 | #include "webrtc/modules/interface/module_common_types.h" |
| 12 | #include "webrtc/modules/utility/interface/audio_frame_operations.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 16 | void 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 | |
| 25 | int AudioFrameOperations::MonoToStereo(AudioFrame* frame) { |
| 26 | if (frame->num_channels_ != 1) { |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 27 | return -1; |
| 28 | } |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 29 | if ((frame->samples_per_channel_ * 2) >= AudioFrame::kMaxDataSizeSamples) { |
| 30 | // Not enough memory to expand from mono to stereo. |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 31 | return -1; |
| 32 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 40 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 43 | void 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 | |
| 51 | int AudioFrameOperations::StereoToMono(AudioFrame* frame) { |
| 52 | if (frame->num_channels_ != 2) { |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 56 | StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); |
| 57 | frame->num_channels_ = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 59 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
andrew@webrtc.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 62 | void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 63 | if (frame->num_channels_ != 2) return; |
andrew@webrtc.org | 1c7bfe0 | 2012-04-26 00:20:28 +0000 | [diff] [blame] | 64 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 65 | 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.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 72 | void AudioFrameOperations::Mute(AudioFrame& frame) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 73 | memset(frame.data_, 0, sizeof(int16_t) * |
| 74 | frame.samples_per_channel_ * frame.num_channels_); |
| 75 | frame.energy_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | } |
| 77 | |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 78 | int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 79 | if (frame.num_channels_ != 2) { |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 80 | return -1; |
| 81 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 83 | 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.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 88 | } |
| 89 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 92 | int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { |
| 93 | int32_t temp_data = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 95 | // Ensure that the output result is saturated [-32768, +32767]. |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 96 | for (int i = 0; i < frame.samples_per_channel_ * frame.num_channels_; |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 97 | i++) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 98 | temp_data = static_cast<int32_t>(scale * frame.data_[i]); |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 99 | if (temp_data < -32768) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 100 | frame.data_[i] = -32768; |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 101 | } else if (temp_data > 32767) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 102 | frame.data_[i] = 32767; |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 103 | } else { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 104 | frame.data_[i] = static_cast<int16_t>(temp_data); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | } |
andrew@webrtc.org | 9c4f6a5 | 2012-04-26 22:32:03 +0000 | [diff] [blame] | 106 | } |
| 107 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 110 | } // namespace webrtc |