#![doc = r#"
Components to make songs programatically
"#]
use bevy::{asset::uuid::Uuid, prelude::*};
use midix::prelude::*;
pub mod simple;
pub use simple::*;
mod song_writer;
pub use song_writer::*;
mod builder;
pub use builder::*;
#[derive(Clone, Copy, Hash, Eq, PartialEq, Debug, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SongId(Uuid);
impl Default for SongId {
fn default() -> Self {
SongId(Uuid::new_v4())
}
}
#[derive(Clone, Debug, Reflect, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MidiSong {
pub(crate) id: SongId,
pub(crate) events: Vec<Timed<ChannelVoiceMessage>>,
pub looped: bool,
pub paused: bool,
}
impl MidiSong {
#[inline]
pub fn id(&self) -> SongId {
self.id
}
pub fn builder() -> MidiSongBuilder {
MidiSongBuilder::default()
}
pub fn new(events: Vec<Timed<ChannelVoiceMessage>>) -> Self {
Self {
id: SongId::default(),
events,
looped: false,
paused: false,
}
}
pub fn events_mut(&mut self) -> &mut Vec<Timed<ChannelVoiceMessage>> {
&mut self.events
}
pub fn set_paused(mut self) -> Self {
self.paused = true;
self
}
pub fn set_looped(mut self) -> Self {
self.looped = true;
self
}
pub fn set_speed(mut self, speed: f64) -> Self {
let speed = 1. / speed;
self.events
.iter_mut()
.for_each(|cmd| cmd.timestamp = (cmd.timestamp as f64 * speed) as u64);
self
}
pub fn events(&self) -> &[Timed<ChannelVoiceMessage>] {
&self.events
}
}
impl SongWriter for MidiSong {
fn song_id(&self) -> Option<SongId> {
Some(self.id)
}
fn events(&self) -> impl Iterator<Item = Timed<ChannelVoiceMessage>> {
self.events.iter().copied()
}
fn looped(&self) -> bool {
self.looped
}
fn paused(&self) -> bool {
self.paused
}
}