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
/// MIDI channel number (0-15).
///
/// MIDI supports 16 channels, numbered 0 through 15. Each channel can
/// play different instruments or sounds independently.
///
/// # Examples
///
/// ```
/// # use klavier_core::channel::Channel;
/// let channel = Channel::new(0); // Channel 1 (0-indexed)
/// assert_eq!(channel.as_u8(), 0);
/// ```
#[derive(serde::Deserialize, serde::Serialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Channel(u8);
impl Channel {
/// Creates a new MIDI channel.
///
/// # Arguments
///
/// * `value` - The channel number (must be 0-15).
///
/// # Panics
///
/// Panics if the value is >= 16.
///
/// # Examples
///
/// ```
/// # use klavier_core::channel::Channel;
/// let channel = Channel::new(5);
/// assert_eq!(channel.as_u8(), 5);
/// ```
pub fn new(value: u8) -> Self {
if value < 16 {
Self(value)
} else {
panic!("Invalid channel value(={}). Should be < 16.", value);
}
}
/// Returns the channel number as a u8.
pub fn as_u8(self) -> u8 {
self.0
}
}