@@ -7,16 +7,12 @@ use core::{
77 ops:: { Add , AddAssign , Sub , SubAssign } ,
88} ;
99
10- use num_enum:: { IntoPrimitive , TryFromPrimitive } ;
11-
1210use crate :: message:: { ChannelVoiceMessage , VoiceEvent } ;
1311
1412/// Identifies a channel for MIDI.
1513///
1614/// To get this channel from a `u8`, use [`Channel::try_from_primitive`].
17- #[ derive(
18- Clone , Copy , PartialEq , Eq , Debug , Hash , IntoPrimitive , TryFromPrimitive , PartialOrd , Ord ,
19- ) ]
15+ #[ derive( Clone , Copy , PartialEq , Eq , Debug , Hash , PartialOrd , Ord ) ]
2016#[ cfg_attr(
2117 feature = "bevy" ,
2218 derive( bevy:: prelude:: Component , bevy:: prelude:: Reflect )
@@ -76,6 +72,33 @@ impl Channel {
7672 pub const fn send_event ( self , event : VoiceEvent ) -> ChannelVoiceMessage {
7773 ChannelVoiceMessage :: new ( self , event)
7874 }
75+ /// Create a `Channel` from a byte.
76+ ///
77+ /// 0 -> `Channel::One`
78+ /// ..
79+ /// 15 -> `Channel::Sixteen`
80+ pub const fn try_from_byte ( byte : u8 ) -> Option < Channel > {
81+ let channel = match byte {
82+ 0 => Channel :: One ,
83+ 1 => Channel :: Two ,
84+ 2 => Channel :: Three ,
85+ 3 => Channel :: Four ,
86+ 4 => Channel :: Five ,
87+ 5 => Channel :: Six ,
88+ 6 => Channel :: Seven ,
89+ 7 => Channel :: Eight ,
90+ 8 => Channel :: Nine ,
91+ 9 => Channel :: Ten ,
92+ 10 => Channel :: Eleven ,
93+ 11 => Channel :: Twelve ,
94+ 12 => Channel :: Thirteen ,
95+ 13 => Channel :: Fourteen ,
96+ 14 => Channel :: Fifteen ,
97+ 15 => Channel :: Sixteen ,
98+ _ => return None ,
99+ } ;
100+ Some ( channel)
101+ }
79102
80103 /// Given a status byte from some [`ChannelVoiceMessage`], perform bitwise ops
81104 /// to get the channel
@@ -97,7 +120,7 @@ impl Channel {
97120
98121impl fmt:: Display for Channel {
99122 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
100- let res: u8 = ( * self ) . into ( ) ;
123+ let res: u8 = * self as u8 ;
101124 res. fmt ( f)
102125 }
103126}
0 commit comments