A GameCube/Wii emulator and debugger written in Rust.
sync core speed to audio
Laylef8f485618cb4
diff --git a/Cargo.lock b/Cargo.lockindex 0c4c829..4b14ce4 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3742,6 +3742,15 @@ dependencies = ["serde",]+[[package]]+name = "spin_sleep"+version = "1.3.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9c07347b7c0301b9adba4350bdcf09c039d0e7160922050db0439b3c6723c8ab"+dependencies = [+ "windows-sys 0.61.2",+]+[[package]]name = "spirv"version = "0.3.0+sdk-1.3.268.0"@@ -3950,6 +3959,7 @@ dependencies = ["pollster","ringbuf","scripting",+ "spin_sleep","tracing","tracing-subscriber","wgpu",diff --git a/crates/gecko/src/audio.rs b/crates/gecko/src/audio.rsindex 0bcbd05..43b5069 100644--- a/crates/gecko/src/audio.rs+++ b/crates/gecko/src/audio.rs@@ -26,6 +26,12 @@ pub trait AudioSink: Send {/// Optional: flush pending state. Most sinks have nothing to do.fn flush(&mut self) {}++ /// Backpressure signal: when the host buffer is sufficiently full, the+ /// emu thread should stall instead of producing more samples.+ fn should_throttle(&self) -> bool {+ false+ }}/// No-op sink. The default `System::audio_sink` so headless tools (dspemu,@@ -76,6 +82,10 @@ impl AudioSink for MultiplexAudioSink {sink.flush();}}++ fn should_throttle(&self) -> bool {+ self.inner.iter().any(|s| s.should_throttle())+ }}/// Decode a 32-byte AID DMA block into 8 stereo s16 frames.diff --git a/crates/tinyapp/Cargo.toml b/crates/tinyapp/Cargo.tomlindex 4339089..a2d75f3 100644--- a/crates/tinyapp/Cargo.toml+++ b/crates/tinyapp/Cargo.toml@@ -32,3 +32,4 @@ cpal = "0.17.3"ringbuf = "0.5.0"ctrlc = "3.5.2"mimalloc = { version = "0.1.50", default-features = false }+spin_sleep = "1.3.3"diff --git a/crates/tinyapp/src/audio.rs b/crates/tinyapp/src/audio.rsindex 669f1ea..a5ee250 100644--- a/crates/tinyapp/src/audio.rs+++ b/crates/tinyapp/src/audio.rs@@ -2,7 +2,7 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};use cpal::{SampleFormat, SampleRate, StreamConfig, SupportedStreamConfig};use gecko::audio::{AidBlock, AudioSink};use ringbuf::HeapRb;-use ringbuf::traits::{Consumer, Producer, Split};+use ringbuf::traits::{Consumer, Observer, Producer, Split};use std::sync::Arc;use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};@@ -87,6 +87,7 @@ pub struct CpalAudioSink {producer: ringbuf::HeapProd<Frame>,overflow_counter: Arc<AtomicU64>,sample_rate: Arc<AtomicU32>,+ throttle_threshold: usize,}impl AudioSink for CpalAudioSink {@@ -116,6 +117,11 @@ impl AudioSink for CpalAudioSink {self.overflow_counter.fetch_add(dropped, Ordering::Relaxed);}}++ #[inline]+ fn should_throttle(&self) -> bool {+ self.producer.occupied_len() >= self.throttle_threshold+ }}pub fn open(emulated_rate: u32) -> Result<CpalBackend, String> {@@ -143,6 +149,7 @@ pub fn open(emulated_rate: u32) -> Result<CpalBackend, String> {}let cap_frames = (emulated_rate.max(host_rate) as usize / 2).max(1024);+ let throttle_threshold = (cap_frames / 4).max(1);let rb = HeapRb::<Frame>::new(cap_frames);let (producer, consumer) = rb.split();let overflow_counter = Arc::new(AtomicU64::new(0));@@ -156,6 +163,7 @@ pub fn open(emulated_rate: u32) -> Result<CpalBackend, String> {emulated_rate,?sample_format,ringbuf_capacity_frames = cap_frames,+ throttle_threshold_frames = throttle_threshold,"Opened CPAL output");@@ -212,6 +220,7 @@ pub fn open(emulated_rate: u32) -> Result<CpalBackend, String> {producer,overflow_counter,sample_rate: input_rate,+ throttle_threshold,},stream,})diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rsindex 2876587..ab2f324 100644--- a/crates/tinyapp/src/main.rs+++ b/crates/tinyapp/src/main.rs@@ -78,6 +78,10 @@ struct Args {#[arg(long)]no_audio: bool,+ /// Disable audio-driven framerate cap; emu runs as fast as possible+ #[arg(long)]+ no_cap: bool,+/// Enable per-block JIT heatmap CSV dumps (requires --features jit-stats)#[cfg(feature = "jit-stats")]#[arg(long)]@@ -363,9 +367,10 @@ fn run<const SYSTEM: SystemId>(}}+ let throttle = !args.no_cap;let emu_handle = std::thread::Builder::new().name("emu".into())- .spawn(move || thread::emu_thread::<SYSTEM>(emulator, emu_input, proxy, game_id))+ .spawn(move || thread::emu_thread::<SYSTEM>(emulator, emu_input, proxy, game_id, throttle)).expect("failed to spawn emulator thread");let mut app = app::App {diff --git a/crates/tinyapp/src/thread.rs b/crates/tinyapp/src/thread.rsindex de554dd..3b2e44f 100644--- a/crates/tinyapp/src/thread.rs+++ b/crates/tinyapp/src/thread.rs@@ -1,6 +1,8 @@use gecko::HostInput;use gecko::system::{System, SystemId};+use spin_sleep::SpinSleeper;use std::sync::{Arc, Mutex};+use std::time::Duration;use winit::event_loop::EventLoopProxy;pub fn emu_thread<const SYSTEM: SystemId>(@@ -8,8 +10,16 @@ pub fn emu_thread<const SYSTEM: SystemId>(input: Arc<Mutex<HostInput>>,proxy: EventLoopProxy<()>,game_id: Option<String>,+ throttle: bool,) {+ let sleeper = SpinSleeper::default();+ let throttle_step = Duration::from_micros(500);+loop {+ while throttle && emulator.audio_sink.should_throttle() {+ sleeper.sleep(throttle_step);+ }+let input = *input.lock().unwrap();emulator.apply_host_input(&input);emulator.run_until_vsync();