Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: webrtc/modules/utility/source/audio_frame_operations.cc

Issue 1810413002: Avoid clicks when muting/unmuting a voe::Channel. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: undo loop unrolling Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 size_t end = count;
104 float start_g = 0.0f;
105 if (current_frame_muted) {
106 // Fade out the last |count| samples of frame.
107 RTC_DCHECK(!previous_frame_muted);
108 start = frame->samples_per_channel_ - count;
109 end = frame->samples_per_channel_;
110 start_g = 1.0f;
111 inc = -inc;
112 } else {
113 // Fade in the first |count| samples of frame.
114 RTC_DCHECK(previous_frame_muted);
115 }
116
117 // Perform fade.
118 size_t channels = frame->num_channels_;
119 for (size_t j = 0; j < channels; ++j) {
120 float g = start_g;
121 for (size_t i = start * channels; i < end * channels; i += channels) {
122 g += inc;
123 frame->data_[i + j] *= g;
124 }
125 }
126 }
75 } 127 }
76 128
77 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { 129 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
78 if (frame.num_channels_ != 2) { 130 if (frame.num_channels_ != 2) {
79 return -1; 131 return -1;
80 } 132 }
81 133
82 for (size_t i = 0; i < frame.samples_per_channel_; i++) { 134 for (size_t i = 0; i < frame.samples_per_channel_; i++) {
83 frame.data_[2 * i] = 135 frame.data_[2 * i] =
84 static_cast<int16_t>(left * frame.data_[2 * i]); 136 static_cast<int16_t>(left * frame.data_[2 * i]);
(...skipping 15 matching lines...) Expand all
100 } else if (temp_data > 32767) { 152 } else if (temp_data > 32767) {
101 frame.data_[i] = 32767; 153 frame.data_[i] = 32767;
102 } else { 154 } else {
103 frame.data_[i] = static_cast<int16_t>(temp_data); 155 frame.data_[i] = static_cast<int16_t>(temp_data);
104 } 156 }
105 } 157 }
106 return 0; 158 return 0;
107 } 159 }
108 160
109 } // namespace webrtc 161 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698