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 <algorithm> | |
12 | |
13 #include "webrtc/audio/utility/audio_frame_operations.h" | |
11 #include "webrtc/modules/include/module_common_types.h" | 14 #include "webrtc/modules/include/module_common_types.h" |
12 #include "webrtc/modules/utility/include/audio_frame_operations.h" | |
13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
14 | 16 |
15 namespace webrtc { | 17 namespace webrtc { |
16 namespace { | 18 namespace { |
17 | 19 |
18 // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz. | 20 // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz. |
19 const size_t kMuteFadeFrames = 128; | 21 const size_t kMuteFadeFrames = 128; |
20 const float kMuteFadeInc = 1.0f / kMuteFadeFrames; | 22 const float kMuteFadeInc = 1.0f / kMuteFadeFrames; |
21 | 23 |
22 } // namespace { | 24 } // namespace |
25 | |
26 void AudioFrameOperations::Add(const AudioFrame& frame_to_add, | |
27 AudioFrame* result_frame) { | |
28 // Sanity check | |
29 RTC_DCHECK_GT(result_frame->num_channels_, 0u); | |
hlundin-webrtc
2016/11/30 11:24:36
I know we usually don't allow other than plain mon
aleloi
2016/11/30 11:33:39
Not really. If there is a chance that poly-channel
kwiberg-webrtc
2016/11/30 11:36:04
But there's no good reason to not remove the unnec
aleloi
2016/11/30 11:48:34
I think methods should in general only check their
hlundin-webrtc
2016/11/30 11:51:05
[Logic evaluation stalled due to double negation]
kwiberg-webrtc
2016/11/30 11:55:04
Yes, that's what I meant. I (mis-)interpreted Alex
| |
30 RTC_DCHECK_LT(result_frame->num_channels_, 3u); | |
kwiberg-webrtc
2016/11/30 09:38:34
You don't need the "u"s anymore!
aleloi
2016/11/30 10:11:48
Done. Has the implementation of the DCHECK macros
kwiberg-webrtc
2016/11/30 11:32:44
Yes. (I sent a mail about it yesterday to discuss-
| |
31 if ((result_frame->num_channels_ > 2) || (result_frame->num_channels_ < 1)) | |
32 return; | |
33 if (result_frame->num_channels_ != frame_to_add.num_channels_) | |
34 return; | |
35 | |
36 bool noPrevData = false; | |
hlundin-webrtc
2016/11/30 11:24:36
no_previous_data
| |
37 if (result_frame->samples_per_channel_ != frame_to_add.samples_per_channel_) { | |
38 if (result_frame->samples_per_channel_ == 0) { | |
39 // special case we have no data to start with | |
hlundin-webrtc
2016/11/30 11:24:36
Capital start and end with '.'
| |
40 result_frame->samples_per_channel_ = frame_to_add.samples_per_channel_; | |
41 noPrevData = true; | |
42 } else { | |
43 return; | |
44 } | |
45 } | |
46 | |
47 if ((result_frame->vad_activity_ == AudioFrame::kVadActive) || | |
48 frame_to_add.vad_activity_ == result_frame->kVadActive) { | |
kwiberg-webrtc
2016/11/30 09:38:34
Remove one layer of parentheses, since the relativ
aleloi
2016/11/30 10:11:48
Done.
| |
49 result_frame->vad_activity_ = AudioFrame::kVadActive; | |
50 } else if (result_frame->vad_activity_ == AudioFrame::kVadUnknown || | |
51 frame_to_add.vad_activity_ == AudioFrame::kVadUnknown) { | |
52 result_frame->vad_activity_ = AudioFrame::kVadUnknown; | |
53 } | |
54 | |
55 if (result_frame->speech_type_ != frame_to_add.speech_type_) | |
56 result_frame->speech_type_ = AudioFrame::kUndefined; | |
57 | |
58 if (noPrevData) { | |
59 std::copy(frame_to_add.data_, frame_to_add.data_ + | |
60 frame_to_add.samples_per_channel_ * | |
61 result_frame->num_channels_, | |
62 result_frame->data_); | |
63 } else { | |
64 for (size_t i = 0; | |
65 i < result_frame->samples_per_channel_ * result_frame->num_channels_; | |
66 i++) { | |
67 int32_t wrap_guard = static_cast<int32_t>(result_frame->data_[i]) + | |
kwiberg-webrtc
2016/11/30 09:38:34
const
aleloi
2016/11/30 10:11:48
Done.
| |
68 static_cast<int32_t>(frame_to_add.data_[i]); | |
69 result_frame->data_[i] = ClampToInt16(wrap_guard); | |
70 } | |
71 } | |
72 return; | |
73 } | |
23 | 74 |
24 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, | 75 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, |
25 size_t samples_per_channel, | 76 size_t samples_per_channel, |
26 int16_t* dst_audio) { | 77 int16_t* dst_audio) { |
27 for (size_t i = 0; i < samples_per_channel; i++) { | 78 for (size_t i = 0; i < samples_per_channel; i++) { |
28 dst_audio[2 * i] = src_audio[i]; | 79 dst_audio[2 * i] = src_audio[i]; |
29 dst_audio[2 * i + 1] = src_audio[i]; | 80 dst_audio[2 * i + 1] = src_audio[i]; |
30 } | 81 } |
31 } | 82 } |
32 | 83 |
(...skipping 28 matching lines...) Expand all Loading... | |
61 return -1; | 112 return -1; |
62 } | 113 } |
63 | 114 |
64 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); | 115 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); |
65 frame->num_channels_ = 1; | 116 frame->num_channels_ = 1; |
66 | 117 |
67 return 0; | 118 return 0; |
68 } | 119 } |
69 | 120 |
70 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { | 121 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { |
71 if (frame->num_channels_ != 2) return; | 122 if (frame->num_channels_ != 2) |
123 return; | |
72 | 124 |
73 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { | 125 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { |
74 int16_t temp_data = frame->data_[i]; | 126 int16_t temp_data = frame->data_[i]; |
75 frame->data_[i] = frame->data_[i + 1]; | 127 frame->data_[i] = frame->data_[i + 1]; |
76 frame->data_[i + 1] = temp_data; | 128 frame->data_[i + 1] = temp_data; |
77 } | 129 } |
78 } | 130 } |
79 | 131 |
80 void AudioFrameOperations::Mute(AudioFrame* frame, bool previous_frame_muted, | 132 void AudioFrameOperations::Mute(AudioFrame* frame, |
133 bool previous_frame_muted, | |
81 bool current_frame_muted) { | 134 bool current_frame_muted) { |
82 RTC_DCHECK(frame); | 135 RTC_DCHECK(frame); |
83 if (!previous_frame_muted && !current_frame_muted) { | 136 if (!previous_frame_muted && !current_frame_muted) { |
84 // Not muted, don't touch. | 137 // Not muted, don't touch. |
85 } else if (previous_frame_muted && current_frame_muted) { | 138 } else if (previous_frame_muted && current_frame_muted) { |
86 // Frame fully muted. | 139 // Frame fully muted. |
87 size_t total_samples = frame->samples_per_channel_ * frame->num_channels_; | 140 size_t total_samples = frame->samples_per_channel_ * frame->num_channels_; |
88 RTC_DCHECK_GE(AudioFrame::kMaxDataSizeSamples, total_samples); | 141 RTC_DCHECK_GE(AudioFrame::kMaxDataSizeSamples, total_samples); |
89 memset(frame->data_, 0, sizeof(frame->data_[0]) * total_samples); | 142 memset(frame->data_, 0, sizeof(frame->data_[0]) * total_samples); |
90 } else { | 143 } else { |
(...skipping 27 matching lines...) Expand all Loading... | |
118 for (size_t j = 0; j < channels; ++j) { | 171 for (size_t j = 0; j < channels; ++j) { |
119 float g = start_g; | 172 float g = start_g; |
120 for (size_t i = start * channels; i < end * channels; i += channels) { | 173 for (size_t i = start * channels; i < end * channels; i += channels) { |
121 g += inc; | 174 g += inc; |
122 frame->data_[i + j] *= g; | 175 frame->data_[i + j] *= g; |
123 } | 176 } |
124 } | 177 } |
125 } | 178 } |
126 } | 179 } |
127 | 180 |
181 void AudioFrameOperations::Mute(AudioFrame* frame) { | |
182 Mute(frame, true, true); | |
183 } | |
184 | |
185 void AudioFrameOperations::ApplyHalfGain(AudioFrame* frame) { | |
186 RTC_DCHECK_GT(frame->num_channels_, 0u); | |
187 RTC_DCHECK_LT(frame->num_channels_, 3u); | |
kwiberg-webrtc
2016/11/30 09:38:34
Remove "u"s.
aleloi
2016/11/30 10:11:48
Done.
| |
188 if ((frame->num_channels_ > 2) || (frame->num_channels_ < 1)) | |
kwiberg-webrtc
2016/11/30 09:38:34
Remove the extra parentheses.
aleloi
2016/11/30 10:11:48
Done.
hlundin-webrtc
2016/11/30 11:24:36
Again, does this function really need the restrict
aleloi
2016/11/30 11:33:38
No, there is no such assumption here either.
| |
189 return; | |
190 | |
191 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_; | |
192 i++) { | |
193 frame->data_[i] = static_cast<int16_t>(frame->data_[i] >> 1); | |
194 } | |
195 } | |
196 | |
128 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { | 197 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { |
129 if (frame.num_channels_ != 2) { | 198 if (frame.num_channels_ != 2) { |
130 return -1; | 199 return -1; |
131 } | 200 } |
132 | 201 |
133 for (size_t i = 0; i < frame.samples_per_channel_; i++) { | 202 for (size_t i = 0; i < frame.samples_per_channel_; i++) { |
134 frame.data_[2 * i] = | 203 frame.data_[2 * i] = static_cast<int16_t>(left * frame.data_[2 * i]); |
135 static_cast<int16_t>(left * frame.data_[2 * i]); | |
136 frame.data_[2 * i + 1] = | 204 frame.data_[2 * i + 1] = |
137 static_cast<int16_t>(right * frame.data_[2 * i + 1]); | 205 static_cast<int16_t>(right * frame.data_[2 * i + 1]); |
138 } | 206 } |
139 return 0; | 207 return 0; |
140 } | 208 } |
141 | 209 |
142 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { | 210 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { |
143 int32_t temp_data = 0; | 211 int32_t temp_data = 0; |
144 | 212 |
145 // Ensure that the output result is saturated [-32768, +32767]. | 213 // Ensure that the output result is saturated [-32768, +32767]. |
146 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_; | 214 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_; |
147 i++) { | 215 i++) { |
148 temp_data = static_cast<int32_t>(scale * frame.data_[i]); | 216 temp_data = static_cast<int32_t>(scale * frame.data_[i]); |
149 if (temp_data < -32768) { | 217 if (temp_data < -32768) { |
150 frame.data_[i] = -32768; | 218 frame.data_[i] = -32768; |
151 } else if (temp_data > 32767) { | 219 } else if (temp_data > 32767) { |
152 frame.data_[i] = 32767; | 220 frame.data_[i] = 32767; |
153 } else { | 221 } else { |
154 frame.data_[i] = static_cast<int16_t>(temp_data); | 222 frame.data_[i] = static_cast<int16_t>(temp_data); |
155 } | 223 } |
156 } | 224 } |
157 return 0; | 225 return 0; |
158 } | 226 } |
159 | 227 |
228 int16_t ClampToInt16(int32_t input) { | |
229 if (input < -0x00008000) { | |
230 return -0x8000; | |
231 } else if (input > 0x00007FFF) { | |
232 return 0x7FFF; | |
233 } else { | |
234 return static_cast<int16_t>(input); | |
235 } | |
236 } | |
kwiberg-webrtc
2016/11/30 09:38:34
Instead of defining this function, use rtc::satura
aleloi
2016/11/30 10:11:48
Done. Great that we have saturated_cast! I missed
kwiberg-webrtc
2016/11/30 11:32:44
Except for the name---it should have been "saturat
| |
237 | |
160 } // namespace webrtc | 238 } // namespace webrtc |
OLD | NEW |