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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::prelude::*;
use core::fmt;
/// Identifies the velocity of a key press, or a key unpress, or an aftertouch.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Velocity(DataByte);
impl Velocity {
/// Creates a new velocity from the provided byte
///
/// Checks for correctness (leading 0 bit)
pub fn new<B, E>(rep: B) -> Result<Self, std::io::Error>
where
B: TryInto<DataByte, Error = E>,
E: Into<io::Error>,
{
rep.try_into().map(Self).map_err(Into::into)
}
/// Returns a max velocity
pub fn max() -> Self {
Self(DataByte::new_unchecked(127))
}
/// Returns a velocity of zero.
pub fn zero() -> Self {
Self(DataByte::new_unchecked(0))
}
/// Get a reference to the underlying byte
pub fn byte(&self) -> u8 {
self.0.0
}
/// Get the dynamic of the velocity...fortississississimo
pub fn dynamic(&self) -> Dynamic {
match self.byte() {
0 => Dynamic::off(),
1..16 => Dynamic::ppp(),
16..32 => Dynamic::pp(),
32..48 => Dynamic::p(),
48..64 => Dynamic::mp(),
64..80 => Dynamic::mf(),
80..96 => Dynamic::f(),
96..112 => Dynamic::ff(),
_ => Dynamic::fff(),
}
}
}
impl fmt::Display for Velocity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
/// The musical analog of the digital velocity
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Dynamic {
/// no sound
Off,
/// very quiet
Pianississimo,
/// pretty quiet
Pianissimo,
/// quiet
Piano,
/// kinda quiet
MezzoPiano,
/// kinda loud
MezzoForte,
/// loud
Forte,
/// pretty loud
Fortissimo,
/// very loud
Fortississimo,
}
impl Dynamic {
/// No sound
pub fn off() -> Self {
Self::Off
}
/// very quiet
pub fn ppp() -> Self {
Self::Pianississimo
}
/// pretty quiet
pub fn pp() -> Self {
Self::Pianissimo
}
/// quiet
pub fn p() -> Self {
Self::Piano
}
/// kinda quiet
pub fn mp() -> Self {
Self::MezzoPiano
}
/// kinda loud
pub fn mf() -> Self {
Self::MezzoForte
}
/// loud
pub fn f() -> Self {
Self::Forte
}
/// pretty loud
pub fn ff() -> Self {
Self::Fortissimo
}
/// very loud
pub fn fff() -> Self {
Self::Fortississimo
}
}