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.

94 lines
2.3KB

  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
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. */
  11. #include <cmath>
  12. #include <cassert>
  13. #include <cstring>
  14. #include <pthread.h>
  15. #include "FFTwrapper.h"
  16. static pthread_mutex_t *mutex = NULL;
  17. FFTwrapper::FFTwrapper(int fftsize_)
  18. {
  19. //first one will spawn the mutex (yeah this may be a race itself)
  20. if(!mutex) {
  21. mutex = new pthread_mutex_t;
  22. pthread_mutex_init(mutex, NULL);
  23. }
  24. fftsize = fftsize_;
  25. time = new fftw_real[fftsize];
  26. fft = new fftw_complex[fftsize + 1];
  27. pthread_mutex_lock(mutex);
  28. planfftw = fftw_plan_dft_r2c_1d(fftsize,
  29. time,
  30. fft,
  31. FFTW_ESTIMATE);
  32. planfftw_inv = fftw_plan_dft_c2r_1d(fftsize,
  33. fft,
  34. time,
  35. FFTW_ESTIMATE);
  36. pthread_mutex_unlock(mutex);
  37. }
  38. FFTwrapper::~FFTwrapper()
  39. {
  40. pthread_mutex_lock(mutex);
  41. fftw_destroy_plan(planfftw);
  42. fftw_destroy_plan(planfftw_inv);
  43. pthread_mutex_unlock(mutex);
  44. delete [] time;
  45. delete [] fft;
  46. }
  47. void FFTwrapper::smps2freqs(const float *smps, fft_t *freqs)
  48. {
  49. //Load data
  50. for(int i = 0; i < fftsize; ++i)
  51. time[i] = static_cast<double>(smps[i]);
  52. //DFT
  53. fftw_execute(planfftw);
  54. //Grab data
  55. memcpy((void *)freqs, (const void *)fft, fftsize * sizeof(double));
  56. }
  57. void FFTwrapper::freqs2smps(const fft_t *freqs, float *smps)
  58. {
  59. //Load data
  60. memcpy((void *)fft, (const void *)freqs, fftsize * sizeof(double));
  61. //clear unused freq channel
  62. fft[fftsize / 2][0] = 0.0f;
  63. fft[fftsize / 2][1] = 0.0f;
  64. //IDFT
  65. fftw_execute(planfftw_inv);
  66. //Grab data
  67. for(int i = 0; i < fftsize; ++i)
  68. smps[i] = static_cast<float>(time[i]);
  69. }
  70. void FFT_cleanup()
  71. {
  72. fftw_cleanup();
  73. pthread_mutex_destroy(mutex);
  74. delete mutex;
  75. mutex = NULL;
  76. }