1 | /**************************************************************************/ |
2 | /* audio_effect_spectrum_analyzer.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_spectrum_analyzer.h" |
32 | #include "servers/audio_server.h" |
33 | |
34 | static void smbFft(float *fftBuffer, long fftFrameSize, long sign) |
35 | /* |
36 | FFT routine, (C)1996 S.M.Bernsee. Sign = -1 is FFT, 1 is iFFT (inverse) |
37 | Fills fftBuffer[0...2*fftFrameSize-1] with the Fourier transform of the |
38 | time domain data in fftBuffer[0...2*fftFrameSize-1]. The FFT array takes |
39 | and returns the cosine and sine parts in an interleaved manner, ie. |
40 | fftBuffer[0] = cosPart[0], fftBuffer[1] = sinPart[0], asf. fftFrameSize |
41 | must be a power of 2. It expects a complex input signal (see footnote 2), |
42 | ie. when working with 'common' audio signals our input signal has to be |
43 | passed as {in[0],0.,in[1],0.,in[2],0.,...} asf. In that case, the transform |
44 | of the frequencies of interest is in fftBuffer[0...fftFrameSize]. |
45 | */ |
46 | { |
47 | float wr, wi, arg, *p1, *p2, temp; |
48 | float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i; |
49 | long i, bitm, j, le, le2, k; |
50 | |
51 | for (i = 2; i < 2 * fftFrameSize - 2; i += 2) { |
52 | for (bitm = 2, j = 0; bitm < 2 * fftFrameSize; bitm <<= 1) { |
53 | if (i & bitm) { |
54 | j++; |
55 | } |
56 | j <<= 1; |
57 | } |
58 | if (i < j) { |
59 | p1 = fftBuffer + i; |
60 | p2 = fftBuffer + j; |
61 | temp = *p1; |
62 | *(p1++) = *p2; |
63 | *(p2++) = temp; |
64 | temp = *p1; |
65 | *p1 = *p2; |
66 | *p2 = temp; |
67 | } |
68 | } |
69 | for (k = 0, le = 2; k < (long)(log((double)fftFrameSize) / log(2.) + .5); k++) { |
70 | le <<= 1; |
71 | le2 = le >> 1; |
72 | ur = 1.0; |
73 | ui = 0.0; |
74 | arg = Math_PI / (le2 >> 1); |
75 | wr = cos(arg); |
76 | wi = sign * sin(arg); |
77 | for (j = 0; j < le2; j += 2) { |
78 | p1r = fftBuffer + j; |
79 | p1i = p1r + 1; |
80 | p2r = p1r + le2; |
81 | p2i = p2r + 1; |
82 | for (i = j; i < 2 * fftFrameSize; i += le) { |
83 | tr = *p2r * ur - *p2i * ui; |
84 | ti = *p2r * ui + *p2i * ur; |
85 | *p2r = *p1r - tr; |
86 | *p2i = *p1i - ti; |
87 | *p1r += tr; |
88 | *p1i += ti; |
89 | p1r += le; |
90 | p1i += le; |
91 | p2r += le; |
92 | p2i += le; |
93 | } |
94 | tr = ur * wr - ui * wi; |
95 | ui = ur * wi + ui * wr; |
96 | ur = tr; |
97 | } |
98 | } |
99 | } |
100 | |
101 | void AudioEffectSpectrumAnalyzerInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { |
102 | uint64_t time = OS::get_singleton()->get_ticks_usec(); |
103 | |
104 | //copy everything over first, since this only really does capture |
105 | for (int i = 0; i < p_frame_count; i++) { |
106 | p_dst_frames[i] = p_src_frames[i]; |
107 | } |
108 | |
109 | //capture spectrum |
110 | while (p_frame_count) { |
111 | int to_fill = fft_size * 2 - temporal_fft_pos; |
112 | to_fill = MIN(to_fill, p_frame_count); |
113 | const double to_fill_step = Math_TAU / (double)fft_size; |
114 | |
115 | float *fftw = temporal_fft.ptrw(); |
116 | for (int i = 0; i < to_fill; i++) { //left and right buffers |
117 | float window = -0.5 * Math::cos(to_fill_step * (double)temporal_fft_pos) + 0.5; |
118 | fftw[temporal_fft_pos * 2] = window * p_src_frames->l; |
119 | fftw[temporal_fft_pos * 2 + 1] = 0; |
120 | fftw[(temporal_fft_pos + fft_size * 2) * 2] = window * p_src_frames->r; |
121 | fftw[(temporal_fft_pos + fft_size * 2) * 2 + 1] = 0; |
122 | ++p_src_frames; |
123 | ++temporal_fft_pos; |
124 | } |
125 | |
126 | p_frame_count -= to_fill; |
127 | |
128 | if (temporal_fft_pos == fft_size * 2) { |
129 | //time to do a FFT |
130 | smbFft(fftw, fft_size * 2, -1); |
131 | smbFft(fftw + fft_size * 4, fft_size * 2, -1); |
132 | int next = (fft_pos + 1) % fft_count; |
133 | |
134 | AudioFrame *hw = (AudioFrame *)fft_history[next].ptr(); //do not use write, avoid cow |
135 | |
136 | for (int i = 0; i < fft_size; i++) { |
137 | //abs(vec)/fft_size normalizes each frequency |
138 | hw[i].l = Vector2(fftw[i * 2], fftw[i * 2 + 1]).length() / float(fft_size); |
139 | hw[i].r = Vector2(fftw[fft_size * 4 + i * 2], fftw[fft_size * 4 + i * 2 + 1]).length() / float(fft_size); |
140 | } |
141 | |
142 | fft_pos = next; //swap |
143 | temporal_fft_pos = 0; |
144 | } |
145 | } |
146 | |
147 | //determine time of capture |
148 | double remainer_sec = (temporal_fft_pos / mix_rate); //subtract remainder from mix time |
149 | last_fft_time = time - uint64_t(remainer_sec * 1000000.0); |
150 | } |
151 | |
152 | void AudioEffectSpectrumAnalyzerInstance::_bind_methods() { |
153 | ClassDB::bind_method(D_METHOD("get_magnitude_for_frequency_range" , "from_hz" , "to_hz" , "mode" ), &AudioEffectSpectrumAnalyzerInstance::get_magnitude_for_frequency_range, DEFVAL(MAGNITUDE_MAX)); |
154 | BIND_ENUM_CONSTANT(MAGNITUDE_AVERAGE); |
155 | BIND_ENUM_CONSTANT(MAGNITUDE_MAX); |
156 | } |
157 | |
158 | Vector2 AudioEffectSpectrumAnalyzerInstance::get_magnitude_for_frequency_range(float p_begin, float p_end, MagnitudeMode p_mode) const { |
159 | if (last_fft_time == 0) { |
160 | return Vector2(); |
161 | } |
162 | uint64_t time = OS::get_singleton()->get_ticks_usec(); |
163 | float diff = double(time - last_fft_time) / 1000000.0 + base->get_tap_back_pos(); |
164 | diff -= AudioServer::get_singleton()->get_output_latency(); |
165 | float fft_time_size = float(fft_size) / mix_rate; |
166 | |
167 | int fft_index = fft_pos; |
168 | |
169 | while (diff > fft_time_size) { |
170 | diff -= fft_time_size; |
171 | fft_index -= 1; |
172 | if (fft_index < 0) { |
173 | fft_index = fft_count - 1; |
174 | } |
175 | } |
176 | |
177 | int begin_pos = p_begin * fft_size / (mix_rate * 0.5); |
178 | int end_pos = p_end * fft_size / (mix_rate * 0.5); |
179 | |
180 | begin_pos = CLAMP(begin_pos, 0, fft_size - 1); |
181 | end_pos = CLAMP(end_pos, 0, fft_size - 1); |
182 | |
183 | if (begin_pos > end_pos) { |
184 | SWAP(begin_pos, end_pos); |
185 | } |
186 | const AudioFrame *r = fft_history[fft_index].ptr(); |
187 | |
188 | if (p_mode == MAGNITUDE_AVERAGE) { |
189 | Vector2 avg; |
190 | |
191 | for (int i = begin_pos; i <= end_pos; i++) { |
192 | avg += Vector2(r[i]); |
193 | } |
194 | |
195 | avg /= float(end_pos - begin_pos + 1); |
196 | |
197 | return avg; |
198 | } else { |
199 | Vector2 max; |
200 | |
201 | for (int i = begin_pos; i <= end_pos; i++) { |
202 | max.x = MAX(max.x, r[i].l); |
203 | max.y = MAX(max.y, r[i].r); |
204 | } |
205 | |
206 | return max; |
207 | } |
208 | } |
209 | |
210 | Ref<AudioEffectInstance> AudioEffectSpectrumAnalyzer::instantiate() { |
211 | Ref<AudioEffectSpectrumAnalyzerInstance> ins; |
212 | ins.instantiate(); |
213 | ins->base = Ref<AudioEffectSpectrumAnalyzer>(this); |
214 | static const int fft_sizes[FFT_SIZE_MAX] = { 256, 512, 1024, 2048, 4096 }; |
215 | ins->fft_size = fft_sizes[fft_size]; |
216 | ins->mix_rate = AudioServer::get_singleton()->get_mix_rate(); |
217 | ins->fft_count = (buffer_length / (float(ins->fft_size) / ins->mix_rate)) + 1; |
218 | ins->fft_pos = 0; |
219 | ins->last_fft_time = 0; |
220 | ins->fft_history.resize(ins->fft_count); |
221 | ins->temporal_fft.resize(ins->fft_size * 8); //x2 stereo, x2 amount of samples for freqs, x2 for input |
222 | ins->temporal_fft_pos = 0; |
223 | for (int i = 0; i < ins->fft_count; i++) { |
224 | ins->fft_history.write[i].resize(ins->fft_size); //only magnitude matters |
225 | for (int j = 0; j < ins->fft_size; j++) { |
226 | ins->fft_history.write[i].write[j] = AudioFrame(0, 0); |
227 | } |
228 | } |
229 | return ins; |
230 | } |
231 | |
232 | void AudioEffectSpectrumAnalyzer::set_buffer_length(float p_seconds) { |
233 | buffer_length = p_seconds; |
234 | } |
235 | |
236 | float AudioEffectSpectrumAnalyzer::get_buffer_length() const { |
237 | return buffer_length; |
238 | } |
239 | |
240 | void AudioEffectSpectrumAnalyzer::set_tap_back_pos(float p_seconds) { |
241 | tapback_pos = p_seconds; |
242 | } |
243 | |
244 | float AudioEffectSpectrumAnalyzer::get_tap_back_pos() const { |
245 | return tapback_pos; |
246 | } |
247 | |
248 | void AudioEffectSpectrumAnalyzer::set_fft_size(FFTSize p_fft_size) { |
249 | ERR_FAIL_INDEX(p_fft_size, FFT_SIZE_MAX); |
250 | fft_size = p_fft_size; |
251 | } |
252 | |
253 | AudioEffectSpectrumAnalyzer::FFTSize AudioEffectSpectrumAnalyzer::get_fft_size() const { |
254 | return fft_size; |
255 | } |
256 | |
257 | void AudioEffectSpectrumAnalyzer::_bind_methods() { |
258 | ClassDB::bind_method(D_METHOD("set_buffer_length" , "seconds" ), &AudioEffectSpectrumAnalyzer::set_buffer_length); |
259 | ClassDB::bind_method(D_METHOD("get_buffer_length" ), &AudioEffectSpectrumAnalyzer::get_buffer_length); |
260 | |
261 | ClassDB::bind_method(D_METHOD("set_tap_back_pos" , "seconds" ), &AudioEffectSpectrumAnalyzer::set_tap_back_pos); |
262 | ClassDB::bind_method(D_METHOD("get_tap_back_pos" ), &AudioEffectSpectrumAnalyzer::get_tap_back_pos); |
263 | |
264 | ClassDB::bind_method(D_METHOD("set_fft_size" , "size" ), &AudioEffectSpectrumAnalyzer::set_fft_size); |
265 | ClassDB::bind_method(D_METHOD("get_fft_size" ), &AudioEffectSpectrumAnalyzer::get_fft_size); |
266 | |
267 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "buffer_length" , PROPERTY_HINT_RANGE, "0.1,4,0.1,suffix:s" ), "set_buffer_length" , "get_buffer_length" ); |
268 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tap_back_pos" , PROPERTY_HINT_RANGE, "0.1,4,0.1" ), "set_tap_back_pos" , "get_tap_back_pos" ); |
269 | ADD_PROPERTY(PropertyInfo(Variant::INT, "fft_size" , PROPERTY_HINT_ENUM, "256,512,1024,2048,4096" ), "set_fft_size" , "get_fft_size" ); |
270 | |
271 | BIND_ENUM_CONSTANT(FFT_SIZE_256); |
272 | BIND_ENUM_CONSTANT(FFT_SIZE_512); |
273 | BIND_ENUM_CONSTANT(FFT_SIZE_1024); |
274 | BIND_ENUM_CONSTANT(FFT_SIZE_2048); |
275 | BIND_ENUM_CONSTANT(FFT_SIZE_4096); |
276 | BIND_ENUM_CONSTANT(FFT_SIZE_MAX); |
277 | } |
278 | |
279 | AudioEffectSpectrumAnalyzer::AudioEffectSpectrumAnalyzer() { |
280 | buffer_length = 2; |
281 | tapback_pos = 0.01; |
282 | fft_size = FFT_SIZE_1024; |
283 | } |
284 | |