midi_msg/
system_real_time.rs

1use super::parse_error::*;
2use alloc::vec::Vec;
3
4/// A fairly limited set of messages used for device synchronization.
5/// Used in [`MidiMsg`](crate::MidiMsg).
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum SystemRealTimeMsg {
8    /// Used to synchronize clocks. Sent at a rate of 24 per quarter note.
9    TimingClock,
10    /// Start at the beginning of the song or sequence.
11    Start,
12    /// Continue from the current location in the song or sequence.
13    Continue,
14    /// Stop playback.
15    Stop,
16    /// Sent every 300ms or less whenever other MIDI data is not sent.
17    /// Used to indicate that the given device is still connected.
18    ActiveSensing,
19    /// Request that all devices are reset to their power-up state.
20    ///
21    /// This is not a valid message in a MIDI file, since it overlaps
22    /// with the MIDI file's Meta messages. If you add this message to a
23    /// MIDI file, it will be ignored upon serialization.
24    SystemReset,
25}
26
27impl SystemRealTimeMsg {
28    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
29        match self {
30            Self::TimingClock => v.push(0xF8),
31            Self::Start => v.push(0xFA),
32            Self::Continue => v.push(0xFB),
33            Self::Stop => v.push(0xFC),
34            Self::ActiveSensing => v.push(0xFE),
35            Self::SystemReset => v.push(0xFF),
36        }
37    }
38
39    pub(crate) fn from_midi(m: &[u8]) -> Result<(Self, usize), ParseError> {
40        match m.first() {
41            Some(0xF8) => Ok((Self::TimingClock, 1)),
42            Some(0xFA) => Ok((Self::Start, 1)),
43            Some(0xFB) => Ok((Self::Continue, 1)),
44            Some(0xFC) => Ok((Self::Stop, 1)),
45            Some(0xFE) => Ok((Self::ActiveSensing, 1)),
46            Some(0xFF) => Ok((Self::SystemReset, 1)),
47            Some(x) => Err(ParseError::UndefinedSystemRealTimeMessage(*x)),
48            None => panic!("Should not be reachable"),
49        }
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use crate::*;
56    extern crate std;
57    use std::vec;
58
59    #[test]
60    fn serialize_system_real_time_msg() {
61        assert_eq!(
62            MidiMsg::SystemRealTime {
63                msg: SystemRealTimeMsg::TimingClock
64            }
65            .to_midi(),
66            vec![0xF8]
67        );
68    }
69
70    #[test]
71    fn deserialize_system_real_time_msg() {
72        let mut ctx = ReceiverContext::new();
73
74        test_serialization(
75            MidiMsg::SystemRealTime {
76                msg: SystemRealTimeMsg::TimingClock,
77            },
78            &mut ctx,
79        );
80    }
81
82    #[test]
83    fn serde_system_reset() {
84        let system_reset = MidiMsg::SystemRealTime {
85            msg: SystemRealTimeMsg::SystemReset,
86        };
87        assert_eq!(
88            MidiMsg::from_midi_with_context(&system_reset.to_midi(), &mut ReceiverContext::new()),
89            Ok((system_reset, 1)),
90        );
91    }
92}