fft_helpers.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * fft_helpers.h - some functions around FFT analysis
  3. *
  4. * Copyright (c) 2008-2012 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. * Copyright (c) 2019 Martin Pavelek <he29.HS/at/gmail.com>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef FFT_HELPERS_H
  26. #define FFT_HELPERS_H
  27. #include "lmms_export.h"
  28. #include <vector>
  29. #include <fftw3.h>
  30. // NOTE: FFT_BUFFER_SIZE should be considered deprecated!
  31. // It is used by Eq plugin and some older code here, but this should be a user
  32. // switchable parameter, not a constant. Use a value from FFT_BLOCK_SIZES
  33. const unsigned int FFT_BUFFER_SIZE = 2048;
  34. // Allowed FFT block sizes. Ranging from barely useful to barely acceptable
  35. // because of performance and latency reasons.
  36. const std::vector<unsigned int> FFT_BLOCK_SIZES = {256, 512, 1024, 2048, 4096, 8192, 16384};
  37. // List of FFT window functions supported by precomputeWindow()
  38. enum FFT_WINDOWS
  39. {
  40. RECTANGULAR = 0,
  41. BLACKMAN_HARRIS,
  42. HAMMING,
  43. HANNING
  44. };
  45. /** Returns biggest value from abs_spectrum[spec_size] array.
  46. *
  47. * @return -1 on error, 0 on success
  48. */
  49. float LMMS_EXPORT maximum(const float *abs_spectrum, unsigned int spec_size);
  50. float LMMS_EXPORT maximum(const std::vector<float> &abs_spectrum);
  51. /** Normalize the abs_spectrum array of absolute values to a 0..1 range
  52. * based on supplied energy and stores it in the norm_spectrum array.
  53. *
  54. * @return -1 on error
  55. */
  56. int LMMS_EXPORT normalize(const float *abs_spectrum, float *norm_spectrum, unsigned int bin_count, unsigned int block_size);
  57. int LMMS_EXPORT normalize(const std::vector<float> &abs_spectrum, std::vector<float> &norm_spectrum, unsigned int block_size);
  58. /** Check if the spectrum contains any non-zero value.
  59. *
  60. * @return 1 if spectrum contains any non-zero value
  61. * @return 0 otherwise
  62. */
  63. int LMMS_EXPORT notEmpty(const std::vector<float> &spectrum);
  64. /** Precompute a window function for later real-time use.
  65. * Set normalized to false if you do not want to apply amplitude correction.
  66. *
  67. * @return -1 on error
  68. */
  69. int LMMS_EXPORT precomputeWindow(float *window, unsigned int length, FFT_WINDOWS type, bool normalized = true);
  70. /** Compute absolute values of complex_buffer, save to absspec_buffer.
  71. * Take care that - compl_len is not bigger than complex_buffer!
  72. * - absspec buffer is big enough!
  73. *
  74. * @return 0 on success, else -1
  75. */
  76. int LMMS_EXPORT absspec(const fftwf_complex *complex_buffer, float *absspec_buffer,
  77. unsigned int compl_length);
  78. /** Build fewer subbands from many absolute spectrum values.
  79. * Take care that - compressedbands[] array num_new elements long
  80. * - num_old > num_new
  81. *
  82. * @return 0 on success, else -1
  83. */
  84. int LMMS_EXPORT compressbands(const float * _absspec_buffer, float * _compressedband,
  85. int _num_old, int _num_new, int _bottom, int _top);
  86. #endif