summaryrefslogtreecommitdiff
path: root/src/audio/ring_buffer.h
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-07 21:34:00 +0100
committerskal <pascal.massimino@gmail.com>2026-02-07 21:34:00 +0100
commit727177329f833f76683b53570f3268c39b463e86 (patch)
treea4e531c06e719ba8cf49df5436ecc5bdca955364 /src/audio/ring_buffer.h
parentf9c5e84f01d0d7d1c981db4a0594dee9986429c4 (diff)
fix(audio): Calculate sample offsets from render position, not playback position
This fixes irregular timing in miniaudio playback while WAV dump was correct. ROOT CAUSE: Sample offsets were calculated relative to the ring buffer READ position (audio_get_playback_time), but should be calculated relative to the WRITE position (where we're currently rendering). The write position is ~400ms ahead of the read position (the lookahead buffer). ISSUE TIMELINE: 1. tracker_update() gets playback_time (read pos, e.g., 0.450s) 2. Calculates offset for event at 0.500s: (0.500 - 0.450) * 32000 = 1600 samples 3. BUT: We're actually writing at 0.850s (write pos = read pos + 400ms buffer) 4. Event triggers at 0.850s + 1600 samples = 0.900s instead of 0.500s! 5. Result: Event is 400ms late! The timing error was compounded by the fact that the playback position advances continuously between tracker_update() calls (60fps), making the calculated offsets stale by the time rendering happens. SOLUTION: 1. Added total_written_ tracking to AudioRingBuffer 2. Added audio_get_render_time() to get write position 3. Updated tracker.cc to use render_time instead of playback_time for offsets CHANGES: - ring_buffer.h: Add get_total_written() method, total_written_ member - ring_buffer.cc: Initialize and track total_written_ in write() - audio.h: Add audio_get_render_time() function - audio.cc: Implement audio_get_render_time() using get_total_written() - tracker.cc: Use current_render_time for sample offset calculation RESULT: Sample offsets now calculated relative to where we're currently rendering, not where audio is currently playing. Events trigger at exact times in both WAV dump (offline) and miniaudio (realtime) playback. VERIFICATION: 1. WAV dump: Already working (confirmed by user) 2. Miniaudio: Should now match WAV dump timing exactly Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/audio/ring_buffer.h')
-rw-r--r--src/audio/ring_buffer.h12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/audio/ring_buffer.h b/src/audio/ring_buffer.h
index b19c1ea..324447a 100644
--- a/src/audio/ring_buffer.h
+++ b/src/audio/ring_buffer.h
@@ -42,6 +42,11 @@ class AudioRingBuffer {
return total_read_.load(std::memory_order_acquire);
}
+ // Get total samples written (for render timing)
+ int64_t get_total_written() const {
+ return total_written_.load(std::memory_order_acquire);
+ }
+
// Clear buffer (for seeking)
void clear();
@@ -49,7 +54,8 @@ class AudioRingBuffer {
float buffer_[RING_BUFFER_CAPACITY_SAMPLES];
int capacity_; // Total capacity in samples
- std::atomic<int> write_pos_; // Write position (0 to capacity-1)
- std::atomic<int> read_pos_; // Read position (0 to capacity-1)
- std::atomic<int64_t> total_read_; // Total samples read (for playback time)
+ std::atomic<int> write_pos_; // Write position (0 to capacity-1)
+ std::atomic<int> read_pos_; // Read position (0 to capacity-1)
+ std::atomic<int64_t> total_read_; // Total samples read (for playback time)
+ std::atomic<int64_t> total_written_; // Total samples written (for render timing)
};