klavier_core/
channel.rs

1/// MIDI channel number (0-15).
2///
3/// MIDI supports 16 channels, numbered 0 through 15. Each channel can
4/// play different instruments or sounds independently.
5///
6/// # Examples
7///
8/// ```
9/// # use klavier_core::channel::Channel;
10/// let channel = Channel::new(0); // Channel 1 (0-indexed)
11/// assert_eq!(channel.as_u8(), 0);
12/// ```
13#[derive(serde::Deserialize, serde::Serialize)]
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
15pub struct Channel(u8);
16
17impl Channel {
18  /// Creates a new MIDI channel.
19  ///
20  /// # Arguments
21  ///
22  /// * `value` - The channel number (must be 0-15).
23  ///
24  /// # Panics
25  ///
26  /// Panics if the value is >= 16.
27  ///
28  /// # Examples
29  ///
30  /// ```
31  /// # use klavier_core::channel::Channel;
32  /// let channel = Channel::new(5);
33  /// assert_eq!(channel.as_u8(), 5);
34  /// ```
35  pub fn new(value: u8) -> Self {
36    if value < 16 {
37      Self(value)
38    } else {
39      panic!("Invalid channel value(={}). Should be < 16.", value);
40    }
41  }
42
43  /// Returns the channel number as a u8.
44  pub fn as_u8(self) -> u8 {
45    self.0
46  }
47}