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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use crate::parse_error::*;
use crate::util::*;
use alloc::vec;
use alloc::vec::Vec;

/// Indicates that the next MIDI clock message is the first clock of a new measure. Which bar
/// is optionally indicated by this message.
/// Used by [`UniversalRealTimeMsg::BarMarker`](crate::UniversalRealTimeMsg::BarMarker).
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BarMarker {
    /// "Actually, we're not running right now, so there is no bar." Don't know why this is used.
    NotRunning,
    /// The bar is a count-in and are thus negative numbers from 8191-0.
    CountIn(u16), // ?
    /// A regular bar numbered 1-8191.
    Number(u16),
    /// Next clock message will be a new bar, but it's not known what its number is.
    RunningUnknown,
}

impl BarMarker {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        match *self {
            Self::NotRunning => {
                // Most negative number
                v.push(0x00);
                v.push(0x40);
            }
            Self::CountIn(x) => {
                push_i14(-(x.min(8191) as i16), v);
            }
            Self::Number(x) => {
                push_i14(x.min(8191) as i16, v);
            }
            Self::RunningUnknown => {
                // Most positive number
                v.push(0x7F);
                v.push(0x3F);
            }
        }
    }

    #[allow(dead_code)]
    pub(crate) fn from_midi(_m: &[u8]) -> Result<(Self, usize), ParseError> {
        Err(ParseError::NotImplemented("BarMarker"))
    }
}

/// Used to communicate a new time signature to the receiver.
/// Used by [`UniversalRealTimeMsg`](crate::UniversalRealTimeMsg).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeSignature {
    /// The base time signature.
    pub signature: Signature,
    /// How many MIDI clock events per metronome click.
    /// 24 indicates one click per quarter note (unless specified otherwise by `thirty_second_notes_in_midi_quarter_note`)
    pub midi_clocks_in_metronome_click: u8,
    /// Number of notated 32nd notes in a MIDI quarter note.
    /// 8 is the normal value (e.g. a midi quarter note is a quarter note)
    pub thirty_second_notes_in_midi_quarter_note: u8,
    /// At most 61 (!) additional times signatures for compound time definitions
    pub compound: Vec<Signature>,
}

impl Default for TimeSignature {
    fn default() -> Self {
        Self {
            signature: Default::default(),
            midi_clocks_in_metronome_click: 24, // one click per quarter note
            thirty_second_notes_in_midi_quarter_note: 8, // Midi quarter note is a quarter note
            compound: vec![],
        }
    }
}

impl TimeSignature {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        v.push((4 + (self.compound.len() * 2)).min(126) as u8); // Bytes to follow
        self.signature.extend_midi(v);
        v.push(to_u7(self.midi_clocks_in_metronome_click));
        v.push(to_u7(self.thirty_second_notes_in_midi_quarter_note));
        let mut i = 0;
        for s in self.compound.iter() {
            if i >= 61 {
                break;
            }
            s.extend_midi(v);
            i += 1;
        }
    }

    #[allow(dead_code)]
    pub(crate) fn from_midi(_m: &[u8]) -> Result<(Self, usize), ParseError> {
        Err(ParseError::NotImplemented("TimeSignature"))
    }
}

/// A [time signature](https://en.wikipedia.org/wiki/Time_signature). Used by [`TimeSignature`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Signature {
    /// Number of beats in a bar.
    pub beats: u8,
    /// The note value for each beat.
    pub beat_value: BeatValue,
}

impl Signature {
    fn extend_midi(&self, v: &mut Vec<u8>) {
        v.push(to_u7(self.beats));
        v.push(self.beat_value.to_u8());
    }

    #[allow(dead_code)]
    pub(crate) fn from_midi(_m: &[u8]) -> Result<(Self, usize), ParseError> {
        Err(ParseError::NotImplemented("Signature"))
    }
}

impl Default for Signature {
    fn default() -> Self {
        Self {
            beats: 4,
            beat_value: BeatValue::Quarter,
        }
    }
}

/// The note value of a beat, used by [`Signature`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BeatValue {
    Whole,
    Half,
    Quarter,
    Eighth,
    Sixteenth,
    ThirtySecond,
    SixtyFourth,
    /// Any other value interpreted as 2^x.
    /// The spec does not limit this value, so the maximum is a crazy 2^127
    Other(u8),
}

impl BeatValue {
    fn to_u8(&self) -> u8 {
        match self {
            Self::Whole => 0,
            Self::Half => 1,
            Self::Quarter => 2,
            Self::Eighth => 3,
            Self::Sixteenth => 4,
            Self::ThirtySecond => 5,
            Self::SixtyFourth => 6,
            Self::Other(x) => to_u7(*x),
        }
    }

    #[allow(dead_code)]
    fn from_byte(_m: u8) -> Self {
        // TODO
        Self::Quarter
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use alloc::vec;

    #[test]
    fn serialize_bar_marker() {
        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::BarMarker(BarMarker::NotRunning),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 03, 01, 0x00, 0x40, 0xF7]
        );

        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::BarMarker(BarMarker::CountIn(1)),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 03, 01, 0x7f, 0x7f, 0xF7]
        );

        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::BarMarker(BarMarker::Number(1)),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 03, 01, 0x01, 0x00, 0xF7]
        );

        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::BarMarker(BarMarker::RunningUnknown),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 03, 01, 0x7f, 0x3f, 0xF7]
        );
    }

    #[test]
    fn serialize_time_signature() {
        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::TimeSignatureDelayed(TimeSignature::default()),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 03, 0x42, 4, 4, 2, 24, 8, 0xF7]
        );

        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::TimeSignature(TimeSignature {
                        compound: vec! {Signature {
                            beats: 3,
                            beat_value: BeatValue::Eighth,
                        }},
                        ..Default::default()
                    }),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 03, 0x02, 6, 4, 2, 24, 8, 3, 3, 0xF7]
        );
    }
}