Skip to content

Commit 178039a

Browse files
fix(lang): a bar a chord is pushed into has its own text; a pushed chord reads at its written place
As it reads a chord pushed onto the next bar's downbeat ('6m), the parser opens that bar and takes it back, so the tokens read meanwhile were marked as starting in a bar they ended before: the bar after a push shared its line's text with the next, and 49 bars (all of Know Me's 4 '6m // 5 // runs) were read-only. A token is now the bar it ended in. A bar's chord line reads each pushed chord where it is written (its sounding start plus its push), so one pushed onto the downbeat is the bar's first chord rather than out of order at its end; this replaces the slot shifting #117 did after the fact. Dev vault: read-only 94 -> 41 bars (pushed over a barline 49 -> 0); notes written back 2,182 -> 2,442, chords 2,281 -> 2,314; note refusals 316 -> 105. Know Me: 154 of 154 bars take a note (was 22).
1 parent cdb4181 commit 178039a

4 files changed

Lines changed: 69 additions & 49 deletions

File tree

‎features/lang/src/edit/chords.rs‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ pub struct ChordBar {
3535
pub locked: Option<String>,
3636
}
3737

38+
/// How far before the beat it is written on `c` sounds: an eighth for one
39+
/// apostrophe, a sixteenth for two, a thirty-second for three. None for a
40+
/// chord not pushed, or pushed by a tuplet's value.
41+
fn push_ticks(c: &keyflow_proto::chart::types::ChordInstance) -> u32 {
42+
use keyflow_proto::chord::PushPullBase;
43+
match &c.push_pull {
44+
Some((true, amount)) if matches!(amount.base, PushPullBase::Standard) => {
45+
TICKS_PER_WHOLE >> (2 + u32::from(amount.level.min(3)))
46+
}
47+
_ => 0,
48+
}
49+
}
50+
3851
/// Ticks of `d` (beats as the measure counts them) in `time`.
3952
fn ticks_of(d: &MusicalDuration, time: (u8, u8)) -> u32 {
4053
use keyflow_proto::time::MusicalPositionExt as _;
@@ -87,10 +100,17 @@ impl ChordBar {
87100
if !rhythm_fills {
88101
// A chord sounds until the next one: each lasts from where it
89102
// starts to where the next starts (the last, to the barline).
103+
// Each chord where it is written: a pushed one (`'G`) sounds an
104+
// eighth or a sixteenth before that, and its mark pushes it
105+
// again when the bar is read back; one pushed onto the downbeat
106+
// sounds in the bar before, and is this bar's first.
90107
let starts: Vec<u32> = measure
91108
.chords
92109
.iter()
93-
.map(|c| ticks_of(&c.position.total_duration, time) % length.max(1))
110+
.map(|c| {
111+
let sounds = ticks_of(&c.position.total_duration, time);
112+
(sounds + push_ticks(c)) % length.max(1)
113+
})
94114
.collect();
95115
if starts.windows(2).any(|w| w[1] <= w[0]) {
96116
bar.locked = Some("its chords are not one after another".to_owned());
@@ -124,28 +144,6 @@ impl ChordBar {
124144
}
125145
}
126146
}
127-
// A pushed chord (`'G`, `''G`) sounds an eighth or a sixteenth
128-
// before the beat it is written on: put back on that beat, with the
129-
// slot before it as much longer, it is its written place, which its
130-
// mark pushes again when the bar is read back. (One pushed onto the
131-
// bar's downbeat sounds in the bar before: not here.)
132-
for i in 1..slots.len() {
133-
let push = match &slots[i].0 {
134-
Slot::Chord(t) => {
135-
parts(t).map_or(0, |p| p.0.chars().filter(|c| *c == '\'').count())
136-
}
137-
_ => 0,
138-
};
139-
let ticks = match push {
140-
1 => TICKS_PER_WHOLE / 8,
141-
2 => TICKS_PER_WHOLE / 16,
142-
_ => 0,
143-
};
144-
if ticks > 0 && slots[i].1 > ticks {
145-
slots[i].1 -= ticks;
146-
slots[i - 1].1 += ticks;
147-
}
148-
}
149147
let total: u32 = slots.iter().map(|s| s.1).sum();
150148
if total != length {
151149
bar.locked = Some("its chords do not fill it exactly".to_owned());

‎features/lang/src/edit/commit.rs‎

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,34 +1122,44 @@ mod tests {
11221122
}
11231123

11241124
#[test]
1125-
fn a_bar_a_push_crosses_into_is_read_only_and_says_why() {
1126-
// `'6m` anticipates bar 2's downbeat from bar 1's last eighth: the
1127-
// two bars share one text, which written out bar by bar would read
1128-
// the push differently.
1129-
// Know Me's intro: a push across a barline lengthens the chord
1130-
// before it, which written out bar by bar reads differently.
1125+
fn a_bar_a_push_crosses_into_has_its_own_text_and_is_edited() {
1126+
// Know Me's intro: `'6m` anticipates the next bar's downbeat from
1127+
// the bar before's last eighth. Each bar has its own text (the
1128+
// parser opens and takes back a bar as it reads a push), so a bar
1129+
// is written in place, and the push stays across its barline.
11311130
let text = "Edit - Test\n#E 127bpm 4/4\n\nIN 8\n4 '6m 4 '6m // 5 //\n4 '6m // 5 // 2m 2m // 2m_8 1/3_4.\n";
11321131
let chart = parse_chart(text).unwrap();
11331132
let score = Score::from_text(text, &chart);
1134-
let at = score
1135-
.read_only
1136-
.iter()
1137-
.position(|r| r.as_deref() == Some(PUSHED_OVER))
1138-
.unwrap_or_else(|| panic!("{:?}", score.read_only));
1139-
let mut score = score;
1140-
let mut input = InputState {
1141-
mode: Mode::NoteName,
1142-
..InputState::default()
1143-
};
1144-
input.apply(
1145-
&mut score,
1146-
Op::MoveTo(crate::edit::Pos { bar: at, tick: 0 }),
1147-
);
1148-
let out = input.apply(&mut score, Op::Letter('C'));
11491133
assert!(
1150-
out.refused
1151-
.is_some_and(|r| r.contains("pushed over its barline"))
1134+
score.read_only.iter().all(Option::is_none),
1135+
"{:?}",
1136+
score.read_only
11521137
);
1138+
let b = (0..measures(&chart).len())
1139+
.find(|&i| bar_text(text, &chart, i).as_deref() == Some("'6m // 5 //"))
1140+
.expect("a bar written `'6m // 5 //`");
1141+
// The 5 after the pushed 6m: made a 5maj7, written in place.
1142+
let mut score = score;
1143+
let mut input = InputState::default();
1144+
assert!(score.chords[b].locked.is_none(), "{:?}", score.chords[b]);
1145+
let at = score.chords[b].starts()[1];
1146+
input.selected = Some(crate::edit::Pos { bar: b, tick: at });
1147+
let out = input.set_quality(&mut score, "maj7");
1148+
let new = write_bars(text, &chart, &score, &out.changed).unwrap();
1149+
assert!(new.contains("'6m // 5maj7 //"), "{new}");
1150+
// The pushed 6m retyped 2m: its push kept, the bar before as it was.
1151+
let chart = parse_chart(&new).unwrap();
1152+
let mut score = Score::from_text(&new, &chart);
1153+
input.selected = Some(crate::edit::Pos { bar: b, tick: 0 });
1154+
let out = input.set_chord(&mut score, "2m");
1155+
let new = write_bars(&new, &chart, &score, &out.changed).unwrap();
1156+
assert!(new.contains("'2m // 5maj7 //"), "{new}");
1157+
// Its push taken off would change the bar before: refused, and why.
1158+
let chart = parse_chart(&new).unwrap();
1159+
let mut score = Score::from_text(&new, &chart);
1160+
let out = input.toggle_mark(&mut score, "'");
1161+
assert!(out.changed.is_empty());
1162+
assert!(out.refused.is_some_and(|r| r.contains("bar before")));
11531163
}
11541164

11551165
#[test]
@@ -1346,7 +1356,7 @@ mod tests {
13461356
let out = input.apply(&mut score, Op::Letter('A'));
13471357
let new = write_bars(text, &chart, &score, &out.changed).unwrap();
13481358
assert!(
1349-
new.contains("E /// G#m / | A_1 | B /// C#sus2 / C#sus2"),
1359+
new.contains("E /// G#m / | A_1 | B /// C#sus2 / | C#sus2"),
13501360
"{new}"
13511361
);
13521362
}
@@ -1506,7 +1516,7 @@ mod tests {
15061516
let new = write_bars(text, &chart, &score, &changed).unwrap();
15071517
// The other run (C#sus2 held into bar 4) keeps its text.
15081518
assert!(
1509-
new.contains("E /// G#m / | A_1 | B /// C#sus2 / C#sus2"),
1519+
new.contains("E /// G#m / | A_1 | B /// C#sus2 / | C#sus2"),
15101520
"{new}"
15111521
);
15121522
// Unchanged, typed back over itself: the text stays as written.

‎features/lang/src/edit/input.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ impl InputState {
12601260
&& score.chords[bar].starts().get(i) == Some(&0)
12611261
{
12621262
return Outcome::refuse(
1263-
"A push on a bar's first chord sounds in the bar before: push a chord after the downbeat",
1263+
"A push on a bar's first chord sounds in the bar before, so it is not put on or taken off here: push a chord after the downbeat",
12641264
);
12651265
}
12661266
self.rechord(score, |old| super::chords::with_mark(old, mark))

‎features/lang/src/text/chart/parser/chords/line/mod.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,18 @@ fn bar_spans(
986986
let mut range: Vec<Option<(usize, usize)>> = vec![None; bars];
987987
let mut shared: Vec<bool> = vec![false; bars];
988988
let mut spilled = vec![false; bars];
989+
// A pushed chord opens the bar it pushes from and the parser takes it
990+
// back after: a token read then is marked as starting in a bar it
991+
// ended before. It is the bar it ended in.
992+
let marks: Vec<TokenMark> = marks
993+
.iter()
994+
.map(|m| TokenMark {
995+
before: m.before.min(m.after),
996+
after: m.after,
997+
beats_after: m.beats_after,
998+
})
999+
.collect();
1000+
let marks = marks.as_slice();
9891001
// The bar a run of such tokens went back to: the bar after it, which
9901002
// the next of them would otherwise open on.
9911003
let mut carried: Option<usize> = None;

0 commit comments

Comments
 (0)