audio_effect_spectrum_analyzer.cpp 10 KB

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