midi_control/
note.rs

1//
2// (c) 2020 Hubert Figuière
3//
4// License: LGPL-3.0-or-later
5
6//! Utilities to handle MIDI notes.
7
8/// A Note value in Midi
9///
10/// MIDI Note are simply 0..127
11pub type MidiNote = u8;
12
13/// Return if the midi_note is an accidental.
14///
15/// This can be used to determine if the note is a black key
16/// on a piano style keyboard.
17pub fn midi_is_accident(midi_note: MidiNote) -> bool {
18    // C-1 is 0, C0 is 12, etc.
19    // A simple calculation of the modulo is enough.
20    matches!(midi_note % 12, 1 | 3 | 6 | 8 | 10)
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    pub fn test_midi_is_accident() {
29        assert!(!midi_is_accident(36));
30        assert!(midi_is_accident(37));
31        assert!(!midi_is_accident(38));
32        assert!(midi_is_accident(39));
33        assert!(!midi_is_accident(40));
34        assert!(!midi_is_accident(41));
35    }
36}