BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

analyzer.hpp (2459B)


      1 #pragma once
      2 
      3 #include "assert.h"
      4 #include <math.h>
      5 
      6 #include "ffft/FFTReal.h"
      7 
      8 #include "buffer.hpp"
      9 
     10 namespace bogaudio {
     11 namespace dsp {
     12 
     13 struct Window {
     14 	int _size;
     15 	float* _window;
     16 	float _sum;
     17 
     18 	Window(int size)
     19 	: _size(size)
     20 	, _window(new float[_size] {})
     21 	, _sum(0.0)
     22 	{}
     23 	virtual ~Window() {
     24 		delete[] _window;
     25 	}
     26 
     27 	inline int size() { return _size; }
     28 	inline float at(int i) { assert(i >= 0 && i < _size); return _window[i]; }
     29 	inline float sum() { return _sum; }
     30 	void apply(float* in, float* out);
     31 };
     32 
     33 struct HanningWindow : Window {
     34 	HanningWindow(int size, float alpha = 0.5);
     35 };
     36 
     37 struct HammingWindow : HanningWindow {
     38 	HammingWindow(int size) : HanningWindow(size, 0.54) {}
     39 };
     40 
     41 struct KaiserWindow : Window {
     42 	KaiserWindow(int size, float alpha = 7.865f);
     43 
     44 	float i0(float x);
     45 };
     46 
     47 struct PlanckTaperWindow : Window {
     48 	PlanckTaperWindow(int size, int taperSamples);
     49 };
     50 
     51 struct FFT1024 {
     52 	void* _fft = NULL;
     53 	FFT1024();
     54 	~FFT1024();
     55 
     56 	void do_fft(float* out, float* in);
     57 };
     58 
     59 struct FFT4096 {
     60 	void* _fft = NULL;
     61 	FFT4096();
     62 	~FFT4096();
     63 
     64 	void do_fft(float* out, float* in);
     65 };
     66 
     67 struct FFT8192 {
     68 	void* _fft = NULL;
     69 	FFT8192();
     70 	~FFT8192();
     71 
     72 	void do_fft(float* out, float* in);
     73 };
     74 
     75 struct FFT16384 {
     76 	void* _fft = NULL;
     77 	FFT16384();
     78 	~FFT16384();
     79 
     80 	void do_fft(float* out, float* in);
     81 };
     82 
     83 struct FFT32768 {
     84 	void* _fft = NULL;
     85 	FFT32768();
     86 	~FFT32768();
     87 
     88 	void do_fft(float* out, float* in);
     89 };
     90 
     91 struct SpectrumAnalyzer : OverlappingBuffer<float> {
     92 	enum Size {
     93 		SIZE_128 = 128,
     94 		SIZE_256 = 256,
     95 		SIZE_512 = 512,
     96 		SIZE_1024 = 1024,
     97 		SIZE_2048 = 2048,
     98 		SIZE_4096 = 4096,
     99 		SIZE_8192 = 8192,
    100 		SIZE_16384 = 16384,
    101 		SIZE_32768 = 32768
    102 	};
    103 	static constexpr Size maxSize = SIZE_32768;
    104 
    105 	enum Overlap {
    106 		OVERLAP_1 = 1,
    107 		OVERLAP_2 = 2,
    108 		OVERLAP_4 = 4,
    109 		OVERLAP_8 = 8
    110 	};
    111 
    112 	enum WindowType {
    113 		WINDOW_NONE,
    114 		WINDOW_HANNING,
    115 		WINDOW_HAMMING,
    116 		WINDOW_KAISER
    117 	};
    118 
    119 	const float _sampleRate;
    120 	ffft::FFTReal<float>* _fft = NULL;
    121 	FFT1024* _fft1024 = NULL;
    122 	FFT4096* _fft4096 = NULL;
    123 	FFT8192* _fft8192 = NULL;
    124 	FFT16384* _fft16384 = NULL;
    125 	FFT32768* _fft32768 = NULL;
    126 	Window* _window = NULL;
    127 	float* _windowOut = NULL;
    128 	float* _fftOut = NULL;
    129 
    130 	SpectrumAnalyzer(
    131 		Size size,
    132 		Overlap overlap,
    133 		WindowType windowType,
    134 		float sampleRate,
    135 		bool autoProcess = true
    136 	);
    137 	virtual ~SpectrumAnalyzer();
    138 
    139 	void processBuffer(float* samples) override;
    140 	void getMagnitudes(float* bins, int nBins);
    141 };
    142 
    143 } // namespace dsp
    144 } // namespace bogaudio