Skip to content

Commit

Permalink
Rework midi loading
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Jan 28, 2021
1 parent 20195ca commit 63f0b71
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 39 deletions.
6 changes: 6 additions & 0 deletions lib_midi/src/midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ impl Midi {
}
}

// merged_track.notes = merged_track
// .notes
// .into_iter()
// .filter(|n| n.ch == 9)
// .collect();

merged_track
.notes
.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
Expand Down
12 changes: 8 additions & 4 deletions src/output_manager/midi_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ impl MidiBackend {
}

impl OutputConnection for MidiOutputConnection {
fn note_on(&mut self, _ch: u8, key: u8, vel: u8) {
self.send(&[0x90, key, vel]).ok();
fn note_on(&mut self, ch: u8, key: u8, vel: u8) {
if ch <= 15 {
self.send(&[0x90 | ch, key, vel]).ok();
}
}
fn note_off(&mut self, _ch: u8, key: u8) {
self.send(&[0x80, key, 0]).ok();
fn note_off(&mut self, ch: u8, key: u8) {
if ch <= 15 {
self.send(&[0x80 | ch, key, 0]).ok();
}
}
}

Expand Down
67 changes: 38 additions & 29 deletions src/scene/menu_scene/iced_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Controls {
}

pub struct IcedMenu {
midi_file: Option<lib_midi::Midi>,
midi_file: bool,
pub font_path: Option<PathBuf>,

pub carousel: Carousel,
Expand All @@ -37,21 +37,24 @@ pub struct IcedMenu {
#[derive(Debug, Clone)]
pub enum Message {
FileSelectPressed,
FileSelected(PathBuf),

FontSelectPressed,

PrevPressed,
NextPressed,
PlayPressed,
EscPressed,

MidiFileUpdate(bool),
OutputsUpdated(Vec<OutputDescriptor>),

MainMenuDone(lib_midi::Midi, OutputDescriptor),
MainMenuDone(OutputDescriptor),
}

impl IcedMenu {
pub fn new(
midi_file: Option<lib_midi::Midi>,
midi_file: bool,
outputs: Vec<OutputDescriptor>,
out_id: Option<usize>,
font_path: Option<PathBuf>,
Expand Down Expand Up @@ -92,17 +95,20 @@ impl Program for IcedMenu {
{
Response::Okay(path) => {
log::info!("File path = {:?}", path);
let midi = lib_midi::Midi::new(path.to_str().unwrap());
// let midi = lib_midi::Midi::new(path.to_str().unwrap());

if let Err(e) = &midi {
log::error!("{}", e);
}
// if let Err(e) = &midi {
// log::error!("{}", e);
// }

self.midi_file = if let Ok(midi) = midi {
Some(midi)
} else {
None
};
// self.midi_file = if let Ok(midi) = midi {
// Some(midi)
// } else {
// None
// };
//

return Command::from(async { Message::FileSelected(path) });
}
_ => {
log::error!("User canceled dialog");
Expand Down Expand Up @@ -140,26 +146,26 @@ impl Program for IcedMenu {
}

Message::PlayPressed => {
if self.midi_file.is_some() {
if self.midi_file {
async fn play(m: Message) -> Message {
m
}

if self.midi_file.is_some() {
if let Some(midi) = std::mem::replace(&mut self.midi_file, None) {
if let Some(port) = self.carousel.get_item() {
let port = match port {
#[cfg(feature = "synth")]
OutputDescriptor::Synth(_) => OutputDescriptor::Synth(
std::mem::replace(&mut self.font_path, None),
),
_ => port.clone(),
};
let event = Message::MainMenuDone(midi, port);
return Command::from(play(event));
}
}
// if self.midi_file.is_some() {
// if let Some(midi) = std::mem::replace(&mut self.midi_file, None) {
if let Some(port) = self.carousel.get_item() {
let port = match port {
#[cfg(feature = "synth")]
OutputDescriptor::Synth(_) => OutputDescriptor::Synth(
std::mem::replace(&mut self.font_path, None),
),
_ => port.clone(),
};
let event = Message::MainMenuDone(port);
return Command::from(play(event));
}
// }
// }
}
}

Expand All @@ -172,11 +178,14 @@ impl Program for IcedMenu {
}
},

Message::MidiFileUpdate(is) => self.midi_file = is,

Message::OutputsUpdated(outs) => {
self.carousel.update(outs);
}

Message::MainMenuDone(_, _) => {}
Message::FileSelected(_) => {}
Message::MainMenuDone(_) => {}
}

Command::none()
Expand Down Expand Up @@ -212,7 +221,7 @@ impl Program for IcedMenu {

let footer: Element<_, _> = {
let content: Element<Self::Message, Self::Renderer> =
if self.midi_file.is_some() && self.carousel.get_item().is_some() {
if self.midi_file && self.carousel.get_item().is_some() {
let btn = NeoBtn::new(
&mut self.play_button,
Text::new("Play")
Expand Down
20 changes: 16 additions & 4 deletions src/scene/menu_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl MenuScene {
let timer = Timer::new();

let menu = IcedMenu::new(
std::mem::replace(&mut state.midi_file, None),
state.midi_file.is_some(),
state.output_manager.get_outputs(),
state.output_manager.selected_output_id,
state.output_manager.selected_font_path.clone(),
Expand Down Expand Up @@ -170,10 +170,22 @@ impl Scene for MenuScene {
let event = crate::block_on(async { f.await });

match event {
iced_menu::Message::MainMenuDone(midi, out) => {
let program = self.iced_state.program();
iced_menu::Message::FileSelected(path) => {
let midi = lib_midi::Midi::new(path.to_str().unwrap());

if let Err(e) = &midi {
log::error!("{}", e);
}

self.main_state.midi_file = midi.ok();

self.main_state.midi_file = Some(midi);
self.iced_state
.queue_message(iced_menu::Message::MidiFileUpdate(
self.main_state.midi_file.is_some(),
));
}
iced_menu::Message::MainMenuDone(out) => {
let program = self.iced_state.program();

self.main_state.output_manager.selected_output_id =
Some(program.carousel.id());
Expand Down
1 change: 0 additions & 1 deletion src/scene/playing_scene/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ impl PianoKeyboard {
Color::from_rgba8(125, 69, 134, 1.0),
],
];

let white_keys = white_keys.into_iter().map(|note| {
let color = colors[note.1 % 2];
if note.0 {
Expand Down
2 changes: 1 addition & 1 deletion src/scene/playing_scene/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Notes {

let mut longer_than_88 = false;
for note in midi.merged_track.notes.iter() {
if note.note >= 21 && note.note <= 108 {
if note.note >= 21 && note.note <= 108 && note.ch != 9 {
let key = &keys[note.note as usize - 21];
let ar = window_w / window_h;

Expand Down

0 comments on commit 63f0b71

Please sign in to comment.