Skip to content

Commit

Permalink
Use avail instead of delay to avoid glitches
Browse files Browse the repository at this point in the history
  • Loading branch information
HEnquist committed Jan 4, 2025
1 parent 8135e5e commit edbb4ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/alsadevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,14 @@ fn playback_loop_bytes(
match msg {
Ok(AudioMessage::Audio(chunk)) => {
// measure delay only on running non-stalled device
let delay_at_chunk_recvd = if !device_stalled
let avail_at_chunk_recvd = if !device_stalled
&& pcmdevice.state_raw() == alsa_sys::SND_PCM_STATE_RUNNING as i32
{
pcmdevice.status().ok().map(|status| status.get_delay())
pcmdevice.avail().ok()
} else {
None
};
//trace!("PB: Delay at chunk rcvd: {:?}", delay_at_chunk_recvd);
//trace!("PB: Avail at chunk rcvd: {:?}", avail_at_chunk_recvd);

conversion_result =
chunk_to_buffer_rawbytes(&chunk, &mut buffer, &params.sample_format);
Expand Down Expand Up @@ -602,10 +602,9 @@ fn playback_loop_bytes(
} else {
xtrace!("playback status blocked, skip update");
}
if let Some(delay) = delay_at_chunk_recvd {
if delay != 0 {
buffer_avg.add_value(delay as f64);
}
if let Some(avail) = avail_at_chunk_recvd {
let delay = buf_manager.current_delay(avail);
buffer_avg.add_value(delay as f64);
}
if timer.larger_than_millis((1000.0 * params.adjust_period) as u64) {
if let Some(avg_delay) = buffer_avg.average() {
Expand Down
10 changes: 10 additions & 0 deletions src/alsadevice_buffermanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub trait DeviceBufferManager {
// +1 to make sure the device really stalls
data.bufsize - data.avail_min + 1
}

fn current_delay(&self, avail: Frames) -> Frames;
}

#[derive(Debug)]
Expand Down Expand Up @@ -182,6 +184,10 @@ impl DeviceBufferManager for CaptureBufferManager {
self.data.threshold = threshold;
Ok(())
}

fn current_delay(&self, avail: Frames) -> Frames {
avail
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -233,4 +239,8 @@ impl DeviceBufferManager for PlaybackBufferManager {
self.data.threshold = threshold;
Ok(())
}

fn current_delay(&self, avail: Frames) -> Frames {
self.data.bufsize - avail
}
}

0 comments on commit edbb4ce

Please # to comment.