1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use super::parse_error::*;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SystemRealTimeMsg {
TimingClock,
Start,
Continue,
Stop,
ActiveSensing,
SystemReset,
}
impl SystemRealTimeMsg {
pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
match self {
Self::TimingClock => v.push(0xF8),
Self::Start => v.push(0xFA),
Self::Continue => v.push(0xFB),
Self::Stop => v.push(0xFC),
Self::ActiveSensing => v.push(0xFE),
Self::SystemReset => v.push(0xFF),
}
}
pub(crate) fn from_midi(m: &[u8]) -> Result<(Self, usize), ParseError> {
match m.first() {
Some(0xF8) => Ok((Self::TimingClock, 1)),
Some(0xFA) => Ok((Self::Start, 1)),
Some(0xFB) => Ok((Self::Continue, 1)),
Some(0xFC) => Ok((Self::Stop, 1)),
Some(0xFE) => Ok((Self::ActiveSensing, 1)),
Some(0xFF) => Ok((Self::SystemReset, 1)),
Some(x) => Err(ParseError::Invalid(format!(
"Undefined System Real Time message: {}",
x
))),
None => panic!("Should not be reachable"),
}
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn serialize_system_real_time_msg() {
assert_eq!(
MidiMsg::SystemRealTime {
msg: SystemRealTimeMsg::TimingClock
}
.to_midi(),
vec![0xF8]
);
}
#[test]
fn deserialize_system_real_time_msg() {
let mut ctx = ReceiverContext::new();
test_serialization(
MidiMsg::SystemRealTime {
msg: SystemRealTimeMsg::TimingClock,
},
&mut ctx,
);
}
}