Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-2107]): keySignature() resolves minor keys to relative major

Adam Eivyb18ecfe63ae3
diff --git a/client/src/lib/scoreNotation.js b/client/src/lib/scoreNotation.js
index 169f17264d..2a97151e9d 100644
--- a/client/src/lib/scoreNotation.js
+++ b/client/src/lib/scoreNotation.js
@@ -68,13 +68,37 @@ const FLAT_ORDER = ['B', 'E', 'A', 'D', 'G', 'C', 'F'];
const SHARP_KEYS = { G: 1, D: 2, A: 3, E: 4, B: 5, 'F#': 6, 'C#': 7 };
const FLAT_KEYS = { F: 1, Bb: 2, Eb: 3, Ab: 4, Db: 5, Gb: 6, Cb: 7 };
-// Resolve a key string ("C major", "Eb", "F# minor") to a key-signature
-// descriptor. We read only the tonic (letter + optional single accidental) and
-// treat it as major — good enough for a lead sheet, and the renderer only uses
-// this to draw the accidental cluster at the clef. Unknown → C (no accidentals).
+// Minor tonic → its relative major (tonic + minor third up), so a minor key
+// resolves through the SHARP_KEYS/FLAT_KEYS major lookup above instead of
+// (incorrectly) reusing its own letter as if it were major. Covers the 15
+// keys reachable through SHARP_KEYS/FLAT_KEYS + natural C/Am. Explicit modes
+// (dorian, mixolydian, …) are out of scope — score those in C (all naturals)
+// per the existing convention, same as the seed data does.
+const MINOR_TO_MAJOR = {
+ A: 'C', E: 'G', B: 'D', 'F#': 'A', 'C#': 'E', 'G#': 'B', 'D#': 'F#', 'A#': 'C#',
+ D: 'F', G: 'Bb', C: 'Eb', F: 'Ab', Bb: 'Db', Eb: 'Gb', Ab: 'Cb',
+};
+
+// Matches a minor qualifier immediately following the tonic: `m`, `min`, or
+// `minor` (case-insensitive), optionally preceded by whitespace. The trailing
+// `\b` is what keeps this from misfiring on "major"/"maj" — after matching the
+// bare `m`, the next character in "major" is the word-char `a`, so there's no
+// word boundary and the whole match fails, leaving the tonic to resolve as
+// major (unchanged default behavior).
+const MINOR_QUALIFIER = /^\s*m(?:in(?:or)?)?\b/i;
+
+// Resolve a key string ("C major", "Eb", "D minor", "F# minor", "Bbm") to a
+// key-signature descriptor. We read the tonic (letter + optional single
+// accidental) plus an optional minor qualifier right after it; a minor tonic
+// is mapped to its relative major before the lookup, so the signature matches
+// the actual key rather than the parallel major. Unknown → C (no accidentals).
export const keySignature = (keyName) => {
- const m = /^\s*([A-Ga-g])([#b]?)/.exec(keyName || '');
- const tonic = m ? `${m[1].toUpperCase()}${m[2]}` : 'C';
+ const raw = keyName || '';
+ const m = /^\s*([A-Ga-g])([#b]?)/.exec(raw);
+ let tonic = m ? `${m[1].toUpperCase()}${m[2]}` : 'C';
+ if (m && MINOR_QUALIFIER.test(raw.slice(m[0].length)) && MINOR_TO_MAJOR[tonic]) {
+ tonic = MINOR_TO_MAJOR[tonic];
+ }
if (SHARP_KEYS[tonic]) {
const count = SHARP_KEYS[tonic];
return { type: 'sharp', count, letters: SHARP_ORDER.slice(0, count) };
diff --git a/client/src/lib/scoreNotation.test.js b/client/src/lib/scoreNotation.test.js
index 62b699534b..942956cacc 100644
--- a/client/src/lib/scoreNotation.test.js
+++ b/client/src/lib/scoreNotation.test.js
@@ -73,11 +73,25 @@ describe('keySignature', () => {
});
it('reads flat keys', () => {
expect(keySignature('F')).toMatchObject({ type: 'flat', count: 1, letters: ['B'] });
- expect(keySignature('Eb minor')).toMatchObject({ type: 'flat', count: 3 });
+ // Eb minor's relative major is Gb (6 flats), not Eb major (3 flats) —
+ // regression check for treating a minor tonic as if it were major.
+ expect(keySignature('Eb minor')).toMatchObject({ type: 'flat', count: 6 });
});
it('falls back to C for unknown input', () => {
expect(keySignature('nonsense')).toMatchObject({ type: 'none', count: 0 });
});
+ it('resolves a minor key to its relative major signature', () => {
+ expect(keySignature('D minor')).toMatchObject({ type: 'flat', count: 1, letters: ['B'] });
+ expect(keySignature('Em')).toMatchObject({ type: 'sharp', count: 1, letters: ['F'] });
+ expect(keySignature('F# minor')).toMatchObject({ type: 'sharp', count: 3, letters: ['F', 'C', 'G'] });
+ expect(keySignature('Bbm')).toMatchObject({ type: 'flat', count: 5, letters: ['B', 'E', 'A', 'D', 'G'] });
+ expect(keySignature('D min')).toMatchObject({ type: 'flat', count: 1 });
+ });
+ it('major behavior is unchanged, and "major"/"maj" never misparse as minor', () => {
+ expect(keySignature('D')).toMatchObject({ type: 'sharp', count: 2 });
+ expect(keySignature('D major')).toMatchObject({ type: 'sharp', count: 2 });
+ expect(keySignature('Dmaj')).toMatchObject({ type: 'sharp', count: 2 });
+ });
});
describe('parseScore', () => {
diff --git a/client/src/lib/singToScore.test.js b/client/src/lib/singToScore.test.js
index 2c0ac38fbd..e083aacbff 100644
--- a/client/src/lib/singToScore.test.js
+++ b/client/src/lib/singToScore.test.js
@@ -167,6 +167,16 @@ describe('segmentsToScoreDsl', () => {
expect(sharp).toContain('A#4');
});
+ it('spells accidentals with flats for a minor key (relative-major signature, not parallel major)', () => {
+ // D minor's relative major is F (1 flat) — it should spell the flat side
+ // (Bb) even though "D" alone (D major, 2 sharps) would spell the sharp
+ // side (A#) for the same pitch.
+ const hz = noteToFrequency({ letter: 'A', accidental: '#', octave: 4 });
+ const quantized = [{ rest: false, hz, code: 'q', dots: 0, beats: 1 }];
+ const minor = segmentsToScoreDsl(quantized, { keySig: keySignature('D minor'), beatsPerBar: 4 });
+ expect(minor).toContain('Bb4');
+ });
+
it('renders an interior rest (and trims leading/trailing rests)', () => {
const quantized = [
{ rest: true, code: 'q', dots: 0, beats: 1 }, // leading rest — trimmed