summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-01-27 22:16:23 +0100
committerskal <pascal.massimino@gmail.com>2026-01-27 22:16:23 +0100
commitad4f87e0ebfd361c69c7ba9adc29292305f21f7c (patch)
tree7e3d4feffce3cac26139df1ace2f879e62bfc00c /src/main.cpp
parent2f68b86ba403fdae97c00569b6bb9b58ad1f33a6 (diff)
feat(audio): Implement real-time spectrogram synthesizer
Adds a multi-voice, real-time audio synthesis engine that generates sound from spectrogram data using an Inverse Discrete Cosine Transform (IDCT). Key features: - A thread-safe, double-buffered system for dynamically updating spectrograms in real-time without interrupting audio playback. - Core DSP components: FDCT, IDCT, and Hamming window functions. - A simple sequencer in the main loop to demonstrate scripted audio events and dynamic updates. - Unit tests for the new synth engine and Hamming window, integrated with CTest. - A file documenting the build process, features, and how to run tests.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 3b61e1e..c1e2789 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,14 +1,64 @@
#include "platform.h"
#include "gpu/gpu.h"
#include "audio/audio.h"
+#include "audio/synth.h"
+#include "util/math.h"
+#include <string.h>
+#include <math.h>
+
+#define DEMO_BPM 120.0f
+#define SECONDS_PER_BEAT (60.0f / DEMO_BPM)
+#define SPEC_FRAMES 16
+
+static float g_spec_buffer_a[SPEC_FRAMES * DCT_SIZE];
+static float g_spec_buffer_b[SPEC_FRAMES * DCT_SIZE];
+
+void generate_tone(float* buffer, float freq) {
+ memset(buffer, 0, SPEC_FRAMES * DCT_SIZE * sizeof(float));
+ for (int frame = 0; frame < SPEC_FRAMES; ++frame) {
+ float* spec_frame = buffer + frame * DCT_SIZE;
+ float amplitude = powf(1.0f - (float)frame / SPEC_FRAMES, 2.0f);
+
+ int bin = (int)(freq / (32000.0f / 2.0f) * DCT_SIZE);
+ if (bin > 0 && bin < DCT_SIZE) {
+ spec_frame[bin] = amplitude;
+ }
+ }
+}
int main() {
platform_init();
gpu_init(platform_get_window());
audio_init();
+ generate_tone(g_spec_buffer_a, 440.0f); // A4
+ generate_tone(g_spec_buffer_b, 880.0f); // A5
+
+ Spectrogram spec = { g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES };
+ int tone_id = synth_register_spectrogram(&spec);
+
+ double last_beat_time = 0.0;
+ int beat_count = 0;
+
while (!platform_should_close()) {
platform_poll();
+
+ double current_time = platform_get_time();
+ if (current_time - last_beat_time > SECONDS_PER_BEAT) {
+ synth_trigger_voice(tone_id, 0.5f, 0.0f);
+ last_beat_time = current_time;
+ beat_count++;
+
+ if (beat_count == 8) {
+ // Time to update the sound!
+ float* back_buffer = synth_begin_update(tone_id);
+ if (back_buffer) {
+ generate_tone(back_buffer, 220.0f); // A3
+ synth_commit_update(tone_id);
+ }
+ }
+ }
+
gpu_draw();
audio_update();
}