From a6a7bf0440dbabdc6c994c0fb21a8ac31c27be07 Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 7 Feb 2026 14:00:23 +0100 Subject: feat(audio): Add SilentBackend, fix peak measurement, reorganize backends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Critical Fixes **Peak Measurement Timing:** - Fixed 400ms audio-visual desync by measuring peak at playback time - Added get_realtime_peak() to AudioBackend interface - Implemented real-time measurement in MiniaudioBackend audio callback - Updated main.cc and test_demo.cc to use audio_get_realtime_peak() **Peak Decay Rate:** - Fixed slow decay (0.95 → 0.7 per callback) - Old: 5.76 seconds to fade to 10% (constant flashing in test_demo) - New: 1.15 seconds to fade to 10% (proper visual sync) ## New Features **SilentBackend:** - Test-only backend for testing audio.cc without hardware - Controllable peak for testing edge cases - Tracks frames rendered and voice triggers - Added 7 comprehensive tests covering: - Lifecycle (init/start/shutdown) - Peak control and tracking - Playback time and buffer management - Integration with AudioEngine ## Refactoring **Backend Organization:** - Created src/audio/backend/ directory - Moved all backend implementations to subdirectory - Updated include paths and CMakeLists.txt - Cleaner codebase structure **Code Cleanup:** - Removed unused register_spec_asset() function - Added deprecation note to synth_get_output_peak() ## Testing - All 28 tests passing (100%) - New test: test_silent_backend - Improved audio.cc test coverage significantly ## Documentation - Created PEAK_FIX_SUMMARY.md with technical details - Created TASKS_SUMMARY.md with complete task report Co-Authored-By: Claude Sonnet 4.5 --- src/audio/backend/wav_dump_backend.h | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/audio/backend/wav_dump_backend.h (limited to 'src/audio/backend/wav_dump_backend.h') diff --git a/src/audio/backend/wav_dump_backend.h b/src/audio/backend/wav_dump_backend.h new file mode 100644 index 0000000..de445d6 --- /dev/null +++ b/src/audio/backend/wav_dump_backend.h @@ -0,0 +1,65 @@ +// This file is part of the 64k demo project. +// WAV dump backend for debugging audio output to file instead of device. +// Stripped in final build (STRIP_ALL). + +#if !defined(DEMO_AUDIO_WAV_DUMP_BACKEND_H) +#define DEMO_AUDIO_WAV_DUMP_BACKEND_H + +#include "../audio_backend.h" +#include +#include + +#if !defined(STRIP_ALL) + +// WAV file dump backend for debugging +// Captures audio from synth_render() and writes to .wav file +class WavDumpBackend : public AudioBackend { + public: + WavDumpBackend(); + ~WavDumpBackend(); + + // AudioBackend interface + void init() override; + void start() override; + void shutdown() override; + float get_realtime_peak() override; + + // Set output filename (call before init()) + void set_output_file(const char* filename); + + // Write audio data to WAV file (stereo interleaved float samples) + // num_samples: Total number of samples (2x num_frames for stereo) + void write_audio(const float* samples, int num_samples); + + // Get total samples written + size_t get_samples_written() const { + return samples_written_; + } + + // Get number of samples that were clipped (diagnostic metric) + size_t get_clipped_samples() const { + return clipped_samples_; + } + + private: + // Write WAV header with known sample count + void write_wav_header(FILE* file, uint32_t num_samples); + + // Update WAV header with final sample count + void update_wav_header(); + + FILE* wav_file_; + std::vector sample_buffer_; + size_t samples_written_; + size_t clipped_samples_; + const char* output_filename_; + bool is_active_; + float duration_sec_; + + static const int kSampleRate = 32000; + static const int kBufferSize = 1024; +}; + +#endif /* !defined(STRIP_ALL) */ + +#endif /* DEMO_AUDIO_WAV_DUMP_BACKEND_H */ -- cgit v1.2.3