SaProcessor.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <atomic>
  29. #include <QColor>
  30. #include <QMutex>
  31. #include <vector>
  32. #include "fft_helpers.h"
  33. #include "SaControls.h"
  34. template<class T>
  35. class LocklessRingBuffer;
  36. //! Receives audio data, runs FFT analysis and stores the result.
  37. class SaProcessor
  38. {
  39. public:
  40. explicit SaProcessor(const SaControls *controls);
  41. virtual ~SaProcessor();
  42. // analysis thread and a method to terminate it
  43. void analyze(LocklessRingBuffer<sampleFrame> &ring_buffer);
  44. void terminate() {m_terminate = true;}
  45. // inform processor if any processing is actually required
  46. void setSpectrumActive(bool active);
  47. void setWaterfallActive(bool active);
  48. void flipRequest() {m_flipRequest = true;} // request refresh of history buffer
  49. // configuration is taken from models in SaControls; some changes require
  50. // an exlicit update request (reallocation and window rebuild)
  51. void reallocateBuffers();
  52. void rebuildWindow();
  53. void clear();
  54. void clearHistory();
  55. const float *getSpectrumL() const {return m_normSpectrumL.data();}
  56. const float *getSpectrumR() const {return m_normSpectrumR.data();}
  57. const uchar *getHistory() const {return m_history.data();}
  58. // information about results and unit conversion helpers
  59. unsigned int inBlockSize() const {return m_inBlockSize;}
  60. unsigned int binCount() const; //!< size of output (frequency domain) data block
  61. bool spectrumNotEmpty(); //!< check if result buffers contain any non-zero values
  62. unsigned int waterfallWidth() const; //!< binCount value capped at 3840 (for display)
  63. unsigned int waterfallHeight() const {return m_waterfallHeight;}
  64. bool waterfallNotEmpty() const {return m_waterfallNotEmpty;}
  65. float binToFreq(unsigned int bin_index) const;
  66. float binBandwidth() const;
  67. float freqToXPixel(float frequency, unsigned int width) const;
  68. float xPixelToFreq(float x, unsigned int width) const;
  69. float ampToYPixel(float amplitude, unsigned int height) const;
  70. float yPixelToAmp(float y, unsigned int height) const;
  71. unsigned int getSampleRate() const;
  72. float getNyquistFreq() const;
  73. float getFreqRangeMin(bool linear = false) const;
  74. float getFreqRangeMax() const;
  75. float getAmpRangeMin(bool linear = false) const;
  76. float getAmpRangeMax() const;
  77. // Reallocation lock prevents the processor from changing size of its buffers.
  78. // It is used to keep consistent bin-to-frequency mapping while drawing the
  79. // spectrum and to make sure reading side does not find itself out of bounds.
  80. // The processor is meanwhile free to work on another block.
  81. QMutex m_reallocationAccess;
  82. // Data access lock prevents the processor from changing both size and content
  83. // of its buffers. It is used when writing to a result buffer, or when a friendly
  84. // class reads them and needs guaranteed data consistency.
  85. // It causes FFT analysis to be paused, so this lock should be used sparingly.
  86. // If using both locks at the same time, reallocation lock MUST be acquired first.
  87. QMutex m_dataAccess;
  88. private:
  89. const SaControls *m_controls;
  90. // thread communication and control
  91. bool m_terminate;
  92. // currently valid configuration
  93. unsigned int m_zeroPadFactor = 2; //!< use n-steps bigger FFT for given block size
  94. std::atomic<unsigned int> m_inBlockSize;//!< size of input (time domain) data block
  95. unsigned int m_fftBlockSize; //!< size of padded block for FFT processing
  96. unsigned int m_sampleRate;
  97. // data buffers (roughly in the order of processing, from input to output)
  98. unsigned int m_framesFilledUp;
  99. std::vector<float> m_bufferL; //!< time domain samples (left)
  100. std::vector<float> m_bufferR; //!< time domain samples (right)
  101. std::vector<float> m_fftWindow; //!< precomputed window function coefficients
  102. std::vector<float> m_filteredBufferL; //!< time domain samples with window function applied (left)
  103. std::vector<float> m_filteredBufferR; //!< time domain samples with window function applied (right)
  104. fftwf_plan m_fftPlanL;
  105. fftwf_plan m_fftPlanR;
  106. fftwf_complex *m_spectrumL; //!< frequency domain samples (complex) (left)
  107. fftwf_complex *m_spectrumR; //!< frequency domain samples (complex) (right)
  108. std::vector<float> m_absSpectrumL; //!< frequency domain samples (absolute) (left)
  109. std::vector<float> m_absSpectrumR; //!< frequency domain samples (absolute) (right)
  110. std::vector<float> m_normSpectrumL; //!< frequency domain samples (normalized) (left)
  111. std::vector<float> m_normSpectrumR; //!< frequency domain samples (normalized) (right)
  112. // spectrum history for waterfall: new normSpectrum lines are added on top
  113. std::vector<uchar> m_history_work; //!< local history buffer for render
  114. std::vector<uchar> m_history; //!< public buffer for reading
  115. bool m_flipRequest; //!< update public buffer only when requested
  116. std::atomic<unsigned int> m_waterfallHeight; //!< number of stored lines in history buffer
  117. // Note: high values may make it harder to see transients.
  118. const unsigned int m_waterfallMaxWidth = 3840;
  119. // book keeping
  120. bool m_spectrumActive;
  121. bool m_waterfallActive;
  122. std::atomic<unsigned int> m_waterfallNotEmpty; //!< number of lines remaining visible on display
  123. bool m_reallocating;
  124. // merge L and R channels and apply gamma correction to make a spectrogram pixel
  125. QRgb makePixel(float left, float right) const;
  126. #ifdef SA_DEBUG
  127. unsigned int m_last_dump_time;
  128. unsigned int m_dump_count;
  129. float m_sum_execution;
  130. float m_max_execution;
  131. #endif
  132. };
  133. #endif // SAPROCESSOR_H