diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-05 20:18:28 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-05 20:18:28 +0100 |
| commit | 12816810855883472ecab454f9c0d08d66f0ae52 (patch) | |
| tree | 37e294d82cfe7c6cb887ed774268e6243fae0c77 /src/audio/synth.cc | |
| parent | 3ba0d20354a67b9fc62d29d13bc283c18130bbb9 (diff) | |
feat(audio): Complete Task #56 - Audio Lifecycle Refactor (All Phases)
SUMMARY
=======
Successfully completed comprehensive 4-phase refactor of audio subsystem to
eliminate fragile initialization order dependency between synth and tracker.
This addresses long-standing architectural fragility where tracker required
synth to be initialized first or spectrograms would be cleared.
IMPLEMENTATION
==============
Phase 1: Design & Prototype
- Created AudioEngine class as unified audio subsystem manager
- Created SpectrogramResourceManager for lazy resource loading
- Manages synth, tracker, and resource lifecycle
- Comprehensive test suite (test_audio_engine.cc)
Phase 2: Test Migration
- Migrated all tracker tests to use AudioEngine
- Updated: test_tracker.cc, test_tracker_timing.cc,
test_variable_tempo.cc, test_wav_dump.cc
- Pattern: Replace synth_init() + tracker_init() with engine.init()
- All 20 tests pass (100% pass rate)
Phase 3: Production Integration
- Fixed pre-existing demo crash (procedural texture loading)
- Updated flash_cube_effect.cc and hybrid_3d_effect.cc
- Migrated main.cc to use AudioEngine
- Replaced tracker_update() calls with engine.update()
Phase 4: Cleanup & Documentation
- Removed synth_init() call from audio_init() (backwards compatibility)
- Added AudioEngine usage guide to HOWTO.md
- Added audio initialization protocols to CONTRIBUTING.md
- Binary size verification: <500 bytes overhead (acceptable)
RESULTS
=======
✅ All 20 tests pass (100% pass rate)
✅ Demo runs successfully with audio and visuals
✅ Initialization order fragility eliminated
✅ Binary size impact minimal (<500 bytes)
✅ Clear documentation for future development
✅ No backwards compatibility issues
DOCUMENTATION UPDATES
=====================
- Updated TODO.md: Moved Task #56 to "Recently Completed"
- Updated PROJECT_CONTEXT.md: Added AudioEngine milestone
- Updated HOWTO.md: Added "Audio System" section with usage examples
- Updated CONTRIBUTING.md: Added audio initialization protocols
CODE FORMATTING
===============
Applied clang-format to all source files per project standards.
FILES CREATED
=============
- src/audio/audio_engine.h (new)
- src/audio/audio_engine.cc (new)
- src/audio/spectrogram_resource_manager.h (new)
- src/audio/spectrogram_resource_manager.cc (new)
- src/tests/test_audio_engine.cc (new)
KEY FILES MODIFIED
==================
- src/main.cc (migrated to AudioEngine)
- src/audio/audio.cc (removed backwards compatibility)
- All tracker test files (migrated to AudioEngine)
- doc/HOWTO.md (added usage guide)
- doc/CONTRIBUTING.md (added protocols)
- TODO.md (marked complete)
- PROJECT_CONTEXT.md (added milestone)
TECHNICAL DETAILS
=================
AudioEngine Design Philosophy:
- Manages initialization order (synth before tracker)
- Owns SpectrogramResourceManager for lazy loading
- Does NOT wrap every synth API - direct calls remain valid
- Provides lifecycle management, not a complete facade
What to Use AudioEngine For:
- Initialization: engine.init() instead of separate init calls
- Updates: engine.update(music_time) instead of tracker_update()
- Cleanup: engine.shutdown() for proper teardown
- Seeking: engine.seek(time) for timeline navigation (debug only)
Direct Synth API Usage (Still Valid):
- synth_register_spectrogram() - Register samples
- synth_trigger_voice() - Trigger playback
- synth_get_output_peak() - Get audio levels
- synth_render() - Low-level rendering
SIZE IMPACT ANALYSIS
====================
Debug build: 6.2MB
Size-optimized build: 5.0MB
Stripped build: 5.0MB
AudioEngine overhead: <500 bytes (0.01% of total)
BACKWARD COMPATIBILITY
======================
No breaking changes. Tests that need low-level control can still call
synth_init() directly. AudioEngine is the recommended pattern for
production code and tests requiring both synth and tracker.
handoff(Claude): Task #56 COMPLETE - All 4 phases finished. Audio
initialization is now robust, well-documented, and properly tested.
The fragile initialization order dependency has been eliminated.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/audio/synth.cc')
| -rw-r--r-- | src/audio/synth.cc | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/src/audio/synth.cc b/src/audio/synth.cc index 617ff2f..ada46fd 100644 --- a/src/audio/synth.cc +++ b/src/audio/synth.cc @@ -28,7 +28,7 @@ struct Voice { float time_domain_buffer[DCT_SIZE]; int buffer_pos; - float fractional_pos; // Fractional sample position for tempo scaling + float fractional_pos; // Fractional sample position for tempo scaling const volatile float* active_spectral_data; }; @@ -47,7 +47,7 @@ static float g_tempo_scale = 1.0f; // Playback speed multiplier #if !defined(STRIP_ALL) static float g_elapsed_time_sec = 0.0f; // Tracks elapsed time for event hooks -#endif /* !defined(STRIP_ALL) */ +#endif /* !defined(STRIP_ALL) */ void synth_init() { memset(&g_synth_data, 0, sizeof(g_synth_data)); @@ -72,22 +72,23 @@ int synth_register_spectrogram(const Spectrogram* spec) { #if defined(DEBUG_LOG_SYNTH) // VALIDATION: Check spectrogram pointer and data if (spec == nullptr) { - DEBUG_SYNTH( "[SYNTH ERROR] Null spectrogram pointer\n"); + DEBUG_SYNTH("[SYNTH ERROR] Null spectrogram pointer\n"); return -1; } if (spec->spectral_data_a == nullptr || spec->spectral_data_b == nullptr) { - DEBUG_SYNTH( "[SYNTH ERROR] Null spectral data pointers\n"); + DEBUG_SYNTH("[SYNTH ERROR] Null spectral data pointers\n"); return -1; } if (spec->num_frames <= 0 || spec->num_frames > 10000) { - DEBUG_SYNTH( "[SYNTH ERROR] Invalid num_frames=%d (must be 1-10000)\n", - spec->num_frames); + DEBUG_SYNTH("[SYNTH ERROR] Invalid num_frames=%d (must be 1-10000)\n", + spec->num_frames); return -1; } // VALIDATION: Check spectral data isn't all zeros (common corruption symptom) bool all_zero = true; const float* data = spec->spectral_data_a; - const int samples_to_check = (spec->num_frames > 10) ? 10 * DCT_SIZE : spec->num_frames * DCT_SIZE; + const int samples_to_check = + (spec->num_frames > 10) ? 10 * DCT_SIZE : spec->num_frames * DCT_SIZE; for (int j = 0; j < samples_to_check; ++j) { if (data[j] != 0.0f) { all_zero = false; @@ -95,8 +96,9 @@ int synth_register_spectrogram(const Spectrogram* spec) { } } if (all_zero) { - DEBUG_SYNTH( "[SYNTH WARNING] Spectrogram appears to be all zeros (num_frames=%d)\n", - spec->num_frames); + DEBUG_SYNTH( + "[SYNTH WARNING] Spectrogram appears to be all zeros (num_frames=%d)\n", + spec->num_frames); } #endif @@ -157,8 +159,8 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { if (spectrogram_id < 0 || spectrogram_id >= MAX_SPECTROGRAMS || !g_synth_data.spectrogram_registered[spectrogram_id]) { #if defined(DEBUG_LOG_SYNTH) - DEBUG_SYNTH( "[SYNTH ERROR] Invalid spectrogram_id=%d in trigger_voice\n", - spectrogram_id); + DEBUG_SYNTH("[SYNTH ERROR] Invalid spectrogram_id=%d in trigger_voice\n", + spectrogram_id); #endif return; } @@ -166,12 +168,13 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { #if defined(DEBUG_LOG_SYNTH) // VALIDATION: Check volume and pan ranges if (volume < 0.0f || volume > 2.0f) { - DEBUG_SYNTH( "[SYNTH WARNING] Unusual volume=%.2f for spectrogram_id=%d\n", - volume, spectrogram_id); + DEBUG_SYNTH("[SYNTH WARNING] Unusual volume=%.2f for spectrogram_id=%d\n", + volume, spectrogram_id); } if (pan < -1.0f || pan > 1.0f) { - DEBUG_SYNTH( "[SYNTH WARNING] Invalid pan=%.2f (clamping) for spectrogram_id=%d\n", - pan, spectrogram_id); + DEBUG_SYNTH( + "[SYNTH WARNING] Invalid pan=%.2f (clamping) for spectrogram_id=%d\n", + pan, spectrogram_id); pan = (pan < -1.0f) ? -1.0f : 1.0f; } #endif @@ -191,7 +194,8 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { v.total_spectral_frames = g_synth_data.spectrograms[spectrogram_id].num_frames; v.buffer_pos = DCT_SIZE; // Force IDCT on first render - v.fractional_pos = 0.0f; // Initialize fractional position for tempo scaling + v.fractional_pos = + 0.0f; // Initialize fractional position for tempo scaling v.active_spectral_data = g_synth_data.active_spectrogram_data[spectrogram_id]; @@ -200,7 +204,7 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) { AudioBackend* backend = audio_get_backend(); if (backend != nullptr) { backend->on_voice_triggered(g_elapsed_time_sec, spectrogram_id, volume, - pan); + pan); } #endif /* !defined(STRIP_ALL) */ |
