-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.rs
More file actions
285 lines (253 loc) · 7.24 KB
/
sample.rs
File metadata and controls
285 lines (253 loc) · 7.24 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
// look at player/csndfile.c csf_read_sample
#![allow(dead_code)] // not nearly done
use std::num::NonZeroU32;
use crate::file::{InFilePtr, err::LoadErr};
use super::header;
#[derive(Debug, Default, Clone, Copy)]
pub enum VibratoWave {
#[default]
Sine = 0,
RampDown = 1,
Square = 2,
Random = 3,
}
impl TryFrom<u8> for VibratoWave {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Sine),
1 => Ok(Self::RampDown),
2 => Ok(Self::Square),
3 => Ok(Self::Random),
_ => Err(value),
}
}
}
enum ImpulseSampleBitWidth {
Seven,
Eight,
Sixteen,
TwentyFour,
ThirtyTwo,
}
impl From<ImpulseSampleBitWidth> for usize {
fn from(value: ImpulseSampleBitWidth) -> Self {
match value {
ImpulseSampleBitWidth::Seven => 7,
ImpulseSampleBitWidth::Eight => 8,
ImpulseSampleBitWidth::Sixteen => 16,
ImpulseSampleBitWidth::TwentyFour => 24,
ImpulseSampleBitWidth::ThirtyTwo => 32,
}
}
}
enum ImpulseSampleChannels {
Mono,
StereoInterleaved,
StereoSplit,
}
enum ImpulseSampleEndianness {
Little,
Big,
}
/// don't know what most of them are. Just took them from include/sndfile.h of schism tracker
enum ImpulseSampleEncoding {
PCMSigned,
PCMUnsigned,
PCMDelta,
IT2_14Comp,
IT2_15Comp,
Ams,
Dmf,
Mdl,
Ptm,
PCM16,
}
/// don't understand what bit 3 is supposed to do
#[derive(Debug, Copy, Clone)]
pub struct SampleFormatConvert(u8);
impl SampleFormatConvert {
// alternative is unsigned
pub fn is_signed(self) -> bool {
(self.0 & 0x1) != 0
}
/// should only be true for very old files (isn't even supported in C schism)
pub fn is_big_endian(self) -> bool {
(self.0 & 0x2) != 0
}
/// alternative is PCM samples
pub fn delta_samples(self) -> bool {
(self.0 & 0x4) != 0
}
/// not supported
pub fn byte_delta(self) -> bool {
(self.0 & 0x8) != 0
}
/// don't support in first step
pub fn tx_wave_12bit(self) -> bool {
(self.0 & 0x10) != 0
}
/// don't know if this also means that the sample is stereo
pub fn should_show_stereo_prompt(self) -> bool {
(self.0 & 0x20) != 0
}
}
#[derive(Debug, Copy, Clone)]
pub struct SampleFormatFlags(u8);
impl SampleFormatFlags {
pub fn has_sample(self) -> bool {
(self.0 & 0x01) != 0
}
pub fn is_16bit(self) -> bool {
(self.0 & 0x02) != 0
}
pub fn is_8bit(self) -> bool {
!self.is_16bit()
}
pub fn is_steroe(self) -> bool {
(self.0 & 0x04) != 0
}
pub fn is_compressed(self) -> bool {
(self.0 & 0x08) != 0
}
pub fn uses_loop(self) -> bool {
(self.0 & 0x10) != 0
}
pub fn uses_sustain_loop(self) -> bool {
(self.0 & 0x20) != 0
}
pub fn ping_pong_loop(self) -> bool {
(self.0 & 0x40) != 0
}
pub fn forward_loop(self) -> bool {
!self.ping_pong_loop()
}
pub fn ping_pong_sustain_loop(self) -> bool {
(self.0 & 0x80) != 0
}
pub fn forward_sustain_loop(self) -> bool {
!self.ping_pong_sustain_loop()
}
}
#[derive(Debug)]
pub struct ImpulseSampleHeader {
pub dos_filename: Box<[u8]>,
pub sample_name: String,
pub global_volume: u8,
pub flags: SampleFormatFlags,
pub default_volume: u8,
pub convert: SampleFormatConvert,
pub default_pan: u8,
// frame count
pub length: u32,
pub loop_start: u32,
pub loop_end: u32,
pub c5_speed: u32,
pub sustain_start: u32,
pub sustain_end: u32,
pub data_ptr: InFilePtr,
pub vibrato_speed: u8,
pub vibrato_depth: u8,
pub vibrato_type: VibratoWave,
pub vibrato_rate: u8,
}
impl ImpulseSampleHeader {
const SIZE: usize = 80;
pub fn parse(buf: &[u8; Self::SIZE]) -> Result<Self, LoadErr> {
if !buf.starts_with(b"IMPS") {
return Err(LoadErr::Invalid);
}
let dos_filename = buf[0x4..=0xF].to_vec().into_boxed_slice();
if buf[0x10] != 0 {
return Err(LoadErr::Invalid);
}
let global_volume = if buf[0x11] > 64 {
// defect_handler(LoadDefect::OutOfBoundsValue);
64
} else {
buf[0x11]
};
let flags = SampleFormatFlags(buf[0x12]);
let default_volume = buf[0x13];
let sample_name = {
let str = buf[0x14..=0x2D].split(|b| *b == 0).next().unwrap().to_vec();
let str = String::from_utf8(str);
if str.is_err() {
// defect_handler(LoadDefect::InvalidText);
}
str.unwrap_or_default()
};
let convert = SampleFormatConvert(buf[0x2E]);
let default_pan = buf[0x2F];
// in samples, not bytes
let length = u32::from_le_bytes([buf[0x30], buf[0x31], buf[0x32], buf[0x33]]);
let loop_start = u32::from_le_bytes([buf[0x34], buf[0x35], buf[0x36], buf[0x37]]);
let loop_end = u32::from_le_bytes([buf[0x38], buf[0x39], buf[0x3A], buf[0x3B]]);
// bytes per second at c5
let c5_speed = {
let speed = u32::from_le_bytes([buf[0x3C], buf[0x3D], buf[0x3E], buf[0x3F]]);
if speed > 9999999 {
// defect_handler(LoadDefect::OutOfBoundsValue);
// no idea what is a good default here
9999999 / 2
} else {
speed
}
};
// in samples, not bytes
let sustain_start = u32::from_le_bytes([buf[0x40], buf[0x41], buf[0x42], buf[0x43]]);
let sustain_end = u32::from_le_bytes([buf[0x44], buf[0x45], buf[0x46], buf[0x47]]);
let data_ptr = {
let value = u32::from_le_bytes([buf[0x48], buf[0x49], buf[0x4A], buf[0x4B]]);
if value < header::ImpulseHeader::BASE_SIZE as u32 {
return Err(LoadErr::Invalid);
}
InFilePtr(NonZeroU32::new(value).unwrap())
};
let vibrato_speed = if buf[0x4C] > 64 {
// defect_handler(LoadDefect::OutOfBoundsValue);
32
} else {
buf[0x4C]
};
let vibrato_depth = if buf[0x4D] > 64 {
// defect_handler(LoadDefect::OutOfBoundsValue);
32
} else {
buf[0x4D]
};
let vibrato_rate = if buf[0x4E] > 64 {
// defect_handler(LoadDefect::OutOfBoundsValue);
32
} else {
buf[0x4E]
};
let vibrato_type = {
let wave = VibratoWave::try_from(buf[0x4F]);
if wave.is_err() {
// defect_handler(LoadDefect::OutOfBoundsValue);
}
wave.unwrap_or_default()
};
Ok(Self {
dos_filename,
sample_name,
global_volume,
flags,
default_volume,
convert,
default_pan,
length,
loop_start,
loop_end,
c5_speed,
sustain_start,
sustain_end,
data_ptr,
vibrato_speed,
vibrato_depth,
vibrato_type,
vibrato_rate,
})
}
}