PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/visualizations/Milkdrop/vis_milkdrop/fft.cpp

https://github.com/xbmc/atv2
C++ | 319 lines | 176 code | 54 blank | 89 comment | 29 complexity | 96c02bc5d069d0b80ded9bc37db4b16a MD5 | raw file
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <math.h>
  25. #include <memory.h>
  26. #include "fft.h"
  27. #define PI 3.141592653589793238462643383279502884197169399f
  28. #define SafeDeleteArray(x) { if (x) { delete [] x; x = 0; } }
  29. /*****************************************************************************/
  30. FFT::FFT()
  31. {
  32. NFREQ = 0;
  33. envelope = 0;
  34. equalize = 0;
  35. bitrevtable = 0;
  36. cossintable = 0;
  37. temp1 = 0;
  38. temp2 = 0;
  39. }
  40. /*****************************************************************************/
  41. FFT::~FFT()
  42. {
  43. CleanUp();
  44. }
  45. /*****************************************************************************/
  46. void FFT::Init(int samples_in, int samples_out, int bEqualize, float envelope_power)
  47. {
  48. // samples_in: # of waveform samples you'll feed into the FFT
  49. // samples_out: # of frequency samples you want out; MUST BE A POWER OF 2.
  50. CleanUp();
  51. m_samples_in = samples_in;
  52. NFREQ = samples_out*2;
  53. InitBitRevTable();
  54. InitCosSinTable();
  55. if (envelope_power > 0)
  56. InitEnvelopeTable(envelope_power);
  57. if (bEqualize)
  58. InitEqualizeTable();
  59. temp1 = new float[NFREQ];
  60. temp2 = new float[NFREQ];
  61. }
  62. /*****************************************************************************/
  63. void FFT::CleanUp()
  64. {
  65. SafeDeleteArray(envelope);
  66. SafeDeleteArray(equalize);
  67. SafeDeleteArray(bitrevtable);
  68. SafeDeleteArray(cossintable);
  69. SafeDeleteArray(temp1);
  70. SafeDeleteArray(temp2);
  71. }
  72. /*****************************************************************************/
  73. void FFT::InitEqualizeTable()
  74. {
  75. int i;
  76. float scaling = -0.02f;
  77. float inv_half_nfreq = 1.0f/(float)(NFREQ/2);
  78. equalize = new float[NFREQ/2];
  79. for (i=0; i<NFREQ/2; i++)
  80. equalize[i] = scaling * logf( (float)(NFREQ/2-i)*inv_half_nfreq );
  81. }
  82. /*****************************************************************************/
  83. void FFT::InitEnvelopeTable(float power)
  84. {
  85. // this precomputation is for multiplying the waveform sample
  86. // by a bell-curve-shaped envelope, so we don't see the ugly
  87. // frequency response (oscillations) of a square filter.
  88. // a power of 1.0 will compute the FFT with exactly one convolution.
  89. // a power of 2.0 is like doing it twice; the resulting frequency
  90. // output will be smoothed out and the peaks will be "fatter".
  91. // a power of 0.5 is closer to not using an envelope, and you start
  92. // to get the frequency response of the square filter as 'power'
  93. // approaches zero; the peaks get tighter and more precise, but
  94. // you also see small oscillations around their bases.
  95. int i;
  96. float mult = 1.0f/(float)m_samples_in * 6.2831853f;
  97. envelope = new float[m_samples_in];
  98. if (power==1.0f)
  99. for (i=0; i<m_samples_in; i++)
  100. envelope[i] = 0.5f + 0.5f*sinf(i*mult - 1.5707963268f);
  101. else
  102. for (i=0; i<m_samples_in; i++)
  103. envelope[i] = powf(0.5f + 0.5f*sinf(i*mult - 1.5707963268f), power);
  104. }
  105. /*****************************************************************************/
  106. void FFT::InitBitRevTable()
  107. {
  108. int i,j,m,temp;
  109. bitrevtable = new int[NFREQ];
  110. for (i=0; i<NFREQ; i++)
  111. bitrevtable[i] = i;
  112. for (i=0,j=0; i < NFREQ; i++)
  113. {
  114. if (j > i)
  115. {
  116. temp = bitrevtable[i];
  117. bitrevtable[i] = bitrevtable[j];
  118. bitrevtable[j] = temp;
  119. }
  120. m = NFREQ >> 1;
  121. while (m >= 1 && j >= m)
  122. {
  123. j -= m;
  124. m >>= 1;
  125. }
  126. j += m;
  127. }
  128. }
  129. /*****************************************************************************/
  130. void FFT::InitCosSinTable()
  131. {
  132. int i,dftsize,tabsize;
  133. float theta;
  134. dftsize = 2;
  135. tabsize = 0;
  136. while (dftsize <= NFREQ)
  137. {
  138. tabsize++;
  139. dftsize <<= 1;
  140. }
  141. cossintable = new float[tabsize][2];
  142. dftsize = 2;
  143. i = 0;
  144. while (dftsize <= NFREQ)
  145. {
  146. theta = (float)(-2.0f*PI/(float)dftsize);
  147. cossintable[i][0] = (float)cosf(theta);
  148. cossintable[i][1] = (float)sinf(theta);
  149. i++;
  150. dftsize <<= 1;
  151. }
  152. }
  153. /*****************************************************************************/
  154. void FFT::time_to_frequency_domain(float *in_wavedata, float *out_spectraldata)
  155. {
  156. // Converts time-domain samples from in_wavedata[]
  157. // into frequency-domain samples in out_spectraldata[].
  158. // The array lengths are the two parameters to Init().
  159. // The last sample of the output data will represent the frequency
  160. // that is 1/4th of the input sampling rate. For example,
  161. // if the input wave data is sampled at 44,100 Hz, then the last
  162. // sample of the spectral data output will represent the frequency
  163. // 11,025 Hz. The first sample will be 0 Hz; the frequencies of
  164. // the rest of the samples vary linearly in between.
  165. // Note that since human hearing is limited to the range 200 - 20,000
  166. // Hz. 200 is a low bass hum; 20,000 is an ear-piercing high shriek.
  167. // Each time the frequency doubles, that sounds like going up an octave.
  168. // That means that the difference between 200 and 300 Hz is FAR more
  169. // than the difference between 5000 and 5100, for example!
  170. // So, when trying to analyze bass, you'll want to look at (probably)
  171. // the 200-800 Hz range; whereas for treble, you'll want the 1,400 -
  172. // 11,025 Hz range.
  173. // If you want to get 3 bands, try it this way:
  174. // a) 11,025 / 200 = 55.125
  175. // b) to get the number of octaves between 200 and 11,025 Hz, solve for n:
  176. // 2^n = 55.125
  177. // n = log 55.125 / log 2
  178. // n = 5.785
  179. // c) so each band should represent 5.785/3 = 1.928 octaves; the ranges are:
  180. // 1) 200 - 200*2^1.928 or 200 - 761 Hz
  181. // 2) 200*2^1.928 - 200*2^(1.928*2) or 761 - 2897 Hz
  182. // 3) 200*2^(1.928*2) - 200*2^(1.928*3) or 2897 - 11025 Hz
  183. // A simple sine-wave-based envelope is convolved with the waveform
  184. // data before doing the FFT, to emeliorate the bad frequency response
  185. // of a square (i.e. nonexistent) filter.
  186. // You might want to slightly damp (blur) the input if your signal isn't
  187. // of a very high quality, to reduce high-frequency noise that would
  188. // otherwise show up in the output.
  189. int j, m, i, dftsize, hdftsize, t;
  190. float wr, wi, wpi, wpr, wtemp, tempr, tempi;
  191. if (!bitrevtable) return;
  192. //if (!envelope) return;
  193. //if (!equalize) return;
  194. if (!temp1) return;
  195. if (!temp2) return;
  196. if (!cossintable) return;
  197. // 1. set up input to the fft
  198. if (envelope)
  199. {
  200. for (i=0; i<NFREQ; i++)
  201. {
  202. int idx = bitrevtable[i];
  203. if (idx < m_samples_in)
  204. temp1[i] = in_wavedata[idx] * envelope[idx];
  205. else
  206. temp1[i] = 0;
  207. }
  208. }
  209. else
  210. {
  211. for (i=0; i<NFREQ; i++)
  212. {
  213. int idx = bitrevtable[i];
  214. if (idx < m_samples_in)
  215. temp1[i] = in_wavedata[idx];// * envelope[idx];
  216. else
  217. temp1[i] = 0;
  218. }
  219. }
  220. memset(temp2, 0, sizeof(float)*NFREQ);
  221. // 2. perform FFT
  222. float *real = temp1;
  223. float *imag = temp2;
  224. dftsize = 2;
  225. t = 0;
  226. while (dftsize <= NFREQ)
  227. {
  228. wpr = cossintable[t][0];
  229. wpi = cossintable[t][1];
  230. wr = 1.0f;
  231. wi = 0.0f;
  232. hdftsize = dftsize >> 1;
  233. for (m = 0; m < hdftsize; m+=1)
  234. {
  235. for (i = m; i < NFREQ; i+=dftsize)
  236. {
  237. j = i + hdftsize;
  238. tempr = wr*real[j] - wi*imag[j];
  239. tempi = wr*imag[j] + wi*real[j];
  240. real[j] = real[i] - tempr;
  241. imag[j] = imag[i] - tempi;
  242. real[i] += tempr;
  243. imag[i] += tempi;
  244. }
  245. wr = (wtemp=wr)*wpr - wi*wpi;
  246. wi = wi*wpr + wtemp*wpi;
  247. }
  248. dftsize <<= 1;
  249. t++;
  250. }
  251. // 3. take the magnitude & equalize it (on a log10 scale) for output
  252. if (equalize)
  253. for (i=0; i<NFREQ/2; i++)
  254. out_spectraldata[i] = equalize[i] * sqrtf(temp1[i]*temp1[i] + temp2[i]*temp2[i]);
  255. else
  256. for (i=0; i<NFREQ/2; i++)
  257. out_spectraldata[i] = sqrtf(temp1[i]*temp1[i] + temp2[i]*temp2[i]);
  258. }
  259. /*****************************************************************************/