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" |
hlundin-webrtc
2016/12/01 09:44:51
Order of includes is wrong.
| |
12 #include "webrtc/modules/utility/include/audio_frame_operations.h" | |
13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/safe_conversions.h" | |
14 | 17 |
15 namespace webrtc { | 18 namespace webrtc { |
16 namespace { | 19 namespace { |
17 | 20 |
18 // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz. | 21 // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz. |
19 const size_t kMuteFadeFrames = 128; | 22 const size_t kMuteFadeFrames = 128; |
20 const float kMuteFadeInc = 1.0f / kMuteFadeFrames; | 23 const float kMuteFadeInc = 1.0f / kMuteFadeFrames; |
21 | 24 |
22 } // namespace { | 25 } // namespace |
26 | |
27 void AudioFrameOperations::Add(const AudioFrame& frame_to_add, | |
28 AudioFrame* result_frame) { | |
29 // Sanity check. | |
the sun
2016/12/01 20:32:46
RTC_DCHECK(result_frame);
| |
30 RTC_DCHECK_GT(result_frame->num_channels_, 0); | |
31 if (result_frame->num_channels_ < 1) | |
32 return; | |
33 if (result_frame->num_channels_ != frame_to_add.num_channels_) | |
34 return; | |
35 | |
36 bool no_previous_data = false; | |
37 if (result_frame->samples_per_channel_ != frame_to_add.samples_per_channel_) { | |
the sun
2016/12/01 20:32:45
Could this be a DCHECK instead?
It seems what we
aleloi
2016/12/02 12:13:35
This function is called with samples_per_channel_=
| |
38 if (result_frame->samples_per_channel_ == 0) { | |
39 // Special case we have no data to start with. | |
40 result_frame->samples_per_channel_ = frame_to_add.samples_per_channel_; | |
41 no_previous_data = 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) { | |
the sun
2016/12/01 20:32:46
result_frame->kVadActive should be AudioFrame::kVa
aleloi
2016/12/02 12:13:35
Thanks! 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 (no_previous_data) { | |
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 const int32_t wrap_guard = static_cast<int32_t>(result_frame->data_[i]) + | |
68 static_cast<int32_t>(frame_to_add.data_[i]); | |
69 result_frame->data_[i] = rtc::saturated_cast<int16_t>(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 27 matching lines...) Expand all Loading... | |
60 if (frame->num_channels_ != 2) { | 111 if (frame->num_channels_ != 2) { |
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) { |
the sun
2016/12/01 20:32:46
RTC_DCHECK(frame);
| |
71 if (frame->num_channels_ != 2) return; | 122 if (frame->num_channels_ != 2) |
the sun
2016/12/01 20:32:46
nit: it looks like code previously in this file us
aleloi
2016/12/02 12:13:35
Done.
| |
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_, 0); | |
187 if (frame->num_channels_ < 1) | |
188 return; | |
189 | |
190 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_; | |
191 i++) { | |
192 frame->data_[i] = static_cast<int16_t>(frame->data_[i] >> 1); | |
the sun
2016/12/01 20:32:46
Why the need to cast an int16_t to int16_t?
aleloi
2016/12/02 12:13:35
That's strange. Removed. I've not removed this sam
| |
193 } | |
194 } | |
195 | |
128 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { | 196 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { |
129 if (frame.num_channels_ != 2) { | 197 if (frame.num_channels_ != 2) { |
130 return -1; | 198 return -1; |
131 } | 199 } |
132 | 200 |
133 for (size_t i = 0; i < frame.samples_per_channel_; i++) { | 201 for (size_t i = 0; i < frame.samples_per_channel_; i++) { |
134 frame.data_[2 * i] = | 202 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] = | 203 frame.data_[2 * i + 1] = |
137 static_cast<int16_t>(right * frame.data_[2 * i + 1]); | 204 static_cast<int16_t>(right * frame.data_[2 * i + 1]); |
138 } | 205 } |
139 return 0; | 206 return 0; |
140 } | 207 } |
141 | 208 |
142 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { | 209 int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { |
143 int32_t temp_data = 0; | 210 int32_t temp_data = 0; |
144 | 211 |
145 // Ensure that the output result is saturated [-32768, +32767]. | 212 // Ensure that the output result is saturated [-32768, +32767]. |
146 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_; | 213 for (size_t i = 0; i < frame.samples_per_channel_ * frame.num_channels_; |
147 i++) { | 214 i++) { |
148 temp_data = static_cast<int32_t>(scale * frame.data_[i]); | 215 temp_data = static_cast<int32_t>(scale * frame.data_[i]); |
149 if (temp_data < -32768) { | 216 if (temp_data < -32768) { |
150 frame.data_[i] = -32768; | 217 frame.data_[i] = -32768; |
151 } else if (temp_data > 32767) { | 218 } else if (temp_data > 32767) { |
152 frame.data_[i] = 32767; | 219 frame.data_[i] = 32767; |
153 } else { | 220 } else { |
154 frame.data_[i] = static_cast<int16_t>(temp_data); | 221 frame.data_[i] = static_cast<int16_t>(temp_data); |
155 } | 222 } |
156 } | 223 } |
157 return 0; | 224 return 0; |
158 } | 225 } |
159 | |
160 } // namespace webrtc | 226 } // namespace webrtc |
OLD | NEW |