-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument.rs
More file actions
285 lines (258 loc) · 7.83 KB
/
instrument.rs
File metadata and controls
285 lines (258 loc) · 7.83 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#![allow(dead_code)] // not nearly finished
use std::array;
use crate::file::err;
#[derive(Debug, Default)]
pub enum NewNoteAction {
#[default]
Cut = 0,
Continue = 1,
NoteOff = 2,
NoteFade = 3,
}
impl TryFrom<u8> for NewNoteAction {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Cut),
1 => Ok(Self::Continue),
2 => Ok(Self::NoteOff),
3 => Ok(Self::NoteFade),
_ => Err(value),
}
}
}
#[derive(Debug, Default)]
pub enum DuplicateCheckType {
#[default]
Off = 0,
Note = 1,
Sample = 2,
Instrument = 3,
}
impl TryFrom<u8> for DuplicateCheckType {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Off),
1 => Ok(Self::Note),
2 => Ok(Self::Sample),
3 => Ok(Self::Instrument),
_ => Err(()),
}
}
}
#[derive(Debug, Default)]
pub enum DuplicateCheckAction {
#[default]
Cut = 0,
NoteOff = 1,
NoteFade = 2,
}
impl TryFrom<u8> for DuplicateCheckAction {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Cut),
1 => Ok(Self::NoteOff),
2 => Ok(Self::NoteFade),
_ => Err(()),
}
}
}
#[derive(Debug)]
pub struct ImpulseInstrument {
pub dos_file_name: [u8; 12],
pub new_note_action: NewNoteAction,
pub duplicate_check_type: DuplicateCheckType,
pub duplicate_check_action: DuplicateCheckAction,
pub fade_out: u16,
pub pitch_pan_seperation: i8,
pub pitch_pan_center: u8,
pub global_volume: u8,
pub default_pan: Option<u8>,
pub random_volume: u8,
pub random_pan: u8,
pub created_with: u16,
pub number_of_samples: u8,
pub name: String,
pub initial_filter_cutoff: u8,
pub initial_filter_resonance: u8,
pub midi_channel: u8,
pub midi_program: u8,
pub midi_bank: u16,
pub note_sample_table: [(u8, u8); 120],
pub volume_envelope: ImpulseEnvelope,
pub pan_envelope: ImpulseEnvelope,
pub pitch_envelope: ImpulseEnvelope,
}
impl ImpulseInstrument {
const SIZE: usize = 554;
pub fn parse(buf: &[u8; Self::SIZE]) -> Result<Self, err::LoadErr> {
if !buf.starts_with(b"IMPI") {
return Err(err::LoadErr::Invalid);
}
// unwrap is okay as the slice length is const
let dos_file_name: [u8; 12] = buf[0x04..=0x0F].try_into().unwrap();
if buf[10] != 0 {
return Err(err::LoadErr::Invalid);
}
// let new_note_action = match NewNoteAction::try_from(buf[0x11]) {
// Ok(nna) => nna,
// Err(_) => {
// // defect_handler(LoadDefect::OutOfBoundsValue);
// NewNoteAction::default()
// }
// };
let new_note_action = NewNoteAction::try_from(buf[0x11]).unwrap_or_default();
// let duplicate_check_type = match DuplicateCheckType::try_from(buf[0x12]) {
// Ok(dct) => dct,
// Err(_) => {
// // defect_handler(LoadDefect::OutOfBoundsValue);
// DuplicateCheckType::default()
// }
// };
let duplicate_check_type = DuplicateCheckType::try_from(buf[0x12]).unwrap_or_default();
// let duplicate_check_action = match DuplicateCheckAction::try_from(buf[0x13]) {
// Ok(dca) => dca,
// Err(_) => {
// // defect_handler(LoadDefect::OutOfBoundsValue);
// DuplicateCheckAction::default()
// }
// };
let duplicate_check_action = DuplicateCheckAction::try_from(buf[0x13]).unwrap_or_default();
let fade_out = u16::from_le_bytes([buf[0x14], buf[0x15]]);
let pitch_pan_seperation = {
let tmp = i8::from_le_bytes([buf[0x16]]);
if !(-32..=32).contains(&tmp) {
// defect_handler(LoadDefect::OutOfBoundsValue);
0
} else {
tmp
}
};
let pitch_pan_center = if buf[0x17] <= 119 {
buf[0x17]
} else {
// defect_handler(LoadDefect::OutOfBoundsValue);
59
};
let global_volume = if buf[0x18] <= 128 {
buf[0x18]
} else {
// defect_handler(LoadDefect::OutOfBoundsValue);
64
};
let default_pan = if buf[0x19] == 128 {
None
} else if buf[0x19] > 64 {
// defect_handler(LoadDefect::OutOfBoundsValue);
Some(32)
} else {
Some(buf[0x19])
};
let random_volume = buf[0x1A];
assert!(random_volume <= 100);
let random_pan = buf[0x1B];
assert!(random_pan <= 100);
let created_with = u16::from_le_bytes([buf[0x1C], buf[0x1D]]);
let number_of_samples = buf[0x1E];
let name = String::from_utf8(
buf[0x20..=0x39]
.split(|b| *b == 0)
.next()
.unwrap()
.to_owned(),
)
.unwrap();
let initial_filter_cutoff = buf[0x3A];
let initial_filter_resonance = buf[0x3B];
let midi_channel = buf[0x3C];
let midi_program = buf[0x3D];
let midi_bank = u16::from_le_bytes([buf[0x3E], buf[0x3F]]);
let note_sample_table: [(u8, u8); 120] = buf[0x030..0x130]
.chunks_exact(2)
.map(|chunk| (chunk[0], chunk[1]))
.collect::<Vec<(u8, u8)>>()
.try_into()
.unwrap();
let volume_envelope = ImpulseEnvelope::load(
&buf[0x130..0x130 + ImpulseEnvelope::SIZE]
.try_into()
.unwrap(),
);
let pan_envelope = ImpulseEnvelope::load(
&buf[0x182..0x182 + ImpulseEnvelope::SIZE]
.try_into()
.unwrap(),
);
let pitch_envelope = ImpulseEnvelope::load(
&buf[0x1D4..0x1D4 + ImpulseEnvelope::SIZE]
.try_into()
.unwrap(),
);
Ok(Self {
dos_file_name,
new_note_action,
duplicate_check_type,
duplicate_check_action,
fade_out,
pitch_pan_seperation,
pitch_pan_center,
global_volume,
default_pan,
random_volume,
random_pan,
created_with,
number_of_samples,
name,
initial_filter_cutoff,
initial_filter_resonance,
midi_channel,
midi_program,
midi_bank,
note_sample_table,
volume_envelope,
pan_envelope,
pitch_envelope,
})
}
}
/// flags and node values are interpreted differently depending on the type of envelope.
/// doesn't affect loading
#[derive(Debug)]
pub struct ImpulseEnvelope {
flags: u8,
num_node_points: u8,
loop_start: u8,
loop_end: u8,
sustain_loop_start: u8,
sustain_loop_end: u8,
nodes: [(u8, u16); 25],
}
impl ImpulseEnvelope {
const SIZE: usize = 81; // = 0x51
fn load(buf: &[u8; Self::SIZE]) -> Self {
let flags = buf[0];
let num_node_points = buf[1];
let loop_start = buf[2];
let loop_end = buf[3];
let sustain_loop_start = buf[4];
let sustain_loop_end = buf[5];
let nodes = array::from_fn(|idx| {
let chunk = 6 + idx * 3;
(
buf[chunk],
u16::from_le_bytes([buf[chunk + 1], buf[chunk + 2]]),
)
});
Self {
flags,
num_node_points,
loop_start,
loop_end,
sustain_loop_start,
sustain_loop_end,
nodes,
}
}
}