diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-07 14:00:23 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-07 14:00:23 +0100 |
| commit | a6a7bf0440dbabdc6c994c0fb21a8ac31c27be07 (patch) | |
| tree | 26663d3d65b110fca618d6fa33c83f7a8d1e362a /src/audio/backend/jittered_audio_backend.h | |
| parent | da1d4e10731789191d8a23e60c3dd35217e6bdb0 (diff) | |
feat(audio): Add SilentBackend, fix peak measurement, reorganize backends
## 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 <noreply@anthropic.com>
Diffstat (limited to 'src/audio/backend/jittered_audio_backend.h')
| -rw-r--r-- | src/audio/backend/jittered_audio_backend.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/audio/backend/jittered_audio_backend.h b/src/audio/backend/jittered_audio_backend.h new file mode 100644 index 0000000..8bda4c7 --- /dev/null +++ b/src/audio/backend/jittered_audio_backend.h @@ -0,0 +1,60 @@ +// This file is part of the 64k demo project. +// It implements a test backend that simulates jittered audio consumption. +// Useful for stress-testing ring buffer under realistic timing conditions. + +#pragma once + +#if !defined(STRIP_ALL) + +#include "../audio_backend.h" +#include <atomic> +#include <thread> + +// Simulates a real audio device with timing jitter and variable chunk sizes +class JitteredAudioBackend : public AudioBackend { + public: + JitteredAudioBackend(); + ~JitteredAudioBackend() override; + + void init() override; + void start() override; + void shutdown() override; + float get_realtime_peak() override; + + // Control simulation + void set_jitter_amount(float jitter_ms); // Random jitter in ms (default: 5ms) + void set_base_interval( + float interval_ms); // Base interval between reads (default: 10ms) + void set_chunk_size_range(int min_frames, + int max_frames); // Variable chunk sizes + + // Query state + int get_total_frames_consumed() const { + return total_frames_consumed_.load(); + } + int get_underrun_count() const { + return underrun_count_.load(); + } + bool is_running() const { + return running_.load(); + } + + private: + void audio_thread_loop(); + + std::thread audio_thread_; + std::atomic<bool> running_; + std::atomic<bool> should_stop_; + + // Configuration + float jitter_ms_; + float base_interval_ms_; + int min_chunk_frames_; + int max_chunk_frames_; + + // Statistics + std::atomic<int> total_frames_consumed_; + std::atomic<int> underrun_count_; // How many times buffer was empty +}; + +#endif /* !defined(STRIP_ALL) */ |
