Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. FFTwrapper.c - A wrapper for Fast Fourier Transforms
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <cmath>
  18. #include <cassert>
  19. #include <cstring>
  20. #include <pthread.h>
  21. #include "FFTwrapper.h"
  22. static pthread_mutex_t *mutex = NULL;
  23. FFTwrapper::FFTwrapper(int fftsize_)
  24. {
  25. //first one will spawn the mutex (yeah this may be a race itself)
  26. if(!mutex) {
  27. mutex = new pthread_mutex_t;
  28. pthread_mutex_init(mutex, NULL);
  29. }
  30. fftsize = fftsize_;
  31. time = new fftw_real[fftsize];
  32. fft = new fftw_complex[fftsize + 1];
  33. pthread_mutex_lock(mutex);
  34. planfftw = fftw_plan_dft_r2c_1d(fftsize,
  35. time,
  36. fft,
  37. FFTW_ESTIMATE);
  38. planfftw_inv = fftw_plan_dft_c2r_1d(fftsize,
  39. fft,
  40. time,
  41. FFTW_ESTIMATE);
  42. pthread_mutex_unlock(mutex);
  43. }
  44. FFTwrapper::~FFTwrapper()
  45. {
  46. pthread_mutex_lock(mutex);
  47. fftw_destroy_plan(planfftw);
  48. fftw_destroy_plan(planfftw_inv);
  49. pthread_mutex_unlock(mutex);
  50. delete [] time;
  51. delete [] fft;
  52. }
  53. void FFTwrapper::smps2freqs(const float *smps, fft_t *freqs)
  54. {
  55. //Load data
  56. for(int i = 0; i < fftsize; ++i)
  57. time[i] = static_cast<double>(smps[i]);
  58. //DFT
  59. fftw_execute(planfftw);
  60. //Grab data
  61. memcpy((void *)freqs, (const void *)fft, fftsize * sizeof(double));
  62. }
  63. void FFTwrapper::freqs2smps(const fft_t *freqs, float *smps)
  64. {
  65. //Load data
  66. memcpy((void *)fft, (const void *)freqs, fftsize * sizeof(double));
  67. //clear unused freq channel
  68. fft[fftsize / 2][0] = 0.0f;
  69. fft[fftsize / 2][1] = 0.0f;
  70. //IDFT
  71. fftw_execute(planfftw_inv);
  72. //Grab data
  73. for(int i = 0; i < fftsize; ++i)
  74. smps[i] = static_cast<float>(time[i]);
  75. }
  76. void FFT_cleanup()
  77. {
  78. fftw_cleanup();
  79. pthread_mutex_destroy(mutex);
  80. delete mutex;
  81. mutex = NULL;
  82. }