#![warn(missing_docs)] //! A plugin which adds MIDI file and soundfont audio support to the [bevy](https://crates.io/crates/bevy) engine via [rustysynth](https://crates.io/crates/rustysynth). use bevy::{audio::AddAudioSource, prelude::*}; use rustysynth::SoundFont; use std::{ io::{Cursor, Read}, sync::{Arc, OnceLock}, }; mod assets; pub use assets::*; #[cfg(feature = "hl4mgm")] pub(crate) static HL4MGM: &[u8] = include_bytes!("./embedded_assets/hl4mgm.sf2"); pub(crate) static SOUNDFONT: OnceLock> = OnceLock::new(); /// This plugin configures the soundfont used for playback and registers MIDI assets. #[derive(Debug)] pub struct RustySynthPlugin { /// Reader for soundfont data. pub soundfont: R, } #[cfg(feature = "hl4mgm")] impl Default for RustySynthPlugin> { fn default() -> Self { Self { soundfont: Cursor::new(HL4MGM), } } } impl Plugin for RustySynthPlugin { fn build(&self, app: &mut App) { let _ = SOUNDFONT.set(Arc::new( SoundFont::new(&mut self.soundfont.clone()).unwrap(), )); app.add_audio_source::() .init_asset::() .init_asset_loader::(); } }