1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#ifndef AUDIO_FFT_H
#define AUDIO_FFT_H
// NOTE: radix-2 decimation in time fft, split complex (struct of arrays) layout.
// re[] and im[] are separate contiguous arrays so a butterfly loads 4 reals and 4 imaginaries
// with two plain vector loads, no shuffles. twiddles are precomputed per stage and packed
// back to back (stage with half size h lives at [h-1, 2h-1)) so each stage streams through
// its own small table linearly.
typedef struct FFT FFT;
struct FFT {
u32 size;
u32 log2_size;
u16 *bit_reverse;
f32 *twiddle_re;
f32 *twiddle_im;
};
internal void fft_init(Arena *arena, FFT *fft, u32 size_pow2);
internal void fft_forward(FFT *fft, f32 *re, f32 *im); // in place
//- spectrum analyser
#define SPECTRUM_FFT_SIZE 2048
#define SPECTRUM_BARS 56
typedef struct Spectrum Spectrum;
struct Spectrum {
FFT fft;
u32 rate;
f32 *window; // hann
f32 *re;
f32 *im;
f32 *power;
u16 bar_first[SPECTRUM_BARS];
u16 bar_last[SPECTRUM_BARS];
f32 bars[SPECTRUM_BARS]; // smoothed 0..1, what the ui draws
f32 peaks[SPECTRUM_BARS]; // slow falling peak caps
f32 level; // smoothed overall loudness 0..1
};
internal void spectrum_init(Arena *arena, Spectrum *spectrum);
internal void spectrum_set_rate(Spectrum *spectrum, u32 rate);
// stereo has SPECTRUM_FFT_SIZE interleaved frames, the most recent audio that was played
internal void spectrum_update(Spectrum *spectrum, const f32 *stereo, f32 dt);
internal void spectrum_decay(Spectrum *spectrum, f32 dt);
#endif // AUDIO_FFT_H
|