Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/audio/effects/audio_effect_limiter.cpp
10278 views
1
/**************************************************************************/
2
/* audio_effect_limiter.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "audio_effect_limiter.h"
32
33
void AudioEffectLimiterInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
34
float threshdb = base->threshold;
35
float ceiling = Math::db_to_linear(base->ceiling);
36
float ceildb = base->ceiling;
37
float makeup = Math::db_to_linear(ceildb - threshdb);
38
float sc = -base->soft_clip;
39
float scv = Math::db_to_linear(sc);
40
float peakdb = ceildb + 25;
41
float scmult = Math::abs((ceildb - sc) / (peakdb - sc));
42
43
for (int i = 0; i < p_frame_count; i++) {
44
float spl0 = p_src_frames[i].left;
45
float spl1 = p_src_frames[i].right;
46
spl0 = spl0 * makeup;
47
spl1 = spl1 * makeup;
48
float sign0 = (spl0 < 0.0 ? -1.0 : 1.0);
49
float sign1 = (spl1 < 0.0 ? -1.0 : 1.0);
50
float abs0 = Math::abs(spl0);
51
float abs1 = Math::abs(spl1);
52
float overdb0 = Math::linear_to_db(abs0) - ceildb;
53
float overdb1 = Math::linear_to_db(abs1) - ceildb;
54
55
if (abs0 > scv) {
56
spl0 = sign0 * (scv + Math::db_to_linear(overdb0 * scmult));
57
}
58
if (abs1 > scv) {
59
spl1 = sign1 * (scv + Math::db_to_linear(overdb1 * scmult));
60
}
61
62
spl0 = MIN(ceiling, Math::abs(spl0)) * (spl0 < 0.0 ? -1.0 : 1.0);
63
spl1 = MIN(ceiling, Math::abs(spl1)) * (spl1 < 0.0 ? -1.0 : 1.0);
64
65
p_dst_frames[i].left = spl0;
66
p_dst_frames[i].right = spl1;
67
}
68
}
69
70
Ref<AudioEffectInstance> AudioEffectLimiter::instantiate() {
71
Ref<AudioEffectLimiterInstance> ins;
72
ins.instantiate();
73
ins->base = Ref<AudioEffectLimiter>(this);
74
75
return ins;
76
}
77
78
void AudioEffectLimiter::set_threshold_db(float p_threshold) {
79
threshold = p_threshold;
80
}
81
82
float AudioEffectLimiter::get_threshold_db() const {
83
return threshold;
84
}
85
86
void AudioEffectLimiter::set_ceiling_db(float p_ceiling) {
87
ceiling = p_ceiling;
88
}
89
90
float AudioEffectLimiter::get_ceiling_db() const {
91
return ceiling;
92
}
93
94
void AudioEffectLimiter::set_soft_clip_db(float p_soft_clip) {
95
soft_clip = p_soft_clip;
96
}
97
98
float AudioEffectLimiter::get_soft_clip_db() const {
99
return soft_clip;
100
}
101
102
void AudioEffectLimiter::set_soft_clip_ratio(float p_soft_clip) {
103
soft_clip_ratio = p_soft_clip;
104
}
105
106
float AudioEffectLimiter::get_soft_clip_ratio() const {
107
return soft_clip_ratio;
108
}
109
110
void AudioEffectLimiter::_bind_methods() {
111
ClassDB::bind_method(D_METHOD("set_ceiling_db", "ceiling"), &AudioEffectLimiter::set_ceiling_db);
112
ClassDB::bind_method(D_METHOD("get_ceiling_db"), &AudioEffectLimiter::get_ceiling_db);
113
114
ClassDB::bind_method(D_METHOD("set_threshold_db", "threshold"), &AudioEffectLimiter::set_threshold_db);
115
ClassDB::bind_method(D_METHOD("get_threshold_db"), &AudioEffectLimiter::get_threshold_db);
116
117
ClassDB::bind_method(D_METHOD("set_soft_clip_db", "soft_clip"), &AudioEffectLimiter::set_soft_clip_db);
118
ClassDB::bind_method(D_METHOD("get_soft_clip_db"), &AudioEffectLimiter::get_soft_clip_db);
119
120
ClassDB::bind_method(D_METHOD("set_soft_clip_ratio", "soft_clip"), &AudioEffectLimiter::set_soft_clip_ratio);
121
ClassDB::bind_method(D_METHOD("get_soft_clip_ratio"), &AudioEffectLimiter::get_soft_clip_ratio);
122
123
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ceiling_db", PROPERTY_HINT_RANGE, "-20,-0.1,0.1,suffix:dB"), "set_ceiling_db", "get_ceiling_db");
124
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "threshold_db", PROPERTY_HINT_RANGE, "-30,0,0.1,suffix:dB"), "set_threshold_db", "get_threshold_db");
125
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "soft_clip_db", PROPERTY_HINT_RANGE, "0,6,0.1,suffix:dB"), "set_soft_clip_db", "get_soft_clip_db");
126
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "soft_clip_ratio", PROPERTY_HINT_RANGE, "3,20,0.1"), "set_soft_clip_ratio", "get_soft_clip_ratio");
127
}
128
129
AudioEffectLimiter::AudioEffectLimiter() {
130
threshold = 0;
131
ceiling = -0.1;
132
soft_clip = 2;
133
soft_clip_ratio = 10;
134
}
135
136