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
/// MIDI velocity value (0-127).
///
/// Velocity represents how hard a note is played, affecting its volume
/// and sometimes its timbre. In MIDI, velocity ranges from 0 (silent)
/// to 127 (maximum).
///
/// # Examples
///
/// ```
/// # use klavier_core::velocity::Velocity;
/// let soft = Velocity::new(40);
/// let loud = Velocity::new(100);
/// assert_eq!(soft.as_u8(), 40);
/// assert_eq!(loud.as_u8(), 100);
/// ```
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(from="Serialized")]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct Velocity(u8);
impl Default for Velocity {
fn default() -> Self {
Self(64)
}
}
/// Minimum velocity value (0).
pub const MIN_VALUE: u8 = 0;
/// Maximum velocity value (127).
pub const MAX_VALUE: u8 = 127;
/// Minimum velocity constant.
pub const MIN: Velocity = Velocity(0);
/// Maximum velocity constant.
pub const MAX: Velocity = Velocity(127);
#[derive(serde::Deserialize)]
struct Serialized(u8);
impl From<Serialized> for Velocity {
fn from(s: Serialized) -> Self {
Self::new(s.0)
}
}
impl Velocity {
/// Creates a new velocity value.
///
/// Values greater than 127 are clamped to 127.
///
/// # Arguments
///
/// * `value` - The velocity value (0-127, values > 127 are clamped).
///
/// # Examples
///
/// ```
/// # use klavier_core::velocity::Velocity;
/// let v1 = Velocity::new(64);
/// assert_eq!(v1.as_u8(), 64);
///
/// let v2 = Velocity::new(200); // Clamped to 127
/// assert_eq!(v2.as_u8(), 127);
/// ```
#[inline]
pub fn new(value: u8) -> Self {
if MAX_VALUE < value { MAX } else { Velocity(value) }
}
/// Returns the velocity value as a u8.
#[inline]
pub fn as_u8(self) -> u8 {
self.0
}
}
impl std::fmt::Display for Velocity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}