1/**************************************************************************/
2/* audio_effect_reverb.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_reverb.h"
32#include "servers/audio_server.h"
33void AudioEffectReverbInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
34 for (int i = 0; i < 2; i++) {
35 Reverb &r = reverb[i];
36
37 r.set_predelay(base->predelay);
38 r.set_predelay_feedback(base->predelay_fb);
39 r.set_highpass(base->hpf);
40 r.set_room_size(base->room_size);
41 r.set_damp(base->damping);
42 r.set_extra_spread(base->spread);
43 r.set_wet(base->wet);
44 r.set_dry(base->dry);
45 }
46
47 int todo = p_frame_count;
48 int offset = 0;
49
50 while (todo) {
51 int to_mix = MIN(todo, Reverb::INPUT_BUFFER_MAX_SIZE);
52
53 for (int j = 0; j < to_mix; j++) {
54 tmp_src[j] = p_src_frames[offset + j].l;
55 }
56
57 reverb[0].process(tmp_src, tmp_dst, to_mix);
58
59 for (int j = 0; j < to_mix; j++) {
60 p_dst_frames[offset + j].l = tmp_dst[j];
61 tmp_src[j] = p_src_frames[offset + j].r;
62 }
63
64 reverb[1].process(tmp_src, tmp_dst, to_mix);
65
66 for (int j = 0; j < to_mix; j++) {
67 p_dst_frames[offset + j].r = tmp_dst[j];
68 }
69
70 offset += to_mix;
71 todo -= to_mix;
72 }
73}
74
75AudioEffectReverbInstance::AudioEffectReverbInstance() {
76 reverb[0].set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
77 reverb[0].set_extra_spread_base(0);
78 reverb[1].set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
79 reverb[1].set_extra_spread_base(0.000521); //for stereo effect
80}
81
82Ref<AudioEffectInstance> AudioEffectReverb::instantiate() {
83 Ref<AudioEffectReverbInstance> ins;
84 ins.instantiate();
85 ins->base = Ref<AudioEffectReverb>(this);
86 return ins;
87}
88
89void AudioEffectReverb::set_predelay_msec(float p_msec) {
90 predelay = p_msec;
91}
92
93void AudioEffectReverb::set_predelay_feedback(float p_feedback) {
94 predelay_fb = CLAMP(p_feedback, 0, 0.98);
95}
96
97void AudioEffectReverb::set_room_size(float p_size) {
98 room_size = p_size;
99}
100
101void AudioEffectReverb::set_damping(float p_damping) {
102 damping = p_damping;
103}
104
105void AudioEffectReverb::set_spread(float p_spread) {
106 spread = p_spread;
107}
108
109void AudioEffectReverb::set_dry(float p_dry) {
110 dry = p_dry;
111}
112
113void AudioEffectReverb::set_wet(float p_wet) {
114 wet = p_wet;
115}
116
117void AudioEffectReverb::set_hpf(float p_hpf) {
118 hpf = p_hpf;
119}
120
121float AudioEffectReverb::get_predelay_msec() const {
122 return predelay;
123}
124
125float AudioEffectReverb::get_predelay_feedback() const {
126 return predelay_fb;
127}
128
129float AudioEffectReverb::get_room_size() const {
130 return room_size;
131}
132
133float AudioEffectReverb::get_damping() const {
134 return damping;
135}
136
137float AudioEffectReverb::get_spread() const {
138 return spread;
139}
140
141float AudioEffectReverb::get_dry() const {
142 return dry;
143}
144
145float AudioEffectReverb::get_wet() const {
146 return wet;
147}
148
149float AudioEffectReverb::get_hpf() const {
150 return hpf;
151}
152
153void AudioEffectReverb::_bind_methods() {
154 ClassDB::bind_method(D_METHOD("set_predelay_msec", "msec"), &AudioEffectReverb::set_predelay_msec);
155 ClassDB::bind_method(D_METHOD("get_predelay_msec"), &AudioEffectReverb::get_predelay_msec);
156
157 ClassDB::bind_method(D_METHOD("set_predelay_feedback", "feedback"), &AudioEffectReverb::set_predelay_feedback);
158 ClassDB::bind_method(D_METHOD("get_predelay_feedback"), &AudioEffectReverb::get_predelay_feedback);
159
160 ClassDB::bind_method(D_METHOD("set_room_size", "size"), &AudioEffectReverb::set_room_size);
161 ClassDB::bind_method(D_METHOD("get_room_size"), &AudioEffectReverb::get_room_size);
162
163 ClassDB::bind_method(D_METHOD("set_damping", "amount"), &AudioEffectReverb::set_damping);
164 ClassDB::bind_method(D_METHOD("get_damping"), &AudioEffectReverb::get_damping);
165
166 ClassDB::bind_method(D_METHOD("set_spread", "amount"), &AudioEffectReverb::set_spread);
167 ClassDB::bind_method(D_METHOD("get_spread"), &AudioEffectReverb::get_spread);
168
169 ClassDB::bind_method(D_METHOD("set_dry", "amount"), &AudioEffectReverb::set_dry);
170 ClassDB::bind_method(D_METHOD("get_dry"), &AudioEffectReverb::get_dry);
171
172 ClassDB::bind_method(D_METHOD("set_wet", "amount"), &AudioEffectReverb::set_wet);
173 ClassDB::bind_method(D_METHOD("get_wet"), &AudioEffectReverb::get_wet);
174
175 ClassDB::bind_method(D_METHOD("set_hpf", "amount"), &AudioEffectReverb::set_hpf);
176 ClassDB::bind_method(D_METHOD("get_hpf"), &AudioEffectReverb::get_hpf);
177
178 ADD_GROUP("Predelay", "predelay_");
179 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "predelay_msec", PROPERTY_HINT_RANGE, "20,500,1,suffix:ms"), "set_predelay_msec", "get_predelay_msec");
180 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "predelay_feedback", PROPERTY_HINT_RANGE, "0,0.98,0.01"), "set_predelay_feedback", "get_predelay_feedback");
181 ADD_GROUP("", "");
182 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "room_size", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_room_size", "get_room_size");
183 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_damping", "get_damping");
184 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_spread", "get_spread");
185 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "hipass", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_hpf", "get_hpf");
186 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dry", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_dry", "get_dry");
187 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wet", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_wet", "get_wet");
188}
189
190AudioEffectReverb::AudioEffectReverb() {
191 predelay = 150;
192 predelay_fb = 0.4;
193 hpf = 0;
194 room_size = 0.8;
195 damping = 0.5;
196 spread = 1.0;
197 dry = 1.0;
198 wet = 0.5;
199}
200