kira icon indicating copy to clipboard operation
kira copied to clipboard

Ideas for caching StaticSoundData

Open isokissa opened this issue 9 months ago • 1 comments

(I am new to Rust, sorry if question is stupid)

In my application (bevy, but not relevant for this problem), I am trying to load all short sounds into memory, so that it can be available immediately when needed. I have some sort of sound_cache:

pub cached_sounds: HashMap<String, StaticSoundData>;

... 

if let Some(sound_data) = cached_sounds.get(&self.asset) {
    let _handle = kira_manager.play(sound_data.clone()); // TODO can we ever play a song without cloning from cache?
} else {
    tracing::info!("SFX asset not found in cache: {}", self.asset);
}

It seems that the only way to pass the StaticSoundData to play function is to clone it, which feels stupid: we have he whole song data in memory, it's immutable and why on earth we need to make a copy of it every time we want to play it. Song data can be really long.

Question: what would be the most efficient, most normal, most idiomatic way to cache the sounds in memory with Kira.

isokissa avatar Mar 01 '26 20:03 isokissa

OK, now I see that StaticSoundData.clone() is "cheap", it will actually not make a copy of audio data.

isokissa avatar Mar 01 '26 20:03 isokissa