This commit is contained in:
2025-07-31 16:27:44 +03:00
parent 35c09187a4
commit aa949ba2a7
3 changed files with 52 additions and 88 deletions

View File

@ -36,14 +36,25 @@
#include "fft.h"
#pragma data_seg(".pids")
// static allocation saves a few bytes
static int pidMain;
static int pidPost;
// static HDC hDC;
#ifndef EDITOR_CONTROLS
#pragma code_seg(".main")
// FFT buffers
static Complex signal[FFT_SIZE];
static Complex buffer[3 * FFT_SIZE];
static float fft_uniform[FFT_SIZE / 4];
static float shapesData[SHAPES_TEX_SIZE];
void entrypoint(void)
#else
#include "editor.h"
@ -112,7 +123,7 @@ int __cdecl main(int argc, char* argv[])
track.play();
double position = 0.0;
#endif
static float syncs[1 + SU_NUMSYNCS];
long playCursor = 0;
long lastPlayCursor = -1;
volatile float maximum = 0.0; // Helper variable to calculate maximum fft output for normalization
@ -123,17 +134,6 @@ int __cdecl main(int argc, char* argv[])
// Play sound
direct_sound_buffer->Play(0, 0, 0);
static float syncs[1 + SU_NUMSYNCS];
// Init FFT
init_hamming_window();
// FFT buffers
static float fft_input[FFT_SIZE];
static float fft_output[FFT_SIZE / 2]; // Magnitudes
static float fft_uniform[FFT_SIZE / 4];
static float shapesData[SHAPES_TEX_SIZE];
// main note effect
boolean beenPlaying = false;
const int SHAPES_SIZE = 15;
@ -221,7 +221,7 @@ int __cdecl main(int argc, char* argv[])
{
SUsample* samples = (SUsample*)audio_ptr;
for (int i = 0; i < FFT_SIZE; ++i) {
fft_input[i] = (float)samples[i];
signal[i] = Complex((float)samples[i], 0.0);
}
}
@ -229,20 +229,23 @@ int __cdecl main(int argc, char* argv[])
}
// Calculate FFT
compute_fft(fft_input, fft_output);
fft(signal, FFT_SIZE, buffer);
// Normalize output
for (int i = 0; i < (FFT_SIZE / 4); i++)
{
float gain = 50.0f;
float alpha = 0.10f; // "Hidastaa" FFT:n piikkej<EFBFBD>
float gain = 0.025f;
float alpha = 0.15f; // "Hidastaa" FFT:n piikkejä
float threshhold = 0.015f; // Alin arvo mik<69> p<><70>stet<65><74>n shaderille (v<>hent<6E><74> "noisea")
float x_t = (fft_output[i] < threshhold) ? 0.f : fft_output[i] * gain;
float magnitude = sqrt(signal[i].re * signal[i].re + signal[i].im * signal[i].im);
float x_t = (magnitude < threshhold) ? 0.f : magnitude * 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];
}
}
syncs[0] = (float)playCursor / (SU_SAMPLE_RATE * SU_CHANNEL_COUNT * SU_SAMPLE_SIZE); // Aika sekunteina.
for (int i = 0; i < SU_NUMSYNCS; ++i)