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.

98 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. namespace zyncarla {
  17. static pthread_mutex_t *mutex = NULL;
  18. FFTwrapper::FFTwrapper(int fftsize_)
  19. {
  20. //first one will spawn the mutex (yeah this may be a race itself)
  21. if(!mutex) {
  22. mutex = new pthread_mutex_t;
  23. pthread_mutex_init(mutex, NULL);
  24. }
  25. fftsize = fftsize_;
  26. time = new fftw_real[fftsize];
  27. fft = new fftw_complex[fftsize + 1];
  28. pthread_mutex_lock(mutex);
  29. planfftw = fftw_plan_dft_r2c_1d(fftsize,
  30. time,
  31. fft,
  32. FFTW_ESTIMATE);
  33. planfftw_inv = fftw_plan_dft_c2r_1d(fftsize,
  34. fft,
  35. time,
  36. FFTW_ESTIMATE);
  37. pthread_mutex_unlock(mutex);
  38. }
  39. FFTwrapper::~FFTwrapper()
  40. {
  41. pthread_mutex_lock(mutex);
  42. fftw_destroy_plan(planfftw);
  43. fftw_destroy_plan(planfftw_inv);
  44. pthread_mutex_unlock(mutex);
  45. delete [] time;
  46. delete [] fft;
  47. }
  48. void FFTwrapper::smps2freqs(const float *smps, fft_t *freqs)
  49. {
  50. //Load data
  51. for(int i = 0; i < fftsize; ++i)
  52. time[i] = static_cast<double>(smps[i]);
  53. //DFT
  54. fftw_execute(planfftw);
  55. //Grab data
  56. memcpy((void *)freqs, (const void *)fft, fftsize * sizeof(double));
  57. }
  58. void FFTwrapper::freqs2smps(const fft_t *freqs, float *smps)
  59. {
  60. //Load data
  61. memcpy((void *)fft, (const void *)freqs, fftsize * sizeof(double));
  62. //clear unused freq channel
  63. fft[fftsize / 2][0] = 0.0f;
  64. fft[fftsize / 2][1] = 0.0f;
  65. //IDFT
  66. fftw_execute(planfftw_inv);
  67. //Grab data
  68. for(int i = 0; i < fftsize; ++i)
  69. smps[i] = static_cast<float>(time[i]);
  70. }
  71. void FFT_cleanup()
  72. {
  73. fftw_cleanup();
  74. pthread_mutex_destroy(mutex);
  75. delete mutex;
  76. mutex = NULL;
  77. }
  78. }