Add self-contained SoundBanks and MusicBanks with per-sample PCM16 and IMA ADPCM storage, materialized to resident PCM16. Integrate instrument envelopes, automation, shared DSP and host lifecycle controls while preserving staged real-time execution.
83 lines
2.9 KiB
Rust
83 lines
2.9 KiB
Rust
//! Two packets circulate free -> callback -> returned -> service -> free.
|
|
//! While the callback owns a packet, at most one packet can occupy the return
|
|
//! lane, so its capacity-two enqueue cannot fail. Neither lane grows.
|
|
use prometeu_drivers::AudioTransport;
|
|
pub(super) use prometeu_drivers::engine::Packet;
|
|
#[cfg(test)]
|
|
use prometeu_drivers::engine::RETIRED_CAPACITY;
|
|
use ringbuf::{
|
|
HeapRb,
|
|
traits::{Consumer, Producer, Split},
|
|
wrap::{CachingCons, CachingProd},
|
|
};
|
|
use std::sync::Arc;
|
|
type Prod = CachingProd<Arc<HeapRb<Packet>>>;
|
|
type Cons = CachingCons<Arc<HeapRb<Packet>>>;
|
|
pub(super) struct CallbackLane {
|
|
free: Cons,
|
|
returned: Prod,
|
|
}
|
|
/// Main-thread endpoint. Service independently of guest progress or pause.
|
|
pub struct AudioReclaimer {
|
|
pub maximum_retired_pcm_bytes: usize,
|
|
free: Prod,
|
|
returned: Cons,
|
|
}
|
|
pub(super) fn pool() -> (CallbackLane, AudioReclaimer) {
|
|
let (mut free, free_cons) = HeapRb::new(2).split();
|
|
let (returned, returned_cons) = HeapRb::new(2).split();
|
|
for _ in 0..2 {
|
|
assert!(free.try_push(Packet::new()).is_ok());
|
|
}
|
|
(
|
|
CallbackLane { free: free_cons, returned },
|
|
AudioReclaimer { free, returned: returned_cons, maximum_retired_pcm_bytes: 0 },
|
|
)
|
|
}
|
|
impl CallbackLane {
|
|
pub(super) fn take(&mut self) -> Option<Packet> {
|
|
self.free.try_pop()
|
|
}
|
|
pub(super) fn give_back(&mut self, packet: Packet) {
|
|
// Conservation proves capacity; there is no overflow/drop fallback.
|
|
assert!(self.returned.try_push(packet).is_ok(), "audio packet conservation");
|
|
}
|
|
}
|
|
impl AudioReclaimer {
|
|
pub fn service(&mut self, transport: &AudioTransport) {
|
|
while let Some(mut packet) = self.returned.try_pop() {
|
|
self.maximum_retired_pcm_bytes =
|
|
self.maximum_retired_pcm_bytes.max(packet.retained_pcm_bytes());
|
|
packet.recycle(transport);
|
|
assert!(self.free.try_push(packet).is_ok(), "audio packet conservation");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn two_packets_conserve_capacity_across_service_starvation() {
|
|
let (mut callback, mut service) = pool();
|
|
let first = callback.take().unwrap();
|
|
let second = callback.take().unwrap();
|
|
assert!(callback.take().is_none());
|
|
callback.give_back(first);
|
|
callback.give_back(second);
|
|
assert!(callback.take().is_none());
|
|
let transport = AudioTransport::default();
|
|
for _ in 0..1000 {
|
|
service.service(&transport);
|
|
for _ in 0..2 {
|
|
let packet = callback.take().unwrap();
|
|
assert_eq!(packet.capacity(), RETIRED_CAPACITY);
|
|
assert!(packet.is_empty());
|
|
assert!(packet.batches.iter().all(Option::is_none));
|
|
callback.give_back(packet);
|
|
}
|
|
assert!(callback.take().is_none());
|
|
}
|
|
}
|
|
}
|