aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <silas@exvacuum.dev>2024-09-09 18:44:52 -0400
committerLibravatar Silas Bartha <silas@exvacuum.dev>2024-09-09 18:44:52 -0400
commit1e80d0684ddbcd3d995a05e572fafb181263758c (patch)
treec0b6b9366a00fe5148a691faac7e0cc8569e98a7 /src/lib.rs
Initial Commit
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..2906d61
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,31 @@
+#![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::Read,
+ sync::{Arc, OnceLock},
+};
+
+mod assets;
+pub use assets::*;
+
+pub(crate) static SOUNDFONT: OnceLock<Arc<SoundFont>> = OnceLock::new();
+
+/// This plugin configures the soundfont used for playback and registers MIDI assets.
+#[derive(Default, Debug)]
+pub struct RustySynthPlugin<R: Read + Send + Sync + Clone + 'static> {
+ /// Reader for soundfont data. A default is not provided since soundfonts can be quite large.
+ pub soundfont: R,
+}
+
+impl<R: Read + Send + Sync + Clone + 'static> Plugin for RustySynthPlugin<R> {
+ fn build(&self, app: &mut App) {
+ let _ = SOUNDFONT.set(Arc::new(
+ SoundFont::new(&mut self.soundfont.clone()).unwrap(),
+ ));
+ app.add_audio_source::<MidiAudio>().init_asset::<MidiAudio>().init_asset_loader::<MidiAssetLoader>();
+ }
+}