From e0e8ab7f807e488a3f503eec22cf2658c334e49d Mon Sep 17 00:00:00 2001 From: Mason Smith Date: Sun, 19 Nov 2023 11:50:58 -0800 Subject: [PATCH] Apply suggestions from code review fix for piano example, better message debugging Co-authored-by: Mikkel Rasmussen --- examples/piano.rs | 2 +- src/types.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/examples/piano.rs b/examples/piano.rs index ee4688f..daa0f6f 100644 --- a/examples/piano.rs +++ b/examples/piano.rs @@ -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(); diff --git a/src/types.rs b/src/types.rs index 11f6e59..5f59127 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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 @@ -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 { @@ -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> for OwnedLiveEvent { fn from(value: LiveEvent) -> Self { match value {