diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-07 10:53:28 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-07 10:53:28 +0100 |
| commit | 41876cd44b29113fbe72749665f1dd4fd473f27a (patch) | |
| tree | c288ca6e36682cee018c599a2ebb2fe1bb736bce /src/audio/wav_dump_backend.h | |
| parent | 0f79b532c886f338ab80d506d4b06048e1784056 (diff) | |
fix(audio): Remove clipping from WavDumpBackend, add diagnostics
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 <noreply@anthropic.com>
Diffstat (limited to 'src/audio/wav_dump_backend.h')
| -rw-r--r-- | src/audio/wav_dump_backend.h | 6 |
1 files changed, 6 insertions, 0 deletions
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<float> sample_buffer_; size_t samples_written_; + size_t clipped_samples_; const char* output_filename_; bool is_active_; float duration_sec_; |
