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
//! This module contains data types to represent the different messages that can be sent over MIDI.

use crate::Note;

/// An enum with variants for all possible Midi messages.
#[derive(Debug, PartialEq, Clone, Copy, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MidiMessage {
    // Channel voice messages
    /// Note Off message
    NoteOff(Channel, Note, Value7),

    /// Note on message
    NoteOn(Channel, Note, Value7),

    /// KeyPressure message for polyphonic aftertouch
    KeyPressure(Channel, Note, Value7),

    /// Control change message
    ControlChange(Channel, Control, Value7),

    /// Program change message
    ProgramChange(Channel, Program),

    /// Channel pressure message for channel aftertouch
    ChannelPressure(Channel, Value7),

    /// Pitch bend message
    PitchBendChange(Channel, Value14),

    // System common messages
    /// System exclusive message starts
    // SystemExclusive {
    //     /// The system exclusive manufacturer id, this is either a 1 byte or 3 byte number
    //     manufacturer_id: u32,
    // },

    /// System exclusive data is received
    // SystemExclusiveData (Value7),

    /// Signals the end of the system exclusive block
    // EndOfExclusive,

    /// Midi time code quarter frame
    QuarterFrame(QuarterFrame),

    /// Set the song position pointer
    SongPositionPointer(Value14),

    /// Specifies which sequence or song is to be played
    SongSelect(Value7),

    /// Tune all oscillators
    TuneRequest,

    // System real time messages
    /// Timing tick message
    TimingClock,

    /// Start message
    Start,

    /// Continue message
    Continue,

    /// Stop message
    Stop,

    /// Active sensing message
    ActiveSensing,

    /// Reset message
    Reset,
}

impl MidiMessage {
    /// The length of the rendered data, including the status
    #[allow(clippy::len_without_is_empty)]
    pub const fn len(&self) -> usize {
        match self {
            Self::NoteOff(..)
            | Self::NoteOn(..)
            | Self::KeyPressure(..)
            | Self::ControlChange(..)
            | Self::PitchBendChange(..)
            | Self::SongPositionPointer(..) => 3,
            Self::ProgramChange(..)
            | Self::ChannelPressure(..)
            | Self::QuarterFrame(..)
            | Self::SongSelect(..) => 2,
            Self::TuneRequest
            | Self::TimingClock
            | Self::Start
            | Self::Continue
            | Self::Stop
            | Self::ActiveSensing
            | Self::Reset => 1,
        }
    }
}

#[allow(missing_docs)]
/// Status byte constants
pub mod status {
    pub const NOTE_OFF: u8 = 0x80;
    pub const NOTE_ON: u8 = 0x90;
    pub const KEY_PRESSURE: u8 = 0xA0;
    pub const CONTROL_CHANGE: u8 = 0xB0;
    pub const PITCH_BEND_CHANGE: u8 = 0xE0;
    pub const SONG_POSITION_POINTER: u8 = 0xF2;
    pub const PROGRAM_CHANGE: u8 = 0xC0;
    pub const CHANNEL_PRESSURE: u8 = 0xD0;
    pub const QUARTER_FRAME: u8 = 0xF1;
    pub const SONG_SELECT: u8 = 0xF3;
    pub const TUNE_REQUEST: u8 = 0xF6;
    pub const TIMING_CLOCK: u8 = 0xF8;
    pub const START: u8 = 0xFA;
    pub const CONTINUE: u8 = 0xFB;
    pub const STOP: u8 = 0xFC;
    pub const ACTIVE_SENSING: u8 = 0xFE;
    pub const RESET: u8 = 0xFF;

    pub const SYSEX_START: u8 = 0xF0;
    pub const SYSEX_END: u8 = 0xF7;
}

/// Represents a Midi channel, Midi channels can range from 0 to 15, but are represented as 1 based
/// values Channel 1 to 16
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Channel(u8);

impl Channel {
    /// Create a new `Channel`
    ///
    /// # Arguments
    /// * `val` - the 0 based channel value
    ///
    /// # Note
    /// * The `val` will be clamped so it is in the 0..15 valid range.
    ///
    pub const fn new(val: u8) -> Self {
        Self(if val > 15 { 15 } else { val })
    }

    /// MIDI channel 1
    pub const C1: Self = Self::new(0);
    /// MIDI channel 2
    pub const C2: Self = Self::new(1);
    /// MIDI channel 3
    pub const C3: Self = Self::new(2);
    /// MIDI channel 4
    pub const C4: Self = Self::new(3);
    /// MIDI channel 5
    pub const C5: Self = Self::new(4);
    /// MIDI channel 6
    pub const C6: Self = Self::new(5);
    /// MIDI channel 7
    pub const C7: Self = Self::new(6);
    /// MIDI channel 8
    pub const C8: Self = Self::new(7);
    /// MIDI channel 9
    pub const C9: Self = Self::new(8);
    /// MIDI channel 10
    pub const C10: Self = Self::new(9);
    /// MIDI channel 11
    pub const C11: Self = Self::new(10);
    /// MIDI channel 12
    pub const C12: Self = Self::new(11);
    /// MIDI channel 13
    pub const C13: Self = Self::new(12);
    /// MIDI channel 14
    pub const C14: Self = Self::new(13);
    /// MIDI channel 15
    pub const C15: Self = Self::new(14);
    /// MIDI channel 16
    pub const C16: Self = Self::new(15);

    /// The minimum MIDI channel
    pub const MIN: Self = Self::C1;
    /// The maximum MIDI channel
    pub const MAX: Self = Self::C16;
}

impl From<u8> for Channel {
    fn from(channel: u8) -> Self {
        debug_assert!(channel <= 15);
        Self::new(channel)
    }
}

impl From<Channel> for u8 {
    fn from(channel: Channel) -> u8 {
        channel.0
    }
}

/// A Midi controller number
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Control(u8);

impl Control {
    /// Create a new `Control`
    ///
    /// # Arguments
    /// * `val` - the control number value
    ///
    /// # Note
    /// * The `val` will be clamped so it is in the 0..127 valid range
    ///
    pub const fn new(val: u8) -> Self {
        Self(if val > 127 { 127 } else { val })
    }
}

impl From<u8> for Control {
    fn from(control: u8) -> Self {
        debug_assert!(control <= 127);
        Self::new(control)
    }
}

impl From<Control> for u8 {
    fn from(control: Control) -> u8 {
        control.0
    }
}

/// A Midi program number, these usually correspond to presets on Midi devices
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Program(u8);

impl Program {
    /// Create a new `Program`
    ///
    /// # Arguments
    /// * `val` - the program number value
    ///
    /// # Note
    /// * The `val` will be clamped so it is in the 0..127 valid range
    ///
    pub const fn new(val: u8) -> Self {
        Self(if val > 127 { 127 } else { val })
    }
}

impl From<u8> for Program {
    fn from(value: u8) -> Self {
        debug_assert!(value <= 127);
        Self::new(value)
    }
}

impl From<Program> for u8 {
    fn from(program: Program) -> u8 {
        program.0
    }
}

/// A 7 bit Midi data value stored in an unsigned 8 bit integer, the msb is always 0
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Value7(u8);

impl Value7 {
    /// Create a new `Value7`
    ///
    /// # Arguments
    /// * `val` - the value
    ///
    /// # Note
    /// * The `val` will be clamped so it is in the 0..127 valid range
    ///
    pub const fn new(val: u8) -> Self {
        Self(if val > 127 { 127 } else { val })
    }
}

impl From<u8> for Value7 {
    fn from(value: u8) -> Self {
        debug_assert!(value <= 127);
        Self::new(value)
    }
}

impl From<Value7> for u8 {
    fn from(value: Value7) -> u8 {
        value.0
    }
}

/// A 14 bit Midi value stored as two 7 bit Midi data values, where the msb is always 0 to signify
/// that this is a data value.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Value14(u8, u8);

impl Value14 {
    /// Create a new `Value14`
    ///
    /// # Arguments
    /// * `val` - the value
    ///
    /// # Note
    /// * The `val` will be clamped so it is in the 0..127 valid range
    ///
    pub const fn new(val: i16) -> Self {
        let value = (if val <= -8192i16 {
            -8192i16
        } else if val >= 8191i16 {
            8191i16
        } else {
            val
        } + 8192i16) as u16;
        Value14(((value & 0x3f80) >> 7) as u8, (value & 0x007f) as u8)
    }
}

impl From<(u8, u8)> for Value14 {
    fn from(value: (u8, u8)) -> Self {
        debug_assert!(value.0 <= 127);
        debug_assert!(value.1 <= 127);
        Self(value.0.min(127), value.1.min(127))
    }
}

impl From<Value14> for (u8, u8) {
    fn from(value: Value14) -> (u8, u8) {
        (value.0, value.1)
    }
}

impl From<u16> for Value14 {
    fn from(value: u16) -> Self {
        Self(((value & 0x3f80) >> 7) as u8, (value & 0x007f) as u8)
    }
}

impl From<Value14> for u16 {
    fn from(value: Value14) -> u16 {
        (value.0 as u16) * 128 + value.1 as u16
    }
}

///Convert from -8192i16..8191i16
impl From<i16> for Value14 {
    fn from(value: i16) -> Self {
        Self::new(value)
    }
}

///Convert into -8192i16..8191i16
impl From<Value14> for i16 {
    fn from(value: Value14) -> i16 {
        let v: u16 = value.into();
        (v as i16) - 8192i16
    }
}

///Convert from -1.0..1.0
impl From<f32> for Value14 {
    fn from(value: f32) -> Self {
        Self::from((value * if value > 0.0 { 8191.0 } else { 8192.0 }) as i16)
    }
}

///Convert into -1.0..1.0
impl From<Value14> for f32 {
    fn from(value: Value14) -> f32 {
        let v: i16 = value.into();
        let v = v as f32 / if v > 0 { 8191.0 } else { 8192.0 };
        v.clamp(-1.0, 1.0)
    }
}

/*
/// The SMPTE type used. This indicates the number of frames per second
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SmpteType {
    /// 24 frames per second
    Frames24,

    /// 25 frames per second
    Frames25,

    /// 29.97 frames per second
    DropFrame30,

    /// 30 frames per second
    Frames30,
}

/// The value of the quarter frame message, this message contains a message type and a value. Each
/// of these eight messages encodes a 4 bit part of the midi time code. As one of these is sent
/// every quarter frames, the complete midi time code is sent every two frames.
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum QuarterFrameType {
    /// Frame number low nibble
    FramesLS,

    /// Frame count high nibble
    FramesMS,

    /// Seconds low nibble
    SecondsLS,

    /// Seconds high nibble
    SecondsMS,

    /// Minutes low nibble
    MinutesLS,

    /// Minutes high nibble
    MinutesMS,

    /// Hours low nibble
    HoursLS,

    /// Combined hours high nibble and smpte type (frames per second)
    HoursMS,
}
*/

/// A MIDI Quarter Frame value, used for sync.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct QuarterFrame(u8);

impl QuarterFrame {
    /// Create a new `QuarterFrame`
    ///
    /// # Arguments
    /// * `val` - the value
    ///
    /// # Note
    /// * The `val` will be clamped so it is in the 0..127 valid range
    ///
    pub const fn new(val: u8) -> Self {
        Self(if val > 127 { 127 } else { val })
    }
}

/*
impl QuarterFrame {
    pub fn frame_type(&self) -> QuarterFrameType {
        unimplemented!()
    }

    pub fn value(&self) -> u8 {
        unimplemented!()
    }

    pub fn smpte_type(&self) -> SmpteType {
        unimplemented!()
    }
}
*/

impl From<u8> for QuarterFrame {
    fn from(value: u8) -> Self {
        debug_assert!(value <= 127);
        Self::new(value)
    }
}

impl From<QuarterFrame> for u8 {
    fn from(value: QuarterFrame) -> u8 {
        value.0
    }
}

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

    #[test]
    fn should_combine_7_bit_vals_into_14() {
        let val: Value14 = (0b01010101u8, 0b01010101u8).into();
        assert_eq!(0b0010101011010101u16, val.into());
    }

    #[test]
    fn should_split_14_bit_val_into_7() {
        let val: Value14 = 0b0011001100110011u16.into();
        assert_eq!((0b01100110u8, 0b00110011u8), val.into())
    }

    #[test]
    fn conversion_i16_14() {
        let val: Value14 = Value14::from(8191i16);
        assert_eq!((127, 127), val.into());
        assert_eq!(8191i16, val.into());
        assert_eq!(val, Value14::new(8191i16));

        //clamped
        let val: Value14 = Value14::from(8192i16);
        assert_eq!((127, 127), val.into());
        assert_eq!(8191i16, val.into());
        assert_eq!(val, Value14::new(8192i16));

        let val: Value14 = Value14::from(8190i16);
        assert_eq!((127, 126), val.into());
        assert_eq!(8190i16, val.into());

        let val: Value14 = Value14::from(-8192i16);
        assert_eq!((0, 0), val.into());
        assert_eq!(-8192i16, val.into());

        //clamped
        let val: Value14 = Value14::from(-8193i16);
        assert_eq!((0, 0), val.into());
        assert_eq!(-8192i16, val.into());

        let val: Value14 = Value14::from(0i16);
        assert_eq!((64, 0), val.into());
        assert_eq!(0i16, val.into());

        let val: Value14 = Value14::from(1i16);
        assert_eq!((64, 1), val.into());
        assert_eq!(1i16, val.into());
    }

    #[test]
    fn conversion_f32_14() {
        let val: Value14 = Value14::from(0.0f32);
        assert_eq!((64, 0), val.into());
        assert_eq!(0.0f32, val.into());

        let val: Value14 = Value14::from(1.0f32);
        assert_eq!((127, 127), val.into());
        assert_eq!(1.0f32, val.into());

        let val: Value14 = Value14::from(-1.0f32);
        assert_eq!((0, 0), val.into());
        assert_eq!(-1.0f32, val.into());
    }
}