rewrite audio stream handling (#1390)

This commit is contained in:
Logan McNaughton authored and GitHub committed 2026-11-15 11:34:08 +00:00
1 parent 469d07fb27
commit 892ba9175b
4 files changed
+129 -101

No files matched your search

-2
View File
@@ -103,7 +103,6 @@ fn fifo_push(device: &mut device::Device) {
device.ai.regs[AI_STATUS_REG] |= AI_STATUS_BUSY;
do_dma(device);
ui::audio::resume_game_audio(&mut device.ui);
}
}
@@ -118,7 +117,6 @@ fn fifo_pop(device: &mut device::Device) {
} else {
device.ai.regs[AI_STATUS_REG] &= !AI_STATUS_BUSY;
device.ai.delayed_carry = false;
ui::audio::pause_game_audio(&mut device.ui);
}
}
+6 -1
View File
@@ -230,6 +230,7 @@ pub fn load_savestate(device: &mut device::Device, rewind: bool, rewind_frame: O
return;
}
let rollback = rewind && rewind_frame.is_some();
let loaded = if rewind && let Some(rewind_frame) = rewind_frame {
// Keep the entry: GGRS does not re-save the frame it rolls back to, and may
// roll back to the same frame again (e.g. late inputs from another player).
@@ -269,6 +270,11 @@ pub fn load_savestate(device: &mut device::Device, rewind: bool, rewind_frame: O
};
if loaded {
if rollback {
ui::audio::update_freq(device);
} else {
ui::audio::reset_game_audio(device);
}
if !rewind {
ui::video::onscreen_message(
&format!(
@@ -364,7 +370,6 @@ fn apply_state(device: &mut device::Device, state: &SavestateData, run_cheats: b
}
}
ui::audio::update_freq(device);
ui::video::load_state(device, state.rdp_state.as_ptr(), state.rdp_state.len());
if !state.ra_state.is_empty() {
+5
View File
@@ -68,6 +68,9 @@ pub struct Dirs {
pub struct Audio {
pub audio_stream: *mut sdl3_sys::audio::SDL_AudioStream,
pub gain: f32,
pub frequency_ratio: f64,
pub primary_buffer: Vec<i16>,
pub silence_buffer: Vec<u8>,
}
unsafe impl Send for Audio {}
@@ -316,6 +319,8 @@ impl Ui {
audio: Audio {
audio_stream: std::ptr::null_mut(),
gain: 1.0,
frequency_ratio: 1.0,
..Default::default()
},
video: Video {
window: std::ptr::null_mut(),
+118 -98
View File
@@ -2,54 +2,53 @@ use crate::device;
use crate::netplay;
use crate::ui;
static ADJUST_LOCKED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
const BYTES_PER_FRAME: usize = 4; // Stereo, signed 16-bit samples.
const TARGET_FRAMES: f64 = 2.0;
const MAX_FRAMES: f64 = 12.0;
pub fn init(device: &mut device::Device) {
ui::sdl_init(sdl3_sys::init::SDL_INIT_AUDIO);
init_game_audio(device);
}
unsafe extern "C" fn audio_callback(
_userdata: *mut std::ffi::c_void,
audio_stream: *mut sdl3_sys::audio::SDL_AudioStream,
additional_amount: i32,
_total_amount: i32,
) {
if additional_amount > 0 {
unsafe {
sdl3_sys::audio::SDL_PutAudioStreamData(
audio_stream,
vec![0; additional_amount as usize].as_ptr() as *const std::ffi::c_void,
additional_amount,
)
};
adjust_audio_frequency(audio_stream, -0.0005);
}
// Correct clock drift before it causes starvation or excessive latency. Slew at most 0.05% per second of submitted audio.
fn frequency_ratio(current: f64, queued_frames: f64, seconds: f64) -> f64 {
let target = (1.0 + (queued_frames - TARGET_FRAMES) * 0.001).clamp(0.995, 1.005);
let step = seconds * 0.0005;
(current + (target - current).clamp(-step, step)).clamp(0.995, 1.005)
}
pub fn update_freq(device: &mut device::Device) {
let stream = device.ui.audio.audio_stream;
if stream.is_null() {
return;
}
let game_audio_spec = sdl3_sys::audio::SDL_AudioSpec {
format: sdl3_sys::audio::SDL_AUDIO_S16LE,
freq: device.ai.freq as i32,
channels: 2,
};
unsafe {
sdl3_sys::audio::SDL_SetAudioStreamFormat(
device.ui.audio.audio_stream,
&game_audio_spec,
std::ptr::null(),
);
}
if device.ai.regs[device::ai::AI_STATUS_REG] & device::ai::AI_STATUS_BUSY != 0 {
resume_game_audio(&mut device.ui);
} else {
pause_game_audio(&mut device.ui);
sdl3_sys::audio::SDL_SetAudioStreamFormat(stream, &game_audio_spec, std::ptr::null());
}
}
// A user load/rewind changes the audible timeline. Netplay rollback must preserve it.
pub fn reset_game_audio(device: &mut device::Device) {
reset_stream(&mut device.ui.audio);
update_freq(device);
}
fn reset_stream(audio: &mut ui::Audio) {
unsafe {
sdl3_sys::audio::SDL_ClearAudioStream(audio.audio_stream);
sdl3_sys::audio::SDL_SetAudioStreamFrequencyRatio(audio.audio_stream, 1.0);
}
audio.frequency_ratio = 1.0;
}
pub fn init_game_audio(device: &mut device::Device) {
device.ui.audio.frequency_ratio = 1.0;
let game_audio_spec = sdl3_sys::audio::SDL_AudioSpec {
format: sdl3_sys::audio::SDL_AUDIO_S16LE,
freq: device.ai.freq as i32,
@@ -69,18 +68,11 @@ pub fn init_game_audio(device: &mut device::Device) {
}
if !unsafe {
sdl3_sys::audio::SDL_SetAudioStreamGain(device.ui.audio.audio_stream, device.ui.audio.gain)
&& sdl3_sys::audio::SDL_SetAudioStreamGetCallback(
device.ui.audio.audio_stream,
Some(audio_callback),
std::ptr::null_mut(),
)
} {
panic!("Could not initialize audio stream");
}
if device.ai.regs[device::ai::AI_STATUS_REG] & device::ai::AI_STATUS_BUSY != 0 {
resume_game_audio(&mut device.ui);
} else {
pause_game_audio(&mut device.ui);
unsafe {
sdl3_sys::audio::SDL_ResumeAudioStreamDevice(device.ui.audio.audio_stream);
}
}
@@ -88,18 +80,6 @@ pub fn close(ui: &mut ui::Ui) {
close_game_audio(ui);
}
pub fn resume_game_audio(ui: &mut ui::Ui) {
unsafe {
sdl3_sys::audio::SDL_ResumeAudioStreamDevice(ui.audio.audio_stream);
}
}
pub fn pause_game_audio(ui: &mut ui::Ui) {
unsafe {
sdl3_sys::audio::SDL_PauseAudioStreamDevice(ui.audio.audio_stream);
}
}
pub fn close_game_audio(ui: &mut ui::Ui) {
unsafe {
if !ui.audio.audio_stream.is_null() {
@@ -137,40 +117,7 @@ pub fn raise_audio_volume(ui: &mut ui::Ui) {
);
}
unsafe extern "C" fn lock_callback(
_userdata: *mut std::ffi::c_void,
_id: sdl3_sys::timer::SDL_TimerID,
_interval: u32,
) -> u32 {
ADJUST_LOCKED.store(false, std::sync::atomic::Ordering::Relaxed);
0
}
fn adjust_audio_frequency(audio_stream: *mut sdl3_sys::audio::SDL_AudioStream, frequency: f32) {
if ADJUST_LOCKED.load(std::sync::atomic::Ordering::Relaxed) {
return;
}
ADJUST_LOCKED.store(true, std::sync::atomic::Ordering::Relaxed);
unsafe {
sdl3_sys::timer::SDL_AddTimer(1000, Some(lock_callback), std::ptr::null_mut());
let current_ratio = sdl3_sys::everything::SDL_GetAudioStreamFrequencyRatio(audio_stream);
sdl3_sys::everything::SDL_SetAudioStreamFrequencyRatio(
audio_stream,
(current_ratio + frequency).clamp(0.995, 1.005),
);
/*
println!(
"Adjusted audio frequency ratio to {}",
sdl3_sys::everything::SDL_GetAudioStreamFrequencyRatio(audio_stream)
);
*/
}
}
pub fn play_audio(device: &device::Device, dram_addr: usize, length: u64) {
pub fn play_audio(device: &mut device::Device, dram_addr: usize, length: u64) {
if device.ui.audio.audio_stream.is_null() {
return;
}
@@ -179,7 +126,9 @@ pub fn play_audio(device: &device::Device, dram_addr: usize, length: u64) {
return;
}
let mut primary_buffer: Vec<i16> = vec![0; length as usize / 2];
let audio = &mut device.ui.audio;
audio.primary_buffer.resize(length as usize / 2, 0);
let primary_buffer = &mut audio.primary_buffer;
let mut i = 0;
while i < length as usize / 2 {
// Left channel
@@ -192,19 +141,90 @@ pub fn play_audio(device: &device::Device, dram_addr: usize, length: u64) {
i += 2;
}
let audio_queued =
unsafe { sdl3_sys::audio::SDL_GetAudioStreamQueued(device.ui.audio.audio_stream) } as f64;
let samples_per_frame = device.ai.freq as f64 * device.vi.frame_time * 4.0;
let max_latency = samples_per_frame * 8.0;
if audio_queued < max_latency {
unsafe {
queue_audio(
audio,
device.ai.freq,
device.vi.frame_time,
device.ui.speed_limiter.enabled,
);
}
fn queue_audio(audio: &mut ui::Audio, frequency: u64, frame_time: f64, speed_limited: bool) {
let bytes_per_second = frequency as f64 * BYTES_PER_FRAME as f64;
let bytes_per_video_frame = bytes_per_second * frame_time;
if bytes_per_video_frame <= 0.0 || audio.primary_buffer.is_empty() {
return;
}
let incoming = std::mem::size_of_val(audio.primary_buffer.as_slice());
let target_bytes =
(bytes_per_video_frame * TARGET_FRAMES) as usize / BYTES_PER_FRAME * BYTES_PER_FRAME;
// The incoming block contributes to the recovery reserve. Only pad its
// shortfall, so a full block can restart playback without added silence.
let padding = target_bytes.saturating_sub(incoming);
// Prepare reusable silence before locking: the playback thread needs this
// same mutex to consume audio. Only grow the buffer when necessary.
if audio.silence_buffer.len() < padding {
audio.silence_buffer.resize(padding, 0);
}
let seconds = incoming as f64 / bytes_per_second;
let stream = audio.audio_stream;
unsafe {
if !sdl3_sys::audio::SDL_LockAudioStream(stream) {
return;
}
let queued = sdl3_sys::audio::SDL_GetAudioStreamQueued(stream);
let available = sdl3_sys::audio::SDL_GetAudioStreamAvailable(stream);
if queued >= 0 && available >= 0 {
let queued_frames = queued as f64 / bytes_per_video_frame;
// Resampling retains input bytes even after all playable output is consumed.
let starved = available == 0;
// Test the existing backlog: a large arriving DMA block alone is
// not evidence that playback has fallen behind.
let overrun = queued_frames > MAX_FRAMES;
if overrun {
// Prefer current game audio over playing an increasingly stale
// backlog (particularly after fast-forward or a host stall).
// Queue current game audio immediately, without silence padding.
sdl3_sys::audio::SDL_ClearAudioStream(stream);
}
if starved && !overrun && padding > 0 {
// SDL supplies silence while starved. Prime a small reserve on
// recovery so the next host scheduling jitter needn't starve us
// again, counting the incoming audio toward that reserve.
// All padding is aligned to complete stereo frames.
sdl3_sys::audio::SDL_PutAudioStreamData(
stream,
audio.silence_buffer.as_ptr().cast(),
padding as i32,
);
}
let ratio = if speed_limited {
frequency_ratio(
audio.frequency_ratio,
if overrun {
TARGET_FRAMES
} else if starved {
// Retained resampler input isn't playable audio. An
// underrun must slow consumption even if this block
// is large enough to need no recovery padding.
0.0
} else {
queued_frames
},
seconds,
)
} else {
1.0
};
audio.frequency_ratio = ratio;
sdl3_sys::audio::SDL_SetAudioStreamFrequencyRatio(stream, ratio as f32);
sdl3_sys::audio::SDL_PutAudioStreamData(
device.ui.audio.audio_stream,
primary_buffer.as_ptr() as *const std::ffi::c_void,
primary_buffer.len() as i32 * 2,
)
};
} else if device.ui.speed_limiter.enabled {
adjust_audio_frequency(device.ui.audio.audio_stream, 0.0005);
stream,
audio.primary_buffer.as_ptr().cast(),
incoming as i32,
);
}
sdl3_sys::audio::SDL_UnlockAudioStream(stream);
}
}