-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote_event.rs
More file actions
123 lines (107 loc) · 3.02 KB
/
Copy pathnote_event.rs
File metadata and controls
123 lines (107 loc) · 3.02 KB
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
use std::{
error::Error,
fmt::{Display, Write},
};
use crate::project::event_command::NoteCommand;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Note(u8);
impl Note {
pub fn new(value: u8) -> Result<Note, u8> {
if value > 199 {
Err(value)
} else {
Ok(Note(value))
}
}
pub const fn get_octave(self) -> u8 {
self.0 / 12
}
pub const fn get_note_name(self) -> &'static str {
match self.0 % 12 {
0 => "C",
1 => "C#",
2 => "D",
3 => "D#",
4 => "E",
5 => "F",
6 => "F#",
7 => "G",
8 => "G#",
9 => "A",
10 => "A#",
11 => "B",
_ => panic!(),
}
}
pub fn get_frequency(self) -> f32 {
// taken from https://en.wikipedia.org/wiki/MIDI_tuning_standard
440. * ((f32::from(self.0) - 69.) / 12.).exp2()
}
pub const fn get(self) -> u8 {
self.0
}
}
impl Display for Note {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.get_note_name())?;
f.write_char('-')?;
self.get_octave().fmt(f)?;
Ok(())
}
}
impl Default for Note {
fn default() -> Self {
Self(60) // C-5
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoteEvent {
pub note: Note,
pub sample_instr: u8,
pub vol: VolumeEffect,
pub command: NoteCommand,
}
#[derive(Debug, Clone, Copy, Default)]
pub enum VolumeEffect {
FineVolSlideUp(u8),
FineVolSlideDown(u8),
VolSlideUp(u8),
VolSlideDown(u8),
PitchSlideUp(u8),
PitchSlideDown(u8),
SlideToNoteWithSpeed(u8),
VibratoWithSpeed(u8),
Volume(u8),
Panning(u8),
/// Uses Instr / Sample Default Volume
#[default]
None,
}
#[derive(Debug, Clone, Copy)]
pub struct InvalidVolumeEffect;
impl Display for InvalidVolumeEffect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid Volume Effect")
}
}
impl Error for InvalidVolumeEffect {}
impl TryFrom<u8> for VolumeEffect {
type Error = InvalidVolumeEffect;
/// IT Tracker Format Conversion
/// no way to get None, as then it just doesn't get set
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0..=64 => Ok(Self::Volume(value)),
65..=74 => Ok(Self::FineVolSlideUp(value - 65)),
75..=84 => Ok(Self::FineVolSlideDown(value - 75)),
85..=94 => Ok(Self::VolSlideUp(value - 85)),
95..=104 => Ok(Self::VolSlideDown(value - 95)),
105..=114 => Ok(Self::PitchSlideDown(value - 105)),
115..=124 => Ok(Self::PitchSlideUp(value - 115)),
128..=192 => Ok(Self::Panning(value - 128)),
193..=202 => Ok(Self::SlideToNoteWithSpeed(value - 193)),
203..=212 => Ok(Self::VibratoWithSpeed(value - 203)),
_ => Err(InvalidVolumeEffect),
}
}
}