| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/include/module_common_types.h" | 11 #include "webrtc/modules/include/module_common_types.h" |
| 12 #include "webrtc/modules/utility/include/audio_frame_operations.h" | 12 #include "webrtc/modules/utility/include/audio_frame_operations.h" |
| 13 #include "webrtc/base/checks.h" |
| 13 | 14 |
| 14 namespace webrtc { | 15 namespace webrtc { |
| 16 namespace { |
| 17 |
| 18 // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz. |
| 19 const size_t kMuteFadeFrames = 128; |
| 20 const float kMuteFadeInc = 1.0f / kMuteFadeFrames; |
| 21 |
| 22 } // namespace { |
| 15 | 23 |
| 16 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, | 24 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, |
| 17 size_t samples_per_channel, | 25 size_t samples_per_channel, |
| 18 int16_t* dst_audio) { | 26 int16_t* dst_audio) { |
| 19 for (size_t i = 0; i < samples_per_channel; i++) { | 27 for (size_t i = 0; i < samples_per_channel; i++) { |
| 20 dst_audio[2 * i] = src_audio[i]; | 28 dst_audio[2 * i] = src_audio[i]; |
| 21 dst_audio[2 * i + 1] = src_audio[i]; | 29 dst_audio[2 * i + 1] = src_audio[i]; |
| 22 } | 30 } |
| 23 } | 31 } |
| 24 | 32 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { | 70 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { |
| 63 if (frame->num_channels_ != 2) return; | 71 if (frame->num_channels_ != 2) return; |
| 64 | 72 |
| 65 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { | 73 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { |
| 66 int16_t temp_data = frame->data_[i]; | 74 int16_t temp_data = frame->data_[i]; |
| 67 frame->data_[i] = frame->data_[i + 1]; | 75 frame->data_[i] = frame->data_[i + 1]; |
| 68 frame->data_[i + 1] = temp_data; | 76 frame->data_[i + 1] = temp_data; |
| 69 } | 77 } |
| 70 } | 78 } |
| 71 | 79 |
| 72 void AudioFrameOperations::Mute(AudioFrame& frame) { | 80 void AudioFrameOperations::Mute(AudioFrame* frame, bool previous_frame_muted, |
| 73 memset(frame.data_, 0, sizeof(int16_t) * | 81 bool current_frame_muted) { |
| 74 frame.samples_per_channel_ * frame.num_channels_); | 82 RTC_DCHECK(frame); |
| 83 RTC_DCHECK(frame->interleaved_); |
| 84 if (!previous_frame_muted && !current_frame_muted) { |
| 85 // Not muted, don't touch. |
| 86 } else if (previous_frame_muted && current_frame_muted) { |
| 87 // Frame fully muted. |
| 88 size_t total_samples = frame->samples_per_channel_ * frame->num_channels_; |
| 89 RTC_DCHECK_GE(AudioFrame::kMaxDataSizeSamples, total_samples); |
| 90 memset(frame->data_, 0, sizeof(frame->data_[0]) * total_samples); |
| 91 } else { |
| 92 // Limit number of samples to fade, if frame isn't long enough. |
| 93 size_t count = kMuteFadeFrames; |
| 94 float inc = kMuteFadeInc; |
| 95 if (frame->samples_per_channel_ < kMuteFadeFrames) { |
| 96 count = frame->samples_per_channel_; |
| 97 if (count > 0) { |
| 98 inc = 1.0f / count; |
| 99 } |
| 100 } |
| 101 |
| 102 size_t start = 0; |
| 103 float g = 0.0f; |
| 104 if (current_frame_muted) { |
| 105 // Fade out the last |count| samples of frame. |
| 106 RTC_DCHECK(!previous_frame_muted); |
| 107 start = frame->samples_per_channel_ - count; |
| 108 g = 1.0f; |
| 109 inc = -inc; |
| 110 } else { |
| 111 // Fade in the first |count| samples of frame. |
| 112 RTC_DCHECK(previous_frame_muted); |
| 113 } |
| 114 |
| 115 // Perform fade. |
| 116 size_t end = (start + count); |
| 117 size_t channels = frame->num_channels_; |
| 118 for (size_t i = start * channels; i < end * channels; i += channels) { |
| 119 g += inc; |
| 120 for (size_t j = 0; j < channels; ++j) { |
| 121 frame->data_[i + j] *= g; |
| 122 } |
| 123 } |
| 124 } |
| 75 } | 125 } |
| 76 | 126 |
| 77 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { | 127 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { |
| 78 if (frame.num_channels_ != 2) { | 128 if (frame.num_channels_ != 2) { |
| 79 return -1; | 129 return -1; |
| 80 } | 130 } |
| 81 | 131 |
| 82 for (size_t i = 0; i < frame.samples_per_channel_; i++) { | 132 for (size_t i = 0; i < frame.samples_per_channel_; i++) { |
| 83 frame.data_[2 * i] = | 133 frame.data_[2 * i] = |
| 84 static_cast<int16_t>(left * frame.data_[2 * i]); | 134 static_cast<int16_t>(left * frame.data_[2 * i]); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 100 } else if (temp_data > 32767) { | 150 } else if (temp_data > 32767) { |
| 101 frame.data_[i] = 32767; | 151 frame.data_[i] = 32767; |
| 102 } else { | 152 } else { |
| 103 frame.data_[i] = static_cast<int16_t>(temp_data); | 153 frame.data_[i] = static_cast<int16_t>(temp_data); |
| 104 } | 154 } |
| 105 } | 155 } |
| 106 return 0; | 156 return 0; |
| 107 } | 157 } |
| 108 | 158 |
| 109 } // namespace webrtc | 159 } // namespace webrtc |
| OLD | NEW |