Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce latency on Alsa #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/alsa_snd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,24 @@ unsafe fn audio_thread(mut mixer: crate::mixer::Mixer) {
panic!("PCM device is not ready");
}

// // find out how much space is available for playback data
// find out how much space is available for playback data
// teoretically it should reduce latency - we will fill a minimum amount of
// frames just to keep alsa busy and will be able to mix some fresh sounds
// it does, but also randmly panics sometimes

// let frames_to_deliver = sys::snd_pcm_avail_update(pcm_handle);
// println!("{}", frames_to_deliver);
// let frames_to_deliver = if frames_to_deliver > consts::PCM_BUFFER_SIZE as _ {
// consts::PCM_BUFFER_SIZE as i64
// } else {
// frames_to_deliver
// };
let frames_to_deliver = sys::snd_pcm_avail_update(pcm_handle);
let frames_to_deliver = if frames_to_deliver > consts::PCM_BUFFER_SIZE as _ {
consts::PCM_BUFFER_SIZE as i64
} else {
frames_to_deliver
};

let frames_to_deliver = consts::PCM_BUFFER_SIZE as i64;
let buffer = std::slice::from_raw_parts_mut(
buffer.as_mut_ptr(),
frames_to_deliver as usize * consts::CHANNELS as usize,
);

// ask mixer to fill the buffer
mixer.fill_audio_buffer(&mut buffer, frames_to_deliver as usize);
mixer.fill_audio_buffer(buffer, frames_to_deliver as usize);

// send filled buffer back to alsa
let frames_writen = sys::snd_pcm_writei(
Expand Down
2 changes: 1 addition & 1 deletion src/mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{AudioContext, PlaySoundParams};

use std::cell::Cell;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::mpsc;
use std::sync::Arc;

enum AudioMessage {
AddSound(u32, Vec<f32>),
Expand Down