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 "webrtc/audio/utility/audio_frame_operations.h" | 11 #include "webrtc/audio/utility/audio_frame_operations.h" |
12 #include "webrtc/base/checks.h" | 12 #include "webrtc/base/checks.h" |
13 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h" | 13 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h" |
14 #include "webrtc/modules/include/module_common_types.h" | 14 #include "webrtc/modules/include/module_common_types.h" |
15 | 15 |
16 namespace webrtc { | 16 namespace webrtc { |
17 | 17 |
18 uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) { | 18 uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) { |
19 uint32_t energy = 0; | 19 uint32_t energy = 0; |
20 for (size_t position = 0; position < audio_frame.samples_per_channel_; | 20 if (!audio_frame.muted()) { |
hlundin-webrtc
2017/03/17 14:29:39
Early return 0 when muted.
yujo
2017/03/17 23:55:54
Done.
| |
21 position++) { | 21 const int16_t* frame_data = audio_frame.data(); |
22 // TODO(aleloi): This can overflow. Convert to floats. | 22 for (size_t position = 0; position < audio_frame.samples_per_channel_; |
23 energy += audio_frame.data_[position] * audio_frame.data_[position]; | 23 position++) { |
24 // TODO(aleloi): This can overflow. Convert to floats. | |
25 energy += frame_data[position] * frame_data[position]; | |
26 } | |
24 } | 27 } |
25 return energy; | 28 return energy; |
26 } | 29 } |
27 | 30 |
28 void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) { | 31 void Ramp(float start_gain, float target_gain, AudioFrame* audio_frame) { |
29 RTC_DCHECK(audio_frame); | 32 RTC_DCHECK(audio_frame); |
30 RTC_DCHECK_GE(start_gain, 0.0f); | 33 RTC_DCHECK_GE(start_gain, 0.0f); |
31 RTC_DCHECK_GE(target_gain, 0.0f); | 34 RTC_DCHECK_GE(target_gain, 0.0f); |
32 if (start_gain == target_gain) { | 35 if (start_gain == target_gain || audio_frame->muted()) { |
33 return; | 36 return; |
34 } | 37 } |
35 | 38 |
36 size_t samples = audio_frame->samples_per_channel_; | 39 size_t samples = audio_frame->samples_per_channel_; |
37 RTC_DCHECK_LT(0, samples); | 40 RTC_DCHECK_LT(0, samples); |
38 float increment = (target_gain - start_gain) / samples; | 41 float increment = (target_gain - start_gain) / samples; |
39 float gain = start_gain; | 42 float gain = start_gain; |
43 int16_t* frame_data = audio_frame->mutable_data(); | |
40 for (size_t i = 0; i < samples; ++i) { | 44 for (size_t i = 0; i < samples; ++i) { |
41 // If the audio is interleaved of several channels, we want to | 45 // If the audio is interleaved of several channels, we want to |
42 // apply the same gain change to the ith sample of every channel. | 46 // apply the same gain change to the ith sample of every channel. |
43 for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) { | 47 for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) { |
44 audio_frame->data_[audio_frame->num_channels_ * i + ch] *= gain; | 48 frame_data[audio_frame->num_channels_ * i + ch] *= gain; |
45 } | 49 } |
46 gain += increment; | 50 gain += increment; |
47 } | 51 } |
48 } | 52 } |
49 | 53 |
50 void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) { | 54 void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) { |
51 RTC_DCHECK_GE(target_number_of_channels, 1); | 55 RTC_DCHECK_GE(target_number_of_channels, 1); |
52 RTC_DCHECK_LE(target_number_of_channels, 2); | 56 RTC_DCHECK_LE(target_number_of_channels, 2); |
53 if (frame->num_channels_ == 1 && target_number_of_channels == 2) { | 57 if (frame->num_channels_ == 1 && target_number_of_channels == 2) { |
54 AudioFrameOperations::MonoToStereo(frame); | 58 AudioFrameOperations::MonoToStereo(frame); |
55 } else if (frame->num_channels_ == 2 && target_number_of_channels == 1) { | 59 } else if (frame->num_channels_ == 2 && target_number_of_channels == 1) { |
56 AudioFrameOperations::StereoToMono(frame); | 60 AudioFrameOperations::StereoToMono(frame); |
57 } | 61 } |
58 } | 62 } |
59 } // namespace webrtc | 63 } // namespace webrtc |
OLD | NEW |