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

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: rebase 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 {
tlegrand-webrtc 2016/03/23 13:42:41 Is the extra namespace needed?
the sun 2016/03/23 14:25:59 Yes, anything internal to the implementation shoul
17
18 // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz.
19 const size_t kMuteFadeFrames = 128;
20 const float kMuteFadeInc = 1.0f / kMuteFadeFrames;
tlegrand-webrtc 2016/03/23 13:42:41 Not sure what the style guide says, but I'd find i
the sun 2016/03/23 14:25:59 Done.
21 } // namespace {
15 22
16 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, 23 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio,
17 size_t samples_per_channel, 24 size_t samples_per_channel,
18 int16_t* dst_audio) { 25 int16_t* dst_audio) {
19 for (size_t i = 0; i < samples_per_channel; i++) { 26 for (size_t i = 0; i < samples_per_channel; i++) {
20 dst_audio[2 * i] = src_audio[i]; 27 dst_audio[2 * i] = src_audio[i];
21 dst_audio[2 * i + 1] = src_audio[i]; 28 dst_audio[2 * i + 1] = src_audio[i];
22 } 29 }
23 } 30 }
24 31
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { 69 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) {
63 if (frame->num_channels_ != 2) return; 70 if (frame->num_channels_ != 2) return;
64 71
65 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { 72 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
66 int16_t temp_data = frame->data_[i]; 73 int16_t temp_data = frame->data_[i];
67 frame->data_[i] = frame->data_[i + 1]; 74 frame->data_[i] = frame->data_[i + 1];
68 frame->data_[i + 1] = temp_data; 75 frame->data_[i + 1] = temp_data;
69 } 76 }
70 } 77 }
71 78
72 void AudioFrameOperations::Mute(AudioFrame& frame) { 79 void AudioFrameOperations::Mute(AudioFrame* frame, bool start_muted,
peah-webrtc 2016/03/23 15:57:04 I think the naming of start_muted and end_muted ma
peah-webrtc 2016/03/23 15:57:04 Having looked closer at the function, I think it w
peah-webrtc 2016/03/23 15:57:04 Why not split this function in two versions, one c
the sun 2016/03/23 21:30:52 I think it makes sense to keep the logic within on
the sun 2016/03/23 21:30:52 Hmm... "!(!previous_frame_was_muted && !current_fr
the sun 2016/03/23 21:30:53 Good suggestion. Makes the code much easier to rea
peah-webrtc 2016/03/24 07:04:54 I agree with the explosion. But your revised varia
peah-webrtc 2016/03/24 07:04:54 I think that with the parameter name changes that
peah-webrtc 2016/03/24 07:04:54 Acknowledged.
73 memset(frame.data_, 0, sizeof(int16_t) * 80 bool end_muted) {
tlegrand-webrtc 2016/03/23 13:42:41 It's not super clear what start_muted and end_mute
the sun 2016/03/23 14:25:59 Added in the .h.
74 frame.samples_per_channel_ * frame.num_channels_); 81 RTC_DCHECK(frame);
82 if (!start_muted && !end_muted) {
83 // Not muted.
84 } else if (start_muted && end_muted) {
85 // Frame fully muted.
86 memset(frame->data_, 0, sizeof(int16_t) *
peah-webrtc 2016/03/23 15:57:04 Since you know the size of data in this case (it i
peah-webrtc 2016/03/23 15:57:04 I would prefer the sizeof to be done on frame->dat
the sun 2016/03/23 21:30:52 Yes, I blame copy+paste from the old version. :)
the sun 2016/03/23 21:30:52 Good idea! Done.
peah-webrtc 2016/03/24 07:04:54 Acknowledged.
peah-webrtc 2016/03/24 07:04:54 Acknowledged.
87 frame->samples_per_channel_ * frame->num_channels_);
88 } else {
89 // Limit number of samples to fade, if frame isn't long enough.
90 size_t count = kMuteFadeFrames;
91 float inc = kMuteFadeInc;
92 if (frame->samples_per_channel_ < kMuteFadeFrames) {
peah-webrtc 2016/03/23 15:57:04 If frame->samples_per_channel_ == 0 is treated as
the sun 2016/03/23 21:30:52 No, we still need to do the division in case we ha
peah-webrtc 2016/03/24 07:04:54 Yes, I agree. I looked at it a bit broader though
the sun 2016/03/24 09:33:15 What I'd REALLY like to do instead is to send arou
93 count = frame->samples_per_channel_;
94 if (count > 0) {
peah-webrtc 2016/03/23 15:57:04 This should always be true, right? Afaics count ==
the sun 2016/03/23 21:30:52 There's nothing in AudioFrame that prevents it fro
peah-webrtc 2016/03/24 07:04:54 True, but you anyway need to do that check for the
the sun 2016/03/24 09:33:15 Yes, but that is not the common case.
95 inc = 1.0f / count;
peah-webrtc 2016/03/23 15:57:04 How common is it that frame->samples_per_channel_
the sun 2016/03/23 21:30:53 Given that we usually store 10ms of data (though w
peah-webrtc 2016/03/24 07:04:54 Would it be possible to reduce the fading time to
the sun 2016/03/24 09:33:15 That would be nice, but AudioFrame doesn't give us
96 }
97 }
98
99 // Ramp up the first N samples of frame.
tlegrand-webrtc 2016/03/23 13:42:41 N-> "count", or? The comment should probably ment
the sun 2016/03/23 14:25:59 Done.
100 size_t start = 0;
101 float g = 0.0f;
102 if (!start_muted && end_muted) {
103 // Ramp down the last N samples of frame.
104 start = frame->samples_per_channel_ - count;
105 g = 1.0f;
106 inc = -inc;
107 } else {
108 RTC_DCHECK(start_muted && !end_muted);
peah-webrtc 2016/03/23 15:57:04 Why do you assert on this? Should it not be allowe
the sun 2016/03/23 21:30:52 Good call, I've simplified the logic a bit. This a
109 }
110
peah-webrtc 2016/03/23 15:57:04 If you change the code so that you in this block k
the sun 2016/03/23 21:30:52 You leave out the check for frame->samples_per_cha
peah-webrtc 2016/03/24 07:04:54 Acknowledged.
peah-webrtc 2016/03/24 07:04:54 Acknowledged.
111 // Perform fade.
112 const size_t end = (start + count);
peah-webrtc 2016/03/23 15:57:04 This should always be start = frame->samples_per_c
the sun 2016/03/23 21:30:52 end will be 'count' or 'frame->samples_per_channel
peah-webrtc 2016/03/24 07:04:54 True, it feels a bit backward though to recompute
the sun 2016/03/24 09:33:15 You're right, the code is nicer if I just set up e
113 const size_t channels = frame->num_channels_;
114 for (size_t i = start * channels; i < end * channels; i += channels) {
115 g += inc;
peah-webrtc 2016/03/23 15:57:04 You should ideally bound the gain to 0<=g<=1 using
the sun 2016/03/23 21:30:52 No, it is designed to never shoot outside, and the
peah-webrtc 2016/03/24 07:04:54 Acknowledged.
116 for (size_t j = 0; j < channels; ++j) {
117 frame->data_[i + j] = static_cast<int16_t>(g * frame->data_[i + j]);
118 }
peah-webrtc 2016/03/23 15:57:04 Since the number of channels typically is much sma
the sun 2016/03/23 21:30:52 I think that is evil to caches. For a 320 sample s
peah-webrtc 2016/03/24 07:04:54 Great find! We should probably deprecated that API
the sun 2016/03/24 09:33:15 https://codereview.webrtc.org/1830713003/
119 }
120 }
75 } 121 }
76 122
77 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { 123 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) {
78 if (frame.num_channels_ != 2) { 124 if (frame.num_channels_ != 2) {
79 return -1; 125 return -1;
80 } 126 }
81 127
82 for (size_t i = 0; i < frame.samples_per_channel_; i++) { 128 for (size_t i = 0; i < frame.samples_per_channel_; i++) {
83 frame.data_[2 * i] = 129 frame.data_[2 * i] =
84 static_cast<int16_t>(left * frame.data_[2 * i]); 130 static_cast<int16_t>(left * frame.data_[2 * i]);
(...skipping 15 matching lines...) Expand all
100 } else if (temp_data > 32767) { 146 } else if (temp_data > 32767) {
101 frame.data_[i] = 32767; 147 frame.data_[i] = 32767;
102 } else { 148 } else {
103 frame.data_[i] = static_cast<int16_t>(temp_data); 149 frame.data_[i] = static_cast<int16_t>(temp_data);
104 } 150 }
105 } 151 }
106 return 0; 152 return 0;
107 } 153 }
108 154
109 } // namespace webrtc 155 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698