From 18c553b0aaa9a574a2a40e5499120ac8a802b735 Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 4 Feb 2026 13:55:45 +0100 Subject: feat(audio): Add WAV dump backend for debugging audio output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implemented WavDumpBackend that renders audio to .wav file instead of playing on audio device. Useful for debugging audio synthesis, tempo scaling, and tracker output without needing real-time playback. New Files: - src/audio/wav_dump_backend.h: WAV dump backend interface - src/audio/wav_dump_backend.cc: Implementation with WAV file writing Features: - Command line option: --dump_wav [filename] - Default output: audio_dump.wav - Format: 16-bit PCM, mono, 32kHz - Duration: 60 seconds (configurable in code) - Progress indicator during rendering - Properly writes WAV header (RIFF format) Integration (src/main.cc): - Added --dump_wav command line parsing - Optional filename parameter - Sets WavDumpBackend before audio_init() - Skips main loop in WAV dump mode (just render and exit) - Zero size impact (all code under !STRIP_ALL) Usage: ./demo64k --dump_wav # outputs audio_dump.wav ./demo64k --dump_wav my_audio.wav # custom filename Technical Details: - Uses AudioBackend interface (from Task #51) - Calls synth_render() in loop to capture audio - Converts float samples to int16_t for WAV format - Updates WAV header with final sample count on shutdown - Renders 60s worth of audio (1,920,000 samples @ 32kHz) Test Results: ✓ All 16 tests passing (100%) ✓ Successfully renders 3.7 MB WAV file ✓ File verified as valid RIFF WAVE format ✓ Playback in audio players confirmed Perfect for: - Debugging tempo scaling behavior - Verifying tracker pattern timing - Analyzing audio output offline - Creating reference audio for tests handoff(Claude): WAV dump debugging feature complete Co-Authored-By: Claude Sonnet 4.5 --- src/audio/wav_dump_backend.h | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/audio/wav_dump_backend.h (limited to 'src/audio/wav_dump_backend.h') diff --git a/src/audio/wav_dump_backend.h b/src/audio/wav_dump_backend.h new file mode 100644 index 0000000..b037fd1 --- /dev/null +++ b/src/audio/wav_dump_backend.h @@ -0,0 +1,51 @@ +// 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; + + // Set output filename (call before init()) + void set_output_file(const char* filename); + + // Get total samples written + size_t get_samples_written() const { return samples_written_; } + + 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_; + const char* output_filename_; + bool is_active_; + + 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