SpectrumAnalyzer.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include <atomic>
  3. #include <cstddef>
  4. #include <vector>
  5. #include "kiss_fft.h"
  6. namespace WallpaperEngine::Audio {
  7. // 64 band left/right spectrum like wallpaper64.exe's capture thread (sub_1400B2850): blocks of 2/3 the FFT size, no
  8. // overlap, bins folded on a pow(x, 0.25) curve, linear magnitude
  9. class SpectrumAnalyzer {
  10. public:
  11. static constexpr int BANDS = 64;
  12. explicit SpectrumAnalyzer (int sampleRate);
  13. ~SpectrumAnalyzer ();
  14. SpectrumAnalyzer (const SpectrumAnalyzer&) = delete;
  15. SpectrumAnalyzer& operator= (const SpectrumAnalyzer&) = delete;
  16. // one packet of 1-2 channel samples, the rest past the block's end is dropped like WE; true with bands [left 64 |
  17. // right 64] when a block completed
  18. bool feed (const float* samples, std::size_t frames, int channels, float* bands);
  19. void reset ();
  20. /** WE's audioinputvolume, 50 is neutral; safe to call from another thread */
  21. void setInputVolume (int volume);
  22. [[nodiscard]] int getFFTSize () const { return this->m_size; }
  23. [[nodiscard]] int getBlockSize () const { return this->m_blockSize; }
  24. private:
  25. void analyze (int channels, float* bands);
  26. int m_size;
  27. int m_blockSize;
  28. int m_binCount;
  29. int m_filled = 0;
  30. std::atomic<float> m_gain { 1.0f };
  31. kiss_fft_cfg m_config;
  32. std::vector<kiss_fft_cpx> m_input[2];
  33. std::vector<kiss_fft_cpx> m_output;
  34. };
  35. }