SaProcessor.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* SaProcessor.h - declaration of SaProcessor class.
  2. *
  3. * Copyright (c) 2019 Martin Pavelek <he29/dot/HS/at/gmail/dot/com>
  4. *
  5. * Based partially on Eq plugin code,
  6. * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #ifndef SAPROCESSOR_H
  27. #define SAPROCESSOR_H
  28. #include <QColor>
  29. #include <QMutex>
  30. #include <vector>
  31. #include "fft_helpers.h"
  32. #include "SaControls.h"
  33. //! Receives audio data, runs FFT analysis and stores the result.
  34. class SaProcessor
  35. {
  36. public:
  37. explicit SaProcessor(SaControls *controls);
  38. virtual ~SaProcessor();
  39. void analyse(sampleFrame *in_buffer, const fpp_t frame_count);
  40. // inform processor if any processing is actually required
  41. void setSpectrumActive(bool active);
  42. void setWaterfallActive(bool active);
  43. // configuration is taken from models in SaControls; some changes require
  44. // an exlicit update request (reallocation and window rebuild)
  45. void reallocateBuffers();
  46. void rebuildWindow();
  47. void clear();
  48. // information about results and unit conversion helpers
  49. float binToFreq(unsigned int bin_index) const;
  50. float binBandwidth() const;
  51. float freqToXPixel(float frequency, unsigned int width) const;
  52. float xPixelToFreq(float x, unsigned int width) const;
  53. float ampToYPixel(float amplitude, unsigned int height) const;
  54. float yPixelToAmp(float y, unsigned int height) const;
  55. unsigned int getSampleRate() const;
  56. float getNyquistFreq() const;
  57. float getFreqRangeMin(bool linear = false) const;
  58. float getFreqRangeMax() const;
  59. float getAmpRangeMin(bool linear = false) const;
  60. float getAmpRangeMax() const;
  61. // data access lock must be acquired by any friendly class that touches
  62. // the results, mainly to prevent unexpected mid-way reallocation
  63. QMutex m_dataAccess;
  64. private:
  65. SaControls *m_controls;
  66. // currently valid configuration
  67. const unsigned int m_zeroPadFactor = 2; //!< use n-steps bigger FFT for given block size
  68. unsigned int m_inBlockSize; //!< size of input (time domain) data block
  69. unsigned int m_fftBlockSize; //!< size of padded block for FFT processing
  70. unsigned int m_sampleRate;
  71. unsigned int binCount() const; //!< size of output (frequency domain) data block
  72. // data buffers (roughly in the order of processing, from input to output)
  73. unsigned int m_framesFilledUp;
  74. std::vector<float> m_bufferL; //!< time domain samples (left)
  75. std::vector<float> m_bufferR; //!< time domain samples (right)
  76. std::vector<float> m_fftWindow; //!< precomputed window function coefficients
  77. fftwf_plan m_fftPlanL;
  78. fftwf_plan m_fftPlanR;
  79. fftwf_complex *m_spectrumL; //!< frequency domain samples (complex) (left)
  80. fftwf_complex *m_spectrumR; //!< frequency domain samples (complex) (right)
  81. std::vector<float> m_absSpectrumL; //!< frequency domain samples (absolute) (left)
  82. std::vector<float> m_absSpectrumR; //!< frequency domain samples (absolute) (right)
  83. std::vector<float> m_normSpectrumL; //!< frequency domain samples (normalized) (left)
  84. std::vector<float> m_normSpectrumR; //!< frequency domain samples (normalized) (right)
  85. // spectrum history for waterfall: new normSpectrum lines are added on top
  86. std::vector<uchar> m_history;
  87. const unsigned int m_waterfallHeight = 200; // Number of stored lines.
  88. // Note: high values may make it harder to see transients.
  89. // book keeping
  90. bool m_spectrumActive;
  91. bool m_waterfallActive;
  92. unsigned int m_waterfallNotEmpty;
  93. bool m_reallocating;
  94. // merge L and R channels and apply gamma correction to make a spectrogram pixel
  95. QRgb makePixel(float left, float right, float gamma_correction = 0.30) const;
  96. friend class SaSpectrumView;
  97. friend class SaWaterfallView;
  98. };
  99. #endif // SAPROCESSOR_H