From 9cae6f16897338cb33b85d93bb6f1be38a60a93c Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 7 Feb 2026 21:19:19 +0100 Subject: fix(audio): Implement sample-accurate event timing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the "off-beat" timing issue where audio events (drum hits, notes) were triggering with random jitter of up to ±16ms. ROOT CAUSE: Events were quantized to frame boundaries (60fps = 16.6ms intervals) instead of triggering at exact sample positions. When tracker_update() detected an event had passed, it triggered the voice immediately, causing it to start "sometime during this frame". SOLUTION: Implement sample-accurate trigger offsets: 1. Calculate exact sample offset when triggering events 2. Add start_sample_offset field to Voice struct 3. Skip samples in synth_render() until offset elapses CHANGES: - synth.h: Add optional start_offset_samples parameter to synth_trigger_voice() - synth.cc: Add start_sample_offset field to Voice, implement offset logic in render loop - tracker.cc: Calculate sample offsets based on event_trigger_time vs current_playback_time BENEFITS: - Sample-accurate timing (0ms error vs ±16ms before) - Zero CPU overhead (just integer decrement per voice) - Backward compatible (default offset=0) - Improves audio/visual sync, variable tempo accuracy TIMING EXAMPLE: Before: Event at 0.500s could trigger at 0.483s or 0.517s (frame boundaries) After: Event triggers at exactly 0.500s (1600 sample offset calculated) See doc/SAMPLE_ACCURATE_TIMING_FIX.md for detailed explanation. Co-Authored-By: Claude Sonnet 4.5 --- src/audio/synth.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/audio/synth.h') diff --git a/src/audio/synth.h b/src/audio/synth.h index ba96167..b2625b3 100644 --- a/src/audio/synth.h +++ b/src/audio/synth.h @@ -38,7 +38,8 @@ void synth_unregister_spectrogram(int spectrogram_id); float* synth_begin_update(int spectrogram_id); void synth_commit_update(int spectrogram_id); -void synth_trigger_voice(int spectrogram_id, float volume, float pan); +void synth_trigger_voice(int spectrogram_id, float volume, float pan, + int start_offset_samples = 0); void synth_render(float* output_buffer, int num_frames); void synth_set_tempo_scale( float tempo_scale); // Set playback speed (1.0 = normal) -- cgit v1.2.3