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/base/checks.h" | 11 #include "webrtc/base/checks.h" |
12 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h" | 12 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h" |
13 #include "webrtc/modules/include/module_common_types.h" | 13 #include "webrtc/modules/include/module_common_types.h" |
14 #include "webrtc/modules/utility/include/audio_frame_operations.h" | 14 #include "webrtc/modules/utility/include/audio_frame_operations.h" |
15 #include "webrtc/typedefs.h" | 15 #include "webrtc/typedefs.h" |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 namespace { | |
19 // Linear ramping over 80 samples. | |
20 // TODO(hellner): ramp using fix point? | |
21 const float kRampArray[] = { | |
22 0.0000f, 0.0127f, 0.0253f, 0.0380f, 0.0506f, 0.0633f, 0.0759f, 0.0886f, | |
23 0.1013f, 0.1139f, 0.1266f, 0.1392f, 0.1519f, 0.1646f, 0.1772f, 0.1899f, | |
24 0.2025f, 0.2152f, 0.2278f, 0.2405f, 0.2532f, 0.2658f, 0.2785f, 0.2911f, | |
25 0.3038f, 0.3165f, 0.3291f, 0.3418f, 0.3544f, 0.3671f, 0.3797f, 0.3924f, | |
26 0.4051f, 0.4177f, 0.4304f, 0.4430f, 0.4557f, 0.4684f, 0.4810f, 0.4937f, | |
27 0.5063f, 0.5190f, 0.5316f, 0.5443f, 0.5570f, 0.5696f, 0.5823f, 0.5949f, | |
28 0.6076f, 0.6203f, 0.6329f, 0.6456f, 0.6582f, 0.6709f, 0.6835f, 0.6962f, | |
29 0.7089f, 0.7215f, 0.7342f, 0.7468f, 0.7595f, 0.7722f, 0.7848f, 0.7975f, | |
30 0.8101f, 0.8228f, 0.8354f, 0.8481f, 0.8608f, 0.8734f, 0.8861f, 0.8987f, | |
31 0.9114f, 0.9241f, 0.9367f, 0.9494f, 0.9620f, 0.9747f, 0.9873f, 1.0000f}; | |
32 const size_t kRampSize = sizeof(kRampArray) / sizeof(kRampArray[0]); | |
33 } // namespace | |
34 | 18 |
35 uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) { | 19 uint32_t AudioMixerCalculateEnergy(const AudioFrame& audio_frame) { |
36 uint32_t energy = 0; | 20 uint32_t energy = 0; |
37 for (size_t position = 0; position < audio_frame.samples_per_channel_; | 21 for (size_t position = 0; position < audio_frame.samples_per_channel_; |
38 position++) { | 22 position++) { |
39 // TODO(aleloi): This can overflow. Convert to floats. | 23 // TODO(aleloi): This can overflow. Convert to floats. |
40 energy += audio_frame.data_[position] * audio_frame.data_[position]; | 24 energy += audio_frame.data_[position] * audio_frame.data_[position]; |
41 } | 25 } |
42 return energy; | 26 return energy; |
43 } | 27 } |
44 | 28 |
45 void NewMixerRampIn(AudioFrame* audio_frame) { | 29 void Ramp(float current, float target, AudioFrame* audio_frame) { |
46 assert(kRampSize <= audio_frame->samples_per_channel_); | 30 RTC_DCHECK(audio_frame); |
47 for (size_t i = 0; i < kRampSize; i++) { | 31 RTC_DCHECK_GE(current, 0.0f); |
48 audio_frame->data_[i] = | 32 RTC_DCHECK_GE(target, 0.0f); |
49 static_cast<int16_t>(kRampArray[i] * audio_frame->data_[i]); | 33 |
| 34 size_t samples = audio_frame->samples_per_channel_; |
| 35 RTC_DCHECK_LT(0u, samples); |
| 36 float increment = (target - current) / samples; |
| 37 float gain = current; |
| 38 for (size_t i = 0; i < samples; ++i) { |
| 39 // If the audio is interleaved of several channels, we want to |
| 40 // apply the same gain change to the ith sample of every channel. |
| 41 for (size_t ch = 0; ch < audio_frame->num_channels_; ++ch) { |
| 42 audio_frame->data_[audio_frame->num_channels_ * i + ch] *= gain; |
| 43 } |
| 44 gain += increment; |
50 } | 45 } |
51 } | 46 } |
52 | 47 |
53 void NewMixerRampOut(AudioFrame* audio_frame) { | |
54 assert(kRampSize <= audio_frame->samples_per_channel_); | |
55 for (size_t i = 0; i < kRampSize; i++) { | |
56 const size_t kRampPos = kRampSize - 1 - i; | |
57 audio_frame->data_[i] = | |
58 static_cast<int16_t>(kRampArray[kRampPos] * audio_frame->data_[i]); | |
59 } | |
60 memset(&audio_frame->data_[kRampSize], 0, | |
61 (audio_frame->samples_per_channel_ - kRampSize) * | |
62 sizeof(audio_frame->data_[0])); | |
63 } | |
64 | |
65 void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) { | 48 void RemixFrame(size_t target_number_of_channels, AudioFrame* frame) { |
66 RTC_DCHECK_GE(target_number_of_channels, static_cast<size_t>(1)); | 49 RTC_DCHECK_GE(target_number_of_channels, 1u); |
67 RTC_DCHECK_LE(target_number_of_channels, static_cast<size_t>(2)); | 50 RTC_DCHECK_LE(target_number_of_channels, 2u); |
68 if (frame->num_channels_ == 1 && target_number_of_channels == 2) { | 51 if (frame->num_channels_ == 1 && target_number_of_channels == 2) { |
69 AudioFrameOperations::MonoToStereo(frame); | 52 AudioFrameOperations::MonoToStereo(frame); |
70 } else if (frame->num_channels_ == 2 && target_number_of_channels == 1) { | 53 } else if (frame->num_channels_ == 2 && target_number_of_channels == 1) { |
71 AudioFrameOperations::StereoToMono(frame); | 54 AudioFrameOperations::StereoToMono(frame); |
72 } | 55 } |
73 } | 56 } |
74 | |
75 } // namespace webrtc | 57 } // namespace webrtc |
OLD | NEW |