Chromium Code Reviews| 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 | |
| 11 #include "webrtc/modules/include/module_common_types.h" | 13 #include "webrtc/modules/include/module_common_types.h" |
| 12 #include "webrtc/modules/utility/include/audio_frame_operations.h" | 14 #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 { |
| 23 | 25 |
|
aleloi
2016/10/18 11:38:04
All implementations are copied from AudioFrame wit
the sun
2016/10/19 09:23:17
Can we just make it "Add()" or "AddTo()"? Or you c
aleloi
2016/10/20 08:27:06
Done.
| |
| 26 void AudioFrameOperations::AddFrames(const AudioFrame& frame_to_add, | |
| 27 AudioFrame* result_frame) { | |
| 28 // Sanity check | |
| 29 RTC_DCHECK_GT(result_frame->num_channels_, 0u); | |
| 30 RTC_DCHECK_LT(result_frame->num_channels_, 3u); | |
| 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; | |
| 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 | |
| 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) { | |
| 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 // IMPROVEMENT this can be done very fast in assembly | |
|
the sun
2016/10/19 09:23:17
I think you can remove the comment
aleloi
2016/10/20 08:27:06
Done.
| |
| 65 for (size_t i = 0; | |
| 66 i < result_frame->samples_per_channel_ * result_frame->num_channels_; | |
| 67 i++) { | |
| 68 int32_t wrap_guard = static_cast<int32_t>(result_frame->data_[i]) + | |
| 69 static_cast<int32_t>(frame_to_add.data_[i]); | |
| 70 result_frame->data_[i] = ClampToInt16(wrap_guard); | |
| 71 } | |
| 72 } | |
| 73 return; | |
| 74 } | |
| 75 | |
| 24 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, | 76 void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, |
| 25 size_t samples_per_channel, | 77 size_t samples_per_channel, |
| 26 int16_t* dst_audio) { | 78 int16_t* dst_audio) { |
| 27 for (size_t i = 0; i < samples_per_channel; i++) { | 79 for (size_t i = 0; i < samples_per_channel; i++) { |
| 28 dst_audio[2 * i] = src_audio[i]; | 80 dst_audio[2 * i] = src_audio[i]; |
| 29 dst_audio[2 * i + 1] = src_audio[i]; | 81 dst_audio[2 * i + 1] = src_audio[i]; |
| 30 } | 82 } |
| 31 } | 83 } |
| 32 | 84 |
| 33 int AudioFrameOperations::MonoToStereo(AudioFrame* frame) { | 85 int AudioFrameOperations::MonoToStereo(AudioFrame* frame) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 for (size_t j = 0; j < channels; ++j) { | 170 for (size_t j = 0; j < channels; ++j) { |
| 119 float g = start_g; | 171 float g = start_g; |
| 120 for (size_t i = start * channels; i < end * channels; i += channels) { | 172 for (size_t i = start * channels; i < end * channels; i += channels) { |
| 121 g += inc; | 173 g += inc; |
| 122 frame->data_[i + j] *= g; | 174 frame->data_[i + j] *= g; |
| 123 } | 175 } |
| 124 } | 176 } |
| 125 } | 177 } |
| 126 } | 178 } |
| 127 | 179 |
| 180 void AudioFrameOperations::Mute(AudioFrame* frame) { | |
| 181 Mute(frame, true, true); | |
| 182 } | |
| 183 | |
| 184 void AudioFrameOperations::Reset(AudioFrame* frame) { | |
| 185 frame->id_ = -1; | |
|
the sun
2016/10/19 09:23:17
It is dangerous to have the default values in mult
aleloi
2016/10/20 08:27:06
I think it might lead to a (barely noticeable) per
the sun
2016/10/20 08:37:57
Acknowledged.
| |
| 186 // TODO(wu): Zero is a valid value for |timestamp_|. We should initialize | |
| 187 // to an invalid value, or add a new member to indicate invalidity. | |
| 188 frame->timestamp_ = 0; | |
| 189 frame->elapsed_time_ms_ = -1; | |
| 190 frame->ntp_time_ms_ = -1; | |
| 191 frame->samples_per_channel_ = 0; | |
| 192 frame->sample_rate_hz_ = 0; | |
| 193 frame->num_channels_ = 0; | |
| 194 frame->speech_type_ = AudioFrame::kUndefined; | |
| 195 frame->vad_activity_ = AudioFrame::kVadUnknown; | |
| 196 } | |
| 197 | |
| 198 void AudioFrameOperations::ShiftDown(AudioFrame* frame) { | |
| 199 RTC_DCHECK_GT(frame->num_channels_, 0u); | |
| 200 RTC_DCHECK_LT(frame->num_channels_, 3u); | |
| 201 if ((frame->num_channels_ > 2) || (frame->num_channels_ < 1)) | |
| 202 return; | |
| 203 | |
| 204 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_; | |
| 205 i++) { | |
| 206 frame->data_[i] = static_cast<int16_t>(frame->data_[i] >> 1); | |
| 207 } | |
| 208 } | |
| 209 | |
| 128 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { | 210 int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { |
| 129 if (frame.num_channels_ != 2) { | 211 if (frame.num_channels_ != 2) { |
| 130 return -1; | 212 return -1; |
| 131 } | 213 } |
| 132 | 214 |
| 133 for (size_t i = 0; i < frame.samples_per_channel_; i++) { | 215 for (size_t i = 0; i < frame.samples_per_channel_; i++) { |
| 134 frame.data_[2 * i] = | 216 frame.data_[2 * i] = |
| 135 static_cast<int16_t>(left * frame.data_[2 * i]); | 217 static_cast<int16_t>(left * frame.data_[2 * i]); |
| 136 frame.data_[2 * i + 1] = | 218 frame.data_[2 * i + 1] = |
| 137 static_cast<int16_t>(right * frame.data_[2 * i + 1]); | 219 static_cast<int16_t>(right * frame.data_[2 * i + 1]); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 150 frame.data_[i] = -32768; | 232 frame.data_[i] = -32768; |
| 151 } else if (temp_data > 32767) { | 233 } else if (temp_data > 32767) { |
| 152 frame.data_[i] = 32767; | 234 frame.data_[i] = 32767; |
| 153 } else { | 235 } else { |
| 154 frame.data_[i] = static_cast<int16_t>(temp_data); | 236 frame.data_[i] = static_cast<int16_t>(temp_data); |
| 155 } | 237 } |
| 156 } | 238 } |
| 157 return 0; | 239 return 0; |
| 158 } | 240 } |
| 159 | 241 |
| 242 void AudioFrameOperations::UpdateFrame(int id, | |
| 243 uint32_t timestamp, | |
| 244 const int16_t* data, | |
| 245 size_t samples_per_channel, | |
| 246 int sample_rate_hz, | |
| 247 AudioFrame::SpeechType speech_type, | |
| 248 AudioFrame::VADActivity vad_activity, | |
| 249 size_t num_channels, | |
| 250 AudioFrame* frame) { | |
| 251 frame->id_ = id; | |
| 252 frame->timestamp_ = timestamp; | |
| 253 frame->samples_per_channel_ = samples_per_channel; | |
| 254 frame->sample_rate_hz_ = sample_rate_hz; | |
| 255 frame->speech_type_ = speech_type; | |
| 256 frame->vad_activity_ = vad_activity; | |
| 257 frame->num_channels_ = num_channels; | |
| 258 | |
| 259 const size_t length = samples_per_channel * num_channels; | |
| 260 RTC_DCHECK_LT(length, AudioFrame::kMaxDataSizeSamples); | |
| 261 if (data != nullptr) { | |
| 262 std::copy(data, data + length, frame->data_); | |
| 263 } else { | |
| 264 std::fill(frame->data_, frame->data_ + length, 0); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 int16_t ClampToInt16(int32_t input) { | |
| 269 if (input < -0x00008000) { | |
| 270 return -0x8000; | |
| 271 } else if (input > 0x00007FFF) { | |
| 272 return 0x7FFF; | |
| 273 } else { | |
| 274 return static_cast<int16_t>(input); | |
| 275 } | |
| 276 } | |
| 277 | |
| 160 } // namespace webrtc | 278 } // namespace webrtc |
| OLD | NEW |