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.

86 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 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 "FFTwrapper.h"
  21. FFTwrapper::FFTwrapper(int fftsize_)
  22. {
  23. fftsize = fftsize_;
  24. time = new fftw_real[fftsize];
  25. fft = new fftw_complex[fftsize + 1];
  26. planfftw = fftw_plan_dft_r2c_1d(fftsize,
  27. time,
  28. fft,
  29. FFTW_ESTIMATE);
  30. planfftw_inv = fftw_plan_dft_c2r_1d(fftsize,
  31. fft,
  32. time,
  33. FFTW_ESTIMATE);
  34. }
  35. FFTwrapper::~FFTwrapper()
  36. {
  37. fftw_destroy_plan(planfftw);
  38. fftw_destroy_plan(planfftw_inv);
  39. delete [] time;
  40. delete [] fft;
  41. }
  42. void FFTwrapper::smps2freqs(const float *smps, fft_t *freqs)
  43. {
  44. //Load data
  45. for(int i = 0; i < fftsize; ++i)
  46. time[i] = static_cast<double>(smps[i]);
  47. //DFT
  48. fftw_execute(planfftw);
  49. //Grab data
  50. memcpy((void *)freqs, (const void *)fft, fftsize * sizeof(double));
  51. }
  52. void FFTwrapper::freqs2smps(const fft_t *freqs, float *smps)
  53. {
  54. //Load data
  55. memcpy((void *)fft, (const void *)freqs, fftsize * sizeof(double));
  56. //clear unused freq channel
  57. fft[fftsize / 2][0] = 0.0f;
  58. fft[fftsize / 2][1] = 0.0f;
  59. //IDFT
  60. fftw_execute(planfftw_inv);
  61. //Grab data
  62. for(int i = 0; i < fftsize; ++i)
  63. smps[i] = static_cast<float>(time[i]);
  64. }
  65. void FFT_cleanup()
  66. {
  67. fftw_cleanup();
  68. }