Skip to content

Commit 3ea276c

Browse files
committed
several updates and fixes
- fix clippy warnings. - derive more traits, specially Clone, PartialEq, Eq, Default. - update comments. - update example. - rustfmt.
1 parent 71bc0c6 commit 3ea276c

File tree

8 files changed

+81
-21
lines changed

8 files changed

+81
-21
lines changed

examples/parse.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ fn main() {
1414
Please run the example from the examples/ directory",
1515
);
1616

17+
// for region in &i.regions {
18+
// println!("{:?}", region);
19+
// }
20+
21+
1722
println!("{:#?}", i);
1823

1924
println!("groups: {}\nregions: {}", i.groups(), i.regions());

src/sfz/group.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::sfz::{Opcode, OpcodeMap};
77
/// A group is defined with the <group> opcode, and the parameters enumerated
88
/// on it last till the next group opcode, or till the end of the file.
99
///
10-
#[derive(Debug, Default)]
10+
#[derive(Clone, Debug, Default, PartialEq)]
1111
pub struct Group {
1212
/// This list of opcodes overwrites the default ones.
1313
pub opcodes: OpcodeMap,
@@ -17,10 +17,12 @@ pub struct Group {
1717
}
1818

1919
impl Group {
20+
/// New group.
2021
pub fn new() -> Self {
2122
Self::default()
2223
}
2324

25+
/// Add an opcode to the group.
2426
pub fn add_opcode(&mut self, o: &Opcode) {
2527
self.opcodes.insert(o.str_name(), o.clone());
2628
}

src/sfz/headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::sfz::SfzToken;
99
///
1010
/// [sfzformat.com/headers/](https://sfzformat.com/headers/)
1111
12-
#[derive(Debug, PartialEq)]
12+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1313
pub enum Header {
1414
// sfz v1 headers
1515
/// The basic component of an instrument. An instrument is defined by one or more regions.

src/sfz/instrument.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
/// International Pitch Notation (IPN) convention. According to this rules,
2323
/// middle C in the keyboard is C4 and the MIDI note number 60.
2424
///
25-
#[derive(Debug)]
25+
#[derive(Clone, Debug)]
2626
pub struct Instrument {
2727
/// The default opcodes for this instrument.
2828
pub global: OpcodeMap,
@@ -341,7 +341,7 @@ impl Instrument {
341341
}
342342

343343
/// The current status of the parsing of the instrument
344-
#[derive(Debug)]
344+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
345345
struct InstrumentParsingStatus {
346346
is_header_control: bool,
347347
is_header_global: bool,

src/sfz/opcodes/opcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::sfz::types::{
2626
/// - [Cakewalk Extensions Opcodes](https://sfzformat.com/extensions/cakewalk/)
2727
///
2828
#[allow(non_camel_case_types)]
29-
#[derive(Debug, PartialEq, Clone)]
29+
#[derive(Clone, Debug, PartialEq)]
3030
pub enum Opcode {
3131
// sfz v1 opcodes -----------------------------------------------------------
3232
// https://sfzformat.com/misc/sfz2

src/sfz/opcodes/parse.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl Opcode {
172172
("cutoff", _) => {
173173
utils::check_f32_between(value, 0., MAX_SAMPLE_RATE).map(Opcode::cutoff)
174174
}
175-
("fil_type", _) => fil_type::from_str(value).map(Opcode::fil_type),
175+
("fil_type", _) => fil_type::from_name(value).map(Opcode::fil_type),
176176
("fil_veltrack", _) => {
177177
utils::check_i16_between(value, -9600, 9600).map(Opcode::fil_veltrack)
178178
}
@@ -183,16 +183,14 @@ impl Opcode {
183183
// NOTE: lokey v2 accepts i8, from -1:
184184
("lokey", _) => utils::check_midi_note(value).map(Opcode::lokey),
185185
("lovel", _) => utils::check_u8_between(value, 0, 127).map(Opcode::lovel),
186-
("loop_mode", _) => loop_mode::from_str(value).map(Opcode::loop_mode),
186+
("loop_mode", _) => loop_mode::from_name(value).map(Opcode::loop_mode),
187187
("lorand", _) => utils::check_f32_between(value, 0., 1.).map(Opcode::lorand),
188188
("off_by", _) => utils::check_u32_between(value, 0, u32::MAX).map(Opcode::off_by),
189189
("offset", _) => utils::check_u32_between(value, 0, u32::MAX).map(Opcode::offset),
190190
("on_loccN", _) => utils::check_i8_between(value, 0, 127).map(Opcode::on_loccN),
191191
("on_hiccN", _) => utils::check_i8_between(value, 0, 127).map(Opcode::on_hiccN),
192192
("pan", _) => utils::check_f32_between(value, 0., 100.).map(Opcode::pan),
193-
("pitch_keycenter", _) => {
194-
utils::check_midi_note(value).map(Opcode::pitch_keycenter)
195-
}
193+
("pitch_keycenter", _) => utils::check_midi_note(value).map(Opcode::pitch_keycenter),
196194
("pitch_keytrack", _) => {
197195
utils::check_i16_between(value, -1200, 1200).map(Opcode::pitch_keytrack)
198196
}
@@ -203,7 +201,7 @@ impl Opcode {
203201
("sample", _) => Some(Opcode::sample(utils::fix_path_separators(value))),
204202
("seq_lenght", _) => utils::check_u8_between(value, 1, 100).map(Opcode::seq_length),
205203
("seq_position", _) => utils::check_u8_between(value, 1, 100).map(Opcode::seq_position),
206-
("trigger", _) => trigger::from_str(value).map(Opcode::trigger),
204+
("trigger", _) => trigger::from_name(value).map(Opcode::trigger),
207205
("sw_hikey", _) => utils::check_midi_note(value).map(Opcode::sw_hikey),
208206
("sw_last", _) => utils::check_u8_between(value, 0, 127).map(Opcode::sw_last),
209207
("sw_lokey", _) => utils::check_midi_note(value).map(Opcode::sw_lokey),
@@ -232,7 +230,7 @@ impl Opcode {
232230

233231
/// Token for parsing SFZ format elements like headers and tokens
234232
///
235-
#[derive(Logos, Debug, PartialEq)]
233+
#[derive(Logos, Clone, Debug, PartialEq)]
236234
pub(crate) enum SfzToken {
237235
/// Parses a Header
238236
///
@@ -266,7 +264,7 @@ pub(crate) enum SfzToken {
266264
/// Some opcode names contains numbers that must not be interpreted as parameters.
267265
/// It makes sure to filter out false positives,
268266
/// as parameters,
269-
#[derive(Logos, Debug, PartialEq)]
267+
#[derive(Logos, Clone, Debug, PartialEq)]
270268
pub(crate) enum OpcodeParameter {
271269
/// Skip numbers that must not be recognized as parameters,
272270
/// since they are part of the opcode's name.

src/sfz/region.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::sfz::{Opcode, OpcodeMap};
2020
/// All Input Controls defined in a region act using the AND boolean operator.
2121
/// Consequently, all conditions must be matched for the region to play.
2222
///
23-
#[derive(Debug, Default)]
23+
#[derive(Clone, Debug, Default)]
2424
pub struct Region {
2525
/// The opcodes of this group are applied and will override the defaults.
2626
pub group: Option<usize>,
@@ -30,10 +30,12 @@ pub struct Region {
3030
}
3131

3232
impl Region {
33+
/// New region.
3334
pub fn new() -> Self {
3435
Self::default()
3536
}
3637

38+
/// New region with some group.
3739
// FIXME (add group at posteriori)
3840
pub fn with_group(group: usize) -> Self {
3941
Self {

src/sfz/types.rs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub const MAX_SAMPLE_RATE: f32 = 384_000.0;
2222

2323
/// All the possible types allowed in an Opcode
2424
#[allow(non_camel_case_types)]
25-
#[derive(Debug, Clone)]
25+
#[derive(Clone, Debug, PartialEq, Eq)]
2626
pub enum OpcodeType {
2727
i8(Option<i8>),
2828
u8(Option<u8>),
@@ -43,17 +43,41 @@ pub type OpcodeMap = HashMap<String, Opcode>;
4343
/// Allows playing samples with loops defined in the unlooped mode.
4444
///
4545
/// - info: [loop_mode](https://sfzformat.com/opcodes/loop_mode)
46-
#[derive(Debug, Clone, PartialEq)]
46+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4747
#[allow(non_camel_case_types)]
4848
pub enum loop_mode {
49+
/// no looping will be performed. Sample will play straight from start to end,
50+
/// or until note off, whatever reaches first.
51+
///
52+
/// This is the default.
4953
no_loop,
54+
55+
/// sample will play from start to end, ignoring note off. This is commonly
56+
/// used for drums. This mode is engaged automatically if the count opcode
57+
/// is defined.
5058
one_shot,
59+
60+
/// once the player reaches sample loop point, the loop will play until note
61+
/// expiration. This includes looping during the release phase.
5162
loop_continuous,
63+
64+
/// the player will play the loop while the note is held, by keeping it
65+
/// depressed or by using the sustain pedal (CC64). During the release phase,
66+
/// there’s no looping.
5267
loop_sustain,
5368
}
69+
70+
// IMPROVE: `no_loop` for samples without a loop defined,
71+
// `loop_continuous` for samples with defined loop(s).
72+
impl Default for loop_mode {
73+
fn default() -> loop_mode {
74+
Self::no_loop
75+
}
76+
}
77+
5478
impl loop_mode {
5579
/// Constructor from the variant name, as a string
56-
pub fn from_str(name: &str) -> Option<Self> {
80+
pub fn from_name(name: &str) -> Option<Self> {
5781
match name {
5882
"no_loop" => Some(Self::no_loop),
5983
"one_shot" => Some(Self::one_shot),
@@ -67,18 +91,38 @@ impl loop_mode {
6791
/// Sets the trigger which will be used for the sample to play.
6892
///
6993
/// - info: [trigger](https://sfzformat.com/opcodes/trigger)
70-
#[derive(Debug, Clone, PartialEq)]
94+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7195
#[allow(non_camel_case_types)]
7296
pub enum trigger {
97+
/// (Default): Region will play on note-on.
7398
attack,
99+
100+
/// Region will play on note-off or sustain pedal off. The velocity used to
101+
/// play the note-off sample is the velocity value of the corresponding
102+
/// (previous) note-on message.
74103
release,
104+
105+
/// Region will play on note-on, but if there’s no other note going on
106+
/// (comoonly used for or first note in a legato phrase).
75107
first,
108+
109+
/// Region will play on note-on, but only if there’s a note going on
110+
/// (notes after first note in a legato phrase).
76111
legato,
112+
113+
/// Region will play on note-off. Ignores sustain pedal.
77114
release_key, // aria
78115
}
116+
117+
impl Default for trigger {
118+
fn default() -> trigger {
119+
Self::attack
120+
}
121+
}
122+
79123
impl trigger {
80124
/// Constructor from the variant name, as a string
81-
pub fn from_str(name: &str) -> Option<Self> {
125+
pub fn from_name(name: &str) -> Option<Self> {
82126
match name {
83127
"attack" => Some(Self::attack),
84128
"release" => Some(Self::release),
@@ -93,7 +137,7 @@ impl trigger {
93137
/// Allows you to choose which type of filter you use if not specified
94138
///
95139
/// - info: [fil_type](https://sfzformat.com/opcodes/fil_type)
96-
#[derive(Debug, Clone, PartialEq)]
140+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
97141
#[allow(non_camel_case_types)]
98142
pub enum fil_type {
99143
/// One-pole low pass filter (6dB/octave)
@@ -108,6 +152,8 @@ pub enum fil_type {
108152

109153
/// Two-pole low pass filter (12dB/octave)
110154
///
155+
/// This is the default.
156+
///
111157
/// - version: v1
112158
lpf_2p,
113159

@@ -211,9 +257,16 @@ pub enum fil_type {
211257
/// - version: ARIA
212258
peq,
213259
}
260+
261+
impl Default for fil_type {
262+
fn default() -> fil_type {
263+
Self::lpf_2p
264+
}
265+
}
266+
214267
impl fil_type {
215268
/// Constructor from the variant name, as a string
216-
pub fn from_str(name: &str) -> Option<Self> {
269+
pub fn from_name(name: &str) -> Option<Self> {
217270
match name {
218271
"lpf_1p" => Some(Self::lpf_1p),
219272
"hpf_1p" => Some(Self::hpf_1p),

0 commit comments

Comments
 (0)