Skip to content

Commit cdb4181

Browse files
feat(edit): J respells the selected note; normal mode's status line describes the selection (#119)
Two more differences from MuseScore's handbook closed, after #118. ## Closed - **Respell (J):** `KEYFLOW_RESPELL`, J in the chart's view. The selected note is spelled the next way that sounds the same, the plainest first: C♯ → D♭ → B𝄪 → C♯, E → F♭ → D𝄪 → E (`Pitch::respelled`). The handbook: "To change the enharmonic spelling of a selected note, press J." - **The status line describes the selection,** as MuseScore's status bar does. In normal mode it names what is selected: - a note, a chord of notes with its pitches, a rest, or a chord symbol; - its duration and voice; - its bar and beat. For example: "Normal mode · Note G♭3 · quarter · voice 1 · bar 1, beat 4". It used to say only "bar N selected". ## Checked in the app Isolated Architect `main` `48d3894e`: F♯ entered, J made it G♭, and Esc showed the description above. It's in the round 6 recordings on [`pr-media`](https://github.com/FastTrackStudios/keyflow/blob/pr-media/chart-selection-and-input-modes/README.md#round-6-the-three-input-modes) (`recording/round6/`). ## What still differs from the handbook - **Selection colour:** a note selected outside note input takes the theme's selection colour, not its voice's, because the chart's selection doesn't know an element's voice yet. While entering, the entered note is in the voice's colour. - **Not built:** intervals below (no default key in the handbook), the above-staff ruler for input by duration, and re-pitch, real-time and insert input. - **Input via Keyflow syntax** is Keyflow's own mode; MuseScore has no counterpart. ## Tests - **New:** `j_respells_the_selected_note_the_next_way_that_sounds_the_same` and `normal_mode_s_status_line_describes_the_selection`. The manifest test checks J is bound in the chart's view. - **`just ci`:** passes (2,616 passed, 51 skipped).
2 parents 3164597 + b275efa commit cdb4181

4 files changed

Lines changed: 142 additions & 3 deletions

File tree

‎features/extension/extension.toml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ description = "The selected chord symbol's quality stepped back (Alt+↓)."
564564
group = "Keyflow"
565565
effect = "mutating"
566566

567+
[[actions]]
568+
id = "KEYFLOW_RESPELL"
569+
name = "Keyflow: respell the note"
570+
description = "The selected note spelled the next way that sounds the same (J): C♯ becomes D♭, then B𝄪, then C♯ again, as MuseScore's respell."
571+
group = "Keyflow"
572+
effect = "mutating"
573+
567574
[[actions]]
568575
id = "KEYFLOW_INTERVAL_1"
569576
name = "Keyflow: add unison above"
@@ -1278,6 +1285,11 @@ chord = "⌥↓"
12781285
action = "KEYFLOW_QUALITY_DOWN"
12791286
when = "view:chart"
12801287

1288+
[[keys]]
1289+
chord = "J"
1290+
action = "KEYFLOW_RESPELL"
1291+
when = "view:chart"
1292+
12811293
[[keys]]
12821294
chord = "⌥1"
12831295
action = "KEYFLOW_INTERVAL_1"

‎features/extension/src/input.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub const OP_ACTIONS: &[OpAction] = &[
6161
// A chord symbol's quality, as ↑ ↓ step its root.
6262
op("KEYFLOW_QUALITY_UP", Op::Quality(true), "⌥↑"),
6363
op("KEYFLOW_QUALITY_DOWN", Op::Quality(false), "⌥↓"),
64+
op("KEYFLOW_RESPELL", Op::Respell, "J"),
6465
// MuseScore's add interval: Alt+1–9, a unison to a ninth above.
6566
op("KEYFLOW_INTERVAL_1", Op::Interval(1), "⌥1"),
6667
op("KEYFLOW_INTERVAL_2", Op::Interval(2), "⌥2"),

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

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ pub enum Op {
381381
Target(Target),
382382
/// Shift+A–G: a note added above the selected note's chord.
383383
AddToChord(char),
384+
/// J: the selected note spelled the next way that sounds the same
385+
/// (`C#` → `Db`), MuseScore's respell.
386+
Respell,
384387
/// Alt+1–9: a note that interval above the selected note's chord (its
385388
/// top note) added to it, in the key: MuseScore's "add interval".
386389
Interval(u8),
@@ -475,6 +478,7 @@ impl Op {
475478
| Self::Delete
476479
| Self::AddToChord(_)
477480
| Self::Interval(_)
481+
| Self::Respell
478482
| Self::EnterStep(_)
479483
| Self::Double
480484
| Self::Halve
@@ -723,6 +727,12 @@ impl InputState {
723727
}
724728
Op::AddToChord(letter) => self.add_to_chord(score, letter),
725729
Op::Interval(n) => self.add_interval(score, n),
730+
Op::Respell => {
731+
if self.target == Target::Chords {
732+
return Outcome::refuse("J respells a note: switch to entering notes");
733+
}
734+
self.repitch(score, Pitch::respelled)
735+
}
726736
Op::Voice(voice) => {
727737
if voice > 3 {
728738
return Outcome::refuse("There are four voices");
@@ -1396,6 +1406,62 @@ impl InputState {
13961406
Some((pos.bar, i))
13971407
}
13981408

1409+
/// The selected element as MuseScore's status bar describes it: what
1410+
/// it is, its pitch or symbol, its duration, its voice, its bar and beat
1411+
/// (`Note C4 · quarter · voice 1 · bar 3, beat 2`).
1412+
fn describe_selected(&self, score: &Score, p: Pos) -> String {
1413+
let beat_ticks = TICKS_PER_WHOLE / 4;
1414+
let beat = |tick: u32| {
1415+
let b = f64::from(tick) / f64::from(beat_ticks) + 1.0;
1416+
if b.fract() == 0.0 {
1417+
format!("{b:.0}")
1418+
} else {
1419+
format!("{b:.2}").trim_end_matches('0').to_owned()
1420+
}
1421+
};
1422+
if self.target == Target::Chords {
1423+
if let Some((bar, i)) = self.selected_chord(score) {
1424+
let c = &score.chords[bar];
1425+
let at = c.starts().get(i).copied().unwrap_or(p.tick);
1426+
let e = &c.events[i];
1427+
let what = match &e.slot {
1428+
Slot::Chord(t) => format!("Chord symbol {t}"),
1429+
Slot::Rest => "No chord".to_owned(),
1430+
Slot::Hold => "The chord before, held".to_owned(),
1431+
};
1432+
return format!(
1433+
"{what} · {} · bar {}, beat {}",
1434+
e.duration.name().to_lowercase(),
1435+
bar + 1,
1436+
beat(at)
1437+
);
1438+
}
1439+
} else if let Some((bar, i)) = self.selected_event(score) {
1440+
let b = &score.bars[bar];
1441+
let at = b.starts().get(i).copied().unwrap_or(p.tick);
1442+
let e = &b.events[i];
1443+
let what = match e.pitch() {
1444+
Some(pitch) => {
1445+
let above: Vec<String> = e.above.iter().flatten().map(|q| q.name()).collect();
1446+
if above.is_empty() {
1447+
format!("Note {}", pitch.name())
1448+
} else {
1449+
format!("Chord {} {}", pitch.name(), above.join(" "))
1450+
}
1451+
}
1452+
None => "Rest".to_owned(),
1453+
};
1454+
return format!(
1455+
"{what} · {} · voice {} · bar {}, beat {}",
1456+
e.duration.name().to_lowercase(),
1457+
self.voice + 1,
1458+
bar + 1,
1459+
beat(at)
1460+
);
1461+
}
1462+
format!("bar {} selected", p.bar + 1)
1463+
}
1464+
13991465
/// What the status line says: the mode, what the next key enters, and
14001466
/// where.
14011467
#[must_use]
@@ -1448,8 +1514,8 @@ impl InputState {
14481514
match self.mode {
14491515
Mode::Normal => match self.selected {
14501516
Some(p) => format!(
1451-
"Normal mode · bar {} selected · N: input by note name · M: input by duration",
1452-
p.bar + 1
1517+
"Normal mode · {} · N: input by note name · M: input by duration",
1518+
self.describe_selected(score, p)
14531519
),
14541520
None => "Normal mode · N: input by note name · M: input by duration".to_owned(),
14551521
},
@@ -1465,7 +1531,7 @@ impl InputState {
14651531
.unwrap_or_default()
14661532
),
14671533
Mode::NoteName => format!(
1468-
"Input by note name · notes · voice {} · {} {what}{accidental} · next at {at} · A–G enter, Shift+A–G or Alt+1–9 add to the chord, 0 rest, T tie, Y: chord symbols instead, Esc leave{locked}",
1534+
"Input by note name · notes · voice {} · {} {what}{accidental} · next at {at} · A–G enter, Shift+A–G or Alt+1–9 add to the chord, J respell, 0 rest, T tie, Y: chord symbols instead, Esc leave{locked}",
14691535
self.voice + 1,
14701536
self.duration.name()
14711537
),
@@ -1620,6 +1686,39 @@ mod tests {
16201686
assert!(i.apply(&mut s, Op::AddToChord('B')).refused.is_some());
16211687
}
16221688

1689+
#[test]
1690+
fn normal_mode_s_status_line_describes_the_selection() {
1691+
let mut s = score(1);
1692+
let mut i = input(Mode::NoteName);
1693+
i.apply(&mut s, Op::Duration(Quarter));
1694+
i.apply(&mut s, Op::Letter('C'));
1695+
i.apply(&mut s, Op::Letter('E'));
1696+
i.apply(&mut s, Op::Escape);
1697+
// The last note entered stays selected, as MuseScore leaves it.
1698+
let line = i.status(&s);
1699+
assert!(line.starts_with("Normal mode · Note E"), "{line}");
1700+
assert!(line.contains("quarter · voice 1 · bar 1, beat 2"), "{line}");
1701+
}
1702+
1703+
#[test]
1704+
fn j_respells_the_selected_note_the_next_way_that_sounds_the_same() {
1705+
let spell = |p: Pitch| p.respelled().name();
1706+
assert_eq!(spell(Pitch::new('C', 1, 4)), "D♭4");
1707+
assert_eq!(spell(Pitch::new('D', -1, 4)), "B𝄪3");
1708+
assert_eq!(spell(Pitch::new('B', 2, 3)), "C♯4");
1709+
assert_eq!(spell(Pitch::new('E', 0, 4)), "F♭4");
1710+
let mut s = score(1);
1711+
let mut i = input(Mode::NoteName);
1712+
i.apply(&mut s, Op::Duration(Half));
1713+
i.apply(&mut s, Op::Accidental(1));
1714+
i.apply(&mut s, Op::Letter('C'));
1715+
let before = s.bars[0].events[0].pitch().unwrap();
1716+
i.apply(&mut s, Op::Respell);
1717+
let after = s.bars[0].events[0].pitch().unwrap();
1718+
assert_eq!(after.midi(), before.midi(), "it sounds the same");
1719+
assert_eq!((after.letter, after.alter), ('D', -1));
1720+
}
1721+
16231722
#[test]
16241723
fn alt_number_adds_an_interval_above_the_chord_in_the_key() {
16251724
let mut s = score(1);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,33 @@ impl Pitch {
257257
self
258258
}
259259

260+
/// The next way to spell it, sounding the same (MuseScore's J): its
261+
/// spellings with no more than a double sharp or flat, the plainest
262+
/// first (`C#` → `Db` → `B##` → `C#`; `E` → `Fb` → `D##` → `E`).
263+
#[must_use]
264+
pub fn respelled(self) -> Self {
265+
let midi = self.midi();
266+
let mut all: Vec<Self> = Vec::new();
267+
for letter in LETTERS.chars() {
268+
for alter in -2..=2_i8 {
269+
for octave in self.octave - 1..=self.octave + 1 {
270+
let p = Self::new(letter, alter, octave);
271+
if p.midi() == midi {
272+
all.push(p);
273+
}
274+
}
275+
}
276+
}
277+
all.sort_by_key(|p| (p.alter.abs(), p.alter < 0, p.step()));
278+
let at = all
279+
.iter()
280+
.position(|p| {
281+
p.letter == self.letter && p.alter == self.alter && p.octave == self.octave
282+
})
283+
.unwrap_or(0);
284+
all[(at + 1) % all.len()]
285+
}
286+
260287
/// Its MIDI number (C4 = 60).
261288
#[must_use]
262289
pub fn midi(self) -> i32 {

0 commit comments

Comments
 (0)