basic midi import example

This commit is contained in:
🪞👃🪞 2024-11-24 03:47:03 +01:00
parent 35944cf684
commit f5b1f495ad
8 changed files with 114 additions and 66 deletions

View file

@ -1,4 +1,5 @@
use crate::*;
use tek_core::midly::Smf;
pub trait HasPhrases {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>>;
@ -10,11 +11,11 @@ pub enum PhrasePoolCommand {
Add(usize, Phrase),
Delete(usize),
Swap(usize, usize),
Color(usize, ItemColor),
Import(usize, String),
Export(usize, String),
SetName(usize, String),
SetLength(usize, usize),
SetColor(usize, ItemColor),
}
impl<T: HasPhrases> Command<T> for PhrasePoolCommand {
@ -39,12 +40,26 @@ impl<T: HasPhrases> Command<T> for PhrasePoolCommand {
model.phrases_mut().swap(index, other);
return Ok(Some(Self::Swap(index, other)))
},
Self::Color(index, color) => {
let mut color = ItemColorTriplet::from(color);
std::mem::swap(&mut color, &mut model.phrases()[index].write().unwrap().color);
return Ok(Some(Self::Color(index, color.base)))
},
Self::Import(index, path) => {
let bytes = std::fs::read(&path)?;
let smf = Smf::parse(bytes.as_slice())?;
println!("{:?}", &smf.header);
let mut t = 0u32;
let mut events = vec![];
for (i, track) in smf.tracks.iter().enumerate() {
for (j, event) in track.iter().enumerate() {
t += event.delta.as_int();
if let TrackEventKind::Midi { channel, message } = event.kind {
events.push((t, channel.as_int(), message));
}
}
}
let mut phrase = Phrase::new(&path, true, t as usize + 1, None, None);
for event in events.iter() {
println!("{event:?}");
phrase.notes[event.0 as usize].push(event.2);
}
return Self::Add(index, phrase).execute(model)
},
Self::Export(index, path) => {
},
@ -52,6 +67,11 @@ impl<T: HasPhrases> Command<T> for PhrasePoolCommand {
},
Self::SetLength(index, length) => {
},
Self::SetColor(index, color) => {
let mut color = ItemColorTriplet::from(color);
std::mem::swap(&mut color, &mut model.phrases()[index].write().unwrap().color);
return Ok(Some(Self::SetColor(index, color.base)))
},
}
Ok(None)
}