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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
use super::util::*;

/// Channel-level messages that act on a voice. For instance, turning notes on off,
/// or modifying sounding notes. Used in [`MidiMsg`](crate::MidiMsg).
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ChannelVoiceMsg {
    /// Turn on a note
    NoteOn {
        /// A MIDI note number 0-127. Per GM1, 69 = A440
        note: u8,
        /// The velocity the note should be played at, 0-127
        velocity: u8,
    },
    /// Turn off a note
    NoteOff {
        /// Stop playing the given MIDI note at this channel, 0-127
        note: u8,
        /// The velocity the note should stop being played at, 0-127
        velocity: u8,
    },
    /// Generally used for modifying the tones being played. Frequently shortened to 'CC'
    ControlChange { control: ControlChange },
    /// A note on with a preceding HighResVelocity CC per CA-031
    HighResNoteOn { note: u8, velocity: u16 },
    /// A note off with a preceding HighResVelocity CC per CA-031
    HighResNoteOff { note: u8, velocity: u16 },
    /// The amount of pressure being applied to a given note, which is a signal some controllers
    /// after an initial `NoteOn`.
    /// Can act on multiple notes at a time, thus it is "polyphonic".
    PolyPressure {
        /// The note to apply this pressure signal to, 0-127
        note: u8,
        /// The amount of pressure to apply, 0-127
        pressure: u8,
    },
    /// Similar to `PolyPressure`, but only applies at the channel-level.
    ChannelPressure { pressure: u8 },
    /// Which "program", "patch" or "sound" to use when playing any preceding notes, 0-127.
    /// Use [`GMSoundSet`](crate::GMSoundSet) when targeting General MIDI
    ProgramChange { program: u8 },
    /// Apply a pitch bend to all sounding notes. 0-8191 represent negative bends,
    /// 8192 is no bend and8193-16383 are positive bends, with the standard bend rang
    /// being +/-2 semitones per GM2. See [`Parameter::PitchBendSensitivity`]
    PitchBend { bend: u16 },
}

impl ChannelVoiceMsg {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        match self {
            ChannelVoiceMsg::NoteOff { .. } => v.push(0x80),
            ChannelVoiceMsg::NoteOn { .. } => v.push(0x90),
            ChannelVoiceMsg::HighResNoteOff { .. } => v.push(0x80),
            ChannelVoiceMsg::HighResNoteOn { .. } => v.push(0x90),
            ChannelVoiceMsg::PolyPressure { .. } => v.push(0xA0),
            ChannelVoiceMsg::ControlChange { .. } => v.push(0xB0),
            ChannelVoiceMsg::ProgramChange { .. } => v.push(0xC0),
            ChannelVoiceMsg::ChannelPressure { .. } => v.push(0xD0),
            ChannelVoiceMsg::PitchBend { .. } => v.push(0xE0),
        }
        self.extend_midi_running(v);
    }

    /// Out of necessity, pushes a Channel message after the note message for `HighResNoteOn/Off`
    pub(crate) fn extend_midi_running(&self, v: &mut Vec<u8>) {
        match *self {
            ChannelVoiceMsg::NoteOff { note, velocity } => {
                v.push(to_u7(note));
                v.push(to_u7(velocity));
            }
            ChannelVoiceMsg::NoteOn { note, velocity } => {
                v.push(to_u7(note));
                v.push(to_u7(velocity));
            }
            ChannelVoiceMsg::HighResNoteOff { note, velocity } => {
                let [msb, lsb] = to_u14(velocity);
                push_u7(note, v);
                v.push(msb);
                v.push(0xB0);
                v.push(0x58);
                v.push(lsb);
            }
            ChannelVoiceMsg::HighResNoteOn { note, velocity } => {
                let [msb, lsb] = to_u14(velocity);
                push_u7(note, v);
                v.push(msb);
                v.push(0xB0);
                v.push(0x58);
                v.push(lsb);
            }
            ChannelVoiceMsg::PolyPressure { note, pressure } => {
                v.push(to_u7(note));
                v.push(to_u7(pressure));
            }
            ChannelVoiceMsg::ControlChange { control } => control.extend_midi_running(v),
            ChannelVoiceMsg::ProgramChange { program } => v.push(to_u7(program)),
            ChannelVoiceMsg::ChannelPressure { pressure } => v.push(to_u7(pressure)),
            ChannelVoiceMsg::PitchBend { bend } => {
                push_u14(bend, v);
            }
        }
    }

    pub(crate) fn from_midi(_m: &[u8]) -> Result<(Self, usize), &str> {
        Err("TODO: not implemented")
    }
}

/// An enum that defines the MIDI numbers associated with Control Changes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ControlNumber {
    BankSelect = 0,
    BankSelectLSB = 32,
    ModWheel = 1,
    ModWheelLSB = 33,
    Breath = 2,
    BreathLSB = 34,
    Foot = 4,
    FootLSB = 36,
    Portamento = 5,
    PortamentoLSB = 37,
    DataEntry = 6,
    DataEntryLSB = 38,
    Volume = 7,
    VolumeLSB = 39,
    Balance = 8,
    BalanceLSB = 40,
    Pan = 10,
    PanLSB = 42,
    Expression = 11,
    ExpressionLSB = 43,
    Effect1 = 12,
    Effect1LSB = 44,
    Effect2 = 13,
    Effect2LSB = 45,
    GeneralPurpose1 = 16,
    GeneralPurpose1LSB = 48,
    GeneralPurpose2 = 17,
    GeneralPurpose2LSB = 49,
    GeneralPurpose3 = 18,
    GeneralPurpose3LSB = 50,
    GeneralPurpose4 = 19,
    GeneralPurpose4LSB = 51,
    /// AKA Sustain
    Hold = 64,
    TogglePortamento = 65,
    Sostenuto = 66,
    SoftPedal = 67,
    ToggleLegato = 68,
    Hold2 = 69,
    /// AKA SoundVariation
    SoundControl1 = 70,
    /// AKA Timbre
    SoundControl2 = 71,
    /// AKA ReleaseTime
    SoundControl3 = 72,
    /// AKA AttackTime
    SoundControl4 = 73,
    /// AKA Brightness
    SoundControl5 = 74,
    /// AKA DecayTime
    SoundControl6 = 75,
    /// AKA VibratoRate
    SoundControl7 = 76,
    /// AKA VibratoDepth
    SoundControl8 = 77,
    /// AKA VibratoDelay
    SoundControl9 = 78,
    SoundControl10 = 79,
    GeneralPurpose5 = 80,
    GeneralPurpose6 = 81,
    GeneralPurpose7 = 82,
    GeneralPurpose8 = 83,
    PortamentoControl = 84,
    HighResVelocity = 88,
    /// AKA ReverbSendLevel
    Effects1Depth = 91,
    /// AKA TremoloDepth
    Effects2Depth = 92,
    /// AKA ChorusSendLevel
    Effects3Depth = 93,
    /// AKA CelesteDepth
    Effects4Depth = 94,
    /// AKA PhaserDepth
    Effects5Depth = 95,
    DataIncrement = 96,
    DataDecrement = 97,
    NonRegisteredParameterLSB = 98,
    NonRegisteredParameter = 99,
    RegisteredParameterLSB = 100,
    RegisteredParameter = 101,
}

/// Used by [`ChannelVoiceMsg::ControlChange`] to modify sounds.
/// Each control targets a particular [`ControlNumber`], the meaning of which is given by convention.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ControlChange {
    /// 0-16383
    BankSelect(u16),
    /// 0-16383
    ModWheel(u16),
    /// 0-16383
    Breath(u16),
    /// Control number may be any valid Midi CC Control number. May not be > 119.
    Undefined {
        control: u8,
        /// 0-127
        value: u8,
    },
    /// `control1` is associated with the MSB of the value, `control2` with the LSB. Neither `controls` may be > 119.
    UndefinedHighRes {
        control1: u8,
        control2: u8,
        /// 0-16383
        value: u16,
    },
    /// 0-16383
    Foot(u16),
    /// 0-16383
    Portamento(u16),
    /// 0-16383
    Volume(u16),
    /// 0-16383
    Balance(u16),
    /// 0-16383
    Pan(u16),
    /// 0-16383
    Expression(u16),
    /// 0-16383
    Effect1(u16),
    /// 0-16383
    Effect2(u16),
    /// 0-16383
    GeneralPurpose1(u16),
    /// 0-16383
    GeneralPurpose2(u16),
    /// 0-16383
    GeneralPurpose3(u16),
    /// 0-16383
    GeneralPurpose4(u16),
    /// 0-127
    GeneralPurpose5(u8),
    /// 0-127
    GeneralPurpose6(u8),
    /// 0-127
    GeneralPurpose7(u8),
    /// 0-127
    GeneralPurpose8(u8),
    /// 0-127
    Hold(u8),
    /// 0-127
    Hold2(u8),
    /// Turn portamento on or off
    TogglePortamento(bool),
    /// 0-127
    Sostenuto(u8),
    /// 0-127
    SoftPedal(u8),
    /// Turn legato on or off
    ToggleLegato(bool),
    /// Same as SoundControl1
    SoundVariation(u8),
    /// Same as SoundControl2
    Timbre(u8),
    /// Same as SoundControl3
    ReleaseTime(u8),
    /// Same as SoundControl4
    AttackTime(u8),
    /// Same as SoundControl5, and used as the MPE "third dimension" (usually Timbre) control
    /// (RP-021, RP-053)
    Brightness(u8),
    /// Same as SoundControl6 (RP-021)
    DecayTime(u8),
    /// Same as SoundControl7 (RP-021)
    VibratoRate(u8),
    /// Same as SoundControl8 (RP-021)
    VibratoDepth(u8),
    /// Same as SoundControl9 (RP-021)
    VibratoDelay(u8),
    /// 0-127
    SoundControl1(u8),
    /// 0-127
    SoundControl2(u8),
    /// 0-127
    SoundControl3(u8),
    /// 0-127
    SoundControl4(u8),
    /// 0-127
    SoundControl5(u8),
    /// 0-127
    SoundControl6(u8),
    /// 0-127
    SoundControl7(u8),
    /// 0-127
    SoundControl8(u8),
    /// 0-127
    SoundControl9(u8),
    /// 0-127
    SoundControl10(u8),
    /// Used as the LSB of the velocity for the next note on/off message, 0-127.
    /// Defined in CA-031
    HighResVelocity(u8),
    /// 0-127
    PortamentoControl(u8),
    /// 0-127
    Effects1Depth(u8),
    /// 0-127
    Effects2Depth(u8),
    /// 0-127
    Effects3Depth(u8),
    /// 0-127
    Effects4Depth(u8),
    /// 0-127
    Effects5Depth(u8),
    /// Same as Effects1Depth (RP-023)
    ReverbSendLevel(u8),
    /// Same as Effects2Depth
    TremoloDepth(u8),
    /// Same as Effects3Depth (RP-023)
    ChorusSendLevel(u8),
    /// Same as Effects4Depth
    CelesteDepth(u8),
    /// Same as Effects5Depth
    PhaserDepth(u8),
    /// Registered and Unregistered Parameters
    Parameter(Parameter),
    /// Set the value of the last-set Parameter. 0-16383
    DataEntry(u16),
    /// Set the MSB and LSB of the last-set parameter separately.
    DataEntry2(u8, u8),
    /// Increment the value of the last-set Parameter. 0-127
    DataIncrement(u8),
    /// Decrement the value of the last-set Parameter. 0-127
    DataDecrement(u8),
}

impl ControlChange {
    fn high_res_cc(v: &mut Vec<u8>, control: u8, value: u16) {
        let [msb, lsb] = to_u14(value);
        v.push(control);
        v.push(msb);
        v.push(control + 32);
        v.push(lsb);
    }

    fn undefined(v: &mut Vec<u8>, control: u8, value: u8) {
        v.push(control.min(119));
        v.push(to_u7(value));
    }

    fn undefined_high_res(v: &mut Vec<u8>, control1: u8, control2: u8, value: u16) {
        let [msb, lsb] = to_u14(value);
        v.push(control1.min(119));
        v.push(msb);
        v.push(control2.min(119));
        v.push(lsb);
    }
}

impl ControlChange {
    pub fn to_midi_running(&self) -> Vec<u8> {
        let mut r: Vec<u8> = vec![];
        self.extend_midi_running(&mut r);
        r
    }

    pub fn extend_midi_running(&self, v: &mut Vec<u8>) {
        match *self {
            ControlChange::BankSelect(x) => ControlChange::high_res_cc(v, 0, x),
            ControlChange::ModWheel(x) => ControlChange::high_res_cc(v, 1, x),
            ControlChange::Breath(x) => ControlChange::high_res_cc(v, 2, x),
            ControlChange::Undefined { control, value } => {
                ControlChange::undefined(v, control, value)
            }
            ControlChange::UndefinedHighRes {
                control1,
                control2,
                value,
            } => ControlChange::undefined_high_res(v, control1, control2, value),
            ControlChange::Foot(x) => ControlChange::high_res_cc(v, 4, x),
            ControlChange::Portamento(x) => ControlChange::high_res_cc(v, 5, x),
            ControlChange::Volume(x) => ControlChange::high_res_cc(v, 7, x),
            ControlChange::Balance(x) => ControlChange::high_res_cc(v, 8, x),
            ControlChange::Pan(x) => ControlChange::high_res_cc(v, 10, x),
            ControlChange::Expression(x) => ControlChange::high_res_cc(v, 11, x),
            ControlChange::Effect1(x) => ControlChange::high_res_cc(v, 12, x),
            ControlChange::Effect2(x) => ControlChange::high_res_cc(v, 13, x),
            ControlChange::GeneralPurpose1(x) => ControlChange::high_res_cc(v, 0, x),
            ControlChange::GeneralPurpose2(x) => ControlChange::high_res_cc(v, 0, x),
            ControlChange::GeneralPurpose3(x) => ControlChange::high_res_cc(v, 0, x),
            ControlChange::GeneralPurpose4(x) => ControlChange::high_res_cc(v, 0, x),
            ControlChange::GeneralPurpose5(x) => {
                v.push(80);
                v.push(to_u7(x));
            }
            ControlChange::GeneralPurpose6(x) => {
                v.push(82);
                v.push(to_u7(x));
            }
            ControlChange::GeneralPurpose7(x) => {
                v.push(83);
                v.push(to_u7(x));
            }
            ControlChange::GeneralPurpose8(x) => {
                v.push(84);
                v.push(to_u7(x));
            }
            ControlChange::Hold(x) => {
                v.push(64);
                v.push(to_u7(x));
            }
            ControlChange::Hold2(x) => {
                v.push(69);
                v.push(to_u7(x));
            }
            ControlChange::TogglePortamento(on) => {
                v.push(65);
                v.push(if on { 127 } else { 0 });
            }
            ControlChange::Sostenuto(x) => {
                v.push(66);
                v.push(to_u7(x));
            }
            ControlChange::SoftPedal(x) => {
                v.push(67);
                v.push(to_u7(x));
            }
            ControlChange::ToggleLegato(on) => {
                v.push(68);
                v.push(if on { 127 } else { 0 });
            }
            ControlChange::SoundVariation(x) | ControlChange::SoundControl1(x) => {
                v.push(70);
                v.push(to_u7(x));
            }
            ControlChange::Timbre(x) | ControlChange::SoundControl2(x) => {
                v.push(71);
                v.push(to_u7(x));
            }
            ControlChange::ReleaseTime(x) | ControlChange::SoundControl3(x) => {
                v.push(72);
                v.push(to_u7(x));
            }
            ControlChange::AttackTime(x) | ControlChange::SoundControl4(x) => {
                v.push(73);
                v.push(to_u7(x));
            }
            ControlChange::Brightness(x) | ControlChange::SoundControl5(x) => {
                v.push(74);
                v.push(to_u7(x));
            }
            ControlChange::DecayTime(x) | ControlChange::SoundControl6(x) => {
                v.push(75);
                v.push(to_u7(x));
            }
            ControlChange::VibratoRate(x) | ControlChange::SoundControl7(x) => {
                v.push(76);
                v.push(to_u7(x));
            }
            ControlChange::VibratoDepth(x) | ControlChange::SoundControl8(x) => {
                v.push(77);
                v.push(to_u7(x));
            }
            ControlChange::VibratoDelay(x) | ControlChange::SoundControl9(x) => {
                v.push(78);
                v.push(to_u7(x));
            }
            ControlChange::SoundControl10(x) => {
                v.push(79);
                v.push(to_u7(x));
            }
            ControlChange::PortamentoControl(x) => {
                v.push(84);
                v.push(to_u7(x));
            }
            ControlChange::HighResVelocity(x) => {
                v.push(88);
                v.push(to_u7(x));
            }
            ControlChange::Effects1Depth(x) | ControlChange::ReverbSendLevel(x) => {
                v.push(91);
                v.push(to_u7(x));
            }
            ControlChange::Effects2Depth(x) | ControlChange::TremoloDepth(x) => {
                v.push(92);
                v.push(to_u7(x));
            }
            ControlChange::Effects3Depth(x) | ControlChange::ChorusSendLevel(x) => {
                v.push(93);
                v.push(to_u7(x));
            }
            ControlChange::Effects4Depth(x) | ControlChange::CelesteDepth(x) => {
                v.push(94);
                v.push(to_u7(x));
            }
            ControlChange::Effects5Depth(x) | ControlChange::PhaserDepth(x) => {
                v.push(95);
                v.push(to_u7(x));
            }

            // Parameters
            ControlChange::Parameter(p) => p.extend_midi_running(v),
            ControlChange::DataEntry(x) => ControlChange::high_res_cc(v, 6, x),
            ControlChange::DataEntry2(msb, lsb) => {
                v.push(6);
                v.push(msb);
                v.push(6 + 32);
                v.push(lsb);
            }
            ControlChange::DataIncrement(x) => {
                v.push(96);
                v.push(to_u7(x));
            }
            ControlChange::DataDecrement(x) => {
                v.push(97);
                v.push(to_u7(x));
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// Used by [`ControlChange::Parameter`]. "Entry" Parameters can be used to set the given parameters:
/// they will first select that parameter, then send a [`ControlChange::DataEntry`] with the given value.
pub enum Parameter {
    /// A whole bunch of parameters defined by the given number 0-16383, that can be used for whatever.
    Unregistered(u16),
    /// A registered parameter that does nothing.
    /// Defined in GM2.
    Null,
    /// The pitch bend sensitivity in semitones (0-127) and the sensitivity in cents (0-100),
    /// respectively. For example, a value (1, 0) means +/- one semitone (a total range of two
    /// semitones)
    PitchBendSensitivity,
    PitchBendSensitivityEntry(u8, u8),
    /// A value from -8192-8191, representing the fractional cents to shift away from A440
    /// in 1/8192ths of a cent.
    FineTuning,
    FineTuningEntry(i16),
    /// A value from -64-63, the number of semitones to shift away from A44.
    CoarseTuning,
    CoarseTuningEntry(i8),
    /// Which "Tuning Program" to select from: 0-127.
    ///
    /// Defined in the MIDI Tuning Standard (Updated Specification)
    TuningProgramSelect,
    TuningProgramSelectEntry(u8),
    /// Which "Tuning Bank" to select from: 0-127.
    ///
    /// Defined in the MIDI Tuning Standard (Updated Specification)
    TuningBankSelect,
    TuningBankSelectEntry(u8),
    /// The amount of "modulation depth" your mod wheel should apply: 0-16383.
    ///
    /// Defined in CA 26. GM2 defines what this range might mean
    ModulationDepthRange,
    ModulationDepthRangeEntry(u16),
    /// Only valid when sent to channel 1 or channel 16, the former indicating that this
    /// is configuring the number of "lower zone" channels and the latter referring to the
    /// "upper zone". A value between 0 (zone is not configured to be MPE) and 16 (zone has
    /// 16 channels in it). There can be no more than lower zone channels + upper zone channels
    /// active at a given time.
    ///
    /// Defined in RP-053: MIDI Polyphonic Expression
    PolyphonicExpression,
    PolyphonicExpressionEntry(u8),
    /// A value 0-16383 representing -180.00-179.98 degrees.
    ///
    /// Defined in RP-049
    AzimuthAngle3DSound,
    AzimuthAngle3DSoundEntry(u16),
    /// A value 0-16383 representing -180.00-179.98 degrees.
    ///
    /// Defined in RP-049
    ElevationAngle3DSound,
    ElevationAngle3DSoundEntry(u16),
    /// A value 1-16383 representing -163.82-0 dB of gain.
    ///
    /// 0 indicates "negative infinity".
    ///
    /// Defined in RP-049
    Gain3DSound,
    Gain3DSoundEntry(u16),
    /// A value 0-16383 representing a ratio between -0.000061-1.0.
    ///
    /// Defined in RP-049
    DistanceRatio3DSound,
    DistanceRatio3DSoundEntry(u16),
    /// A value 0-16383 representing between 0 and 1000 distance units.
    /// Defined in RP-049
    MaxiumumDistance3DSound,
    MaxiumumDistance3DSoundEntry(u16),
    /// A value 0-16383 representing -163.83-0 dB of gain
    /// Defined in RP-049
    GainAtMaxiumumDistance3DSound,
    GainAtMaxiumumDistance3DSoundEntry(u16),
    /// A value 0-16383 representing a ratio between -0.000061-1.0
    /// Defined in RP-049
    ReferenceDistanceRatio3DSound,
    ReferenceDistanceRatio3DSoundEntry(u16),
    /// A value 0-16383 representing -180.00-179.98 degrees
    /// Defined in RP-049
    PanSpreadAngle3DSound,
    PanSpreadAngle3DSoundEntry(u16),
    /// A value 0-16383 representing -180.00-179.98 degrees
    /// Defined in RP-049
    RollAngle3DSound,
    RollAngle3DSoundEntry(u16),
}

impl Parameter {
    fn extend_midi_running(&self, v: &mut Vec<u8>) {
        match self {
            Self::Null => {
                v.push(100);
                v.push(0x7F);
                v.push(101);
                v.push(0x7F);
            }
            Self::PitchBendSensitivity => {
                v.push(100);
                v.push(0);
                v.push(101);
                v.push(0);
            }
            Self::PitchBendSensitivityEntry(c, f) => {
                Self::PitchBendSensitivity.extend_midi_running(v);
                // Data entry:
                v.push(6);
                v.push(*c);
                v.push(6 + 32);
                v.push((*f).min(100));
            }
            Self::FineTuning => {
                v.push(100);
                v.push(1);
                v.push(101);
                v.push(0);
            }
            Self::FineTuningEntry(x) => {
                Self::FineTuning.extend_midi_running(v);
                // Data entry:
                let [msb, lsb] = i_to_u14(*x);
                v.push(6);
                v.push(msb);
                v.push(6 + 32);
                v.push(lsb);
            }
            Self::CoarseTuning => {
                v.push(100);
                v.push(2);
                v.push(101);
                v.push(0);
            }
            Self::CoarseTuningEntry(x) => {
                Self::CoarseTuning.extend_midi_running(v);
                // Data entry:
                let msb = i_to_u7(*x);
                v.push(6);
                v.push(msb);
                v.push(6 + 32);
                v.push(0);
            }
            Self::TuningProgramSelect => {
                v.push(100);
                v.push(3);
                v.push(101);
                v.push(0);
            }
            Self::TuningProgramSelectEntry(x) => {
                Self::TuningProgramSelect.extend_midi_running(v);
                // Data entry (MSB only)
                v.push(6);
                v.push(*x);
            }
            Self::TuningBankSelect => {
                v.push(100);
                v.push(4);
                v.push(101);
                v.push(0);
            }
            Self::TuningBankSelectEntry(x) => {
                Self::TuningBankSelect.extend_midi_running(v);
                // Data entry (MSB only)
                v.push(6);
                v.push(*x);
            }
            Self::ModulationDepthRange => {
                v.push(100);
                v.push(5);
                v.push(101);
                v.push(0);
            }
            Self::ModulationDepthRangeEntry(x) => {
                Self::ModulationDepthRange.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::PolyphonicExpression => {
                v.push(100);
                v.push(6);
                v.push(101);
                v.push(0);
            }
            Self::PolyphonicExpressionEntry(x) => {
                Self::PolyphonicExpression.extend_midi_running(v);
                // Data entry (MSB only)
                v.push(6);
                v.push((*x).min(16));
            }
            Self::AzimuthAngle3DSound => {
                v.push(100);
                v.push(0);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::AzimuthAngle3DSoundEntry(x) => {
                Self::AzimuthAngle3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::ElevationAngle3DSound => {
                v.push(100);
                v.push(1);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::ElevationAngle3DSoundEntry(x) => {
                Self::ElevationAngle3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::Gain3DSound => {
                v.push(100);
                v.push(2);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::Gain3DSoundEntry(x) => {
                Self::Gain3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::DistanceRatio3DSound => {
                v.push(100);
                v.push(3);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::DistanceRatio3DSoundEntry(x) => {
                Self::DistanceRatio3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::MaxiumumDistance3DSound => {
                v.push(100);
                v.push(4);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::MaxiumumDistance3DSoundEntry(x) => {
                Self::MaxiumumDistance3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::GainAtMaxiumumDistance3DSound => {
                v.push(100);
                v.push(5);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::GainAtMaxiumumDistance3DSoundEntry(x) => {
                Self::GainAtMaxiumumDistance3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::ReferenceDistanceRatio3DSound => {
                v.push(100);
                v.push(6);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::ReferenceDistanceRatio3DSoundEntry(x) => {
                Self::ReferenceDistanceRatio3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::PanSpreadAngle3DSound => {
                v.push(100);
                v.push(7);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::PanSpreadAngle3DSoundEntry(x) => {
                Self::PanSpreadAngle3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::RollAngle3DSound => {
                v.push(100);
                v.push(8);
                v.push(101);
                v.push(61); // 3D Sound
            }
            Self::RollAngle3DSoundEntry(x) => {
                Self::RollAngle3DSound.extend_midi_running(v);
                // Data entry
                ControlChange::high_res_cc(v, 6, *x);
            }
            Self::Unregistered(x) => {
                let [msb, lsb] = to_u14(*x);
                v.push(98);
                v.push(lsb);
                v.push(99);
                v.push(msb);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::*;

    #[test]
    fn serialize_channel_voice_msg() {
        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch1,
                msg: ChannelVoiceMsg::NoteOn {
                    note: 0x88,
                    velocity: 0xff
                }
            }
            .to_midi(),
            vec![0x90, 0x7f, 127]
        );

        assert_eq!(
            MidiMsg::RunningChannelVoice {
                channel: Channel::Ch10,
                msg: ChannelVoiceMsg::PitchBend { bend: 0xff44 }
            }
            .to_midi(),
            vec![0x7f, 0x7f]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch10,
                msg: ChannelVoiceMsg::PitchBend { bend: 1000 }
            }
            .to_midi(),
            vec![0xE9, 0x68, 0x07]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch2,
                msg: ChannelVoiceMsg::ControlChange {
                    control: ControlChange::Volume(1000)
                }
            }
            .to_midi(),
            vec![0xB1, 7, 0x07, 39, 0x68]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch4,
                msg: ChannelVoiceMsg::ControlChange {
                    control: ControlChange::Undefined {
                        control: 85,
                        value: 77
                    }
                }
            }
            .to_midi(),
            vec![0xB3, 85, 77]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch2,
                msg: ChannelVoiceMsg::ControlChange {
                    control: ControlChange::UndefinedHighRes {
                        control1: 3,
                        control2: 35,
                        value: 1000
                    }
                }
            }
            .to_midi(),
            vec![0xB1, 3, 0x07, 35, 0x68]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch3,
                msg: ChannelVoiceMsg::ControlChange {
                    control: ControlChange::TogglePortamento(true)
                }
            }
            .to_midi(),
            vec![0xB2, 65, 0x7f]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch2,
                msg: ChannelVoiceMsg::ControlChange {
                    control: ControlChange::Parameter(Parameter::FineTuning)
                }
            }
            .to_midi(),
            vec![0xB1, 100, 0x01, 101, 0x00]
        );

        assert_eq!(
            MidiMsg::ChannelVoice {
                channel: Channel::Ch2,
                msg: ChannelVoiceMsg::ControlChange {
                    control: ControlChange::Parameter(Parameter::Unregistered(1000))
                }
            }
            .to_midi(),
            vec![0xB1, 98, 0x68, 99, 0x07]
        );
    }
}