midi_toolkit/
notes.rs

1use crate::num::MIDINum;
2
3#[allow(clippy::len_without_is_empty)]
4pub trait MIDINote<T: MIDINum>: std::fmt::Debug {
5    fn start(&self) -> T;
6    fn start_mut(&mut self) -> &mut T;
7    fn len(&self) -> T;
8    fn len_mut(&mut self) -> &mut T;
9
10    fn key(&self) -> u8;
11    fn key_mut(&mut self) -> &mut u8;
12    #[inline(always)]
13    fn set_key(&mut self, key: u8) {
14        *self.key_mut() = key;
15    }
16
17    fn channel(&self) -> u8;
18    fn channel_mut(&mut self) -> &mut u8;
19    #[inline(always)]
20    fn set_channel(&mut self, channel: u8) {
21        *self.channel_mut() = channel;
22    }
23
24    fn velocity(&self) -> u8;
25    fn velocity_mut(&mut self) -> &mut u8;
26    #[inline(always)]
27    fn set_velocity(&mut self, velocity: u8) {
28        *self.velocity_mut() = velocity;
29    }
30
31    #[inline(always)]
32    fn end(&self) -> T {
33        self.start() + self.len()
34    }
35
36    /// Sets the note start and note length, keeping the end the same
37    #[inline(always)]
38    fn move_start(&mut self, start: T) {
39        let end = self.end();
40        self.set_start(start);
41        self.set_len(end - start);
42    }
43
44    /// Sets the note length based on a new end, keeping the start the same
45    #[inline(always)]
46    fn set_end(&mut self, end: T) {
47        self.set_len(end - self.start());
48    }
49
50    /// Sets the note start, keeping the length the same
51    #[inline(always)]
52    fn set_start(&mut self, start: T) {
53        *self.start_mut() = start;
54    }
55
56    /// Sets the note length, keeping the start the same
57    #[inline(always)]
58    fn set_len(&mut self, len: T) {
59        *self.len_mut() = len;
60    }
61}
62
63#[derive(Debug, Clone, PartialEq)]
64pub struct Note<T: MIDINum> {
65    pub start: T,
66    pub len: T,
67    pub key: u8,
68    pub channel: u8,
69    pub velocity: u8,
70}
71
72impl<T: MIDINum> MIDINote<T> for Note<T> {
73    #[inline(always)]
74    fn start(&self) -> T {
75        self.start
76    }
77
78    #[inline(always)]
79    fn start_mut(&mut self) -> &mut T {
80        &mut self.start
81    }
82
83    #[inline(always)]
84    fn len(&self) -> T {
85        self.len
86    }
87
88    #[inline(always)]
89    fn len_mut(&mut self) -> &mut T {
90        &mut self.len
91    }
92
93    #[inline(always)]
94    fn key(&self) -> u8 {
95        self.key
96    }
97
98    #[inline(always)]
99    fn key_mut(&mut self) -> &mut u8 {
100        &mut self.key
101    }
102
103    #[inline(always)]
104    fn channel(&self) -> u8 {
105        self.channel
106    }
107
108    #[inline(always)]
109    fn channel_mut(&mut self) -> &mut u8 {
110        &mut self.channel
111    }
112
113    #[inline(always)]
114    fn velocity(&self) -> u8 {
115        self.velocity
116    }
117
118    #[inline(always)]
119    fn velocity_mut(&mut self) -> &mut u8 {
120        &mut self.velocity
121    }
122}