Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 148x 26x 87x 26x 41x 41x 26x 26x 26x 26x 41x 41x 119x 26x 15x 62x 4x 4x 58x 1x 57x 62x 1x 56x 62x 19x 19x 19x 19x 722x 158x 19x 15x 19x 19x 158x 158x 19x 139x 139x 19x 5x 5x 10x 10x 1x 9x 5x 14x 28x 320x 14x | /**
* Chromatic note-name array (12 entries starting from C);
* flat-preferring by default (also used as MIDI key-signature labels),
* sharp-preferring when keyIndex (kappa) is a sharp-favouring key.
* @param {number} [keyIndex] - 0-11 (kappa); omitted means flat-preferring.
* @return {string[]}
* @example
* noteNames(); // ["C", "Db", "D", "Eb", ...] flat default
* noteNames(0); // ["C", "C#", "D", "D#", ...] C major prefers sharps
* noteNames(5); // ["C", "Db", "D", "Eb", ...] F major prefers flats
*/
export function noteNames(keyIndex) {
return new Set([0, 2, 4, 7, 9, 11]).has(keyIndex)
? "C C# D D# E F F# G G# A A# B".split(" ")
: "C Db D Eb E F Gb G Ab A Bb B".split(" ");
}
/**
* Per-part time-grid; subdivision (tau) is the LCM of voice durations.
* @param {{duration: string}[]} voice - Per-voice configs; only the first 'voices' entries are active.
* @param {number} voices - Active voice count (|S|).
* @param {number} numerator - Time-signature numerator (zeta).
* @param {number} bars - Bar count for this part (iota).
* @return {{subdivision: number, subBeatsPerBar: number, numSubBeats: number, activeBeats: Set<number>[]}}
* Subdivision (tau); sub-beats per bar; T = iota * zeta * tau; per-voice onset sets (omicron).
*/
export function timeGrid(voice, voices, numerator, bars) {
const subdiv = {
whole: 1,
half: 1,
quarter: 1,
eighth: 2,
triplet: 3,
sixteenth: 4,
};
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
const subdivision = voice.reduce((acc, v) => {
const s = subdiv[v.duration] || 1;
return (acc / gcd(acc, s)) * s;
}, 1);
const subBeatsPerBar = numerator * subdivision;
const numSubBeats = bars * subBeatsPerBar;
const stride = {
whole: numerator * subdivision,
half: Math.max(1, Math.round((numerator * subdivision) / 2)),
quarter: subdivision,
eighth: Math.max(1, Math.round(subdivision / 2)),
triplet: Math.max(1, Math.round(subdivision / 3)),
sixteenth: Math.max(1, Math.round(subdivision / 4)),
};
const activeBeats = voice.slice(0, voices).map((v) => {
const s = stride[v.duration];
const count = Math.floor(((bars - 1) * subBeatsPerBar) / s) + 1;
return new Set(Array.from({ length: count }, (_, i) => i * s));
});
return { subdivision, subBeatsPerBar, numSubBeats, activeBeats };
}
/**
* Chord tones per mode (mu): pitch classes (mod 12) for each Roman-numeral.
* Insertion order = diatonic ordering. "NC" allows all classes.
* V and NC are duplicated across major/minor (small DRY trade-off).
* @type {{major: Object<string, number[]>, minor: Object<string, number[]>}}
*/
export const CHORDS = {
major: {
I: [0, 4, 7],
ii: [2, 5, 9],
iii: [4, 7, 11],
IV: [5, 9, 0],
V: [7, 11, 2],
vi: [9, 0, 4],
viio: [11, 2, 5],
NC: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
},
minor: {
i: [0, 3, 7],
iio: [2, 5, 8],
III: [3, 7, 10],
iv: [5, 8, 0],
V: [7, 11, 2],
bVI: [8, 0, 3],
bVII: [10, 2, 5],
NC: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
},
};
/**
* Roman-numeral display: with keyIndex returns the concrete chord name;
* without keyIndex returns the function-label form for UI dropdowns.
* @param {string} node - Roman-numeral chord symbol; "NC" and unknowns pass through.
* @param {number} [keyIndex] - 0-11 (kappa); omitted means label form.
* @return {string}
* @example
* chordName("I"); // "I: Tonic" (label form, key-independent)
* chordName("V", 0); // "G" (dominant in C major)
* chordName("vi", 0); // "Am" (minor from lowercase numeral)
* chordName("viio", 0); // "Bo" (diminished from "o" suffix)
* chordName("NC", 0); // "NC"
*/
export function chordName(node, keyIndex) {
if (keyIndex === undefined) {
const labels = {
I: "Tonic",
ii: "Supertonic",
iii: "Mediant",
IV: "Subdominant",
V: "Dominant",
vi: "Submediant",
viio: "Leading Tone",
i: "Tonic",
iio: "Supertonic",
III: "Mediant",
iv: "Subdominant",
bVI: "Submediant",
bVII: "Subtonic",
NC: "No Chord",
};
return labels[node] ? `${node}: ${labels[node]}` : node;
}
if (node === "NC") {
return "NC";
}
const root = (CHORDS.major[node] ?? CHORDS.minor[node])?.[0];
if (root === undefined) {
return node;
}
const suffix = /o$/.test(node) ? "o" : /^b?[a-z]/.test(node) ? "m" : "";
return noteNames(keyIndex)[(root + keyIndex) % 12] + suffix;
}
/**
* Creates the chord digraph G = (V, E, w) for a given mode, optionally tuned by a corpus preset (pi).
* @param {'major'|'minor'} [mode='major'] - Tonal mode (mu); drives node set and default costs.
* @param {?Object<string, {edges: Object<string, number>, chromatic: string[]}>} [preset]
* Corpus preset (pi) keyed by mode (mu);
* per mode edges (corpus-frequency nu thresholded by epsilon) and chromatic nodes;
* null falls back to DEFAULTS.
* @return {{
* nodes: string[],
* activeNodes: string[],
* edges: [string,string][],
* costs: Map<string,number>,
* tonic: string,
* }}
* nodes (V, incl. chromatic), activeNodes (subset participating in edges),
* edges (E, valid-edge keys of costs), costs (w, edge cost map), tonic ("I" or "i").
*/
export function chordGraph(mode = "major", preset = null) {
const tonic = mode === "minor" ? "i" : "I";
const presetMode = preset?.[mode];
const nodes = [
...Object.keys(CHORDS[mode]),
...(presetMode?.chromatic ?? []),
];
const costs = edgeCosts(nodes, mode, presetMode);
const edges = [...costs.keys()].map((k) => k.split("->"));
const activeNodes = nodes.filter((n) => new Set(edges.flat()).has(n));
return { nodes, activeNodes, edges, costs, tonic };
}
/**
* Default chord-graph configuration:
* structural costs (LOOP, NC, MISSING) and edge tables [cost, {major, minor}].
* CADENCE (after NORMAL) overwrites shared edges.
* MISSING is the sentinel cost for non-edges in the MZN cost matrix:
* higher than any valid cost so the solver never traverses non-edge cells.
* @type {{
* CADENCE: [number, {major: string, minor: string}],
* NORMAL: [number, {major: string, minor: string}],
* NC: number,
* LOOP: number,
* MISSING: number,
* }}
*/
export const EDGES = {
CADENCE: [
1,
{
major: `V->I IV->I ii->V`,
minor: `V->i iv->i iio->V`,
},
],
NORMAL: [
2,
{
major: `
I->ii I->iii I->IV I->V I->vi I->viio
ii->V ii->viio
iii->vi iii->IV
IV->V IV->I IV->viio
V->I V->vi
vi->ii vi->IV
viio->I viio->iii
`,
minor: `
i->iv i->V i->bVI i->bVII i->III i->iio
iv->V iv->i iv->iio iv->bVII
V->i V->bVI
iio->V
III->bVI III->bVII III->iv
bVI->bVII bVI->iv bVI->iio bVI->V
bVII->III bVII->i bVII->bVI
`,
},
],
NC: 4,
LOOP: 8,
MISSING: 16,
};
/**
* @private Calculates costs with preset edges (skipping out-of-graph nodes) or
* default NORMAL+CADENCE table for the mode.
*/
function edgeCosts(nodes, mode, presetMode) {
const costs = new Map();
for (const v of nodes) {
costs.set(`${v}->${v}`, EDGES.LOOP);
if (v === "NC") {
continue;
}
costs.set(`${v}->NC`, EDGES.NC);
costs.set(`NC->${v}`, EDGES.NC);
}
if (presetMode) {
const nodeSet = new Set(nodes);
for (const [edge, cost] of Object.entries(presetMode.edges)) {
const [from, to] = edge.split("->");
if (!nodeSet.has(from) || !nodeSet.has(to)) {
continue;
}
costs.set(edge, from === to ? Math.max(cost, EDGES.LOOP) : cost);
}
return costs; // skip defaults if preset edges exist
}
for (const [cost, byMode] of [EDGES.NORMAL, EDGES.CADENCE]) {
for (const edge of byMode[mode].trim().split(/\s+/)) {
costs.set(edge, cost);
}
}
return costs;
}
|