Chromium Code Reviews| Index: webrtc/audio/utility/audio_frame_operations.cc |
| diff --git a/webrtc/modules/utility/source/audio_frame_operations.cc b/webrtc/audio/utility/audio_frame_operations.cc |
| similarity index 62% |
| rename from webrtc/modules/utility/source/audio_frame_operations.cc |
| rename to webrtc/audio/utility/audio_frame_operations.cc |
| index 102407d0f0f0fd223f6eaf9836dfa2b18278a5f7..7cd19b07e22887741f62b157cfd69f1b36f57a9b 100644 |
| --- a/webrtc/modules/utility/source/audio_frame_operations.cc |
| +++ b/webrtc/audio/utility/audio_frame_operations.cc |
| @@ -8,18 +8,75 @@ |
| * be found in the AUTHORS file in the root of the source tree. |
| */ |
| -#include "webrtc/modules/include/module_common_types.h" |
| -#include "webrtc/modules/utility/include/audio_frame_operations.h" |
| +#include <algorithm> |
|
the sun
2016/12/02 13:39:55
order!
aleloi
2016/12/05 09:18:37
Done.
|
| + |
| +#include "webrtc/audio/utility/audio_frame_operations.h" |
| + |
| #include "webrtc/base/checks.h" |
| +#include "webrtc/base/safe_conversions.h" |
| +#include "webrtc/modules/include/module_common_types.h" |
| namespace webrtc { |
| -namespace { |
| +namespace { |
| // 2.7ms @ 48kHz, 4ms @ 32kHz, 8ms @ 16kHz. |
| const size_t kMuteFadeFrames = 128; |
| const float kMuteFadeInc = 1.0f / kMuteFadeFrames; |
| -} // namespace { |
| +} // namespace |
| + |
| +void AudioFrameOperations::Add(const AudioFrame& frame_to_add, |
| + AudioFrame* result_frame) { |
| + // Sanity check. |
| + RTC_DCHECK(result_frame); |
| + RTC_DCHECK_GT(result_frame->num_channels_, 0); |
| + RTC_DCHECK_EQ(result_frame->num_channels_, frame_to_add.num_channels_); |
| + if (result_frame->num_channels_ < 1) { |
| + return; |
| + } |
| + if (result_frame->num_channels_ != frame_to_add.num_channels_) { |
| + return; |
| + } |
| + |
| + bool no_previous_data = false; |
| + if (result_frame->samples_per_channel_ != frame_to_add.samples_per_channel_) { |
| + RTC_DCHECK_EQ(result_frame->samples_per_channel_, 0); |
| + if (result_frame->samples_per_channel_ == 0) { |
|
the sun
2016/12/02 13:39:55
No need for this conditional handling now that you
aleloi
2016/12/05 09:18:37
Done.
|
| + // Special case we have no data to start with. |
| + result_frame->samples_per_channel_ = frame_to_add.samples_per_channel_; |
| + no_previous_data = true; |
| + } else { |
| + return; |
| + } |
| + } |
| + |
| + if (result_frame->vad_activity_ == AudioFrame::kVadActive || |
| + frame_to_add.vad_activity_ == AudioFrame::kVadActive) { |
| + result_frame->vad_activity_ = AudioFrame::kVadActive; |
| + } else if (result_frame->vad_activity_ == AudioFrame::kVadUnknown || |
| + frame_to_add.vad_activity_ == AudioFrame::kVadUnknown) { |
| + result_frame->vad_activity_ = AudioFrame::kVadUnknown; |
| + } |
| + |
| + if (result_frame->speech_type_ != frame_to_add.speech_type_) |
| + result_frame->speech_type_ = AudioFrame::kUndefined; |
| + |
| + if (no_previous_data) { |
| + std::copy(frame_to_add.data_, frame_to_add.data_ + |
| + frame_to_add.samples_per_channel_ * |
| + result_frame->num_channels_, |
| + result_frame->data_); |
| + } else { |
| + for (size_t i = 0; |
| + i < result_frame->samples_per_channel_ * result_frame->num_channels_; |
| + i++) { |
| + const int32_t wrap_guard = static_cast<int32_t>(result_frame->data_[i]) + |
| + static_cast<int32_t>(frame_to_add.data_[i]); |
| + result_frame->data_[i] = rtc::saturated_cast<int16_t>(wrap_guard); |
| + } |
| + } |
| + return; |
| +} |
| void AudioFrameOperations::MonoToStereo(const int16_t* src_audio, |
| size_t samples_per_channel, |
| @@ -68,7 +125,10 @@ int AudioFrameOperations::StereoToMono(AudioFrame* frame) { |
| } |
| void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { |
| - if (frame->num_channels_ != 2) return; |
| + RTC_DCHECK(frame); |
| + if (frame->num_channels_ != 2) { |
| + return; |
| + } |
| for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { |
| int16_t temp_data = frame->data_[i]; |
| @@ -77,7 +137,8 @@ void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { |
| } |
| } |
| -void AudioFrameOperations::Mute(AudioFrame* frame, bool previous_frame_muted, |
| +void AudioFrameOperations::Mute(AudioFrame* frame, |
| + bool previous_frame_muted, |
| bool current_frame_muted) { |
| RTC_DCHECK(frame); |
| if (!previous_frame_muted && !current_frame_muted) { |
| @@ -125,14 +186,29 @@ void AudioFrameOperations::Mute(AudioFrame* frame, bool previous_frame_muted, |
| } |
| } |
| +void AudioFrameOperations::Mute(AudioFrame* frame) { |
| + Mute(frame, true, true); |
| +} |
| + |
| +void AudioFrameOperations::ApplyHalfGain(AudioFrame* frame) { |
|
the sun
2016/12/02 13:39:55
DCHECK(frame) since you deref it, not just pass on
aleloi
2016/12/05 09:18:37
Done.
|
| + RTC_DCHECK_GT(frame->num_channels_, 0); |
| + if (frame->num_channels_ < 1) { |
| + return; |
| + } |
| + |
| + for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_; |
| + i++) { |
| + frame->data_[i] = frame->data_[i] >> 1; |
| + } |
| +} |
| + |
| int AudioFrameOperations::Scale(float left, float right, AudioFrame& frame) { |
| if (frame.num_channels_ != 2) { |
| return -1; |
| } |
| for (size_t i = 0; i < frame.samples_per_channel_; i++) { |
| - frame.data_[2 * i] = |
| - static_cast<int16_t>(left * frame.data_[2 * i]); |
| + frame.data_[2 * i] = static_cast<int16_t>(left * frame.data_[2 * i]); |
| frame.data_[2 * i + 1] = |
| static_cast<int16_t>(right * frame.data_[2 * i + 1]); |
| } |
| @@ -156,5 +232,4 @@ int AudioFrameOperations::ScaleWithSat(float scale, AudioFrame& frame) { |
| } |
| return 0; |
| } |
| - |
| } // namespace webrtc |