summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-04 13:08:13 +0100
committerskal <pascal.massimino@gmail.com>2026-02-04 13:08:13 +0100
commitc4888bea71326f7a69e8214af0d9c2a62a60b887 (patch)
treedd1e71be51893414c590db8a7be578ddfd6232f0 /src/tests
parentf64f842bb0dabd89308e2378e56358bc8abdd653 (diff)
feat(audio): Implement audio backend abstraction (Task #51.1)
Created interface-based audio backend system to enable testing without hardware. This is the foundation for robust tracker timing verification. Changes: - Created AudioBackend interface with init/start/shutdown methods - Added test-only hooks: on_voice_triggered() and on_frames_rendered() - Moved miniaudio implementation to MiniaudioBackend class - Refactored audio.cc to use backend abstraction with auto-fallback - Added time tracking to synth.cc (elapsed time from rendered frames) - Created test_audio_backend.cc to verify backend injection works - Fixed audio test linking to include util/procedural dependencies All test infrastructure guarded by #if !defined(STRIP_ALL) for zero size impact on final build. Production path unchanged, 100% backward compatible. All 13 tests pass. handoff(Claude): Task #51.1 complete, audio backend abstraction ready Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_audio_backend.cc119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/tests/test_audio_backend.cc b/src/tests/test_audio_backend.cc
new file mode 100644
index 0000000..050956b
--- /dev/null
+++ b/src/tests/test_audio_backend.cc
@@ -0,0 +1,119 @@
+// This file is part of the 64k demo project.
+// It tests the audio backend abstraction layer.
+// Verifies backend injection and event hooks work correctly.
+
+#include "audio/audio.h"
+#include "audio/audio_backend.h"
+#include "audio/synth.h"
+#include <assert.h>
+#include <stdio.h>
+#include <vector>
+
+#if !defined(STRIP_ALL)
+
+// Simple test backend that records events
+class TestBackend : public AudioBackend {
+ public:
+ struct Event {
+ float timestamp;
+ int spectrogram_id;
+ float volume;
+ float pan;
+ };
+
+ std::vector<Event> events;
+ bool init_called = false;
+ bool start_called = false;
+ bool shutdown_called = false;
+
+ void init() override { init_called = true; }
+
+ void start() override { start_called = true; }
+
+ void shutdown() override { shutdown_called = true; }
+
+ void on_voice_triggered(float timestamp, int spectrogram_id, float volume,
+ float pan) override {
+ events.push_back({timestamp, spectrogram_id, volume, pan});
+ }
+};
+
+void test_backend_injection() {
+ TestBackend backend;
+
+ // Inject test backend before audio_init
+ audio_set_backend(&backend);
+
+ audio_init();
+ assert(backend.init_called);
+
+ audio_start();
+ assert(backend.start_called);
+
+ audio_shutdown();
+ assert(backend.shutdown_called);
+
+ printf("Backend injection test PASSED\n");
+}
+
+void test_event_recording() {
+ TestBackend backend;
+ audio_set_backend(&backend);
+
+ synth_init();
+
+ // Create a dummy spectrogram
+ float data[DCT_SIZE * 2] = {0};
+ Spectrogram spec = {data, data, 2};
+ int id = synth_register_spectrogram(&spec);
+
+ // Trigger a voice
+ synth_trigger_voice(id, 0.8f, -0.5f);
+
+ // Render some frames to advance time
+ float output[1024] = {0};
+ synth_render(output, 256); // ~0.008 sec at 32kHz
+
+ // Verify event was recorded
+ assert(backend.events.size() == 1);
+ assert(backend.events[0].spectrogram_id == id);
+ assert(backend.events[0].volume == 0.8f);
+ assert(backend.events[0].pan == -0.5f);
+ assert(backend.events[0].timestamp == 0.0f); // Triggered before any render
+
+ // Trigger another voice after rendering
+ synth_trigger_voice(id, 1.0f, 0.0f);
+
+ assert(backend.events.size() == 2);
+ assert(backend.events[1].timestamp > 0.0f); // Should be > 0 now
+
+ printf("Event recording test PASSED\n");
+}
+
+void test_default_backend() {
+ // Reset backend to nullptr to test default
+ audio_set_backend(nullptr);
+
+ // This should use MiniaudioBackend by default
+ audio_init();
+ audio_start();
+ audio_shutdown();
+
+ printf("Default backend test PASSED\n");
+}
+
+#endif /* !defined(STRIP_ALL) */
+
+int main() {
+#if !defined(STRIP_ALL)
+ printf("Running Audio Backend tests...\n");
+ test_backend_injection();
+ test_event_recording();
+ test_default_backend();
+ printf("All Audio Backend tests PASSED\n");
+ return 0;
+#else
+ printf("Audio Backend tests skipped (STRIP_ALL enabled)\n");
+ return 0;
+#endif /* !defined(STRIP_ALL) */
+}