From 1b0be0ba4904ca4b33066ac1a9863a0be99d9d4d Mon Sep 17 00:00:00 2001 From: beau trepp Date: Sun, 15 Dec 2019 10:29:50 +0800 Subject: [PATCH] Removes midi prefixes to types in midi module. This is more or less implicit in this context, and users can alias if they have conflicts --- src/data/midi/channel.rs | 83 +++++++++++++++++++ src/data/midi/message/channel.rs | 10 +++ .../{midi_message.rs => message/message.rs} | 25 +++++- src/data/midi/message/mod.rs | 4 + src/data/midi/message/system.rs | 0 src/data/midi/midi_channel.rs | 83 ------------------- src/data/midi/midi_velocity.rs | 28 ------- src/data/midi/mod.rs | 6 +- src/data/midi/velocity.rs | 28 +++++++ src/data/usb/constants.rs | 1 + src/data/usb_midi/usb_midi_event_packet.rs | 10 +-- 11 files changed, 156 insertions(+), 122 deletions(-) create mode 100644 src/data/midi/channel.rs create mode 100644 src/data/midi/message/channel.rs rename src/data/midi/{midi_message.rs => message/message.rs} (62%) create mode 100644 src/data/midi/message/mod.rs create mode 100644 src/data/midi/message/system.rs delete mode 100644 src/data/midi/midi_channel.rs delete mode 100644 src/data/midi/midi_velocity.rs create mode 100644 src/data/midi/velocity.rs diff --git a/src/data/midi/channel.rs b/src/data/midi/channel.rs new file mode 100644 index 0000000..ffabae3 --- /dev/null +++ b/src/data/midi/channel.rs @@ -0,0 +1,83 @@ +use core::convert::TryFrom; + +/// The Channel is a value ranging from 0x0 to 0xF +/// This is a standard midi concept +/// Note Channel1 = 0 on the wire +#[derive(Debug)] +#[repr(u8)] +pub enum Channel { + Channel1 = 0x0, Channel2 = 0x1, Channel3 = 0x2, Channel4 = 0x3, + Channel5 = 0x4, Channel6 = 0x5, Channel7 = 0x6, Channel8 = 0x7, + Channel9 = 0x8, Channel10 = 0x9, Channel11 = 0xA, Channel12 = 0xB, + Channel13 = 0xC, Channel14 = 0xD, Channel15 = 0xE, Channel16 = 0xF +} + +pub struct InvalidChannel(u8); + +impl TryFrom for Channel { + type Error = InvalidChannel; + fn try_from(value:u8) -> Result { + match value { + x if x == Channel::Channel1 as u8 => Ok(Channel::Channel1), + x if x == Channel::Channel2 as u8 => Ok(Channel::Channel2), + x if x == Channel::Channel3 as u8 => Ok(Channel::Channel3), + x if x == Channel::Channel4 as u8 => Ok(Channel::Channel4), + x if x == Channel::Channel5 as u8 => Ok(Channel::Channel5), + x if x == Channel::Channel6 as u8 => Ok(Channel::Channel6), + x if x == Channel::Channel7 as u8 => Ok(Channel::Channel7), + x if x == Channel::Channel8 as u8 => Ok(Channel::Channel8), + x if x == Channel::Channel9 as u8 => Ok(Channel::Channel9), + x if x == Channel::Channel10 as u8 => Ok(Channel::Channel10), + x if x == Channel::Channel11 as u8 => Ok(Channel::Channel11), + x if x == Channel::Channel12 as u8 => Ok(Channel::Channel12), + x if x == Channel::Channel13 as u8 => Ok(Channel::Channel13), + x if x == Channel::Channel14 as u8 => Ok(Channel::Channel14), + x if x == Channel::Channel15 as u8 => Ok(Channel::Channel15), + x if x == Channel::Channel16 as u8 => Ok(Channel::Channel16), + _ => Err(InvalidChannel(value)) + } + } + +} + +impl Into for Channel { + fn into(self) -> u8 { + self as u8 + } +} + +#[cfg(test)] +mod tests { + + use super::*; + macro_rules! channel_test { + ($($id:ident:$value:expr,)*) => { + $( + #[test] + fn $id() { + let (input,expected) = $value; + assert_eq!(input as u8, expected); + } + )* + } + } + + channel_test! { + channel_1: (Channel::Channel1,0), + channel_2: (Channel::Channel2,1), + channel_3: (Channel::Channel3,2), + channel_4: (Channel::Channel4,3), + channel_5: (Channel::Channel5,4), + channel_6: (Channel::Channel6,5), + channel_7: (Channel::Channel7,6), + channel_8: (Channel::Channel8,7), + channel_9: (Channel::Channel9,8), + channel_10: (Channel::Channel10,9), + channel_11: (Channel::Channel11,10), + channel_12: (Channel::Channel12,11), + channel_13: (Channel::Channel13,12), + channel_14: (Channel::Channel14,13), + channel_15: (Channel::Channel15,14), + channel_16: (Channel::Channel16,15), + } +} \ No newline at end of file diff --git a/src/data/midi/message/channel.rs b/src/data/midi/message/channel.rs new file mode 100644 index 0000000..21255ae --- /dev/null +++ b/src/data/midi/message/channel.rs @@ -0,0 +1,10 @@ + +pub use crate::data::midi::channel::Channel; +pub use crate::data::midi::notes::Note; +pub use crate::data::midi::velocity::Velocity; + +pub enum Voice { + NoteOff(Channel,Note,Velocity), + NoteOn(Channel,Note,Velocity), + PolyPressure +} diff --git a/src/data/midi/midi_message.rs b/src/data/midi/message/message.rs similarity index 62% rename from src/data/midi/midi_message.rs rename to src/data/midi/message/message.rs index 28275e7..3c25af9 100644 --- a/src/data/midi/midi_message.rs +++ b/src/data/midi/message/message.rs @@ -1,14 +1,33 @@ -use crate::data::midi::midi_channel::MidiChannel; +use crate::data::midi::channel::Channel; +use crate::data::midi::velocity::Velocity; use crate::data::midi::notes::Note; -use crate::data::midi::midi_velocity::MidiVelocity; + +pub enum Mode {Mode} + +pub enum RealTime {} +pub enum Common {} +pub enum SysEx {} + +pub enum System { + RealTime(RealTime), + Common(Common), + SysEx(SysEx) +} + +pub enum Message { + Channel(Channel), + System(System) +} pub struct MidiMessage { payload: [u8;3] } + + impl MidiMessage { - pub fn note_on(channel:MidiChannel, note:Note, velocity:MidiVelocity) + pub fn note_on(channel:Channel, note:Note, velocity:Velocity) -> MidiMessage{ let channel : u8 = channel.into(); let note : u8 = note.into(); diff --git a/src/data/midi/message/mod.rs b/src/data/midi/message/mod.rs new file mode 100644 index 0000000..7043249 --- /dev/null +++ b/src/data/midi/message/mod.rs @@ -0,0 +1,4 @@ +pub mod channel; +pub mod message; + +pub use crate::data::midi::message::message::{MidiMessage}; \ No newline at end of file diff --git a/src/data/midi/message/system.rs b/src/data/midi/message/system.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/data/midi/midi_channel.rs b/src/data/midi/midi_channel.rs deleted file mode 100644 index 513afa4..0000000 --- a/src/data/midi/midi_channel.rs +++ /dev/null @@ -1,83 +0,0 @@ -use core::convert::TryFrom; - -/// The MidiChannel is a value ranging from 0x0 to 0xF -/// This is a standard midi concept -/// Note Channel1 = 0 on the wire -#[derive(Debug)] -#[repr(u8)] -pub enum MidiChannel { - Channel1 = 0x0, Channel2 = 0x1, Channel3 = 0x2, Channel4 = 0x3, - Channel5 = 0x4, Channel6 = 0x5, Channel7 = 0x6, Channel8 = 0x7, - Channel9 = 0x8, Channel10 = 0x9, Channel11 = 0xA, Channel12 = 0xB, - Channel13 = 0xC, Channel14 = 0xD, Channel15 = 0xE, Channel16 = 0xF -} - -pub struct InvalidMidiChannel(u8); - -impl TryFrom for MidiChannel { - type Error = InvalidMidiChannel; - fn try_from(value:u8) -> Result { - match value { - x if x == MidiChannel::Channel1 as u8 => Ok(MidiChannel::Channel1), - x if x == MidiChannel::Channel2 as u8 => Ok(MidiChannel::Channel2), - x if x == MidiChannel::Channel3 as u8 => Ok(MidiChannel::Channel3), - x if x == MidiChannel::Channel4 as u8 => Ok(MidiChannel::Channel4), - x if x == MidiChannel::Channel5 as u8 => Ok(MidiChannel::Channel5), - x if x == MidiChannel::Channel6 as u8 => Ok(MidiChannel::Channel6), - x if x == MidiChannel::Channel7 as u8 => Ok(MidiChannel::Channel7), - x if x == MidiChannel::Channel8 as u8 => Ok(MidiChannel::Channel8), - x if x == MidiChannel::Channel9 as u8 => Ok(MidiChannel::Channel9), - x if x == MidiChannel::Channel10 as u8 => Ok(MidiChannel::Channel10), - x if x == MidiChannel::Channel11 as u8 => Ok(MidiChannel::Channel11), - x if x == MidiChannel::Channel12 as u8 => Ok(MidiChannel::Channel12), - x if x == MidiChannel::Channel13 as u8 => Ok(MidiChannel::Channel13), - x if x == MidiChannel::Channel14 as u8 => Ok(MidiChannel::Channel14), - x if x == MidiChannel::Channel15 as u8 => Ok(MidiChannel::Channel15), - x if x == MidiChannel::Channel16 as u8 => Ok(MidiChannel::Channel16), - _ => Err(InvalidMidiChannel(value)) - } - } - -} - -impl Into for MidiChannel { - fn into(self) -> u8 { - self as u8 - } -} - -#[cfg(test)] -mod tests { - - use super::*; - macro_rules! channel_test { - ($($id:ident:$value:expr,)*) => { - $( - #[test] - fn $id() { - let (input,expected) = $value; - assert_eq!(input as u8, expected); - } - )* - } - } - - channel_test! { - channel_1: (MidiChannel::Channel1,0), - channel_2: (MidiChannel::Channel2,1), - channel_3: (MidiChannel::Channel3,2), - channel_4: (MidiChannel::Channel4,3), - channel_5: (MidiChannel::Channel5,4), - channel_6: (MidiChannel::Channel6,5), - channel_7: (MidiChannel::Channel7,6), - channel_8: (MidiChannel::Channel8,7), - channel_9: (MidiChannel::Channel9,8), - channel_10: (MidiChannel::Channel10,9), - channel_11: (MidiChannel::Channel11,10), - channel_12: (MidiChannel::Channel12,11), - channel_13: (MidiChannel::Channel13,12), - channel_14: (MidiChannel::Channel14,13), - channel_15: (MidiChannel::Channel15,14), - channel_16: (MidiChannel::Channel16,15), - } -} \ No newline at end of file diff --git a/src/data/midi/midi_velocity.rs b/src/data/midi/midi_velocity.rs deleted file mode 100644 index 460ad62..0000000 --- a/src/data/midi/midi_velocity.rs +++ /dev/null @@ -1,28 +0,0 @@ -use core::convert::TryFrom; - -pub struct MidiVelocity(u8); - -pub struct InvalidMidiVelocity(u8); - -impl TryFrom for MidiVelocity{ - type Error = InvalidMidiVelocity; - - fn try_from(value:u8) -> Result { - if value > 0x7F { - Err(InvalidMidiVelocity(value)) - } else { - Ok(MidiVelocity(value)) - } - } -} - -impl Into for MidiVelocity { - fn into(self) -> u8 { - self.0 - } -} - -impl MidiVelocity { - pub const MAX: MidiVelocity= MidiVelocity(0x7F); - pub const MIN: MidiVelocity = MidiVelocity(0); -} \ No newline at end of file diff --git a/src/data/midi/mod.rs b/src/data/midi/mod.rs index c8b3426..6eeac55 100644 --- a/src/data/midi/mod.rs +++ b/src/data/midi/mod.rs @@ -1,5 +1,5 @@ pub mod notes; pub mod code_index_number; -pub mod midi_channel; -pub mod midi_message; -pub mod midi_velocity; \ No newline at end of file +pub mod channel; +pub mod message; +pub mod velocity; \ No newline at end of file diff --git a/src/data/midi/velocity.rs b/src/data/midi/velocity.rs new file mode 100644 index 0000000..2ec05fb --- /dev/null +++ b/src/data/midi/velocity.rs @@ -0,0 +1,28 @@ +use core::convert::TryFrom; + +pub struct Velocity(u8); + +pub struct InvalidVelocity(u8); + +impl TryFrom for Velocity{ + type Error = InvalidVelocity; + + fn try_from(value:u8) -> Result { + if value > 0x7F { + Err(InvalidVelocity(value)) + } else { + Ok(Velocity(value)) + } + } +} + +impl Into for Velocity { + fn into(self) -> u8 { + self.0 + } +} + +impl Velocity { + pub const MAX: Velocity= Velocity(0x7F); + pub const MIN: Velocity = Velocity(0); +} \ No newline at end of file diff --git a/src/data/usb/constants.rs b/src/data/usb/constants.rs index 8ec3203..90f2cb7 100644 --- a/src/data/usb/constants.rs +++ b/src/data/usb/constants.rs @@ -1,3 +1,4 @@ +//This should be better organized in future pub const USB_CLASS_NONE : u8 = 0x00; pub const USB_AUDIO_CLASS: u8 = 0x01; diff --git a/src/data/usb_midi/usb_midi_event_packet.rs b/src/data/usb_midi/usb_midi_event_packet.rs index 1866209..66e3d06 100644 --- a/src/data/usb_midi/usb_midi_event_packet.rs +++ b/src/data/usb_midi/usb_midi_event_packet.rs @@ -1,9 +1,9 @@ use crate::data::usb_midi::cable_number::CableNumber; use crate::data::midi::code_index_number::CodeIndexNumber; use crate::data::midi::notes::Note; -use crate::data::midi::midi_channel::MidiChannel; -use crate::data::midi::midi_message::MidiMessage; -use crate::data::midi::midi_velocity::MidiVelocity; +use crate::data::midi::channel::Channel; +use crate::data::midi::message::MidiMessage; +use crate::data::midi::velocity::Velocity; use crate::util::nibble::{combine_nibble}; /// A packet that communicates with the host @@ -18,9 +18,9 @@ pub struct UsbMidiEventPacket { /// Constructs a note-on midi message given the cable, note and velocity pub fn note_on( cable:CableNumber, - channel: MidiChannel, + channel: Channel, note:Note, - velocity: MidiVelocity) -> UsbMidiEventPacket { + velocity: Velocity) -> UsbMidiEventPacket { let message = MidiMessage::note_on(channel,note,velocity); UsbMidiEventPacket{