audio_effect_spectrum_analyzer.cpp 8.1 KB

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