tinyviolin 0.3.0

Small real-time-safe synthesized instruments
Documentation
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
//! Fixed-capacity MIDI control for [`crate::Synth`].
//!
//! The wrapper accepts self-contained MIDI 1.0 messages. Running status, `SysEx`,
//! and MIDI 2.0 UMP packets are intentionally outside its scope.

use crate::state::{Reader, push_f32, push_u16, push_u32};
use crate::{Event, Instrument, Preset, ProcessError, StateError, Synth, VoiceId};

const MIDI_STATE_MAGIC: &[u8; 4] = b"TVMS";
const MIDI_STATE_VERSION: u16 = 1;

/// Pitch-bend range used for MIDI pitch-wheel messages, in semitones.
pub const PITCH_BEND_RANGE_SEMITONES: f32 = 2.0;

/// Maximum accepted storage size for one self-contained MIDI 1.0 message.
pub const MAX_MESSAGE_BYTES: usize = 4;

/// A self-contained MIDI message backed by at most four bytes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MidiMessage {
    bytes: [u8; MAX_MESSAGE_BYTES],
    len: u8,
}

impl MidiMessage {
    /// Copy a message of no more than four bytes into fixed storage.
    ///
    /// # Errors
    ///
    /// Returns [`MidiError::InvalidLength`] for an empty slice or one longer
    /// than [`MAX_MESSAGE_BYTES`]. Message syntax is checked on dispatch.
    pub fn new(bytes: &[u8]) -> Result<Self, MidiError> {
        if bytes.is_empty() || bytes.len() > MAX_MESSAGE_BYTES {
            return Err(MidiError::InvalidLength);
        }
        let mut storage = [0; MAX_MESSAGE_BYTES];
        storage[..bytes.len()].copy_from_slice(bytes);
        let len = match bytes.len() {
            1 => 1,
            2 => 2,
            3 => 3,
            4 => 4,
            _ => return Err(MidiError::InvalidLength),
        };
        Ok(Self {
            bytes: storage,
            len,
        })
    }

    /// Return the message bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes[..usize::from(self.len)]
    }
}

/// Pitch assigned to one MIDI mapping layer.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MidiPitch {
    /// Standard equal temperament, where note 69 is 440 Hz.
    Note,
    /// Ignore note number and use this positive finite frequency in hertz.
    Fixed(f32),
}

/// One instrument layer triggered by a MIDI channel/note mapping.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MidiLayer {
    /// Instrument to trigger.
    pub instrument: Instrument,
    /// Pitch derivation for the triggered voice.
    pub pitch: MidiPitch,
    /// Linear gain multiplied by MIDI velocity, in `0.0..=1.0`.
    pub gain: f32,
}

/// A MIDI message positioned within an output block.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TimedMidiMessage {
    /// Sample at which to apply the message; block length means block end.
    pub sample_offset: usize,
    /// Fixed-size message.
    pub message: MidiMessage,
}

impl TimedMidiMessage {
    /// Construct a timed message.
    #[must_use]
    pub const fn new(sample_offset: usize, message: MidiMessage) -> Self {
        Self {
            sample_offset,
            message,
        }
    }
}

/// MIDI message, mapping, or timed-stream error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MidiError {
    /// Message storage length was zero or greater than four.
    InvalidLength,
    /// A status, data byte, or status-specific length was malformed.
    MalformedMessage,
    /// The status is well-formed but outside the supported subset.
    UnsupportedMessage,
    /// Channel was outside `0..16`.
    InvalidChannel,
    /// Note was outside `0..128`.
    InvalidNote,
    /// Layer index exceeded the wrapper's fixed layer count, or it is zero.
    InvalidLayer,
    /// Layer gain or fixed frequency was invalid.
    InvalidLayerSettings,
    /// Timed messages were unordered or outside the output block.
    InvalidTiming,
    /// The underlying synthesizer rejected configuration.
    Synth,
    /// No currently available preset has the requested stable ID.
    UnknownPreset,
}

impl core::fmt::Display for MidiError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            Self::InvalidLength => "MIDI message length must be in 1..=4",
            Self::MalformedMessage => "malformed MIDI message",
            Self::UnsupportedMessage => "unsupported MIDI message",
            Self::InvalidChannel => "MIDI channel must be in 0..16",
            Self::InvalidNote => "MIDI note must be in 0..128",
            Self::InvalidLayer => "MIDI layer capacity or index is invalid",
            Self::InvalidLayerSettings => "MIDI layer frequency or gain is invalid",
            Self::InvalidTiming => "timed MIDI messages are unordered or outside the block",
            Self::Synth => "synthesizer rejected MIDI configuration",
            Self::UnknownPreset => "unknown preset ID",
        })
    }
}

impl std::error::Error for MidiError {}

impl From<ProcessError> for MidiError {
    fn from(_: ProcessError) -> Self {
        Self::Synth
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct MidiMapping<const LAYERS: usize> {
    layers: [Option<MidiLayer>; LAYERS],
}

impl<const LAYERS: usize> MidiMapping<LAYERS> {
    const EMPTY: Self = Self {
        layers: [None; LAYERS],
    };
}

/// A synthesizer controlled through fixed channel/note MIDI mappings.
///
/// `VOICES` fixes polyphony and `LAYERS` fixes the number of instruments one
/// channel/note pair can trigger. Mapping mutation is a setup operation; message
/// dispatch and processing neither allocate nor lock.
pub struct MidiSynth<const VOICES: usize = 32, const LAYERS: usize = 2> {
    synth: Synth<VOICES>,
    mappings: [[MidiMapping<LAYERS>; 128]; 16],
    pitch_bend_semitones: [f32; 16],
    modulation: [f32; 16],
    selected_preset: Option<Preset>,
}

impl<const VOICES: usize, const LAYERS: usize> MidiSynth<VOICES, LAYERS> {
    /// Create an engine with empty mappings.
    ///
    /// # Errors
    ///
    /// Returns [`MidiError::InvalidLayer`] if `LAYERS` is zero, or
    /// [`MidiError::Synth`] if the sample rate or voice count is invalid.
    pub fn new(sample_rate: f32) -> Result<Self, MidiError> {
        if LAYERS == 0 {
            return Err(MidiError::InvalidLayer);
        }
        Self::from_synth(Synth::new(sample_rate)?)
    }

    pub(crate) fn from_synth(synth: Synth<VOICES>) -> Result<Self, MidiError> {
        if LAYERS == 0 {
            return Err(MidiError::InvalidLayer);
        }
        Ok(Self {
            synth,
            mappings: [[MidiMapping::EMPTY; 128]; 16],
            pitch_bend_semitones: [0.0; 16],
            modulation: [0.0; 16],
            selected_preset: None,
        })
    }

    /// Return the underlying engine for read-only inspection.
    #[must_use]
    pub const fn engine(&self) -> &Synth<VOICES> {
        &self.synth
    }

    pub(crate) const fn engine_mut(&mut self) -> &mut Synth<VOICES> {
        &mut self.synth
    }

    /// Return every preset available in this library version.
    #[must_use]
    pub const fn available_presets(&self) -> &'static [Preset] {
        Preset::available()
    }

    /// Return the selected built-in preset, or `None` after manual mapping.
    #[must_use]
    pub const fn selected_preset(&self) -> Option<Preset> {
        self.selected_preset
    }

    /// Replace all channel/note mappings with a built-in preset.
    ///
    /// The preset is applied to all 16 MIDI channels using each instrument's
    /// perceived-loudness-calibrated [`Instrument::default_gain`]. Additional
    /// mapping layers are cleared. This setup operation performs bounded
    /// fixed-storage writes.
    pub fn select_preset(&mut self, preset: Preset) {
        for channel in &mut self.mappings {
            for (note, mapping) in (0_u8..=127).zip(channel) {
                *mapping = MidiMapping::EMPTY;
                let instrument = preset.instrument(note);
                mapping.layers[0] = Some(MidiLayer {
                    instrument,
                    pitch: if preset.uses_midi_pitch() {
                        MidiPitch::Note
                    } else {
                        MidiPitch::Fixed(percussion_frequency(instrument))
                    },
                    gain: instrument.default_gain(),
                });
            }
        }
        self.selected_preset = Some(preset);
    }

    /// Select a built-in preset by its stable runtime ID.
    ///
    /// # Errors
    ///
    /// Returns [`MidiError::UnknownPreset`] if the ID is not advertised by
    /// [`Self::available_presets`].
    pub fn select_preset_by_id(&mut self, id: &str) -> Result<(), MidiError> {
        let preset = Preset::from_id(id).ok_or(MidiError::UnknownPreset)?;
        self.select_preset(preset);
        Ok(())
    }

    /// Immediately clear voice and oscillator state, center pitch bend, and
    /// turn modulation off while preserving mappings.
    pub fn reset_dsp(&mut self) {
        self.synth.reset_dsp();
        self.pitch_bend_semitones.fill(0.0);
        self.modulation.fill(0.0);
    }

    /// Alias for [`Self::reset_dsp`] named after the conventional host action.
    pub fn panic(&mut self) {
        self.reset_dsp();
    }

    /// Serialize MIDI mappings and preset selection for host session storage.
    ///
    /// Voice, oscillator, and other DSP state is intentionally omitted. This
    /// setup operation allocates the returned byte vector.
    #[must_use]
    pub fn serialize_state(&self) -> Vec<u8> {
        let mut output = Vec::new();
        output.extend_from_slice(MIDI_STATE_MAGIC);
        push_u16(&mut output, MIDI_STATE_VERSION);
        push_u32(&mut output, u32::try_from(LAYERS).unwrap_or(u32::MAX));
        if let Some(preset) = self.selected_preset {
            let id = preset.id().as_bytes();
            output.push(u8::try_from(id.len()).unwrap_or(u8::MAX));
            output.extend_from_slice(id);
        } else {
            output.push(0);
        }
        for channel in &self.mappings {
            for mapping in channel {
                for layer in mapping.layers {
                    match layer {
                        None => output.push(0),
                        Some(layer) => {
                            output.push(1);
                            output.push(instrument_code(layer.instrument));
                            match layer.pitch {
                                MidiPitch::Note => output.push(0),
                                MidiPitch::Fixed(frequency) => {
                                    output.push(1);
                                    push_f32(&mut output, frequency);
                                }
                            }
                            push_f32(&mut output, layer.gain);
                        }
                    }
                }
            }
        }
        output
    }

    /// Load MIDI configuration previously returned by [`Self::serialize_state`].
    ///
    /// Loading is transactional: malformed or incompatible data leaves mappings
    /// unchanged. Existing voices and oscillator state are intentionally left
    /// untouched; call [`Self::reset_dsp`] separately when a panic is desired.
    ///
    /// # Errors
    ///
    /// Returns a [`StateError`] for malformed, unsupported, incompatible, or
    /// invalid configuration data.
    pub fn load_state(&mut self, state: &[u8]) -> Result<(), StateError> {
        let mut reader = Reader::new(state);
        if reader.read_exact(MIDI_STATE_MAGIC.len())? != MIDI_STATE_MAGIC {
            return Err(StateError::InvalidData);
        }
        if reader.u16()? != MIDI_STATE_VERSION {
            return Err(StateError::UnsupportedVersion);
        }
        let layers = usize::try_from(reader.u32()?).map_err(|_| StateError::InvalidData)?;
        if layers != LAYERS {
            return Err(StateError::IncompatibleMidiLayers);
        }
        let preset_id_len = usize::from(reader.u8()?);
        let selected_preset = if preset_id_len == 0 {
            None
        } else {
            let id = core::str::from_utf8(reader.read_exact(preset_id_len)?)
                .map_err(|_| StateError::InvalidData)?;
            Preset::from_id(id)
        };

        let mut mappings = [[MidiMapping::EMPTY; 128]; 16];
        for channel in &mut mappings {
            for mapping in channel {
                for layer in &mut mapping.layers {
                    *layer = match reader.u8()? {
                        0 => None,
                        1 => {
                            let instrument = instrument_from_code(reader.u8()?)
                                .ok_or(StateError::InvalidConfiguration)?;
                            let pitch = match reader.u8()? {
                                0 => MidiPitch::Note,
                                1 => MidiPitch::Fixed(reader.f32()?),
                                _ => return Err(StateError::InvalidConfiguration),
                            };
                            let value = MidiLayer {
                                instrument,
                                pitch,
                                gain: reader.f32()?,
                            };
                            validate_layer(value).map_err(|_| StateError::InvalidConfiguration)?;
                            Some(value)
                        }
                        _ => return Err(StateError::InvalidConfiguration),
                    };
                }
            }
        }
        if !reader.is_finished() {
            return Err(StateError::InvalidData);
        }

        self.mappings = mappings;
        self.selected_preset = selected_preset;
        Ok(())
    }

    /// Set one channel/note mapping layer.
    ///
    /// This setup method is not intended for an audio callback.
    ///
    /// # Errors
    ///
    /// Returns a [`MidiError`] for an out-of-range channel, note, or layer, or
    /// for a non-finite/out-of-range gain or invalid fixed frequency.
    pub fn set_layer(
        &mut self,
        channel: u8,
        note: u8,
        layer_index: usize,
        layer: MidiLayer,
    ) -> Result<(), MidiError> {
        let slot = mapping_slot(channel, note)?;
        if layer_index >= LAYERS {
            return Err(MidiError::InvalidLayer);
        }
        validate_layer(layer)?;
        self.mappings[slot.0][slot.1].layers[layer_index] = Some(layer);
        self.selected_preset = None;
        Ok(())
    }

    /// Apply the same layer to all 128 notes on one channel.
    ///
    /// This setup convenience performs bounded fixed-storage writes.
    ///
    /// # Errors
    ///
    /// Returns a [`MidiError`] for an invalid channel, layer index, or settings.
    pub fn set_channel_layer(
        &mut self,
        channel: u8,
        layer_index: usize,
        layer: MidiLayer,
    ) -> Result<(), MidiError> {
        if channel >= 16 {
            return Err(MidiError::InvalidChannel);
        }
        if layer_index >= LAYERS {
            return Err(MidiError::InvalidLayer);
        }
        validate_layer(layer)?;
        for mapping in &mut self.mappings[usize::from(channel)] {
            mapping.layers[layer_index] = Some(layer);
        }
        self.selected_preset = None;
        Ok(())
    }

    /// Remove one mapping layer.
    ///
    /// # Errors
    ///
    /// Returns a [`MidiError`] for an out-of-range channel, note, or layer.
    pub fn clear_layer(
        &mut self,
        channel: u8,
        note: u8,
        layer_index: usize,
    ) -> Result<(), MidiError> {
        let slot = mapping_slot(channel, note)?;
        if layer_index >= LAYERS {
            return Err(MidiError::InvalidLayer);
        }
        self.mappings[slot.0][slot.1].layers[layer_index] = None;
        self.selected_preset = None;
        Ok(())
    }

    /// Parse and apply one MIDI message immediately.
    ///
    /// This callback-safe method performs no allocation or locking.
    ///
    /// # Errors
    ///
    /// Returns [`MidiError::MalformedMessage`] or
    /// [`MidiError::UnsupportedMessage`] when parsing fails.
    pub fn dispatch(&mut self, message: MidiMessage) -> Result<(), MidiError> {
        let parsed = parse(message)?;
        self.dispatch_parsed(parsed);
        Ok(())
    }

    pub(crate) fn dispatch_validated(&mut self, message: MidiMessage) {
        if let Ok(parsed) = parse(message) {
            self.dispatch_parsed(parsed);
        }
    }

    /// Fill a mono block while applying ordered MIDI messages at sample offsets.
    ///
    /// The complete stream is validated before output or engine state changes.
    /// This callback-safe method performs no allocation or locking.
    ///
    /// # Errors
    ///
    /// Returns [`MidiError::InvalidTiming`] for unordered/out-of-block offsets,
    /// or a parsing error for malformed or unsupported messages.
    pub fn process(
        &mut self,
        output: &mut [f32],
        messages: &[TimedMidiMessage],
    ) -> Result<(), MidiError> {
        validate_timed_messages(messages, output.len())?;
        let mut cursor = 0;
        for timed in messages {
            self.synth.render(&mut output[cursor..timed.sample_offset]);
            // Parsing cannot fail after the complete pre-validation pass.
            if let Ok(parsed) = parse(timed.message) {
                self.dispatch_parsed(parsed);
            }
            cursor = timed.sample_offset;
        }
        self.synth.render(&mut output[cursor..]);
        Ok(())
    }

    fn dispatch_parsed(&mut self, message: ParsedMessage) {
        match message {
            ParsedMessage::NoteOn {
                channel,
                note,
                velocity,
            } => {
                let mapping = self.mappings[usize::from(channel)][usize::from(note)];
                for (index, layer) in mapping.layers.into_iter().enumerate() {
                    if let Some(layer) = layer {
                        let frequency_hz = match layer.pitch {
                            MidiPitch::Note => midi_frequency(note),
                            MidiPitch::Fixed(frequency) => frequency,
                        };
                        let id = midi_voice_id(channel, note, index);
                        self.synth.dispatch_validated(Event::NoteOn {
                            id,
                            instrument: layer.instrument,
                            frequency_hz,
                            gain: layer.gain * (f32::from(velocity) / 127.0),
                        });
                        self.synth.assign_control_group(
                            id,
                            channel,
                            self.pitch_bend_semitones[usize::from(channel)],
                            self.modulation[usize::from(channel)],
                        );
                    }
                }
            }
            ParsedMessage::NoteOff { channel, note } => {
                self.visit_note_ids(channel, note, false);
            }
            ParsedMessage::PitchBend { channel, value } => {
                let semitones = pitch_bend_semitones(value);
                self.pitch_bend_semitones[usize::from(channel)] = semitones;
                self.synth.set_group_pitch_bend(channel, semitones);
            }
            ParsedMessage::ModWheel { channel, value } => {
                let amount = f32::from(value) / 127.0;
                self.modulation[usize::from(channel)] = amount;
                self.synth.set_group_modulation(channel, amount);
            }
            ParsedMessage::AllNotesOff(channel) => {
                for note in 0_u8..=127 {
                    self.visit_note_ids(channel, note, false);
                }
            }
            ParsedMessage::AllSoundOff(channel) => {
                for note in 0_u8..=127 {
                    self.visit_note_ids(channel, note, true);
                }
            }
        }
    }

    fn visit_note_ids(&mut self, channel: u8, note: u8, stop: bool) {
        for index in 0..LAYERS {
            let id = midi_voice_id(channel, note, index);
            if stop {
                self.synth.stop_id(id);
            } else {
                self.synth.release_id(id);
            }
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ParsedMessage {
    NoteOn { channel: u8, note: u8, velocity: u8 },
    NoteOff { channel: u8, note: u8 },
    PitchBend { channel: u8, value: u16 },
    ModWheel { channel: u8, value: u8 },
    AllNotesOff(u8),
    AllSoundOff(u8),
}

fn parse(message: MidiMessage) -> Result<ParsedMessage, MidiError> {
    let bytes = message.as_bytes();
    let status = bytes[0];
    if status < 0x80 {
        return Err(MidiError::MalformedMessage);
    }
    let kind = status & 0xf0;
    if !matches!(kind, 0x80 | 0x90 | 0xb0 | 0xe0) {
        return Err(MidiError::UnsupportedMessage);
    }
    if bytes.len() != 3 || bytes[1] >= 0x80 || bytes[2] >= 0x80 {
        return Err(MidiError::MalformedMessage);
    }
    let channel = status & 0x0f;
    match kind {
        0x80 => Ok(ParsedMessage::NoteOff {
            channel,
            note: bytes[1],
        }),
        0x90 if bytes[2] == 0 => Ok(ParsedMessage::NoteOff {
            channel,
            note: bytes[1],
        }),
        0x90 => Ok(ParsedMessage::NoteOn {
            channel,
            note: bytes[1],
            velocity: bytes[2],
        }),
        0xb0 if bytes[1] == 1 => Ok(ParsedMessage::ModWheel {
            channel,
            value: bytes[2],
        }),
        0xb0 if bytes[1] == 120 => Ok(ParsedMessage::AllSoundOff(channel)),
        0xb0 if bytes[1] == 123 => Ok(ParsedMessage::AllNotesOff(channel)),
        0xe0 => Ok(ParsedMessage::PitchBend {
            channel,
            value: u16::from(bytes[1]) | (u16::from(bytes[2]) << 7),
        }),
        _ => Err(MidiError::UnsupportedMessage),
    }
}

pub(crate) fn validate_timed_messages(
    messages: &[TimedMidiMessage],
    output_len: usize,
) -> Result<(), MidiError> {
    let mut previous = 0;
    for timed in messages {
        if timed.sample_offset > output_len || timed.sample_offset < previous {
            return Err(MidiError::InvalidTiming);
        }
        parse(timed.message)?;
        previous = timed.sample_offset;
    }
    Ok(())
}

fn mapping_slot(channel: u8, note: u8) -> Result<(usize, usize), MidiError> {
    if channel >= 16 {
        return Err(MidiError::InvalidChannel);
    }
    if note >= 128 {
        return Err(MidiError::InvalidNote);
    }
    Ok((usize::from(channel), usize::from(note)))
}

fn validate_layer(layer: MidiLayer) -> Result<(), MidiError> {
    if !layer.gain.is_finite() || !(0.0..=1.0).contains(&layer.gain) {
        return Err(MidiError::InvalidLayerSettings);
    }
    if let MidiPitch::Fixed(frequency) = layer.pitch {
        if !frequency.is_finite() || frequency <= 0.0 {
            return Err(MidiError::InvalidLayerSettings);
        }
    }
    Ok(())
}

fn percussion_frequency(instrument: Instrument) -> f32 {
    match instrument {
        Instrument::BassDrum => 60.0,
        Instrument::Tom => 130.0,
        Instrument::Snare => 180.0,
        Instrument::HiHat => 6_000.0,
        _ => 440.0,
    }
}

const fn instrument_code(instrument: Instrument) -> u8 {
    match instrument {
        Instrument::Sine => 0,
        Instrument::Square => 1,
        Instrument::Triangle => 2,
        Instrument::Bass => 3,
        Instrument::Pad => 4,
        Instrument::Lead => 5,
        Instrument::BassDrum => 6,
        Instrument::Tom => 7,
        Instrument::Snare => 8,
        Instrument::HiHat => 9,
        Instrument::Pluck => 10,
    }
}

const fn instrument_from_code(code: u8) -> Option<Instrument> {
    match code {
        0 => Some(Instrument::Sine),
        1 => Some(Instrument::Square),
        2 => Some(Instrument::Triangle),
        3 => Some(Instrument::Bass),
        4 => Some(Instrument::Pad),
        5 => Some(Instrument::Lead),
        6 => Some(Instrument::BassDrum),
        7 => Some(Instrument::Tom),
        8 => Some(Instrument::Snare),
        9 => Some(Instrument::HiHat),
        10 => Some(Instrument::Pluck),
        _ => None,
    }
}

fn pitch_bend_semitones(value: u16) -> f32 {
    let centered = f32::from(value) - 8_192.0;
    let divisor = if value < 8_192 { 8_192.0 } else { 8_191.0 };
    (centered / divisor) * PITCH_BEND_RANGE_SEMITONES
}

fn midi_frequency(note: u8) -> f32 {
    440.0 * 2.0_f32.powf((f32::from(note) - 69.0) / 12.0)
}

fn midi_voice_id(channel: u8, note: u8, layer_index: usize) -> VoiceId {
    let layer = u64::try_from(layer_index).unwrap_or(u64::MAX);
    VoiceId((1_u64 << 63) | (u64::from(channel) << 56) | (u64::from(note) << 48) | layer)
}

#[cfg(test)]
mod tests {
    use super::{
        MidiError, MidiMessage, MidiPitch, MidiSynth, PITCH_BEND_RANGE_SEMITONES, ParsedMessage,
        midi_frequency, parse, pitch_bend_semitones,
    };
    use crate::{Instrument, Preset};

    fn message(bytes: &[u8]) -> MidiMessage {
        MidiMessage::new(bytes).unwrap()
    }

    #[test]
    fn parses_supported_channel_messages() {
        for channel in 0..16 {
            assert_eq!(
                parse(message(&[0x90 | channel, 127, 64])),
                Ok(ParsedMessage::NoteOn {
                    channel,
                    note: 127,
                    velocity: 64
                })
            );
            assert_eq!(
                parse(message(&[0x80 | channel, 0, 0])),
                Ok(ParsedMessage::NoteOff { channel, note: 0 })
            );
            assert_eq!(
                parse(message(&[0x90 | channel, 42, 0])),
                Ok(ParsedMessage::NoteOff { channel, note: 42 })
            );
            assert_eq!(
                parse(message(&[0xe0 | channel, 0, 64])),
                Ok(ParsedMessage::PitchBend {
                    channel,
                    value: 8_192
                })
            );
            assert_eq!(
                parse(message(&[0xb0 | channel, 1, 127])),
                Ok(ParsedMessage::ModWheel {
                    channel,
                    value: 127
                })
            );
            assert_eq!(
                parse(message(&[0xb0 | channel, 123, 0])),
                Ok(ParsedMessage::AllNotesOff(channel))
            );
            assert_eq!(
                parse(message(&[0xb0 | channel, 120, 0])),
                Ok(ParsedMessage::AllSoundOff(channel))
            );
        }
    }

    #[test]
    fn rejects_bad_lengths_data_and_statuses() {
        assert_eq!(
            parse(message(&[0x90, 60])),
            Err(MidiError::MalformedMessage)
        );
        assert_eq!(
            parse(message(&[0x90, 60, 1, 2])),
            Err(MidiError::MalformedMessage)
        );
        assert_eq!(
            parse(message(&[0x90, 0x80, 1])),
            Err(MidiError::MalformedMessage)
        );
        assert_eq!(
            parse(message(&[0x70, 60, 1])),
            Err(MidiError::MalformedMessage)
        );
        assert_eq!(
            parse(message(&[0xd0, 0, 0])),
            Err(MidiError::UnsupportedMessage)
        );
        assert_eq!(
            parse(message(&[0xb0, 2, 0])),
            Err(MidiError::UnsupportedMessage)
        );
    }

    #[test]
    fn equal_tempered_pitch_and_bend_have_expected_references() {
        assert!((midi_frequency(69) - 440.0).abs() < f32::EPSILON);
        assert!((midi_frequency(81) - 880.0).abs() < 0.001);
        assert!((pitch_bend_semitones(0) + PITCH_BEND_RANGE_SEMITONES).abs() < f32::EPSILON);
        assert!(pitch_bend_semitones(8_192).abs() < f32::EPSILON);
        assert!((pitch_bend_semitones(16_383) - PITCH_BEND_RANGE_SEMITONES).abs() < f32::EPSILON);
    }

    #[test]
    fn percussion_preset_populates_one_synth_on_all_keys_and_channels() {
        let mut midi = MidiSynth::<8, 2>::new(48_000.0).unwrap();
        midi.select_preset(Preset::PercussionKit);

        for channel in &midi.mappings {
            for mapping in channel {
                let layer = mapping.layers[0].unwrap();
                assert!((layer.gain - layer.instrument.default_gain()).abs() < f32::EPSILON);
                assert!(mapping.layers[1].is_none());
            }
        }
        let channel = &midi.mappings[9];
        assert_eq!(
            channel[35].layers[0].unwrap().instrument,
            Instrument::BassDrum
        );
        assert_eq!(channel[38].layers[0].unwrap().instrument, Instrument::Snare);
        assert_eq!(channel[41].layers[0].unwrap().instrument, Instrument::Tom);
        assert_eq!(channel[42].layers[0].unwrap().instrument, Instrument::HiHat);
        assert_eq!(channel[44].layers[0].unwrap().instrument, Instrument::HiHat);
        assert_eq!(channel[46].layers[0].unwrap().instrument, Instrument::HiHat);
        assert_eq!(channel[49].layers[0].unwrap().instrument, Instrument::Tom);
        assert_eq!(
            channel[42].layers[0].unwrap().pitch,
            MidiPitch::Fixed(6_000.0)
        );
    }
}