start some ui, refactor midi library a bit

This commit is contained in:
Nicole Tietz-Sokolskaya 2024-12-08 22:07:14 -05:00
parent 0675e7f26e
commit de6dd59e48
6 changed files with 53 additions and 13 deletions

View file

@ -5,6 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.86" anyhow = "1.0.86"
eframe = "0.29.1"
egui = "0.29.1"
enigo = { version = "0.2.1", features = ["serde"] } enigo = { version = "0.2.1", features = ["serde"] }
hex = "0.4.3" hex = "0.4.3"
midir = "0.10.0" midir = "0.10.0"

View file

@ -13,6 +13,8 @@ fn main() {
//enigo.text("echo \"hello world\"").unwrap(); //enigo.text("echo \"hello world\"").unwrap();
//enigo.key(Key::Return, Direction::Press).unwrap(); //enigo.key(Key::Return, Direction::Press).unwrap();
midi_keys::ui::run();
match run() { match run() {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
@ -28,7 +30,7 @@ fn run() -> Result<()> {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
println!("error: {}", e); println!("error: {}", e);
return replay_file("assets/windsynth.log"); return Ok(());
} }
}; };
let port_name = midi_in.port_name(&midi_device)?; let port_name = midi_in.port_name(&midi_device)?;
@ -49,6 +51,8 @@ fn run() -> Result<()> {
let mut buf = String::new(); let mut buf = String::new();
stdin().read_line(&mut buf)?; stdin().read_line(&mut buf)?;
// TODO
let _ = replay_file("assets/windsynth.log");
Ok(()) Ok(())
} }

View file

@ -1,3 +1,4 @@
pub mod log; pub mod log;
pub mod midi; pub mod midi;
pub mod parser; pub mod parser;
pub mod ui;

View file

@ -26,6 +26,7 @@ pub enum VoiceCategory {
ProgramChange { value: u8 }, ProgramChange { value: u8 },
ChannelPressure { pressure: u8 }, ChannelPressure { pressure: u8 },
PitchWheel { value: u16 }, PitchWheel { value: u16 },
Unknown,
} }
#[derive(PartialEq, Eq, Debug, Clone)] #[derive(PartialEq, Eq, Debug, Clone)]

View file

@ -42,10 +42,7 @@ fn parse_system_common(status_byte: u8, bytes: &[u8]) -> IResult<&[u8], SystemCo
song_number, song_number,
}), }),
0xf6 => Ok((bytes, SystemCommon::TuneRequest)), 0xf6 => Ok((bytes, SystemCommon::TuneRequest)),
_ => Err(nom::Err::Error(nom::error::Error { _ => Ok((bytes, SystemCommon::Unknown)),
input: bytes,
code: nom::error::ErrorKind::Fail,
})),
} }
} }
@ -66,7 +63,6 @@ pub fn parse_voice_message(status_byte: u8, remainder: &[u8]) -> IResult<&[u8],
let category_nibble = 0xf0 & status_byte; let category_nibble = 0xf0 & status_byte;
let channel = 0x0f & status_byte; let channel = 0x0f & status_byte;
println!("category_nibble = {:#x}", category_nibble);
let (remainder, category) = match category_nibble { let (remainder, category) = match category_nibble {
0x80 => parse_voice_note(remainder, true)?, 0x80 => parse_voice_note(remainder, true)?,
0x90 => parse_voice_note(remainder, false)?, 0x90 => parse_voice_note(remainder, false)?,
@ -82,12 +78,7 @@ pub fn parse_voice_message(status_byte: u8, remainder: &[u8]) -> IResult<&[u8],
pressure, pressure,
})?, })?,
0xe0 => parse_pitch_wheel(remainder)?, 0xe0 => parse_pitch_wheel(remainder)?,
_ => { _ => (remainder, VoiceCategory::Unknown),
return Err(nom::Err::Error(nom::error::Error {
input: remainder,
code: nom::error::ErrorKind::Fail,
}))
}
}; };
Ok((remainder, VoiceMessage::new(category, channel))) Ok((remainder, VoiceMessage::new(category, channel)))
@ -110,7 +101,7 @@ pub fn parse_pitch_wheel(bytes: &[u8]) -> IResult<&[u8], VoiceCategory> {
pub fn parse_system_exclusive(bytes: &[u8]) -> IResult<&[u8], SystemCommon> { pub fn parse_system_exclusive(bytes: &[u8]) -> IResult<&[u8], SystemCommon> {
let (remainder, data) = take_till(is_status_byte)(bytes)?; let (remainder, data) = take_till(is_status_byte)(bytes)?;
let (remainder, _) = opt(tag([0xf7]))(remainder)?; let (remainder, _) = tag([0xf7])(remainder)?;
let data: Vec<u8> = data.into(); let data: Vec<u8> = data.into();

41
src/ui.rs Normal file
View file

@ -0,0 +1,41 @@
use std::time::Instant;
/// Launches the UI and runs it until it's done executing.
pub fn run() {
let native_options = eframe::NativeOptions::default();
// TODO: don't ignore result
let _ = eframe::run_native(
"Midi Keys",
native_options,
Box::new(|cc| Ok(Box::new(MidiKeysApp::new(cc)))),
);
}
struct MidiKeysApp {
previous_frame_time: Instant,
}
impl MidiKeysApp {
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
// this is where to hook in for customizing eguji, like fonts and visuals.
let previous_frame_time = Instant::now();
MidiKeysApp {
previous_frame_time,
}
}
}
impl eframe::App for MidiKeysApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
let duration = self.previous_frame_time.elapsed().as_secs_f32();
self.previous_frame_time = Instant::now();
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Hello world");
let framerate = format!("{:>8.2}", 1. / duration);
ui.label(framerate);
});
}
}