From 41876cd44b29113fbe72749665f1dd4fd473f27a Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 7 Feb 2026 10:53:28 +0100 Subject: fix(audio): Remove clipping from WavDumpBackend, add diagnostics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed design flaw where WavDumpBackend was clamping samples to [-1.0, 1.0] before writing to file. This prevented detection of audio problems. Changes: - Removed sample clamping (lines 57-60 in old code) - WAV dump now records audio "as is" (matches MiniaudioBackend behavior) - Added clipped_samples_ counter to track diagnostic metric - Added get_clipped_samples() method for programmatic access - Report clipping statistics in shutdown(): - "✓ No clipping detected" when clean - "WARNING: N samples clipped (X% of total)" when clipping occurs - Suggests reducing volume to fix Why this matters: - MiniaudioBackend does NOT clip samples (passes directly to miniaudio) - WavDumpBackend should match this behavior - Clipping in WAV files helps identify audio distortion problems - Developers can compare WAV output to expected values - Diagnostic metric helps tune audio levels Testing: - Added test_clipping_detection() test case - Verifies clipping counter works correctly (200 clipped / 1000 samples) - Existing tests show "✓ No clipping detected" for normal audio - All 27 tests pass Example output: WAV file written: test.wav (2.02 seconds, 128986 samples) ✓ No clipping detected WAV file written: loud.wav (10.5 seconds, 336000 samples) WARNING: 4521 samples clipped (1.35% of total) This indicates audio distortion - consider reducing volume Co-Authored-By: Claude Sonnet 4.5 --- src/audio/wav_dump_backend.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/audio/wav_dump_backend.h') diff --git a/src/audio/wav_dump_backend.h b/src/audio/wav_dump_backend.h index 6482ef3..c3c5302 100644 --- a/src/audio/wav_dump_backend.h +++ b/src/audio/wav_dump_backend.h @@ -35,6 +35,11 @@ class WavDumpBackend : public AudioBackend { 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); @@ -45,6 +50,7 @@ class WavDumpBackend : public AudioBackend { 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_; -- cgit v1.2.3