mod reclamation; pub use reclamation::AudioReclaimer; use reclamation::{CallbackLane, Packet}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use prometeu_drivers::{AudioCommand, Channel, MAX_CHANNELS, OUTPUT_SAMPLE_RATE}; use prometeu_drivers::{AudioDelivery, AudioTransport}; use prometeu_hal::LoopMode; use ringbuf::HeapRb; use ringbuf::traits::{Consumer, Producer, Split}; use std::error::Error; use std::fmt; use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::time::{Duration, Instant}; trait AudioStreamHandle { fn stop(&self) {} } impl AudioStreamHandle for cpal::Stream { fn stop(&self) { let _ = self.pause(); } } #[derive(Debug, Clone, PartialEq, Eq)] pub enum HostAudioError { NoOutputDevice, BuildStream(String), PlayStream(String), } impl fmt::Display for HostAudioError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { HostAudioError::NoOutputDevice => write!(f, "no output device available"), HostAudioError::BuildStream(err) => write!(f, "failed to build audio stream: {}", err), HostAudioError::PlayStream(err) => write!(f, "failed to play audio stream: {}", err), } } } impl Error for HostAudioError {} trait AudioRuntime { type Device; type Stream: AudioStreamHandle + 'static; fn default_output_device(&self) -> Option; fn build_output_stream( &self, device: &Self::Device, config: &cpal::StreamConfig, data_callback: D, error_callback: E, ) -> Result where D: FnMut(&mut [f32]) + Send + 'static, E: FnMut(String) + Send + 'static; fn play_stream(&self, stream: &Self::Stream) -> Result<(), HostAudioError>; } struct CpalAudioRuntime; impl AudioRuntime for CpalAudioRuntime { type Device = cpal::Device; type Stream = cpal::Stream; fn default_output_device(&self) -> Option { cpal::default_host().default_output_device() } fn build_output_stream( &self, device: &Self::Device, config: &cpal::StreamConfig, mut data_callback: D, mut error_callback: E, ) -> Result where D: FnMut(&mut [f32]) + Send + 'static, E: FnMut(String) + Send + 'static, { device .build_output_stream( config, move |data: &mut [f32], _: &cpal::OutputCallbackInfo| data_callback(data), move |err| error_callback(err.to_string()), None, ) .map_err(|err| HostAudioError::BuildStream(err.to_string())) } fn play_stream(&self, stream: &Self::Stream) -> Result<(), HostAudioError> { stream.play().map_err(|err| HostAudioError::PlayStream(err.to_string())) } } pub struct HostAudio { transport: AudioTransport, failed: Arc, heartbeat: Arc, last_heartbeat: u64, last_progress: Instant, pub perf_consumer: Option>>>, _stream: Option>, reclaimer: Option, } impl Default for HostAudio { fn default() -> Self { Self::new() } } impl HostAudio { pub fn new() -> Self { Self { transport: AudioTransport::default(), failed: Arc::new(AtomicBool::new(false)), heartbeat: Arc::new(AtomicU64::new(0)), last_heartbeat: 0, last_progress: Instant::now(), perf_consumer: None, _stream: None, reclaimer: None, } } pub fn init(&mut self, transport: AudioTransport) -> Result<(), HostAudioError> { self.transport = transport; self.init_with_runtime(&CpalAudioRuntime) } fn init_with_runtime(&mut self, runtime: &R) -> Result<(), HostAudioError> { self.failed = Arc::new(AtomicBool::new(false)); self.heartbeat = Arc::new(AtomicU64::new(0)); self.last_heartbeat = 0; self.last_progress = Instant::now(); self.perf_consumer = None; self.stop_stream(); let device = runtime.default_output_device().ok_or(HostAudioError::NoOutputDevice)?; let config = cpal::StreamConfig { channels: 2, sample_rate: cpal::SampleRate(OUTPUT_SAMPLE_RATE), buffer_size: cpal::BufferSize::Default, }; let transport = self.transport.clone(); let failed = self.failed.clone(); let heartbeat = self.heartbeat.clone(); let (mut callback, reclaimer) = AudioCallback::new(); // To pass performance data from the audio thread to the main thread let audio_perf_rb = HeapRb::::new(64); let (mut perf_prod, perf_cons) = audio_perf_rb.split(); let stream = runtime.build_output_stream( &device, &config, move |data: &mut [f32]| { heartbeat.fetch_add(1, Ordering::Relaxed); callback.render(&transport, data); // Sends processing time in microseconds let _ = perf_prod.try_push(callback.mixer.last_processing_time.as_micros() as u64); }, move |err| { failed.store(true, Ordering::Relaxed); eprintln!("audio stream error: {err}; switching to silent audio"); }, )?; runtime.play_stream(&stream)?; self.reclaimer = Some(reclaimer); self.perf_consumer = Some(perf_cons); self._stream = Some(Box::new(stream)); Ok(()) } /// Called even while guest execution is backpressured. A failed or stalled /// device cannot retain the only publication slot indefinitely. pub fn service(&mut self) { if let Some(reclaimer) = &mut self.reclaimer { reclaimer.service(&self.transport); } let heartbeat = self.heartbeat.load(Ordering::Relaxed); if heartbeat != self.last_heartbeat { self.last_heartbeat = heartbeat; self.last_progress = Instant::now(); } if self._stream.is_some() && (self.failed.load(Ordering::Relaxed) || self.last_progress.elapsed() >= Duration::from_secs(2)) { eprintln!("[HostAudio] Output unavailable/stalled; consuming audio silently."); self.stop_stream(); self.perf_consumer = None; } if self._stream.is_none() { self.transport.consume_silently(); } } fn stop_stream(&mut self) { if let Some(stream) = self._stream.take() { stream.stop(); drop(stream); // Stop/drop the callback before any silent consumer takes over. } if let Some(mut reclaimer) = self.reclaimer.take() { reclaimer.service(&self.transport); } } pub fn update_stats(&mut self, stats: &mut crate::stats::HostStats) { if let Some(cons) = &mut self.perf_consumer { while let Some(us) = cons.try_pop() { stats.record_audio_perf(us); } } } } impl Drop for HostAudio { fn drop(&mut self) { self.stop_stream(); self.transport.consume_silently(); } } /// The same bounded runtime-owned data path used by CPAL and allocator tests. pub struct AudioCallback { mixer: AudioMixer, lane: CallbackLane, } impl AudioCallback { pub fn new() -> (Self, AudioReclaimer) { let (lane, reclaimer) = reclamation::pool(); (Self { mixer: AudioMixer::new(), lane }, reclaimer) } pub fn render(&mut self, transport: &AudioTransport, data: &mut [f32]) { let mut packet = self.lane.take(); if let Some(packet) = &mut packet { // Reserve reclamation before acquiring any publication ownership. self.mixer.retire_inactive(packet); if let Some(delivery) = transport.try_take() { self.mixer.apply_delivery_into(delivery, packet); } } self.mixer.mix(data); if let Some(mut packet) = packet { self.mixer.retire_inactive(&mut packet); self.lane.give_back(packet); } } } #[cfg(test)] #[allow(clippy::items_after_test_module)] mod tests { use super::*; use crate::stats::HostStats; struct FakeStream; impl AudioStreamHandle for FakeStream {} #[derive(Clone, Copy)] struct FakeDevice; struct FakeRuntime { has_device: bool, build_result: Result<(), HostAudioError>, play_result: Result<(), HostAudioError>, } impl AudioRuntime for FakeRuntime { type Device = FakeDevice; type Stream = FakeStream; fn default_output_device(&self) -> Option { self.has_device.then_some(FakeDevice) } fn build_output_stream( &self, _device: &Self::Device, _config: &cpal::StreamConfig, _data_callback: D, _error_callback: E, ) -> Result where D: FnMut(&mut [f32]) + Send + 'static, E: FnMut(String) + Send + 'static, { self.build_result.clone().map(|_| FakeStream) } fn play_stream(&self, _stream: &Self::Stream) -> Result<(), HostAudioError> { self.play_result.clone() } } #[test] fn test_service_without_audio_is_safe() { let mut audio = HostAudio::new(); audio.service(); assert!(audio.transport.ready()); } #[test] fn test_update_stats_without_audio_is_safe() { let mut audio = HostAudio::new(); let mut stats = HostStats::new(); audio.update_stats(&mut stats); assert_eq!(stats.audio_load_accum_us, 0); assert_eq!(stats.audio_load_samples, 0); } #[test] fn test_init_returns_error_when_no_output_device_exists() { let mut audio = HostAudio::new(); let runtime = FakeRuntime { has_device: false, build_result: Ok(()), play_result: Ok(()) }; let result = audio.init_with_runtime(&runtime); assert_eq!(result, Err(HostAudioError::NoOutputDevice)); assert!(audio.perf_consumer.is_none()); assert!(audio._stream.is_none()); } #[test] fn test_init_returns_error_when_stream_build_fails() { let mut audio = HostAudio::new(); let runtime = FakeRuntime { has_device: true, build_result: Err(HostAudioError::BuildStream("simulated build failure".to_string())), play_result: Ok(()), }; let result = audio.init_with_runtime(&runtime); assert_eq!(result, Err(HostAudioError::BuildStream("simulated build failure".to_string()))); assert!(audio.perf_consumer.is_none()); assert!(audio._stream.is_none()); } #[test] fn test_init_returns_error_when_stream_play_fails() { let mut audio = HostAudio::new(); let runtime = FakeRuntime { has_device: true, build_result: Ok(()), play_result: Err(HostAudioError::PlayStream("simulated play failure".to_string())), }; let result = audio.init_with_runtime(&runtime); assert_eq!(result, Err(HostAudioError::PlayStream("simulated play failure".to_string()))); assert!(audio.perf_consumer.is_none()); assert!(audio._stream.is_none()); } #[test] fn test_init_populates_audio_state_on_success() { let mut audio = HostAudio::new(); let runtime = FakeRuntime { has_device: true, build_result: Ok(()), play_result: Ok(()) }; audio.init_with_runtime(&runtime).unwrap(); assert!(audio.perf_consumer.is_some()); assert!(audio._stream.is_some()); } } pub struct AudioMixer { voices: [Channel; MAX_CHANNELS], pub last_processing_time: Duration, paused: bool, epoch: u64, } impl Default for AudioMixer { fn default() -> Self { Self::new() } } impl AudioMixer { pub fn new() -> Self { Self { voices: Default::default(), last_processing_time: Duration::ZERO, paused: false, epoch: 0, } } fn apply_delivery_into(&mut self, delivery: AudioDelivery, packet: &mut Packet) { if delivery.epoch < self.epoch { packet.batches = [delivery.frame, delivery.lifecycle]; return; } if delivery.epoch != self.epoch { for voice in &mut self.voices { packet.retire(voice.sample.take()); *voice = Channel::default(); } self.epoch = delivery.epoch; } for (slot, batch) in packet.batches.iter_mut().zip([delivery.frame, delivery.lifecycle]) { *slot = batch; } // Move commands out of slots, keeping each vector allocation in its packet. for index in 0..2 { if let Some(mut batch) = packet.batches[index].take() { for command in &mut batch { let command = std::mem::replace(command, AudioCommand::Stop { voice_id: MAX_CHANNELS }); self.process_command(command, packet); } packet.batches[index] = Some(batch); } } self.paused = delivery.paused; } fn retire_inactive(&mut self, packet: &mut Packet) { for voice in &mut self.voices { if !voice.active { packet.retire(voice.sample.take()); } } } // Signal-only tests use synchronous reclamation outside a data callback. #[cfg(test)] fn apply_delivery(&mut self, delivery: AudioDelivery) { self.apply_delivery_into(delivery, &mut Packet::new()); } #[cfg(test)] fn fill_buffer(&mut self, buffer: &mut [f32]) { self.mix(buffer); self.retire_inactive(&mut Packet::new()); } fn process_command(&mut self, cmd: AudioCommand, packet: &mut Packet) { match cmd { AudioCommand::Play { sample, voice_id, volume, pan, pitch, priority, loop_mode } => { if voice_id < MAX_CHANNELS { packet.retire(self.voices[voice_id].sample.take()); self.voices[voice_id] = Channel { sample: Some(sample), active: true, pos: 0.0, pitch, volume, pan, loop_mode, priority, }; } else { packet.retire(Some(sample)); } } AudioCommand::Stop { voice_id } => { if voice_id < MAX_CHANNELS { self.voices[voice_id].active = false; packet.retire(self.voices[voice_id].sample.take()); } } AudioCommand::SetVolume { voice_id, volume } => { if voice_id < MAX_CHANNELS { self.voices[voice_id].volume = volume; } } AudioCommand::SetPan { voice_id, pan } => { if voice_id < MAX_CHANNELS { self.voices[voice_id].pan = pan; } } AudioCommand::SetPitch { voice_id, pitch } => { if voice_id < MAX_CHANNELS { self.voices[voice_id].pitch = pitch; } } } } fn mix(&mut self, buffer: &mut [f32]) { let start = std::time::Instant::now(); // Zeroes the buffer (stereo) for sample in buffer.iter_mut() { *sample = 0.0; } if self.paused { self.last_processing_time = start.elapsed(); return; } for voice in self.voices.iter_mut() { if !voice.active { continue; } let Some(sample) = voice.sample.as_ref() else { continue; }; let len = sample.data.len(); if len == 0 { voice.active = false; continue; } let (loop_start, end) = match (voice.loop_mode, sample.loop_start, sample.loop_end) { (LoopMode::On, Some(start), Some(end)) if start < end && end as usize <= len => { (start as f64, end as f64) } _ => (0.0, len as f64), }; let ratio = sample.sample_rate as f64 / OUTPUT_SAMPLE_RATE as f64; let volume = voice.volume as f32 / 255.0; let pan = voice.pan as f32 / 255.0; let left = volume * (1.0 - pan).sqrt(); let right = volume * pan.sqrt(); for frame in buffer.as_chunks_mut::<2>().0 { let pos = voice.pos as usize; if pos >= len { voice.active = false; break; } let s1 = sample.data[pos] as f32 / 32768.0; let next = if voice.loop_mode == LoopMode::On && (pos + 1) as f64 >= end { loop_start as usize } else { pos + 1 }; let s2 = sample.data.get(next).copied().unwrap_or(0) as f32 / 32768.0; let value = s1 + (s2 - s1) * (voice.pos - pos as f64) as f32; frame[0] += value * left; frame[1] += value * right; if ratio == 0.0 { continue; } if voice.pitch >= (end - voice.pos) / ratio { if voice.loop_mode == LoopMode::Off { voice.active = false; break; } let length = end - loop_start; // Reduce before multiplication: every finite positive pitch, // including f64::MAX, remains finite and wraps multiple loops. let step = (voice.pitch % (length / ratio)) * ratio; voice.pos = loop_start + (voice.pos - loop_start + step).rem_euclid(length); if voice.pos >= end { voice.pos = loop_start; } } else { voice.pos += voice.pitch * ratio; } } } // Final clamp to avoid clipping (optional if using f32, but good for fidelity) for sample in buffer.iter_mut() { *sample = sample.clamp(-1.0, 1.0); } self.last_processing_time = start.elapsed(); } } #[cfg(test)] mod control_tests { use super::*; use prometeu_drivers::{Audio, MemoryBanks}; use prometeu_hal::{AudioBridge, AudioOpStatus, sample::Sample}; fn pair() -> (Audio, AudioMixer) { (Audio::new(Arc::new(MemoryBanks::new())), AudioMixer::new()) } fn apply(audio: &Audio, mixer: &mut AudioMixer) { mixer.apply_delivery(audio.transport().try_take().unwrap()); } fn play(audio: &mut Audio, sample: Arc, pitch: f64, looping: bool) { assert_eq!( audio.play_sample( sample, 0, 255, 0, pitch, 0, if looping { LoopMode::On } else { LoopMode::Off } ), AudioOpStatus::Ok ); } #[test] fn one_shot_ignores_markers_and_loop_preserves_intro() { for looping in [false, true] { let (mut audio, mut mixer) = pair(); let sample = Sample::new(48_000, vec![1024, 2048, 4096, 8192, 12288, 16384, 20480, 24576]) .with_loop(2, 4); play(&mut audio, Arc::new(sample), 1.0, looping); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 20]; mixer.fill_buffer(&mut pcm); let expected = if looping { [1024, 2048, 4096, 8192, 4096, 8192, 4096, 8192, 4096, 8192] } else { [1024, 2048, 4096, 8192, 12288, 16384, 20480, 24576, 0, 0] }; for (frame, value) in pcm.as_chunks::<2>().0.iter().zip(expected) { assert_eq!(*frame, [value as f32 / 32768.0, 0.0]); } assert_eq!(mixer.voices[0].active, looping); assert_eq!(mixer.voices[0].sample.is_some(), looping); } } #[test] fn absent_partial_and_invalid_markers_fall_back_to_full_sample() { for (start, end) in [ (None, None), (Some(1), None), (None, Some(2)), (Some(2), Some(2)), (Some(3), Some(1)), (Some(0), Some(5)), (Some(u32::MAX), Some(u32::MAX)), (Some(0), Some(0)), ] { for looping in [false, true] { let (mut audio, mut mixer) = pair(); let mut sample = Sample::new(48_000, vec![4096, 8192, 12288, 16384]); sample.loop_start = start; sample.loop_end = end; play(&mut audio, Arc::new(sample), 1.0, looping); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 16]; mixer.fill_buffer(&mut pcm); for (i, frame) in pcm.as_chunks::<2>().0.iter().enumerate() { let value = if looping || i < 4 { (i % 4 + 1) as f32 / 8.0 } else { 0.0 }; assert_eq!(*frame, [value, 0.0], "markers {start:?}/{end:?}, loop {looping}"); } assert_eq!(mixer.voices[0].active, looping); } } } #[test] fn fractional_interpolation_uses_loop_seam_or_actual_tail() { // Both rate conversion and fractional pitch produce a half-frame step. for (rate, pitch) in [(48_000, 0.5), (24_000, 1.0), (96_000, 0.25)] { for looping in [false, true] { let (mut audio, mut mixer) = pair(); play( &mut audio, Arc::new(Sample::new(rate, vec![4096, 8192, 16384, 24576]).with_loop(1, 3)), pitch, looping, ); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 18]; mixer.fill_buffer(&mut pcm); let expected = if looping { [0.125, 0.1875, 0.25, 0.375, 0.5, 0.375, 0.25, 0.375, 0.5] } else { [0.125, 0.1875, 0.25, 0.375, 0.5, 0.625, 0.75, 0.375, 0.0] }; for (frame, value) in pcm.as_chunks::<2>().0.iter().zip(expected) { assert!((frame[0] - value).abs() <= 1e-6); assert_eq!(frame[1], 0.0); } assert_eq!(mixer.voices[0].active, looping); } } } #[test] fn empty_samples_terminate_without_indexing() { for looping in [false, true] { for marked in [false, true] { let (mut audio, mut mixer) = pair(); let sample = Sample::new(48_000, vec![]); let sample = if marked { sample.with_loop(0, 1) } else { sample }; play(&mut audio, Arc::new(sample), f64::MAX, looping); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [1.0; 8]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 8]); assert!(!mixer.voices[0].active); assert!(mixer.voices[0].sample.is_none()); } } } #[test] fn multiple_wrap_pitch_preserves_selected_interval_and_signal() { for (rate, pitch) in [(48_000, 5.0), (96_000, 2.5)] { let (mut audio, mut mixer) = pair(); play( &mut audio, Arc::new(Sample::new(rate, vec![4096, 8192, 16384, 24576]).with_loop(1, 3)), pitch, true, ); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 10]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.125, 0.0, 0.25, 0.0, 0.5, 0.0, 0.25, 0.0, 0.5, 0.0]); assert_eq!(mixer.voices[0].pos, 1.0); } } #[test] fn extreme_pitch_and_source_rates_keep_positions_finite() { for rate in [0, 1, 44_100, 48_000, 96_000, u32::MAX] { for looping in [false, true] { let (mut audio, mut mixer) = pair(); play( &mut audio, Arc::new(Sample::new(rate, vec![4096, 8192, 16384, 24576]).with_loop(1, 3)), f64::MAX, looping, ); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 128]; mixer.fill_buffer(&mut pcm); assert!(pcm.iter().all(|value| value.is_finite())); assert!(mixer.voices[0].pos.is_finite()); if rate == 0 { assert_eq!(mixer.voices[0].pos, 0.0); assert!(pcm.as_chunks::<2>().0.iter().all(|frame| *frame == [0.125, 0.0])); } else if looping { assert!((1.0..3.0).contains(&mixer.voices[0].pos)); assert!( pcm.as_chunks::<2>() .0 .iter() .skip(1) .all(|frame| (0.25..=0.5).contains(&frame[0])) ); } else { assert_eq!(&pcm[..2], &[0.125, 0.0]); assert!(pcm[2..].iter().all(|value| *value == 0.0)); } assert_eq!(mixer.voices[0].active, looping || rate == 0); } } } #[test] fn pcm_controls_preserve_position_and_stop_is_idempotent() { let (mut audio, mut mixer) = pair(); play(&mut audio, Arc::new(Sample::new(48_000, vec![16384; 32])), 1.0, true); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 4]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.5, 0.0, 0.5, 0.0]); assert_eq!(mixer.voices[0].pos, 2.0); audio.set_pan(0, 255); audio.set_volume(0, 51); audio.set_pitch(0, 2.0); audio.finish_frame(); apply(&audio, &mut mixer); assert_eq!(mixer.voices[0].pos, 2.0); mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0, 0.1, 0.0, 0.1]); assert_eq!(mixer.voices[0].pos, 6.0); audio.set_volume(0, 0); audio.finish_frame(); apply(&audio, &mut mixer); mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 4]); assert_eq!(mixer.voices[0].pos, 10.0); audio.stop(0); audio.stop(0); audio.finish_frame(); apply(&audio, &mut mixer); mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 4]); assert!(mixer.voices[0].sample.is_none()); } #[test] fn fifo_reuse_and_inactive_setters_do_not_create_play_defaults() { let (mut audio, mut mixer) = pair(); audio.set_volume(0, 0); audio.set_pitch(0, 4.0); play(&mut audio, Arc::new(Sample::new(48_000, vec![16384; 16])), 1.0, true); audio.stop(0); play(&mut audio, Arc::new(Sample::new(48_000, vec![8192; 16])), 1.0, true); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 2]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.25, 0.0]); assert_eq!(mixer.voices[0].pos, 1.0); } #[test] fn pause_and_session_reset_work_with_pending_batches() { let (mut audio, mut mixer) = pair(); audio.set_session(Some(1)); play(&mut audio, Arc::new(Sample::new(48_000, vec![16384; 16])), 1.0, true); audio.finish_frame(); audio.set_paused(true); apply(&audio, &mut mixer); let mut pcm = [1.0; 4]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 4]); assert_eq!(mixer.voices[0].pos, 0.0); audio.begin_lifecycle(); audio.set_pan(0, 255); audio.finish_lifecycle(true); audio.set_paused(false); apply(&audio, &mut mixer); mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0, 0.5, 0.0, 0.5]); assert_eq!(mixer.voices[0].pos, 2.0); play(&mut audio, Arc::new(Sample::new(48_000, vec![16384; 16])), 1.0, true); audio.finish_frame(); let stale = audio.transport().try_take().unwrap(); audio.reset(); apply(&audio, &mut mixer); mixer.apply_delivery(stale); mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 4]); } #[test] fn play_before_home_cannot_restart_after_callback_stop_and_resume() { let (mut audio, mut mixer) = pair(); play(&mut audio, Arc::new(Sample::new(48_000, vec![16384; 16])), 1.0, true); // Home occurs before FRAME_SYNC; the old play must precede the hook stop. audio.set_paused(true); audio.begin_lifecycle(); audio.stop(0); audio.finish_lifecycle(true); apply(&audio, &mut mixer); assert!(mixer.voices[0].sample.is_none()); audio.begin_lifecycle(); audio.finish_lifecycle(true); audio.set_paused(false); apply(&audio, &mut mixer); let mut pcm = [1.0; 16]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 16]); audio.finish_frame(); apply(&audio, &mut mixer); mixer.fill_buffer(&mut pcm); assert_eq!(pcm, [0.0; 16]); assert!(mixer.voices[0].sample.is_none()); } #[test] fn short_loops_and_extreme_pitch_remain_finite_and_release_at_completion() { for pitch in [0.125, 1.0, 4.0, 1000.0, f64::MAX] { let (mut audio, mut mixer) = pair(); play( &mut audio, Arc::new(Sample::new(96_000, vec![16384; 3]).with_loop(1, 3)), pitch, true, ); audio.finish_frame(); apply(&audio, &mut mixer); let mut pcm = [0.0; 128]; mixer.fill_buffer(&mut pcm); assert!(pcm.iter().all(|v| v.is_finite())); assert!(pcm.as_chunks::<2>().0.iter().all(|frame| *frame == [0.5, 0.0])); assert!(mixer.voices[0].active); assert!(mixer.voices[0].pos >= 1.0 && mixer.voices[0].pos < 3.0); } let (mut audio, mut mixer) = pair(); let sample = Arc::new(Sample::new(48_000, vec![16384])); let weak = Arc::downgrade(&sample); play(&mut audio, sample, 1.0, false); audio.finish_frame(); apply(&audio, &mut mixer); mixer.fill_buffer(&mut [0.0; 4]); assert!(weak.upgrade().is_none()); } #[test] fn unloading_bank_keeps_playback_until_stop() { use prometeu_drivers::SoundBankPoolInstaller; use prometeu_hal::sound_bank::SoundBank; let banks = Arc::new(MemoryBanks::new()); let sample = Arc::new(Sample::new(48_000, vec![16384; 4])); let weak = Arc::downgrade(&sample); banks.install_sound_bank(0, Arc::new(SoundBank::new(vec![sample]))); let mut audio = Audio::new(banks.clone()); let mut mixer = AudioMixer::new(); assert_eq!(audio.play(0, 0, 0, 255, 0, 1.0, 0, LoopMode::On), AudioOpStatus::Ok); audio.finish_frame(); banks.clear_sound_banks(); apply(&audio, &mut mixer); let mut pcm = [0.0; 8]; mixer.fill_buffer(&mut pcm); assert_eq!(pcm[0], 0.5); assert!(weak.upgrade().is_some()); assert_eq!(audio.play(0, 0, 0, 255, 0, 1.0, 0, LoopMode::On), AudioOpStatus::BankInvalid); audio.stop(0); audio.finish_frame(); apply(&audio, &mut mixer); assert!(weak.upgrade().is_none()); } #[test] fn failed_or_stalled_output_releases_backpressure() { for failed in [true, false] { let (mut audio, _) = pair(); audio.stop(0); audio.finish_frame(); let mut host = HostAudio::new(); host.transport = audio.transport(); host._stream = Some(Box::new(FakeSmokeStream)); host.failed.store(failed, Ordering::Relaxed); if !failed { host.last_progress = Instant::now() - Duration::from_secs(3); } host.service(); assert!(audio.ready()); assert!(host._stream.is_none()); } } struct FakeSmokeStream; impl AudioStreamHandle for FakeSmokeStream {} #[test] #[ignore = "requires a real desktop audio device; emits a brief quiet tone"] fn desktop_audio_device_smoke() { let (mut audio, _) = pair(); let mut host = HostAudio::new(); host.init(audio.transport()).expect("physical output device"); let data = (0..4800) .map(|i| ((i as f64 * std::f64::consts::TAU * 440.0 / 48_000.0).sin() * 1500.0) as i16) .collect(); play(&mut audio, Arc::new(Sample::new(48_000, data)), 1.0, true); audio.finish_frame(); let deadline = Instant::now() + Duration::from_secs(2); while !audio.ready() && Instant::now() < deadline { host.service(); std::thread::sleep(Duration::from_millis(10)); } assert!(audio.ready()); assert!(host.heartbeat.load(Ordering::Relaxed) > 0); audio.set_volume(0, 127); audio.set_pan(0, 255); audio.set_pitch(0, 1.5); audio.finish_frame(); for _ in 0..10 { host.service(); std::thread::sleep(Duration::from_millis(10)); } assert!(audio.ready()); audio.stop(0); audio.finish_frame(); for _ in 0..10 { host.service(); std::thread::sleep(Duration::from_millis(10)); } assert!(audio.ready()); assert!(!host.failed.load(Ordering::Relaxed)); } } #[cfg(test)] mod reclamation_tests { use super::*; use prometeu_drivers::{Audio, MemoryBanks}; use prometeu_hal::{AudioBridge, sample::Sample}; #[test] fn stale_delivery_moves_whole_vectors_to_service_without_changing_playback() { let mut audio = Audio::new(Arc::new(MemoryBanks::new())); let sample = Arc::new(Sample::new(48000, vec![1000; 16])); let weak = Arc::downgrade(&sample); audio.play_sample(sample, 0, 255, 0, 1.0, 0, LoopMode::On); audio.finish_frame(); let stale = audio.transport().try_take().unwrap(); let allocation = stale.frame.as_ref().unwrap().as_ptr(); audio.reset(); let mut mixer = AudioMixer::new(); mixer.apply_delivery(audio.transport().try_take().unwrap()); let mut packet = Packet::new(); mixer.apply_delivery_into(stale, &mut packet); assert_eq!(packet.batches[0].as_ref().unwrap().as_ptr(), allocation); assert!(weak.upgrade().is_some()); mixer.mix(&mut [0.0; 32]); assert!(mixer.voices.iter().all(|voice| !voice.active)); drop(packet); // Main/service thread, including stale command owners. assert!(weak.upgrade().is_none()); } type Callback = Box; struct ManualStream(Arc>>); impl AudioStreamHandle for ManualStream {} impl Drop for ManualStream { fn drop(&mut self) { self.0.lock().unwrap().take(); } } struct ManualRuntime(Arc>>); impl AudioRuntime for ManualRuntime { type Device = (); type Stream = ManualStream; fn default_output_device(&self) -> Option<()> { Some(()) } fn build_output_stream( &self, _: &(), _: &cpal::StreamConfig, data: D, _: E, ) -> Result where D: FnMut(&mut [f32]) + Send + 'static, E: FnMut(String) + Send + 'static, { *self.0.lock().unwrap() = Some(Box::new(data)); Ok(ManualStream(self.0.clone())) } fn play_stream(&self, _: &ManualStream) -> Result<(), HostAudioError> { Ok(()) } } #[test] fn failure_stall_and_teardown_release_callback_and_pending_owners() { for mode in 0..3 { let callbacks = Arc::new(std::sync::Mutex::new(None)); let mut audio = Audio::new(Arc::new(MemoryBanks::new())); let mut host = HostAudio::new(); host.transport = audio.transport(); host.init_with_runtime(&ManualRuntime(callbacks.clone())).unwrap(); let mut owners = vec![]; for _ in 0..2 { let sample = Arc::new(Sample::new(48000, vec![1000; 16])); owners.push(Arc::downgrade(&sample)); audio.play_sample(sample, 0, 255, 0, 1.0, 0, LoopMode::On); audio.finish_frame(); callbacks.lock().unwrap().as_mut().unwrap()(&mut []); } let sample = Arc::new(Sample::new(48000, vec![1000; 16])); owners.push(Arc::downgrade(&sample)); audio.play_sample(sample, 0, 255, 0, 1.0, 0, LoopMode::On); audio.finish_frame(); assert!(!audio.ready()); if mode == 0 { host.failed.store(true, Ordering::Relaxed); } if mode == 1 { host.last_progress = Instant::now() - Duration::from_secs(3); host.last_heartbeat = host.heartbeat.load(Ordering::Relaxed); } if mode < 2 { host.service(); assert!(host._stream.is_none()); } drop(host); assert!(audio.ready()); assert!(callbacks.lock().unwrap().is_none()); assert!(owners.iter().all(|owner| owner.upgrade().is_none())); } } }