Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
fix for piano example, better message debugging

Co-authored-by: Mikkel Rasmussen <theluja@gmail.com>
  • Loading branch information
masonium and BlackPhlox authored Nov 19, 2023
1 parent cc92f63 commit e0e8ab7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/piano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn handle_midi_input(
for data in midi_events.read() {
match data.message {
OwnedLiveEvent::Midi {
message: MidiMessage::NoteOn { key, .. },
message: MidiMessage::NoteOn { key, .. } | MidiMessage::NoteOff { key, .. },
..
} => {
let index: u8 = key.into();
Expand Down
55 changes: 52 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Module definined owned variants of `[midly]` structures. These owned variants allow for more
//! ergonomic usage.
use std::fmt::Debug;
use midly::live::{LiveEvent, SystemCommon};
use midly::num;
use midly::num::{self, u4, u7};
pub use midly::{
live::{MtcQuarterFrameMessage, SystemRealtime},
MidiMessage,
};

use crate::KEY_RANGE;
/// Owned version of a [`midly::live::LiveEvent`].
///
/// Standard [`midly::live::LiveEvent`]s have a lifetime parameter limiting them to the scope in
Expand All @@ -15,7 +16,7 @@ pub use midly::{
///
/// Creating [`OwnedLiveEvent`]s only allocates when the message is a an [`OwnedSystemCommon`] that
/// itself contains an allocation.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum OwnedLiveEvent {
/// A midi message with a channel and music data.
Midi {
Expand Down Expand Up @@ -86,6 +87,54 @@ impl OwnedLiveEvent {
}
}

fn fmt_note(
f: &mut std::fmt::Formatter<'_>,
msg: &str,
ch: &u4,
key: &u7,
vel: &u7,
) -> std::fmt::Result {
let index: u8 = key.as_int();
let off = index % 12;
let oct = index.overflowing_div(12).0;
let key_str = KEY_RANGE.iter().nth(off.into()).unwrap();

f.write_fmt(format_args!(
"Ch: {} {}: {}{:?} Vel: {}",
ch, msg, key_str, oct, vel
))
}

impl Debug for OwnedLiveEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Midi { channel, message } => {
{
let _ = match message {
MidiMessage::NoteOff { key, vel } => {
fmt_note(f, "NoteOff", channel, key, vel)
}
MidiMessage::NoteOn { key, vel } => {
fmt_note(f, "NoteOn", channel, key, vel)
}
MidiMessage::Aftertouch { key, vel } => {
fmt_note(f, "Aftertouch", channel, key, vel)
}
_ => f
.debug_struct("Midi")
.field("channel", channel)
.field("message", message)
.finish(),
};
};
Ok(())
}
Self::Common(arg) => f.debug_tuple("Common").field(arg).finish(),
Self::Realtime(arg) => f.debug_tuple("Realtime").field(arg).finish(),
}
}
}

impl<'a> From<LiveEvent<'a>> for OwnedLiveEvent {
fn from(value: LiveEvent) -> Self {
match value {
Expand Down

0 comments on commit e0e8ab7

Please sign in to comment.