VJ Master 1.5
Real-time audio analysis and visualisation.
Loading...
Searching...
No Matches
ST_FFTThread.h
1// Copyright (c) 2025 Aaron Trotter (ShaderTech). All Rights Reserved.
2
3#pragma once
4#include "CoreMinimal.h"
5#include "HAL/ThreadSafeBool.h"
6#include "HAL/RunnableThread.h"
7#include "HAL/Runnable.h"
8#include "HAL/Event.h"
9
10#if WITH_KISSFFT
11#include "kiss_fft.h"
12#include "tools/kiss_fftr.h"
13#endif
14#include "Main/ST_AudioProcessingSettings.h"
15
19class ST_AUDIOPROCESSING_API FST_FFTThread : public FRunnable
20{
21public:
30 FString InName,
31 FFFTIOData InFFTIOData,
32 TObjectPtr<UFFTConfig> InFFTConfig,
33 class UST_AudioIOBase* InAnalyserBase);
34
37
43 virtual bool CreateThread(bool bStartPaused);
44
46 virtual void PauseThread();
47
49 virtual void ContinueThread();
50
52 virtual void Main();
53
55 virtual void ExTrigger();
56
61 virtual bool IsThreadPaused() const;
62
67 virtual void EnsureCompletion();
68
69protected:
71 virtual bool Init() override;
72
74 virtual uint32 Run() override;
75
77 virtual void Stop() override;
78
80 virtual void Exit() override;
81
82private:
84 bool Compute_FFT();
85
87 void Compute_FrequencyBands(uint32 ChannelIndex);
88
90 void Compute_Amplitude(uint32 ChannelIndex);
91
93 void Compute_Pitch(uint32 ChannelIndex);
94
96 void Compute_Beat(uint32 ChannelIndex);
97
104 float PerformChecksum(const float* buffer, uint32 size);
105
111 void Filter(FFFTFilterConfig InFilterConfig, TArray<float>& FilteredMagnitudes, uint32 ChannelIndex);
112
114 void Filter_Butterworth(FFFTFilterConfig InFilterConfig, TArray<float>& FilteredMagnitudes, uint32 ChannelIndex);
115
117 void Filter_ChebyshevTypeI(FFFTFilterConfig InFilterConfig, TArray<float>& FilteredMagnitudes, uint32 ChannelIndex);
118
120 bool Windowing_Hann(uint32 ChannelIndex);
121
123 bool Windowing_Hamming(uint32 ChannelIndex);
124
126 bool Windowing_BlackmanHarris(uint32 ChannelIndex);
127
129 bool Windowing_Gaussian(uint32 ChannelIndex);
130
132 void SpectrumSpacing_Log();
133
135 void SpectrumSpacing_OctaveSubdivision();
136
138 void SpectrumSpacing_Linear();
139
141 void SpectrumSpacing_Quadratic();
142
143private:
145 TUniquePtr<FRunnableThread> Thread = nullptr;
146
148 FEvent* m_semaphore = nullptr;
149
151 FThreadSafeBool m_Kill;
152
154 FThreadSafeBool m_Pause;
155
157 FString m_name = "";
158
160 FCriticalSection FFTLock;
161
163 FFFTIOData FFTIOData;
164
166 TObjectPtr<UFFTConfig> FFTConfig = nullptr;
167
169 TFunction<void(const TArray<float>&)> m_OnProcessedAudioReady;
170
171#if WITH_KISSFFT
173 kiss_fft_cpx* fftOutputBuffer = nullptr;
174
176 kiss_fftr_cfg cfg = NULL;
177#endif
178
180 TArray<float> RawMagnitudes;
181
183 TArray<float> RawMagnitudesHistory;
184
186 TArray<float> FrequencyBandsHistory;
187
189 TArray<float> FrequencyBands;
190
192 TArray<float> FrequencyBandEdges;
193
195 TArray<uint32> FrequencyBinsByBand;
196
198 TArray<float> AmplitudeHistory;
199
201 TArray<float> PitchHistory;
202
204 TArray<float> BeatHistory;
205
207 TArray<float> BeatRawMagnitudes;
208
210 TArray<FChannelBeatDetection> BeatDetectionForChannel;
211
213 TArray<bool> ProcessedChannel;
214
216 class UST_AudioIOBase* cachedAnalyserBase = nullptr;
217
219 TArray<float*> PaddedFFTInputBuffer;
220
222 TArray<float> LastBufferChecksum;
223
225 uint32 ChannelMultiplier = 1;
226
228 TArray<FDetectionBeat> DetectedBeats;
229
231 int32 BeatSkipCount = 0;
232
234 std::atomic<bool> bFFTEventPending{ false };
235};
236
238{
239 return (bool)m_Pause;
240}
241
243{
244 m_Pause = true;
245}
virtual void Exit() override
Clean up after thread has stopped.
Definition ST_FFTThread.cpp:145
virtual void EnsureCompletion()
Use this method to kill the thread! Safely ensures thread completion.
Definition ST_FFTThread.cpp:63
FST_FFTThread(FString InName, FFFTIOData InFFTIOData, TObjectPtr< UFFTConfig > InFFTConfig, class UST_AudioIOBase *InAnalyserBase)
Constructor.
Definition ST_FFTThread.cpp:16
virtual bool CreateThread(bool bStartPaused)
Use this method to create the thread!
Definition ST_FFTThread.cpp:34
virtual bool Init() override
Initialize the thread. Called once before Run().
Definition ST_FFTThread.cpp:81
virtual void Main()
The main processing method.
Definition ST_FFTThread.cpp:253
virtual void PauseThread()
Pause the thread.
Definition ST_FFTThread.h:242
virtual void ContinueThread()
Continue/unpause the thread.
Definition ST_FFTThread.cpp:52
virtual bool IsThreadPaused() const
Check if the thread is currently paused.
Definition ST_FFTThread.h:237
virtual void Stop() override
Request the thread to stop.
Definition ST_FFTThread.cpp:220
virtual uint32 Run() override
Main loop where work is performed.
Definition ST_FFTThread.cpp:186
virtual void ExTrigger()
Wake the FFT worker thread if it is waiting.
Definition ST_FFTThread.cpp:231
Base class for Audio IO functionality in ShaderTech.
Definition ST_AudioIOBase.h:40
Configuration structure for FFT filter settings. Contains various filter settings including the filte...
Definition ST_AudioProcessingSettings.h:323
Structure that holds the miniaudio playback and capture settings.
Definition ST_AudioProcessingSettings.h:835