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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
use super::util::*;

/// Used to synchronize device positions, by [`SystemCommonMsg::TimeCodeQuarterFrameX`](crate::SystemCommonMsg::TimeCodeQuarterFrame1)
/// as well as [`UniversalRealTimeMsg::TimeCodeFull`](crate::UniversalRealTimeMsg::TimeCodeFull).
///
/// Based on [the SMTPE time code standard](https://en.wikipedia.org/wiki/SMPTE_timecode).
///
/// As defined in the MIDI Time Code spec (MMA0001 / RP004 / RP008)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TimeCode {
    /// The position in frames, 0-29
    pub frames: u8,
    /// The position in seconds, 0-59
    pub seconds: u8,
    /// The position in minutes, 0-59
    pub minutes: u8,
    /// The position in hours, 0-23
    pub hours: u8,
    pub code_type: TimeCodeType,
}

impl TimeCode {
    /// Return the four byte representation of the frame: [frame, seconds, minutes, timecode + hours]
    pub fn to_bytes(self) -> [u8; 4] {
        [
            self.frames.min(29),
            self.seconds.min(59),
            self.minutes.min(59),
            self.hours.min(23) + ((self.code_type as u8) << 5),
        ]
    }

    /// Return an 8 byte, Quarter Frame representation of the Frame
    pub fn to_nibbles(self) -> [u8; 8] {
        let [frame, seconds, minutes, codehour] = self.to_bytes();

        let [frame_msb, frame_lsb] = to_nibble(frame);
        let [seconds_msb, seconds_lsb] = to_nibble(seconds);
        let [minutes_msb, minutes_lsb] = to_nibble(minutes);
        let [codehour_msb, codehour_lsb] = to_nibble(codehour);
        [
            (0 << 4) + frame_lsb,
            (1 << 4) + frame_msb,
            (2 << 4) + seconds_lsb,
            (3 << 4) + seconds_msb,
            (4 << 4) + minutes_lsb,
            (5 << 4) + minutes_msb,
            (6 << 4) + codehour_lsb,
            (7 << 4) + codehour_msb,
        ]
    }

    // Returns the quarter frame number
    pub(crate) fn extend(&mut self, nibble: u8) -> u8 {
        let frame_number = nibble >> 4;
        let nibble = nibble & 0b00001111;

        match frame_number {
            0 => self.frames = (self.frames & 0b11110000) + nibble,
            1 => self.frames = (self.frames & 0b00001111) + (nibble << 4),
            2 => self.seconds = (self.seconds & 0b11110000) + nibble,
            3 => self.seconds = (self.seconds & 0b00001111) + (nibble << 4),
            4 => self.minutes = (self.minutes & 0b11110000) + nibble,
            5 => self.minutes = (self.minutes & 0b00001111) + (nibble << 4),
            6 => self.hours = (self.hours & 0b11110000) + nibble,
            7 => {
                self.hours = (self.hours & 0b00001111) + ((nibble & 0b0001) << 4);
                self.code_type = match (nibble & 0b0110) >> 1 {
                    0 => TimeCodeType::FPS24,
                    1 => TimeCodeType::FPS25,
                    2 => TimeCodeType::DF30,
                    3 => TimeCodeType::NDF30,
                    _ => panic!("Should not be reachable"),
                }
            }
            _ => panic!("Should not be reachable"),
        }

        frame_number
    }
}

/// Indicates the frame rate of the given [`TimeCode`].
///
/// See [the SMTPE time code standard](https://en.wikipedia.org/wiki/SMPTE_timecode).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeCodeType {
    /// 24 Frames per second
    FPS24 = 0,
    /// 25 Frames per second
    FPS25 = 1,
    /// 30 Frames per second, Drop Frame
    DF30 = 2,
    /// 30 Frames per second, Non-Drop Frame
    NDF30 = 3,
}

impl Default for TimeCodeType {
    fn default() -> Self {
        Self::NDF30
    }
}

impl TimeCodeType {
    fn from_code_hour(code_hour: u8) -> Self {
        match (code_hour & 0b01100000) >> 5 {
            0 => Self::FPS24,
            1 => Self::FPS25,
            2 => Self::DF30,
            3 => Self::NDF30,
            _ => panic!("Should not be reachable"),
        }
    }
}

#[cfg(feature = "sysex")]
mod sysex_types {
    use super::*;
    use crate::MidiMsg;
    use crate::ParseError;
    use alloc::vec::Vec;
    use bstr::BString;

    impl TimeCode {
        pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
            let [frame, seconds, minutes, codehour] = self.to_bytes();
            v.extend_from_slice(&[codehour, minutes, seconds, frame]);
        }

        pub(crate) fn from_midi(m: &[u8]) -> Result<Self, ParseError> {
            if m.len() < 4 {
                return Err(crate::ParseError::UnexpectedEnd);
            }
            let code_hour = u8_from_u7(m[0])?;
            Ok(Self {
                frames: u8_from_u7(m[3])?,
                seconds: u8_from_u7(m[2])?,
                minutes: u8_from_u7(m[1])?,
                hours: code_hour & 0b00011111,
                code_type: TimeCodeType::from_code_hour(code_hour),
            })
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
    /// Like [`TimeCode`] but includes `fractional_frames`. Used in `TimeCodeCueingSetupMsg` and the SMF `Meta` event.
    ///
    /// As defined in the MIDI Time Code spec (MMA0001 / RP004 / RP008)
    pub struct HighResTimeCode {
        /// 0-99
        pub fractional_frames: u8,
        /// 0-29
        pub frames: u8,
        /// 0-59
        pub seconds: u8,
        /// 0-59
        pub minutes: u8,
        /// 0-23
        pub hours: u8,
        pub code_type: TimeCodeType,
    }

    impl HighResTimeCode {
        /// Return the five byte representation of the frame:
        /// [fractional_frames, frames, seconds, minutes, timecode + hours]
        pub fn to_bytes(self) -> [u8; 5] {
            [
                self.fractional_frames.min(99),
                self.frames.min(29),
                self.seconds.min(59),
                self.minutes.min(59),
                self.hours.min(23) + ((self.code_type as u8) << 5),
            ]
        }

        pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
            let [fractional_frames, frames, seconds, minutes, codehour] = self.to_bytes();
            v.extend_from_slice(&[codehour, minutes, seconds, frames, fractional_frames]);
        }

        pub(crate) fn from_midi(v: &[u8]) -> Result<(Self, usize), ParseError> {
            if v.len() < 5 {
                return Err(ParseError::UnexpectedEnd);
            }
            let code_hour = u8_from_u7(v[0])?;
            Ok((
                Self {
                    fractional_frames: u8_from_u7(v[4])?,
                    frames: u8_from_u7(v[3])?,
                    seconds: u8_from_u7(v[2])?,
                    minutes: u8_from_u7(v[1])?,
                    hours: code_hour & 0b00011111,
                    code_type: TimeCodeType::from_code_hour(code_hour),
                },
                5,
            ))
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
    /// Like [`TimeCode`] but uses `subframes` to optionally include status flags, and fractional frames.
    /// Also may be negative. Used in [`MachineControlCommandMsg`](crate::MachineControlCommandMsg).
    ///
    /// As defined in MIDI Machine Control 1.0 (MMA0016 / RP013) and
    /// MIDI Show Control 1.1.1 (RP002/RP014)
    pub struct StandardTimeCode {
        pub subframes: SubFrames,
        /// The position in frames, where a negative value indicates a negative TimeCode, -29-29
        pub frames: i8,
        /// The position in seconds, 0-59
        pub seconds: u8,
        /// The position in minutes, 0-59
        pub minutes: u8,
        /// The position in hours, 0-23
        pub hours: u8,
        pub code_type: TimeCodeType,
    }

    impl StandardTimeCode {
        /// Return the five byte representation of the frame:
        /// [fractional_frames, frames, seconds, minutes, timecode + hours]
        pub fn to_bytes(self) -> [u8; 5] {
            let [subframes, frames] = self.to_bytes_short();
            [
                subframes,
                frames,
                self.seconds.min(59),
                self.minutes.min(59),
                self.hours.min(23) + ((self.code_type as u8) << 5),
            ]
        }

        /// The two byte representation of the frame:
        /// [fractional_frames, frames]
        pub fn to_bytes_short(self) -> [u8; 2] {
            let mut frames = self.frames.abs().min(29) as u8;
            if let SubFrames::Status(_) = self.subframes {
                frames += 1 << 5;
            }
            if self.frames < 0 {
                frames += 1 << 6;
            }
            [self.subframes.to_byte(), frames]
        }

        pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
            let [subframes, frames, seconds, minutes, codehour] = self.to_bytes();
            v.extend_from_slice(&[codehour, minutes, seconds, frames, subframes]);
        }

        #[allow(dead_code)]
        pub(crate) fn extend_midi_short(&self, v: &mut Vec<u8>) {
            let [subframes, frames] = self.to_bytes_short();
            v.extend_from_slice(&[frames, subframes]);
        }
    }

    impl From<TimeCode> for StandardTimeCode {
        fn from(t: TimeCode) -> Self {
            Self {
                subframes: Default::default(),
                frames: t.frames as i8,
                seconds: t.seconds,
                minutes: t.minutes,
                hours: t.hours,
                code_type: t.code_type,
            }
        }
    }

    /// Used by [`StandardTimeCode`].
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum SubFrames {
        /// The position in fractional frames, 0-99
        FractionalFrames(u8),
        /// Additional flags describing the status of this timecode.
        Status(TimeCodeStatus),
    }

    impl Default for SubFrames {
        fn default() -> Self {
            Self::FractionalFrames(0)
        }
    }

    impl SubFrames {
        fn to_byte(&self) -> u8 {
            match *self {
                Self::FractionalFrames(ff) => ff.min(99),
                Self::Status(s) => s.to_byte(),
            }
        }
    }

    /// Used by [`StandardTimeCode`].
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
    pub struct TimeCodeStatus {
        pub estimated_code: bool,
        pub invalid_code: bool,
        pub video_field1: bool,
        pub no_time_code: bool,
    }

    impl TimeCodeStatus {
        fn to_byte(&self) -> u8 {
            let mut b: u8 = 0;
            if self.estimated_code {
                b += 1 << 6;
            }
            if self.invalid_code {
                b += 1 << 5;
            }
            if self.video_field1 {
                b += 1 << 4;
            }
            if self.no_time_code {
                b += 1 << 3;
            }
            b
        }
    }

    /// 32 bits defined by SMPTE for "special functions". Used in [`UniversalRealTimeMsg::TimeCodeUserBits`](crate::UniversalRealTimeMsg::TimeCodeUserBits).
    /// See [the SMTPE time code standard](https://en.wikipedia.org/wiki/SMPTE_timecode).
    ///
    /// As defined in the MIDI Time Code spec (MMA0001 / RP004 / RP008)
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub struct UserBits {
        /// Full bytes can be used here. Sent such that the first is considered
        /// the "most significant" value
        pub bytes: (u8, u8, u8, u8),
        /// SMPTE time code bit 43 (EBU bit 27)
        pub flag1: bool,
        /// SMPTE time code bit 59 (EBU bit 43)
        pub flag2: bool,
    }

    impl UserBits {
        /// Turn the `UserBits` into its 9 nibble representation:
        /// [nibble_1, nibble_2, nibble_3, nibble_4, nibble_5, nibble_6, nibble_7, nibble_8, nibble_9, nibble_flags]
        pub fn to_nibbles(&self) -> [u8; 9] {
            let [uh, ug] = to_nibble(self.bytes.0);
            let [uf, ue] = to_nibble(self.bytes.1);
            let [ud, uc] = to_nibble(self.bytes.2);
            let [ub, ua] = to_nibble(self.bytes.3);
            let mut flags: u8 = 0;
            if self.flag1 {
                flags += 1;
            }
            if self.flag2 {
                flags += 2;
            }
            [ua, ub, uc, ud, ue, uf, ug, uh, flags]
        }
    }

    /// Like [`UserBits`] but allows for the embedding of a "secondary time code".
    ///
    /// As defined in MIDI Machine Control 1.0 (MMA0016 / RP013)
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub struct StandardUserBits {
        /// Full bytes can be used here. Sent such that the first is considered
        /// the "most significant" value
        pub bytes: (u8, u8, u8, u8),
        /// SMPTE time code bit 43 (EBU bit 27)
        pub flag1: bool,
        /// SMPTE time code bit 59 (EBU bit 43)
        pub flag2: bool,
        /// Contains a secondary time code
        pub secondary_time_code: bool,
    }

    impl StandardUserBits {
        /// Turn the `UserBits` into its 9 nibble representation:
        /// [nibble_1, nibble_2, nibble_3, nibble_4, nibble_5, nibble_6, nibble_7, nibble_8, nibble_9, nibble_flags]
        pub fn to_nibbles(&self) -> [u8; 9] {
            let [uh, ug] = to_nibble(self.bytes.0);
            let [uf, ue] = to_nibble(self.bytes.1);
            let [ud, uc] = to_nibble(self.bytes.2);
            let [ub, ua] = to_nibble(self.bytes.3);
            let mut flags: u8 = 0;
            if self.flag1 {
                flags += 1;
            }
            if self.flag2 {
                flags += 2;
            }
            if self.secondary_time_code {
                flags += 4;
            }
            [ua, ub, uc, ud, ue, uf, ug, uh, flags]
        }
    }

    impl From<StandardUserBits> for TimeCode {
        fn from(t: StandardUserBits) -> Self {
            let [ua, ub, uc, ud, ue, uf, ug, uh, _] = t.to_nibbles();
            let frames = (ub << 4) + ua;
            let seconds = (ud << 4) + uc;
            let minutes = (uf << 4) + ue;
            let hours = ((uh & 0b0001) << 4) + ug;
            let code_type = (uh & 0b0110) >> 1;

            TimeCode {
                frames,
                seconds,
                minutes,
                hours,
                code_type: match code_type {
                    3 => TimeCodeType::NDF30,
                    2 => TimeCodeType::DF30,
                    1 => TimeCodeType::FPS25,
                    0 => TimeCodeType::FPS24,
                    _ => panic!("Should not be reachable"),
                },
            }
        }
    }

    impl From<TimeCode> for StandardUserBits {
        fn from(t: TimeCode) -> Self {
            let [frame, seconds, minutes, codehour] = t.to_bytes();
            StandardUserBits {
                bytes: (codehour, minutes, seconds, frame),
                flag1: false,
                flag2: false,
                secondary_time_code: true,
            }
        }
    }

    impl From<UserBits> for StandardUserBits {
        fn from(t: UserBits) -> Self {
            Self {
                bytes: t.bytes,
                flag1: t.flag1,
                flag2: t.flag2,
                secondary_time_code: false,
            }
        }
    }

    /// Non-realtime Time Code Cueing. Used by [`UniversalNonRealTimeMsg::TimeCodeCueingSetup`](crate::UniversalNonRealTimeMsg::TimeCodeCueingSetup).
    ///
    /// As defined in the MIDI Time Code spec (MMA0001 / RP004 / RP008)
    #[derive(Debug, Clone, PartialEq)]
    pub enum TimeCodeCueingSetupMsg {
        TimeCodeOffset {
            time_code: HighResTimeCode,
        },
        EnableEventList,
        DisableEventList,
        ClearEventList,
        SystemStop,
        EventListRequest {
            time_code: HighResTimeCode,
        },
        PunchIn {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        PunchOut {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        DeletePunchIn {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        DeletePunchOut {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        EventStart {
            time_code: HighResTimeCode,
            event_number: u16,
            additional_information: Vec<MidiMsg>,
        },
        EventStop {
            time_code: HighResTimeCode,
            event_number: u16,
            additional_information: Vec<MidiMsg>,
        },
        DeleteEventStart {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        DeleteEventStop {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        Cue {
            time_code: HighResTimeCode,
            event_number: u16,
            additional_information: Vec<MidiMsg>,
        },
        DeleteCue {
            time_code: HighResTimeCode,
            event_number: u16,
        },
        EventName {
            time_code: HighResTimeCode,
            event_number: u16,
            name: BString,
        },
    }

    impl TimeCodeCueingSetupMsg {
        pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
            match self {
                Self::TimeCodeOffset { time_code } => {
                    v.push(0x00);
                    time_code.extend_midi(v);
                    v.push(0x00);
                    v.push(0x00);
                }
                Self::EnableEventList => {
                    v.push(0x00);
                    HighResTimeCode::default().extend_midi(v);
                    v.push(0x01);
                    v.push(0x00);
                }
                Self::DisableEventList => {
                    v.push(0x00);
                    HighResTimeCode::default().extend_midi(v);
                    v.push(0x02);
                    v.push(0x00);
                }
                Self::ClearEventList => {
                    v.push(0x00);
                    HighResTimeCode::default().extend_midi(v);
                    v.push(0x03);
                    v.push(0x00);
                }
                Self::SystemStop => {
                    v.push(0x00);
                    HighResTimeCode::default().extend_midi(v);
                    v.push(0x04);
                    v.push(0x00);
                }
                Self::EventListRequest { time_code } => {
                    v.push(0x00);
                    time_code.extend_midi(v);
                    v.push(0x05);
                    v.push(0x00);
                }
                Self::PunchIn {
                    time_code,
                    event_number,
                } => {
                    v.push(0x01);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::PunchOut {
                    time_code,
                    event_number,
                } => {
                    v.push(0x02);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::DeletePunchIn {
                    time_code,
                    event_number,
                } => {
                    v.push(0x03);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::DeletePunchOut {
                    time_code,
                    event_number,
                } => {
                    v.push(0x04);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::EventStart {
                    time_code,
                    event_number,
                    additional_information,
                } => {
                    if additional_information.is_empty() {
                        v.push(0x05);
                    } else {
                        v.push(0x07);
                    }
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                    push_nibblized_midi(additional_information, v);
                }
                Self::EventStop {
                    time_code,
                    event_number,
                    additional_information,
                } => {
                    if additional_information.is_empty() {
                        v.push(0x06);
                    } else {
                        v.push(0x08);
                    }
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                    push_nibblized_midi(additional_information, v);
                }
                Self::DeleteEventStart {
                    time_code,
                    event_number,
                } => {
                    v.push(0x09);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::DeleteEventStop {
                    time_code,
                    event_number,
                } => {
                    v.push(0x0A);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::Cue {
                    time_code,
                    event_number,
                    additional_information,
                } => {
                    if additional_information.is_empty() {
                        v.push(0x0B);
                    } else {
                        v.push(0x0C);
                    }
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                    push_nibblized_midi(additional_information, v);
                }
                Self::DeleteCue {
                    time_code,
                    event_number,
                } => {
                    v.push(0x0D);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                }
                Self::EventName {
                    time_code,
                    event_number,
                    name,
                } => {
                    v.push(0x0E);
                    time_code.extend_midi(v);
                    push_u14(*event_number, v);
                    push_nibblized_name(name, v);
                }
            }
        }

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

    /// Realtime Time Code Cueing. Used by [`UniversalRealTimeMsg::TimeCodeCueing`](crate::UniversalRealTimeMsg::TimeCodeCueing).
    ///
    /// As defined in the MIDI Time Code spec (MMA0001 / RP004 / RP008)
    #[derive(Debug, Clone, PartialEq)]
    pub enum TimeCodeCueingMsg {
        SystemStop,
        PunchIn {
            event_number: u16,
        },
        PunchOut {
            event_number: u16,
        },
        EventStart {
            event_number: u16,
            additional_information: Vec<MidiMsg>,
        },
        EventStop {
            event_number: u16,
            additional_information: Vec<MidiMsg>,
        },
        Cue {
            event_number: u16,
            additional_information: Vec<MidiMsg>,
        },
        EventName {
            event_number: u16,
            name: BString,
        },
    }

    fn push_nibblized_midi(msgs: &[MidiMsg], v: &mut Vec<u8>) {
        for msg in msgs.iter() {
            for b in msg.to_midi().iter() {
                let [msn, lsn] = to_nibble(*b);
                v.push(lsn);
                v.push(msn);
            }
        }
    }

    fn push_nibblized_name(name: &BString, v: &mut Vec<u8>) {
        // Not sure if this actually handles newlines correctly
        for b in name.iter() {
            let [msn, lsn] = to_nibble(*b);
            v.push(lsn);
            v.push(msn);
        }
    }

    impl TimeCodeCueingMsg {
        pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
            match self {
                Self::SystemStop => {
                    v.push(0x00);
                    v.push(0x04);
                    v.push(0x00);
                }
                Self::PunchIn { event_number } => {
                    v.push(0x01);
                    push_u14(*event_number, v);
                }
                Self::PunchOut { event_number } => {
                    v.push(0x02);
                    push_u14(*event_number, v);
                }
                Self::EventStart {
                    event_number,
                    additional_information,
                } => {
                    if additional_information.is_empty() {
                        v.push(0x05);
                    } else {
                        v.push(0x07);
                    }
                    push_u14(*event_number, v);
                    push_nibblized_midi(additional_information, v);
                }
                Self::EventStop {
                    event_number,
                    additional_information,
                } => {
                    if additional_information.is_empty() {
                        v.push(0x06);
                    } else {
                        v.push(0x08);
                    }
                    push_u14(*event_number, v);
                    push_nibblized_midi(additional_information, v);
                }
                Self::Cue {
                    event_number,
                    additional_information,
                } => {
                    if additional_information.is_empty() {
                        v.push(0x0B);
                    } else {
                        v.push(0x0C);
                    }
                    push_u14(*event_number, v);
                    push_nibblized_midi(additional_information, v);
                }
                Self::EventName { event_number, name } => {
                    v.push(0x0E);
                    push_u14(*event_number, v);
                    push_nibblized_name(&name, v);
                }
            }
        }

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

#[cfg(feature = "sysex")]
pub use sysex_types::*;

#[cfg(test)]
#[cfg(feature = "sysex")]
mod tests {
    use crate::*;
    use std::vec;
    extern crate std;

    #[test]
    fn serialize_time_code_cuing_setup_msg() {
        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalNonRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalNonRealTimeMsg::TimeCodeCueingSetup(
                        TimeCodeCueingSetupMsg::SystemStop
                    ),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7E, 0x7f, 04, 00, 96, 00, 00, 00, 00, 04, 00, 0xF7]
        );
    }

    #[test]
    fn serialize_time_code_cuing_msg() {
        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::TimeCodeCueing(TimeCodeCueingMsg::SystemStop),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 05, 00, 04, 00, 0xF7]
        );

        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::TimeCodeCueing(TimeCodeCueingMsg::EventStart {
                        event_number: 511,
                        additional_information: vec![]
                    }),
                },
            }
            .to_midi(),
            vec![0xF0, 0x7F, 0x7f, 05, 05, 0x7f, 0x03, 0xF7]
        );

        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::TimeCodeCueing(TimeCodeCueingMsg::EventStart {
                        event_number: 511,
                        additional_information: vec![MidiMsg::ChannelVoice {
                            channel: Channel::Ch2,
                            msg: ChannelVoiceMsg::NoteOn {
                                note: 0x55,
                                velocity: 0x67
                            }
                        }]
                    }),
                },
            }
            .to_midi(),
            vec![
                0xF0, 0x7F, 0x7f, 05, 07, 0x7f, 0x03,
                // Note on midi msg: 0x91, 0x55, 0x67
                0x01, 0x09, 0x05, 0x05, 0x07, 0x06, // End
                0xF7
            ]
        );
    }
}