Skip to content

Commit d73c54c

Browse files
committed
parser,muxml: some small changes
1 parent 2d8f254 commit d73c54c

File tree

3 files changed

+43
-48
lines changed

3 files changed

+43
-48
lines changed

src/backend/muxml/formatters.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use crate::backend::muxml::{NoteProperties, Vibrato2};
1+
use crate::backend::muxml::{NoteProperties, Vibrato};
22
use crate::debugln;
33
use itoa::Buffer;
4-
4+
// This file uses explicit .write_str() -s, instead of writing a format!()ted string, because I
5+
// benchmarked it and it was faster.
6+
// Maybe there is a nice solution to this - but I've yet to find anything as performant as this one.
57
#[inline]
68
pub fn write_muxml2_rest(
79
buf: &mut impl std::fmt::Write, r#type: &str, duration: u8,
@@ -76,7 +78,7 @@ pub fn write_muxml2_note(
7678
if let Some(vibrato) = vibrato {
7779
buf.write_str("<ornaments>\n")?;
7880
buf.write_str("<wavy-line type=\"")?;
79-
buf.write_str(if matches!(Vibrato2::Start, vibrato) { "start" } else { "stop" })?;
81+
buf.write_str(if matches!(vibrato, Vibrato::Start) { "start" } else { "stop" })?;
8082
buf.write_str("\" />\n")?;
8183
buf.write_str("</ornaments>\n")?;
8284
}

src/backend/muxml/mod.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -136,41 +136,41 @@ impl Muxml2TabElement {
136136
}
137137

138138
#[derive(Default, Debug)]
139-
pub struct Slur2 {
139+
pub struct Slur {
140140
pub number: u16,
141141
pub start: bool,
142142
}
143-
impl Slur2 {
143+
impl Slur {
144144
pub fn new(number: u16, start: bool) -> Self {
145-
Slur2 { number, start }
145+
Slur { number, start }
146146
}
147147
pub fn start(number: u16) -> Self {
148-
Slur2 { number, start: true }
148+
Slur { number, start: true }
149149
}
150150
pub fn stop(number: u16) -> Self {
151-
Slur2 { number, start: false }
151+
Slur { number, start: false }
152152
}
153153
}
154154
#[derive(Default, Debug)]
155-
pub struct Slide2 {
155+
pub struct Slide {
156156
pub number: u16,
157157
pub start: bool,
158158
}
159-
impl Slide2 {
159+
impl Slide {
160160
pub fn new(number: u16, start: bool) -> Self {
161-
Slide2 { number, start }
161+
Slide { number, start }
162162
}
163163
}
164164
/// TODO: make this a bitstruct and see if that is faster
165165
/// TODO: try making this a SoA
166166
#[derive(Default, Debug)]
167167
pub struct NoteProperties {
168-
pub slurs: Vec<Slur2>,
169-
pub slide: Option<Slide2>,
170-
pub vibrato: Option<Vibrato2>,
168+
pub slurs: Vec<Slur>,
169+
pub slide: Option<Slide>,
170+
pub vibrato: Option<Vibrato>,
171171
}
172172
#[derive(Debug)]
173-
pub enum Vibrato2 {
173+
pub enum Vibrato {
174174
Start,
175175
Stop,
176176
}
@@ -222,13 +222,13 @@ fn gen_muxml2(
222222
TabElement3::Vibrato => {
223223
let last_idx = stream_idx.saturating_sub(6);
224224
note_properties.entry(last_idx as u32).or_default().vibrato =
225-
Some(Vibrato2::Start);
225+
Some(Vibrato::Start);
226226
let next_idx = stream_idx + 6;
227227
if next_idx >= parsed.tick_stream.len() {
228228
parsed.tick_stream.extend([const { TabElement3::Rest }; 6]);
229229
}
230230
note_properties.entry(next_idx as u32).or_default().vibrato =
231-
Some(Vibrato2::Stop);
231+
Some(Vibrato::Stop);
232232
}
233233
TabElement3::Bend
234234
| TabElement3::HammerOn
@@ -242,7 +242,7 @@ fn gen_muxml2(
242242
);
243243
slur_cnt += 1;
244244
let idx32 = last_idx as u32;
245-
note_properties.entry(idx32).or_default().slurs.push(Slur2::start(slur_cnt));
245+
note_properties.entry(idx32).or_default().slurs.push(Slur::start(slur_cnt));
246246
let next_idx = stream_idx + 6;
247247

248248
match &parsed.tick_stream.get(next_idx) {
@@ -257,11 +257,8 @@ fn gen_muxml2(
257257

258258
parsed.tick_stream.extend([const { TabElement3::Rest }; 6]);
259259
parsed.tick_stream[next_idx] = TabElement3::Fret(x + 1);
260-
note_properties
261-
.entry(next_idx as u32)
262-
.or_default()
263-
.slurs
264-
.push(Slur2::stop(slur_cnt));
260+
let entry = note_properties.entry(next_idx as u32).or_default();
261+
entry.slurs.push(Slur::stop(slur_cnt));
265262
}
266263
// since we know that with a "hanging bend" the next element in this track is going to be a rest, we can just silently replace it and add the correct note
267264
Some(TabElement3::Rest) => {
@@ -275,18 +272,12 @@ fn gen_muxml2(
275272
traceln!(
276273
"hanging bend on {stream_idx}, replacing {next_idx} with a Fret"
277274
);
278-
note_properties
279-
.entry(next_idx as u32)
280-
.or_default()
281-
.slurs
282-
.push(Slur2::stop(slur_cnt));
275+
let entry = note_properties.entry(next_idx as u32).or_default();
276+
entry.slurs.push(Slur::stop(slur_cnt));
283277
}
284278
_ => {
285-
note_properties
286-
.entry(next_idx as u32)
287-
.or_default()
288-
.slurs
289-
.push(Slur2::stop(slur_cnt));
279+
let entry = note_properties.entry(next_idx as u32).or_default();
280+
entry.slurs.push(Slur::stop(slur_cnt));
290281
}
291282
}
292283

@@ -301,11 +292,11 @@ fn gen_muxml2(
301292
);
302293
slide_count += 1;
303294
note_properties.entry(last_idx as u32).or_default().slide =
304-
Some(Slide2::new(slide_count, true));
295+
Some(Slide::new(slide_count, true));
305296
let next_idx = stream_idx + 6;
306297
if next_idx < parsed.tick_stream.len() {
307298
note_properties.entry(next_idx as u32).or_default().slide =
308-
Some(Slide2::new(slide_count, false));
299+
Some(Slide::new(slide_count, false));
309300
}
310301
traceln!(
311302
depth = 1,

src/parser/parser.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ pub fn line_is_valid(line: &str) -> bool {
99
let line = line.trim();
1010
let first_is_alphanumeric = line.chars().next().map(|x| x.is_alphanumeric()).unwrap_or(false);
1111
let second_is_measure_sep = line.as_bytes().get(1).map(|x| *x == b'|').unwrap_or(false);
12-
let last_is_measure_end = line.as_bytes().last().map(|x| *x == b'|').unwrap_or(false);
12+
let last_is_measure_end = line.ends_with('|');
1313
let ret = first_is_alphanumeric && second_is_measure_sep && last_is_measure_end;
1414
traceln!("line_is_valid({line}) -> {ret}");
1515
ret
1616
}
17+
1718
#[derive(Debug)]
1819
pub struct Measure {
1920
pub data_range: RangeInclusive<u32>,
@@ -48,18 +49,19 @@ impl ParseResult {
4849
for tick in 0..tick_cnt {
4950
let max_width =
5051
(0..6).map(|x| self.tick_stream[tick * 6 + x].repr_len()).max().unwrap() as usize;
51-
for s in 0..6 {
52+
for (s, buf) in bufs.iter_mut().enumerate() {
5253
use tab_element::TabElement3::*;
54+
let to_padded = |c: char| format!("{1:<0$}", max_width, c);
5355
match self.tick_stream[tick * 6 + s] {
54-
Fret(x) => bufs[s].push_str(&format!("{x:<0$}", max_width)),
55-
Rest => bufs[s].push_str(&format!("{1:<0$}", max_width, "-")),
56-
DeadNote => bufs[s].push_str(&format!("{1:<0$}", max_width, "x")),
57-
Slide => bufs[s].push_str(&format!("{1:<0$}", max_width, "/")),
58-
Bend => bufs[s].push_str(&format!("{1:<0$}", max_width, "b")),
59-
HammerOn => bufs[s].push_str(&format!("{1:<0$}", max_width, "h")),
60-
Pull => bufs[s].push_str(&format!("{1:<0$}", max_width, "p")),
61-
Release => bufs[s].push_str(&format!("{1:<0$}", max_width, "r")),
62-
Vibrato => bufs[s].push_str(&format!("{1:<0$}", max_width, "~")),
56+
Fret(x) => buf.push_str(&format!("{x:<0$}", max_width)),
57+
Rest => buf.push_str(&to_padded('-')),
58+
DeadNote => buf.push_str(&to_padded('x')),
59+
Slide => buf.push_str(&to_padded('/')),
60+
Bend => buf.push_str(&to_padded('b')),
61+
HammerOn => buf.push_str(&to_padded('h')),
62+
Pull => buf.push_str(&to_padded('p')),
63+
Release => buf.push_str(&to_padded('r')),
64+
Vibrato => buf.push_str(&to_padded('~')),
6365
}
6466
}
6567
}
@@ -118,8 +120,7 @@ pub fn parse(lines: &[String]) -> ParseResult {
118120
let mut tick_cnt_est = part[0].len();
119121
while tick < tick_cnt_est {
120122
traceln!("parsing tick {tick}");
121-
let mut is_multichar = false;
122-
let mut is_multi_on = [false; 6];
123+
let (mut is_multichar, mut is_multi_on) = (false, [false; 6]);
123124
for s in 0..6 {
124125
traceln!(depth = 1, "remaining on string {s}: {}", part[s]);
125126
if s == 0 && part[s].starts_with("|") {
@@ -293,6 +294,7 @@ pub fn source_location_from_stream(r: &ParseResult, tick_location: u32) -> (u32,
293294
traceln!("expecting the error to be at character idx {offset_on_line}");
294295
(actual_line, offset_on_line)
295296
}
297+
296298
pub fn dump_source(input: &Vec<&str>) -> String {
297299
use itertools::Itertools;
298300
input.iter().join("\n")

0 commit comments

Comments
 (0)