feat(audio): add 32-voice PCM mixing and native music sequencing
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.
This commit is contained in:
@@ -1,40 +1,16 @@
|
||||
//! 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::{AudioCommand, AudioTransport, MAX_CHANNELS, MAX_COMMANDS_PER_FRAME};
|
||||
use prometeu_hal::sample::Sample;
|
||||
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;
|
||||
|
||||
pub(super) const RETIRED_CAPACITY: usize = 3 * MAX_COMMANDS_PER_FRAME + MAX_CHANNELS;
|
||||
pub(super) struct Packet {
|
||||
retired: Vec<Arc<Sample>>,
|
||||
pub batches: [Option<Vec<AudioCommand>>; 2],
|
||||
}
|
||||
impl Packet {
|
||||
pub(super) fn new() -> Self {
|
||||
Self { retired: Vec::with_capacity(RETIRED_CAPACITY), batches: [None, None] }
|
||||
}
|
||||
pub(super) fn retire(&mut self, sample: Option<Arc<Sample>>) {
|
||||
if let Some(sample) = sample {
|
||||
// Every owner comes from at most 3072 Play commands or 16 slots.
|
||||
assert!(self.retired.len() < RETIRED_CAPACITY, "audio retirement bound");
|
||||
self.retired.push(sample);
|
||||
}
|
||||
}
|
||||
fn recycle(&mut self, transport: &AudioTransport) {
|
||||
self.retired.clear();
|
||||
for batch in &mut self.batches {
|
||||
if let Some(batch) = batch.take() {
|
||||
transport.recycle_commands(batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
type Prod = CachingProd<Arc<HeapRb<Packet>>>;
|
||||
type Cons = CachingCons<Arc<HeapRb<Packet>>>;
|
||||
pub(super) struct CallbackLane {
|
||||
@@ -43,6 +19,7 @@ pub(super) struct CallbackLane {
|
||||
}
|
||||
/// Main-thread endpoint. Service independently of guest progress or pause.
|
||||
pub struct AudioReclaimer {
|
||||
pub maximum_retired_pcm_bytes: usize,
|
||||
free: Prod,
|
||||
returned: Cons,
|
||||
}
|
||||
@@ -52,7 +29,10 @@ pub(super) fn pool() -> (CallbackLane, AudioReclaimer) {
|
||||
for _ in 0..2 {
|
||||
assert!(free.try_push(Packet::new()).is_ok());
|
||||
}
|
||||
(CallbackLane { free: free_cons, returned }, AudioReclaimer { free, returned: returned_cons })
|
||||
(
|
||||
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> {
|
||||
@@ -66,6 +46,8 @@ impl CallbackLane {
|
||||
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");
|
||||
}
|
||||
@@ -89,8 +71,8 @@ mod tests {
|
||||
service.service(&transport);
|
||||
for _ in 0..2 {
|
||||
let packet = callback.take().unwrap();
|
||||
assert_eq!(packet.retired.capacity(), RETIRED_CAPACITY);
|
||||
assert!(packet.retired.is_empty());
|
||||
assert_eq!(packet.capacity(), RETIRED_CAPACITY);
|
||||
assert!(packet.is_empty());
|
||||
assert!(packet.batches.iter().all(Option::is_none));
|
||||
callback.give_back(packet);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user