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
This commit is contained in:
beau trepp 2019-12-15 10:29:50 +08:00
parent 93ef2b7576
commit 1b0be0ba49
11 changed files with 156 additions and 122 deletions

83
src/data/midi/channel.rs Normal file
View file

@ -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<u8> for Channel {
type Error = InvalidChannel;
fn try_from(value:u8) -> Result<Self,Self::Error> {
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<u8> 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),
}
}

View file

@ -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
}

View file

@ -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();

View file

@ -0,0 +1,4 @@
pub mod channel;
pub mod message;
pub use crate::data::midi::message::message::{MidiMessage};

View file

View file

@ -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<u8> for MidiChannel {
type Error = InvalidMidiChannel;
fn try_from(value:u8) -> Result<Self,Self::Error> {
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<u8> 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),
}
}

View file

@ -1,28 +0,0 @@
use core::convert::TryFrom;
pub struct MidiVelocity(u8);
pub struct InvalidMidiVelocity(u8);
impl TryFrom<u8> for MidiVelocity{
type Error = InvalidMidiVelocity;
fn try_from(value:u8) -> Result<Self,Self::Error> {
if value > 0x7F {
Err(InvalidMidiVelocity(value))
} else {
Ok(MidiVelocity(value))
}
}
}
impl Into<u8> for MidiVelocity {
fn into(self) -> u8 {
self.0
}
}
impl MidiVelocity {
pub const MAX: MidiVelocity= MidiVelocity(0x7F);
pub const MIN: MidiVelocity = MidiVelocity(0);
}

View file

@ -1,5 +1,5 @@
pub mod notes;
pub mod code_index_number;
pub mod midi_channel;
pub mod midi_message;
pub mod midi_velocity;
pub mod channel;
pub mod message;
pub mod velocity;

28
src/data/midi/velocity.rs Normal file
View file

@ -0,0 +1,28 @@
use core::convert::TryFrom;
pub struct Velocity(u8);
pub struct InvalidVelocity(u8);
impl TryFrom<u8> for Velocity{
type Error = InvalidVelocity;
fn try_from(value:u8) -> Result<Self,Self::Error> {
if value > 0x7F {
Err(InvalidVelocity(value))
} else {
Ok(Velocity(value))
}
}
}
impl Into<u8> for Velocity {
fn into(self) -> u8 {
self.0
}
}
impl Velocity {
pub const MAX: Velocity= Velocity(0x7F);
pub const MIN: Velocity = Velocity(0);
}

View file

@ -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;

View file

@ -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{