optimointeja ja vanha fft takas

This commit is contained in:
2025-08-01 14:37:45 +03:00
parent 1d8d47b374
commit 266e4bd85d
4 changed files with 100 additions and 64 deletions

View File

@ -40,8 +40,8 @@ static int pidPost;
#ifndef EDITOR_CONTROLS
#pragma code_seg(".main")
// FFT buffers
static Complex signal[FFT_SIZE];
static Complex buffer[3 * FFT_SIZE];
static float fft_input[FFT_SIZE];
static float fft_output[FFT_SIZE / 2];
static float fft_uniform[FFT_SIZE / 4];
void entrypoint(void)
@ -123,6 +123,9 @@ int __cdecl main(int argc, char* argv[])
// Play sound
direct_sound_buffer->Play(0, 0, 0);
// Init FFT
init_hamming_window();
PFNGLUNIFORM1FVPROC glUniform1fvProc = ((PFNGLUNIFORM1FVPROC)wglGetProcAddress("glUniform1fv"));
const ULONGLONG targetIntervalMs = 1000 / 60; // For 60 FPS FFT updates
@ -178,7 +181,7 @@ int __cdecl main(int argc, char* argv[])
{
SUsample* samples = (SUsample*)audio_ptr;
for (int i = 0; i < FFT_SIZE; ++i) {
signal[i] = Complex((float)samples[i], 0.0);
fft_input[i] = (float)samples[i];
}
}
@ -186,20 +189,18 @@ int __cdecl main(int argc, char* argv[])
}
// Calculate FFT
fft(signal, FFT_SIZE, buffer);
compute_fft(fft_input, fft_output);
// Normalize output
for (int i = 0; i < (FFT_SIZE / 4); i++)
{
float gain = 0.05f;
float gain = 50.0f;
float alpha = 0.10f; // "Hidastaa" FFT:n piikkejä
float threshhold = 0.00015f; // Alin arvo mik<EFBFBD> p<EFBFBD><EFBFBD>stet<EFBFBD><EFBFBD>n shaderille (v<EFBFBD>hent<EFBFBD><EFBFBD> "noisea")
// float magnitude = sqrt(signal[i].re * signal[i].re + signal[i].im * signal[i].im); // signal strength
float magnitude = (float)signal[i].re;
float x_t = (magnitude < threshhold) ? 0.f : magnitude * gain;
float threshhold = 0.05f; // Alin arvo mikä päästetään shaderille (vähentää "noisea")
float x_t = fft_output[i] * gain;
// Exponential smoothing kaava
// s(t) = alpha*x(t)+(1-alpha)*s(t-1)
fft_uniform[i] = alpha * (x_t)+(1 - alpha) * fft_uniform[i];
fft_uniform[i] = (x_t < threshhold) ? 0.f : alpha * (x_t)+(1 - alpha) * fft_uniform[i];
}
}