1 | /**************************************************************************/ |
2 | /* audio_effect_phaser.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_phaser.h" |
32 | #include "core/math/math_funcs.h" |
33 | #include "servers/audio_server.h" |
34 | |
35 | void AudioEffectPhaserInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { |
36 | float sampling_rate = AudioServer::get_singleton()->get_mix_rate(); |
37 | |
38 | float dmin = base->range_min / (sampling_rate / 2.0); |
39 | float dmax = base->range_max / (sampling_rate / 2.0); |
40 | |
41 | float increment = Math_TAU * (base->rate / sampling_rate); |
42 | |
43 | for (int i = 0; i < p_frame_count; i++) { |
44 | phase += increment; |
45 | |
46 | while (phase >= Math_TAU) { |
47 | phase -= Math_TAU; |
48 | } |
49 | |
50 | float d = dmin + (dmax - dmin) * ((sin(phase) + 1.f) / 2.f); |
51 | |
52 | //update filter coeffs |
53 | for (int j = 0; j < 6; j++) { |
54 | allpass[0][j].delay(d); |
55 | allpass[1][j].delay(d); |
56 | } |
57 | |
58 | //calculate output |
59 | float y = allpass[0][0].update( |
60 | allpass[0][1].update( |
61 | allpass[0][2].update( |
62 | allpass[0][3].update( |
63 | allpass[0][4].update( |
64 | allpass[0][5].update(p_src_frames[i].l + h.l * base->feedback)))))); |
65 | h.l = y; |
66 | |
67 | p_dst_frames[i].l = p_src_frames[i].l + y * base->depth; |
68 | |
69 | y = allpass[1][0].update( |
70 | allpass[1][1].update( |
71 | allpass[1][2].update( |
72 | allpass[1][3].update( |
73 | allpass[1][4].update( |
74 | allpass[1][5].update(p_src_frames[i].r + h.r * base->feedback)))))); |
75 | h.r = y; |
76 | |
77 | p_dst_frames[i].r = p_src_frames[i].r + y * base->depth; |
78 | } |
79 | } |
80 | |
81 | Ref<AudioEffectInstance> AudioEffectPhaser::instantiate() { |
82 | Ref<AudioEffectPhaserInstance> ins; |
83 | ins.instantiate(); |
84 | ins->base = Ref<AudioEffectPhaser>(this); |
85 | ins->phase = 0; |
86 | ins->h = AudioFrame(0, 0); |
87 | |
88 | return ins; |
89 | } |
90 | |
91 | void AudioEffectPhaser::set_range_min_hz(float p_hz) { |
92 | range_min = p_hz; |
93 | } |
94 | |
95 | float AudioEffectPhaser::get_range_min_hz() const { |
96 | return range_min; |
97 | } |
98 | |
99 | void AudioEffectPhaser::set_range_max_hz(float p_hz) { |
100 | range_max = p_hz; |
101 | } |
102 | |
103 | float AudioEffectPhaser::get_range_max_hz() const { |
104 | return range_max; |
105 | } |
106 | |
107 | void AudioEffectPhaser::set_rate_hz(float p_hz) { |
108 | rate = p_hz; |
109 | } |
110 | |
111 | float AudioEffectPhaser::get_rate_hz() const { |
112 | return rate; |
113 | } |
114 | |
115 | void AudioEffectPhaser::set_feedback(float p_fbk) { |
116 | feedback = p_fbk; |
117 | } |
118 | |
119 | float AudioEffectPhaser::get_feedback() const { |
120 | return feedback; |
121 | } |
122 | |
123 | void AudioEffectPhaser::set_depth(float p_depth) { |
124 | depth = p_depth; |
125 | } |
126 | |
127 | float AudioEffectPhaser::get_depth() const { |
128 | return depth; |
129 | } |
130 | |
131 | void AudioEffectPhaser::_bind_methods() { |
132 | ClassDB::bind_method(D_METHOD("set_range_min_hz" , "hz" ), &AudioEffectPhaser::set_range_min_hz); |
133 | ClassDB::bind_method(D_METHOD("get_range_min_hz" ), &AudioEffectPhaser::get_range_min_hz); |
134 | |
135 | ClassDB::bind_method(D_METHOD("set_range_max_hz" , "hz" ), &AudioEffectPhaser::set_range_max_hz); |
136 | ClassDB::bind_method(D_METHOD("get_range_max_hz" ), &AudioEffectPhaser::get_range_max_hz); |
137 | |
138 | ClassDB::bind_method(D_METHOD("set_rate_hz" , "hz" ), &AudioEffectPhaser::set_rate_hz); |
139 | ClassDB::bind_method(D_METHOD("get_rate_hz" ), &AudioEffectPhaser::get_rate_hz); |
140 | |
141 | ClassDB::bind_method(D_METHOD("set_feedback" , "fbk" ), &AudioEffectPhaser::set_feedback); |
142 | ClassDB::bind_method(D_METHOD("get_feedback" ), &AudioEffectPhaser::get_feedback); |
143 | |
144 | ClassDB::bind_method(D_METHOD("set_depth" , "depth" ), &AudioEffectPhaser::set_depth); |
145 | ClassDB::bind_method(D_METHOD("get_depth" ), &AudioEffectPhaser::get_depth); |
146 | |
147 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range_min_hz" , PROPERTY_HINT_RANGE, "10,10000,suffix:Hz" ), "set_range_min_hz" , "get_range_min_hz" ); |
148 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range_max_hz" , PROPERTY_HINT_RANGE, "10,10000,suffix:Hz" ), "set_range_max_hz" , "get_range_max_hz" ); |
149 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rate_hz" , PROPERTY_HINT_RANGE, "0.01,20,suffix:Hz" ), "set_rate_hz" , "get_rate_hz" ); |
150 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "feedback" , PROPERTY_HINT_RANGE, "0.1,0.9,0.1" ), "set_feedback" , "get_feedback" ); |
151 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth" , PROPERTY_HINT_RANGE, "0.1,4,0.1" ), "set_depth" , "get_depth" ); |
152 | } |
153 | |
154 | AudioEffectPhaser::AudioEffectPhaser() { |
155 | range_min = 440; |
156 | range_max = 1600; |
157 | rate = 0.5; |
158 | feedback = 0.7; |
159 | depth = 1; |
160 | } |
161 | |