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
use crate::parse_error::*;
use crate::util::*;
use alloc::vec::Vec;

/// Change the tunings of one or more notes, either real-time or not.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg) and [`UniversalRealTimeMsg`](crate::UniversalRealTimeMsg).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TuningNoteChange {
    /// Which tuning program is targeted, 0-127. See [`Parameter::TuningProgramSelect`](crate::Parameter::TuningProgramSelect).
    pub tuning_program_num: u8,
    /// Which tuning bank is targeted, 0-127. See [`Parameter::TuningBankSelect`](crate::Parameter::TuningBankSelect).
    pub tuning_bank_num: Option<u8>,
    /// At most 127 `(MIDI note number, Option<Tuning>)`` pairs.
    /// A `None` value represents "No change".
    pub tunings: Vec<(u8, Option<Tuning>)>,
}

impl TuningNoteChange {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        // The tuning_bank_num is pushed by the caller if needed
        push_u7(self.tuning_program_num, v);
        push_u7(self.tunings.len() as u8, v);
        for (note, tuning) in self.tunings.iter() {
            push_u7(*note, v);
            if let Some(tuning) = tuning {
                tuning.extend_midi(v);
            } else {
                // "No change"
                v.push(0x7F);
                v.push(0x7F);
                v.push(0x7F);
            }
        }
    }

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

/// Set the tunings of all 128 notes.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyBasedTuningDump {
    /// Which tuning program is targeted, 0-127. See [`Parameter::TuningProgramSelect`](crate::Parameter::TuningProgramSelect).
    pub tuning_program_num: u8,
    /// Which tuning bank is targeted, 0-127. See [`Parameter::TuningBankSelect`](crate::Parameter::TuningBankSelect).
    pub tuning_bank_num: Option<u8>,
    /// An exactly 16 character name
    pub name: [u8; 16],
    /// Should be exactly 128 Tunings with the index of each value = the MIDI note number being tuned.
    /// Excess values will be ignored. If fewer than 128 values are supplied, equal temperament
    /// will be applied to the remaining notes.
    /// A `None` value represents "No change".
    pub tunings: Vec<Option<Tuning>>,
}

impl KeyBasedTuningDump {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        if let Some(bank_num) = self.tuning_bank_num {
            v.push(to_u7(bank_num))
        }
        push_u7(self.tuning_program_num, v);
        for ch in self.name.iter() {
            v.push(*ch);
        }
        let mut i = 0;
        loop {
            if i >= 128 {
                break;
            }
            if let Some(tuning) = self.tunings.get(i) {
                if let Some(tuning) = tuning {
                    tuning.extend_midi(v);
                } else {
                    // "No change"
                    v.push(0x7F);
                    v.push(0x7F);
                    v.push(0x7F);
                }
            } else {
                // The equivalent of equal temperament tuning
                push_u7(i as u8, v);
                v.push(0);
                v.push(0);
            }
            i += 1;
        }
        v.push(0); // Checksum <- Will be written over by `SystemExclusiveMsg.extend_midi`
    }

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

/// Used to represent a tuning by [`TuningNoteChange`] and [`KeyBasedTuningDump`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Tuning {
    /// The semitone corresponding with the same MIDI note number, 0-127
    pub semitone: u8,
    /// Fraction of semitones above the `semitone`, in .0061-cent units.
    /// 0-16383
    pub fraction: u16,
}

impl Tuning {
    pub fn from_freq(freq: f32) -> Self {
        if freq < 8.17358 {
            Self {
                semitone: 0,
                fraction: 0,
            }
        } else if freq > 13289.73 {
            Self {
                semitone: 127,
                fraction: 16383,
            }
        } else {
            let (semitone, c) = freq_to_midi_note_cents(freq);
            Self {
                semitone: semitone as u8,
                fraction: cents_to_u14(c).min(0x3FFE),
            }
        }
    }

    fn extend_midi(&self, v: &mut Vec<u8>) {
        push_u7(self.semitone, v);
        let [msb, lsb] = to_u14(self.fraction);
        v.push(msb); // For some reason this is the opposite order of everything else???
        v.push(lsb);
    }
}

/// Set the tuning of all octaves for a tuning program/bank.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg).
///
/// As defined in MIDI Tuning Updated Specification (CA-020/CA-021/RP-020)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ScaleTuningDump1Byte {
    /// Which tuning program is targeted, 0-127. See [`Parameter::TuningProgramSelect`](crate::Parameter::TuningProgramSelect).
    pub tuning_program_num: u8,
    /// Which tuning bank is targeted, 0-127. See [`Parameter::TuningBankSelect`](crate::Parameter::TuningBankSelect).
    pub tuning_bank_num: u8,
    /// An exactly 16 character name.
    pub name: [u8; 16],
    /// 12 semitones of tuning adjustments repeated over all octaves, starting with C
    /// Each value represents that number of cents plus the equal temperament tuning,
    /// from -64 to 63 cents
    pub tuning: [i8; 12],
}

impl ScaleTuningDump1Byte {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        push_u7(self.tuning_bank_num, v);
        push_u7(self.tuning_program_num, v);
        for ch in self.name.iter() {
            v.push(*ch);
        }

        for t in self.tuning.iter() {
            v.push(i_to_u7(*t));
        }

        v.push(0); // Checksum <- Will be written over by `SystemExclusiveMsg.extend_midi`
    }

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

/// Set the high-res tuning of all octaves for a tuning program/bank.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg).
///
/// As defined in MIDI Tuning Updated Specification (CA-020/CA-021/RP-020)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ScaleTuningDump2Byte {
    /// Which tuning program is targeted, 0-127. See [`Parameter::TuningProgramSelect`](crate::Parameter::TuningProgramSelect).
    pub tuning_program_num: u8,
    /// Which tuning bank is targeted, 0-127. See [`Parameter::TuningBankSelect`](crate::Parameter::TuningBankSelect).
    pub tuning_bank_num: u8,
    /// An exactly 16 character name.
    pub name: [u8; 16],
    /// 12 semitones of tuning adjustments repeated over all octaves, starting with C
    /// Each value represents that fractional number of cents plus the equal temperament tuning,
    /// from -8192 to 8192 (steps of .012207 cents)
    pub tuning: [i16; 12],
}

impl ScaleTuningDump2Byte {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        push_u7(self.tuning_bank_num, v);
        push_u7(self.tuning_program_num, v);
        for ch in self.name.iter() {
            v.push(*ch);
        }

        for t in self.tuning.iter() {
            let [msb, lsb] = i_to_u14(*t);
            v.push(lsb);
            v.push(msb);
        }

        v.push(0); // Checksum <- Will be written over by `SystemExclusiveMsg.extend_midi`
    }

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

/// Set the tuning of all octaves for a set of channels.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg) and [`UniversalRealTimeMsg`](crate::UniversalRealTimeMsg).
///
/// As defined in MIDI Tuning Updated Specification (CA-020/CA-021/RP-020)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ScaleTuning1Byte {
    pub channels: ChannelBitMap,
    /// 12 semitones of tuning adjustments repeated over all octaves, starting with C
    /// Each value represents that number of cents plus the equal temperament tuning,
    /// from -64 to 63 cents
    pub tuning: [i8; 12],
}

impl ScaleTuning1Byte {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        self.channels.extend_midi(v);
        for t in self.tuning.iter() {
            v.push(i_to_u7(*t));
        }
    }

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

/// Set the high-res tuning of all octaves for a set of channels.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg) and [`UniversalRealTimeMsg`](crate::UniversalRealTimeMsg).
///
/// As defined in MIDI Tuning Updated Specification (CA-020/CA-021/RP-020)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ScaleTuning2Byte {
    pub channels: ChannelBitMap,
    /// 12 semitones of tuning adjustments repeated over all octaves, starting with C
    /// Each value represents that fractional number of cents plus the equal temperament tuning,
    /// from -8192 to 8192 (steps of .012207 cents)
    pub tuning: [i16; 12],
}

impl ScaleTuning2Byte {
    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        self.channels.extend_midi(v);
        for t in self.tuning.iter() {
            let [msb, lsb] = i_to_u14(*t);
            v.push(lsb);
            v.push(msb);
        }
    }

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

/// The set of channels to apply this tuning message to. Used by [`ScaleTuning1Byte`] and [`ScaleTuning2Byte`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct ChannelBitMap {
    pub channel_1: bool,
    pub channel_2: bool,
    pub channel_3: bool,
    pub channel_4: bool,
    pub channel_5: bool,
    pub channel_6: bool,
    pub channel_7: bool,
    pub channel_8: bool,
    pub channel_9: bool,
    pub channel_10: bool,
    pub channel_11: bool,
    pub channel_12: bool,
    pub channel_13: bool,
    pub channel_14: bool,
    pub channel_15: bool,
    pub channel_16: bool,
}

impl ChannelBitMap {
    /// All channels set
    pub fn all() -> Self {
        Self {
            channel_1: true,
            channel_2: true,
            channel_3: true,
            channel_4: true,
            channel_5: true,
            channel_6: true,
            channel_7: true,
            channel_8: true,
            channel_9: true,
            channel_10: true,
            channel_11: true,
            channel_12: true,
            channel_13: true,
            channel_14: true,
            channel_15: true,
            channel_16: true,
        }
    }

    /// No channels set
    pub fn none() -> Self {
        Self::default()
    }

    pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
        let mut byte1: u8 = 0;
        if self.channel_16 {
            byte1 += 1 << 1;
        }
        if self.channel_15 {
            byte1 += 1 << 0;
        }
        v.push(byte1);

        let mut byte2: u8 = 0;
        if self.channel_14 {
            byte2 += 1 << 6;
        }
        if self.channel_13 {
            byte2 += 1 << 5;
        }
        if self.channel_12 {
            byte2 += 1 << 4;
        }
        if self.channel_11 {
            byte2 += 1 << 3;
        }
        if self.channel_10 {
            byte2 += 1 << 2;
        }
        if self.channel_9 {
            byte2 += 1 << 1;
        }
        if self.channel_8 {
            byte2 += 1 << 0;
        }
        v.push(byte2);

        let mut byte3: u8 = 0;
        if self.channel_7 {
            byte3 += 1 << 6;
        }
        if self.channel_6 {
            byte3 += 1 << 5;
        }
        if self.channel_5 {
            byte3 += 1 << 4;
        }
        if self.channel_4 {
            byte3 += 1 << 3;
        }
        if self.channel_3 {
            byte3 += 1 << 2;
        }
        if self.channel_2 {
            byte3 += 1 << 1;
        }
        if self.channel_1 {
            byte3 += 1 << 0;
        }
        v.push(byte3);
    }

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

#[cfg(test)]
mod tests {
    use crate::*;
    use alloc::vec;
    use bstr::B;
    use core::convert::TryInto;

    #[test]
    fn serialize_tuning_note_change() {
        assert_eq!(
            MidiMsg::SystemExclusive {
                msg: SystemExclusiveMsg::UniversalRealTime {
                    device: DeviceID::AllCall,
                    msg: UniversalRealTimeMsg::TuningNoteChange(TuningNoteChange {
                        tuning_program_num: 5,
                        tuning_bank_num: None,
                        tunings: vec![
                            (
                                1,
                                Some(Tuning {
                                    semitone: 1,
                                    fraction: 255,
                                }),
                            ),
                            (
                                0x33,
                                Some(Tuning {
                                    semitone: 0x33,
                                    fraction: 511,
                                }),
                            ),
                            (0x45, None),
                            (0x78, Some(Tuning::from_freq(8372.0630)))
                        ],
                    }),
                },
            }
            .to_midi(),
            &[
                0xF0, 0x7F, 0x7F, 0x08, 0x02, 0x05, 4, // Number of changes
                0x01, 0x01, 0x01, 0x7f, // Tuning 1
                0x33, 0x33, 0x03, 0x7f, // Tuning 2
                0x45, 0x7f, 0x7f, 0x7f, // Tuning 3 (no change)
                // 0x78, 0x78, 0x00, 0x01, // Tuning 4, exact
                0x78, 0x78, 0x00, 0x02, // Tuning 4, micromath approximation
                0xF7,
            ]
        );
    }

    #[test]
    fn serialize_tuning_bulk_dump_reply() {
        let packet_msg = MidiMsg::SystemExclusive {
            msg: SystemExclusiveMsg::UniversalNonRealTime {
                device: DeviceID::AllCall,
                msg: UniversalNonRealTimeMsg::KeyBasedTuningDump(KeyBasedTuningDump {
                    tuning_program_num: 5,
                    tuning_bank_num: None,
                    name: B("A tuning program").try_into().unwrap(), // B creates a &[u8], try_into converts it into an array
                    tunings: vec![Some(Tuning {
                        semitone: 1,
                        fraction: 255,
                    })],
                }),
            },
        }
        .to_midi();

        assert_eq!(packet_msg.len(), 408);
        assert_eq!(
            &packet_msg[0..7],
            &[0xF0, 0x7E, 0x7F, 0x08, 0x01, 0x05, b"A"[0]]
        );

        assert_eq!(
            &packet_msg[22..31],
            &[
                0x01, 0x01, 0x7f, // Provided tuning
                0x01, 0x00, 0x00, // Default tuning
                0x02, 0x00, 0x00 // Default tuning
            ]
        );
    }
}