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:
@@ -7,6 +7,7 @@
|
||||
//! the render read surface.
|
||||
|
||||
use prometeu_hal::glyph_bank::GlyphBank;
|
||||
use prometeu_hal::music_bank::MusicBank;
|
||||
use prometeu_hal::render_resources::RenderResourceAccess as HalRenderResourceAccess;
|
||||
use prometeu_hal::scene_bank::SceneBank;
|
||||
use prometeu_hal::sound_bank::SoundBank;
|
||||
@@ -30,6 +31,7 @@ pub trait GlyphBankPoolInstaller: Send + Sync {
|
||||
|
||||
/// Non-generic interface for peripherals to access sound banks.
|
||||
pub trait SoundBankPoolAccess: Send + Sync {
|
||||
fn music_bank_slot(&self, slot: usize) -> Option<Arc<MusicBank>>;
|
||||
/// Returns a reference to the resident SoundBank in the specified slot, if any.
|
||||
fn sound_bank_slot(&self, slot: usize) -> Option<Arc<SoundBank>>;
|
||||
/// Returns the total number of slots available in this bank.
|
||||
@@ -38,6 +40,9 @@ pub trait SoundBankPoolAccess: Send + Sync {
|
||||
|
||||
/// Non-generic interface for the AssetManager to install sound banks.
|
||||
pub trait SoundBankPoolInstaller: Send + Sync {
|
||||
fn install_music_bank(&self, slot: usize, bank: Arc<MusicBank>);
|
||||
fn clear_music_banks(&self);
|
||||
fn unload_audio_bank(&self, music: bool, slot: usize);
|
||||
/// Atomically swaps the resident SoundBank in the specified slot.
|
||||
fn install_sound_bank(&self, slot: usize, bank: Arc<SoundBank>);
|
||||
/// Clears all resident SoundBank slots.
|
||||
@@ -68,6 +73,7 @@ pub trait SceneBankPoolInstaller: Send + Sync {
|
||||
pub struct MemoryBanks {
|
||||
glyph_bank_pool: Arc<RwLock<[Option<Arc<GlyphBank>>; 16]>>,
|
||||
sound_bank_pool: Arc<RwLock<[Option<Arc<SoundBank>>; 16]>>,
|
||||
music_bank_pool: Arc<RwLock<[Option<Arc<MusicBank>>; 16]>>,
|
||||
scene_bank_pool: Arc<RwLock<[Option<Arc<SceneBank>>; 16]>>,
|
||||
}
|
||||
|
||||
@@ -83,6 +89,7 @@ impl MemoryBanks {
|
||||
Self {
|
||||
glyph_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
sound_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
music_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
scene_bank_pool: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
|
||||
}
|
||||
}
|
||||
@@ -113,6 +120,9 @@ impl GlyphBankPoolInstaller for MemoryBanks {
|
||||
}
|
||||
|
||||
impl SoundBankPoolAccess for MemoryBanks {
|
||||
fn music_bank_slot(&self, slot: usize) -> Option<Arc<MusicBank>> {
|
||||
self.music_bank_pool.read().unwrap().get(slot).and_then(Clone::clone)
|
||||
}
|
||||
fn sound_bank_slot(&self, slot: usize) -> Option<Arc<SoundBank>> {
|
||||
let pool = self.sound_bank_pool.read().unwrap();
|
||||
pool.get(slot).and_then(|s| s.as_ref().map(Arc::clone))
|
||||
@@ -124,15 +134,61 @@ impl SoundBankPoolAccess for MemoryBanks {
|
||||
}
|
||||
|
||||
impl SoundBankPoolInstaller for MemoryBanks {
|
||||
fn install_music_bank(&self, slot: usize, bank: Arc<MusicBank>) {
|
||||
if slot >= 16 {
|
||||
return;
|
||||
}
|
||||
let prepared = Arc::new(bank.resident_copy());
|
||||
let mut pool = self.music_bank_pool.write().unwrap();
|
||||
if let Some(target) = pool.get_mut(slot) {
|
||||
if let Some(old) = target.take() {
|
||||
old.invalidate();
|
||||
}
|
||||
*target = Some(prepared);
|
||||
}
|
||||
}
|
||||
fn clear_music_banks(&self) {
|
||||
let mut pool = self.music_bank_pool.write().unwrap();
|
||||
for bank in pool.iter_mut() {
|
||||
if let Some(old) = bank.take() {
|
||||
old.invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
fn unload_audio_bank(&self, music: bool, slot: usize) {
|
||||
if music {
|
||||
if let Some(bank) =
|
||||
self.music_bank_pool.write().unwrap().get_mut(slot).and_then(Option::take)
|
||||
{
|
||||
bank.invalidate();
|
||||
}
|
||||
} else if let Some(bank) =
|
||||
self.sound_bank_pool.write().unwrap().get_mut(slot).and_then(Option::take)
|
||||
{
|
||||
bank.invalidate();
|
||||
}
|
||||
}
|
||||
fn install_sound_bank(&self, slot: usize, bank: Arc<SoundBank>) {
|
||||
if slot >= 16 {
|
||||
return;
|
||||
}
|
||||
let prepared = Arc::new(bank.resident_copy());
|
||||
let mut pool = self.sound_bank_pool.write().unwrap();
|
||||
if slot < 16 {
|
||||
pool[slot] = Some(bank);
|
||||
if let Some(old) = pool[slot].take() {
|
||||
old.invalidate();
|
||||
}
|
||||
pool[slot] = Some(prepared);
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_sound_banks(&self) {
|
||||
self.sound_bank_pool.write().unwrap().fill(None);
|
||||
let mut pool = self.sound_bank_pool.write().unwrap();
|
||||
for bank in pool.iter_mut() {
|
||||
if let Some(old) = bank.take() {
|
||||
old.invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,3 +302,84 @@ mod tests {
|
||||
assert_eq!(HalRenderResourceAccess::scene_bank_count(&banks), 16);
|
||||
}
|
||||
}
|
||||
|
||||
/// Owner-thread diagnostics: canonical bank costs and unique PCM allocation costs
|
||||
/// are separate, so loading the same asset in two slots does not double-count RAM.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AudioSlotMemory {
|
||||
pub music: bool,
|
||||
pub slot: usize,
|
||||
pub name: String,
|
||||
pub metrics: prometeu_hal::sound_bank::AudioBankMetrics,
|
||||
}
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AudioMemorySnapshot {
|
||||
pub banks: Vec<AudioSlotMemory>,
|
||||
pub unique_pcm_bytes: usize,
|
||||
pub allocated_pcm_bytes: usize,
|
||||
/// Resident Rust structs/vector/string capacities; excludes allocator/Arc headers and host caches.
|
||||
pub metadata_bytes: usize,
|
||||
pub total_accounted_bytes: usize,
|
||||
}
|
||||
impl MemoryBanks {
|
||||
pub fn audio_memory(&self) -> AudioMemorySnapshot {
|
||||
let mut snapshot = AudioMemorySnapshot::default();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut metadata = 0usize;
|
||||
let mut vectors = std::collections::HashSet::new();
|
||||
let mut add =
|
||||
|music, slot, name: String, metrics, samples: &[Arc<prometeu_hal::sample::Sample>]| {
|
||||
for sample in samples {
|
||||
if seen.insert(Arc::as_ptr(sample)) {
|
||||
metadata += std::mem::size_of::<prometeu_hal::sample::Sample>();
|
||||
snapshot.unique_pcm_bytes += sample.data.len() * 2;
|
||||
snapshot.allocated_pcm_bytes += sample.data.capacity() * 2;
|
||||
}
|
||||
}
|
||||
snapshot.banks.push(AudioSlotMemory { music, slot, name, metrics });
|
||||
};
|
||||
for (slot, bank) in self.sound_bank_pool.read().unwrap().iter().enumerate() {
|
||||
if let Some(b) = bank {
|
||||
add(false, slot, b.name.clone(), b.metrics, &b.samples);
|
||||
snapshot.metadata_bytes += std::mem::size_of::<SoundBank>()
|
||||
+ b.name.capacity()
|
||||
+ std::mem::size_of::<std::sync::atomic::AtomicBool>();
|
||||
if vectors.insert(Arc::as_ptr(&b.samples) as usize) {
|
||||
snapshot.metadata_bytes += std::mem::size_of::<
|
||||
Vec<Arc<prometeu_hal::sample::Sample>>,
|
||||
>() + b.samples.capacity()
|
||||
* std::mem::size_of::<Arc<prometeu_hal::sample::Sample>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
for (slot, bank) in self.music_bank_pool.read().unwrap().iter().enumerate() {
|
||||
if let Some(b) = bank {
|
||||
add(true, slot, b.name.clone(), b.metrics, &b.samples);
|
||||
snapshot.metadata_bytes += std::mem::size_of::<MusicBank>()
|
||||
+ b.name.capacity()
|
||||
+ std::mem::size_of::<std::sync::atomic::AtomicBool>();
|
||||
if vectors.insert(Arc::as_ptr(&b.samples) as usize) {
|
||||
snapshot.metadata_bytes += std::mem::size_of::<
|
||||
Vec<Arc<prometeu_hal::sample::Sample>>,
|
||||
>() + b.samples.capacity()
|
||||
* std::mem::size_of::<Arc<prometeu_hal::sample::Sample>>();
|
||||
}
|
||||
if vectors.insert(Arc::as_ptr(&b.instruments) as usize) {
|
||||
snapshot.metadata_bytes += std::mem::size_of::<
|
||||
Vec<prometeu_hal::music_bank::Instrument>,
|
||||
>() + b.instruments.capacity()
|
||||
* std::mem::size_of::<prometeu_hal::music_bank::Instrument>();
|
||||
}
|
||||
if vectors.insert(Arc::as_ptr(&b.events) as usize) {
|
||||
snapshot.metadata_bytes += std::mem::size_of::<
|
||||
Vec<prometeu_hal::music_bank::TimedMusicEvent>,
|
||||
>() + b.events.capacity()
|
||||
* std::mem::size_of::<prometeu_hal::music_bank::TimedMusicEvent>();
|
||||
}
|
||||
}
|
||||
}
|
||||
snapshot.metadata_bytes += metadata;
|
||||
snapshot.total_accounted_bytes = snapshot.allocated_pcm_bytes + snapshot.metadata_bytes;
|
||||
snapshot
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user