Skip drain on track skip for instant track changes

This fixes the 2-5s delay when rapidly changing tracks. The drain() was
blocking for hundreds of milliseconds waiting for the PCM buffer to empty,
even when the user was skipping tracks.

Root cause (playback_engine.rs:136):
- drain() was called ALWAYS after playback loop ended
- drain() blocks until PCM buffer is completely empty (~125-500ms)
- When user skips tracks, we don't need elegant buffer drain
- This caused 'need to wait 2-5s' or 'double click next' behavior

Solution:
- Added 'natural_end' flag to track WHY playback stopped
- natural_end=true: Song finished normally → DO drain (elegant end)
- natural_end=false: User skipped/paused → SKIP drain (instant response)

Benefits:
- Track changes now instant (no 2-5s wait)
- Songs that finish naturally still drain properly (no clicks/pops)
- Maintains audio quality while improving responsiveness

Testing:
- Rapid track skipping should be instant
- Songs that play to end should have smooth transition
- No audio artifacts (clicks/pops) in either case
This commit is contained in:
vicrodh committed 2026-01-19 11:40:19 -06:00
1 parent 0a361712ce
commit 8947bcf9c3
1 file changed
+11 -3
+11 -3
View File
@@ -96,6 +96,7 @@ impl PlaybackEngine {
let mut total_frames: u64 = 0;
let mut source_iter = source.into_iter();
let mut natural_end = false;
loop {
// Check if we should stop
@@ -116,6 +117,7 @@ impl PlaybackEngine {
if buffer_i16.is_empty() {
// End of stream
log::info!("[ALSA Direct Engine] Stream ended (total frames: {})", total_frames);
natural_end = true;
break;
}
@@ -132,9 +134,15 @@ impl PlaybackEngine {
duration_clone.store(total_frames, Ordering::SeqCst);
}
// Drain PCM buffer to ensure all samples are played
if let Err(e) = stream_clone.drain() {
log::warn!("[ALSA Direct Engine] Drain failed: {}", e);
// Only drain if song ended naturally (not skipped/stopped)
// This prevents 2-5s delay when rapidly changing tracks
if natural_end {
log::info!("[ALSA Direct Engine] Song ended naturally, draining buffer");
if let Err(e) = stream_clone.drain() {
log::warn!("[ALSA Direct Engine] Drain failed: {}", e);
}
} else {
log::info!("[ALSA Direct Engine] Playback interrupted, skipping drain for faster response");
}
is_playing_clone.store(false, Ordering::SeqCst);