@audiofool/moondog 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +4 -0
- package/README.md +8 -4
- package/licenses/dawn-sdk-mit.md +27 -0
- package/package.json +2 -1
- package/src/core/capability-catalog.mjs +28 -5
- package/src/core/moondog-application.mjs +54 -3
- package/src/i18n/messages/de.mjs +3 -2
- package/src/i18n/messages/en.mjs +3 -2
- package/src/i18n/messages/es.mjs +3 -2
- package/src/i18n/messages/fr.mjs +3 -2
- package/src/i18n/messages/it.mjs +3 -2
- package/src/i18n/messages/ja.mjs +3 -2
- package/src/i18n/messages/ko.mjs +3 -2
- package/src/i18n/messages/pt.mjs +3 -2
- package/src/i18n/messages/zh-Hant.mjs +3 -2
- package/src/i18n/messages/zh.mjs +3 -2
- package/src/i18n/screen/es.mjs +36 -16
- package/src/i18n/screen/ja.mjs +36 -16
- package/src/i18n/screen/pt.mjs +36 -16
- package/src/i18n/screen/zh.mjs +36 -16
- package/src/integrations/score/render-score.mjs +105 -0
- package/src/integrations/score/sheet.mjs +31 -0
- package/src/integrations/score/track-score.mjs +325 -0
- package/src/integrations/score/vendor/dawn-sdk.mjs +561 -0
- package/src/integrations/stable-audio/local-generator.mjs +139 -42
- package/src/integrations/stable-audio/setup.mjs +95 -46
- package/src/integrations/stable-audio/sources.mjs +151 -2
- package/src/runtime/pi/agent-runtime.mjs +148 -16
- package/src/runtime/pi/model-settings.mjs +13 -7
- package/src/surfaces/cli/brand-components.mjs +1 -1
- package/src/surfaces/cli/command-completions.mjs +11 -3
- package/src/surfaces/cli/format-output.mjs +2 -2
- package/src/surfaces/cli/tui.mjs +54 -25
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// What a lead sheet may hold: the limits and the names the agent's tool and the score builder share.
|
|
2
|
+
// It is kept apart from the builder so that reading it never loads the renderer.
|
|
3
|
+
|
|
4
|
+
export const SCORE_LIMITS = Object.freeze({
|
|
5
|
+
tempo: Object.freeze({ min: 50, max: 180 }),
|
|
6
|
+
parts: 6,
|
|
7
|
+
sections: 12,
|
|
8
|
+
sectionBars: 16,
|
|
9
|
+
repeat: 4,
|
|
10
|
+
seconds: Object.freeze({ min: 15, max: 300 }),
|
|
11
|
+
lineCharacters: 1_500,
|
|
12
|
+
drumBars: 4,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const SCORE_ROLES = Object.freeze(["chords", "bass", "drums", "line"]);
|
|
16
|
+
|
|
17
|
+
// The sounds a part can have, by role. The renderer synthesizes every one; none is a sample or a noise bed.
|
|
18
|
+
export const SCORE_SOUNDS = Object.freeze({
|
|
19
|
+
chords: Object.freeze(["rhodes", "wurlitzer", "felt-piano", "pad", "harp", "dulcimer"]),
|
|
20
|
+
bass: Object.freeze(["round-bass", "upright-bass", "sub-bass"]),
|
|
21
|
+
drums: Object.freeze(["lofi", "acoustic", "808", "909", "electro", "trap"]),
|
|
22
|
+
line: Object.freeze(["vibes", "marimba", "kalimba", "glass-bell", "flute", "cello", "viola", "voice", "rhodes", "felt-piano", "harp", "upright-bass", "round-bass"]),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// How a chords part plays each bar's chord, and how a bass part plays its root.
|
|
26
|
+
export const SCORE_STYLES = Object.freeze({
|
|
27
|
+
chords: Object.freeze(["held", "rolled", "arp", "pulse"]),
|
|
28
|
+
bass: Object.freeze(["long", "push", "pulse", "root-fifth"]),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const SCORE_DRUM_VOICES = Object.freeze(["kick", "snare", "rim", "clap", "hat", "openhat", "tom"]);
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// A track written as a score. Moondog's agent writes a compact lead sheet (tempo, key, parts, and
|
|
2
|
+
// sections with one chord a bar, drum grids and written lines); this module turns it into notes with
|
|
3
|
+
// the DAWN SDK (vendor/dawn-sdk.mjs), checks them with the SDK's lint and hands back a score the
|
|
4
|
+
// renderer plays. Nothing here needs a model, a GPU or a download, so it works on every machine.
|
|
5
|
+
import {
|
|
6
|
+
ambientVoicing, chord, hit, lintSong, note, progression, scoreFromJSON, song, SONG_CALIBRATION_LATEST, track,
|
|
7
|
+
} from "./vendor/dawn-sdk.mjs";
|
|
8
|
+
import { SCORE_DRUM_VOICES, SCORE_LIMITS, SCORE_ROLES, SCORE_SOUNDS, SCORE_STYLES } from "./sheet.mjs";
|
|
9
|
+
|
|
10
|
+
export { SCORE_DRUM_VOICES, SCORE_LIMITS, SCORE_ROLES, SCORE_SOUNDS, SCORE_STYLES };
|
|
11
|
+
|
|
12
|
+
const WARMTH = 0.6;
|
|
13
|
+
const VOICINGS = Object.freeze({
|
|
14
|
+
rhodes: "tine-piano", wurlitzer: "reed-piano", "felt-piano": "felt-piano", pad: "drift-pad", harp: "harp", dulcimer: "dulcimer",
|
|
15
|
+
vibes: "soft-vibes", marimba: "marimba", kalimba: "thumb-piano", "glass-bell": "glass-bell", flute: "bamboo-flute",
|
|
16
|
+
cello: "cello", viola: "viola", voice: "alto-vowel", "upright-bass": "upright-bass",
|
|
17
|
+
});
|
|
18
|
+
const KITS = Object.freeze({ lofi: "lofi", acoustic: "acoustic", 808: "syn808", 909: "syn909", electro: "electro", trap: "trap" });
|
|
19
|
+
|
|
20
|
+
// How a sound is played: the SDK's own voicings, and two plain synth basses.
|
|
21
|
+
function instrumentOf(sound) {
|
|
22
|
+
if (sound === "round-bass") return { instrument: "triangle", synth: { attack: 0.02, decay: 1.2, sustain: 0.7, release: 0.35 }, filter: { cutoff: 240, ftype: "24db" } };
|
|
23
|
+
if (sound === "sub-bass") return { instrument: "sine", synth: { attack: 0.02, decay: 1.2, sustain: 0.8, release: 0.3 } };
|
|
24
|
+
if (KITS[sound]) return { instrument: "kit", kit: KITS[sound] };
|
|
25
|
+
const voiced = ambientVoicing(VOICINGS[sound], WARMTH);
|
|
26
|
+
// The slow tremolo and slight drift of an electric piano through an old amplifier.
|
|
27
|
+
return sound === "rhodes" ? { ...voiced, keys: { ...voiced.keys, vib: 0.45, vibmod: 0.035, vibe: 0.24, vibehz: 2.1 } } : voiced;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const MIX = Object.freeze({
|
|
31
|
+
chords: { volume: 0.72, reverb: { mix: 0.26, size: 0.7, fade: 2.2, lowpass: 5200, dim: 2400, predelay: 0.02 } },
|
|
32
|
+
bass: { volume: 0.6 },
|
|
33
|
+
drums: { volume: 0.55, reverb: { mix: 0.08, size: 0.4, fade: 0.8 } },
|
|
34
|
+
line: { volume: 0.5, reverb: { mix: 0.3, size: 0.75, fade: 2.6, lowpass: 6000 } },
|
|
35
|
+
});
|
|
36
|
+
const MASTER = Object.freeze({ tape: { drive: 3, mix: 0.6 }, limiter: { ceiling: -1.5 }, target: -16 });
|
|
37
|
+
|
|
38
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
39
|
+
const text = (value) => (typeof value === "string" ? value.trim() : "");
|
|
40
|
+
const round = (value) => Math.round(value * 1000) / 1000;
|
|
41
|
+
|
|
42
|
+
function chordsOf(value) {
|
|
43
|
+
const symbols = Array.isArray(value) ? value.map(text) : text(value).split(/[\s|,]+/u);
|
|
44
|
+
return symbols.filter(Boolean);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// "Ab4:1 F5:1.5 -:0.5": one pitch and its length in beats after another; "-" is a rest.
|
|
48
|
+
function parseLine(source, where, problems) {
|
|
49
|
+
const notes = [];
|
|
50
|
+
let at = 0;
|
|
51
|
+
for (const token of text(source).split(/[\s,|]+/u).filter(Boolean)) {
|
|
52
|
+
const match = /^(-|r|[A-Ga-g][#b]?-?\d):(\d+(?:\.\d+)?|\d+\/\d+)$/u.exec(token);
|
|
53
|
+
if (!match) {
|
|
54
|
+
problems.push({ where, problem: `"${token.slice(0, 16)}" is not pitch:beats. Write notes like Ab4:1 or F#3:0.5, and rests like -:2.` });
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const [whole, over] = match[2].split("/");
|
|
58
|
+
const beats = over ? Number(whole) / Number(over) : Number(whole);
|
|
59
|
+
if (!(beats > 0) || beats > 64) {
|
|
60
|
+
problems.push({ where, problem: `"${token}" needs a length in beats above 0.` });
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
if (match[1] !== "-" && match[1] !== "r") notes.push({ pitch: match[1][0].toUpperCase() + match[1].slice(1), at, beats });
|
|
64
|
+
at += beats;
|
|
65
|
+
}
|
|
66
|
+
return { notes, beats: at };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// "x...x...x...x...": sixteenth steps, x a hit, X an accent, o a soft one, . a rest.
|
|
70
|
+
function parseGrid(source, stepsPerBar, where, problems) {
|
|
71
|
+
const steps = text(source).replace(/[\s|]/gu, "");
|
|
72
|
+
if (!steps || /[^xXo.\-]/u.test(steps)) {
|
|
73
|
+
problems.push({ where, problem: "A drum grid uses only x (hit), X (accent), o (soft) and . (rest)." });
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
if (steps.length % stepsPerBar !== 0 || steps.length / stepsPerBar > SCORE_LIMITS.drumBars) {
|
|
77
|
+
problems.push({ where, problem: `A drum grid has ${stepsPerBar} steps a bar (sixteenths), for 1 to ${SCORE_LIMITS.drumBars} bars; this one has ${steps.length} steps.` });
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return steps;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const VELOCITY = Object.freeze({ x: 0.6, X: 0.85, o: 0.3 });
|
|
84
|
+
|
|
85
|
+
function bassNotes(style, rootPitch, at, beatsPerBar, plainFifth) {
|
|
86
|
+
const fifth = plainFifth ? rootPitch + 7 : rootPitch;
|
|
87
|
+
if (style === "pulse") return Array.from({ length: beatsPerBar }, (_, beat) => note(rootPitch, at + beat, 0.9, beat === 0 ? 0.62 : 0.5));
|
|
88
|
+
if (style === "root-fifth") {
|
|
89
|
+
const half = beatsPerBar === 4 ? 2 : beatsPerBar - 1;
|
|
90
|
+
return [note(rootPitch, at, half - 0.1, 0.62), note(fifth, at + half, beatsPerBar - half - 0.1, 0.52)];
|
|
91
|
+
}
|
|
92
|
+
if (style === "push") {
|
|
93
|
+
return beatsPerBar === 4
|
|
94
|
+
? [note(rootPitch, at, 2.3, 0.62), note(rootPitch, at + 2.5, 1.2, 0.5)]
|
|
95
|
+
: [note(rootPitch, at, beatsPerBar - 1.1, 0.62), note(rootPitch, at + beatsPerBar - 1, 0.9, 0.5)];
|
|
96
|
+
}
|
|
97
|
+
return [note(rootPitch, at, beatsPerBar - 0.1, 0.62)];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The chords of one part across the whole track. Voice-led chords move least, but a seventh or a ninth
|
|
101
|
+
// can then land a semitone under the root low in the keyboard, which sounds muddy; the lint names that
|
|
102
|
+
// (harmony.low-second), so the voicing with the fewest such clashes is kept.
|
|
103
|
+
function chordNotes(style, bars, beatsPerBar, options) {
|
|
104
|
+
const base = { vel: 0.5, anchor: "C3", ...options };
|
|
105
|
+
const notes = [];
|
|
106
|
+
for (const run of bars) {
|
|
107
|
+
if (style === "pulse") {
|
|
108
|
+
const symbols = run.symbols.flatMap((symbol) => Array.from({ length: beatsPerBar }, () => symbol));
|
|
109
|
+
notes.push(...progression(symbols, { ...base, from: run.at, each: 1 }));
|
|
110
|
+
} else if (style === "arp") {
|
|
111
|
+
notes.push(...progression(run.symbols, { ...base, from: run.at, each: beatsPerBar, perform: "arp-up", rate: 0.5 }));
|
|
112
|
+
} else {
|
|
113
|
+
notes.push(...progression(run.symbols, { ...base, from: run.at, each: beatsPerBar, strum: style === "rolled" ? 0.07 : 0.02 }));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return notes;
|
|
117
|
+
}
|
|
118
|
+
const CHORD_VOICINGS = Object.freeze([{ lead: true, anchor: "C3" }, { lead: true, anchor: "F3" }, { lead: false, anchor: "C3" }]);
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Turns a lead sheet into a score. Returns { ok: true, score, seconds, bars, summary, notes } where
|
|
122
|
+
* score is what the renderer takes and notes are what the lint heard in it, or { ok: false, problems }
|
|
123
|
+
* with one plain sentence for each thing to fix.
|
|
124
|
+
*/
|
|
125
|
+
export function buildTrackScore(sheet) {
|
|
126
|
+
const problems = [];
|
|
127
|
+
const fail = () => ({ ok: false, problems: problems.slice(0, 8) });
|
|
128
|
+
if (!isRecord(sheet)) return { ok: false, problems: [{ where: "score", problem: "The score must be an object." }] };
|
|
129
|
+
|
|
130
|
+
const tempo = Number(sheet.tempo);
|
|
131
|
+
if (!Number.isFinite(tempo) || tempo < SCORE_LIMITS.tempo.min || tempo > SCORE_LIMITS.tempo.max) {
|
|
132
|
+
problems.push({ where: "tempo", problem: `tempo is ${SCORE_LIMITS.tempo.min} to ${SCORE_LIMITS.tempo.max} beats a minute.` });
|
|
133
|
+
}
|
|
134
|
+
const key = text(sheet.key);
|
|
135
|
+
if (!/^[A-Ga-g][#b]?\s+\S+/u.test(key)) problems.push({ where: "key", problem: 'key is a tonic and a scale, such as "Db major", "A minor" or "D dorian".' });
|
|
136
|
+
const beatsPerBar = sheet.beats_per_bar === undefined ? 4 : Number(sheet.beats_per_bar);
|
|
137
|
+
if (![3, 4].includes(beatsPerBar)) problems.push({ where: "beats_per_bar", problem: "beats_per_bar is 3 or 4." });
|
|
138
|
+
|
|
139
|
+
const parts = new Map();
|
|
140
|
+
const partList = Array.isArray(sheet.parts) ? sheet.parts : [];
|
|
141
|
+
if (partList.length < 1 || partList.length > SCORE_LIMITS.parts) problems.push({ where: "parts", problem: `A track has 1 to ${SCORE_LIMITS.parts} parts.` });
|
|
142
|
+
for (const [index, part] of partList.slice(0, SCORE_LIMITS.parts).entries()) {
|
|
143
|
+
const where = `parts[${index}]`;
|
|
144
|
+
const name = text(part?.name).toLowerCase();
|
|
145
|
+
if (!/^[a-z][a-z0-9 _-]{0,23}$/u.test(name) || parts.has(name)) {
|
|
146
|
+
problems.push({ where, problem: "Each part needs its own short name, such as keys, bass, drums or lead." });
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (!SCORE_ROLES.includes(part.role)) {
|
|
150
|
+
problems.push({ where, problem: `role is one of ${SCORE_ROLES.join(", ")}.` });
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (!SCORE_SOUNDS[part.role].includes(part.sound)) {
|
|
154
|
+
problems.push({ where, problem: `A ${part.role} part's sound is one of ${SCORE_SOUNDS[part.role].join(", ")}.` });
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const styles = SCORE_STYLES[part.role];
|
|
158
|
+
const style = part.style ?? styles?.[0];
|
|
159
|
+
if (styles && !styles.includes(style)) {
|
|
160
|
+
problems.push({ where, problem: `A ${part.role} part's style is one of ${styles.join(", ")}.` });
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
let grids = null;
|
|
164
|
+
if (part.role === "drums") {
|
|
165
|
+
const beat = isRecord(part.beat) ? part.beat : {};
|
|
166
|
+
grids = [];
|
|
167
|
+
for (const [voice, grid] of Object.entries(beat)) {
|
|
168
|
+
if (!SCORE_DRUM_VOICES.includes(voice)) problems.push({ where: `${where}.beat`, problem: `Drum voices are ${SCORE_DRUM_VOICES.join(", ")}; "${voice.slice(0, 16)}" is not one.` });
|
|
169
|
+
else {
|
|
170
|
+
const steps = parseGrid(grid, beatsPerBar * 4, `${where}.beat.${voice}`, problems);
|
|
171
|
+
if (steps) grids.push({ voice, steps });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!Object.keys(beat).length) problems.push({ where: `${where}.beat`, problem: 'A drums part needs a beat, such as { "kick": "x.......x.x.....", "snare": "....x.......x...", "hat": "x.x.x.x.x.x.x.x." }.' });
|
|
175
|
+
}
|
|
176
|
+
const level = part.level === undefined ? 1 : Number(part.level);
|
|
177
|
+
if (!(level >= 0.2 && level <= 1.2)) problems.push({ where, problem: "level is 0.2 to 1.2; 1 is the usual balance." });
|
|
178
|
+
parts.set(name, { name, role: part.role, sound: part.sound, style, grids, level: level >= 0.2 && level <= 1.2 ? level : 1, notes: [], runs: [] });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const sections = Array.isArray(sheet.sections) ? sheet.sections : [];
|
|
182
|
+
if (sections.length < 1 || sections.length > SCORE_LIMITS.sections) problems.push({ where: "sections", problem: `A track has 1 to ${SCORE_LIMITS.sections} sections.` });
|
|
183
|
+
if (problems.length) return fail();
|
|
184
|
+
|
|
185
|
+
const harmony = [];
|
|
186
|
+
const form = [];
|
|
187
|
+
let bar = 0;
|
|
188
|
+
for (const [index, section] of sections.entries()) {
|
|
189
|
+
const where = `sections[${index}]`;
|
|
190
|
+
const symbols = chordsOf(section?.chords);
|
|
191
|
+
if (symbols.length < 1 || symbols.length > SCORE_LIMITS.sectionBars) {
|
|
192
|
+
problems.push({ where: `${where}.chords`, problem: `A section has 1 to ${SCORE_LIMITS.sectionBars} bars: write one chord symbol for each bar, and repeat a symbol to hold it.` });
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
for (const symbol of symbols) {
|
|
196
|
+
try { chord(symbol, 0, 1); } catch { problems.push({ where: `${where}.chords`, problem: `"${symbol.slice(0, 16)}" is not a chord symbol. Use symbols like Dbmaj7, Bbm7, G7sus4, Fadd9 or C/E.` }); }
|
|
197
|
+
}
|
|
198
|
+
const repeat = section.repeat === undefined ? 1 : Number(section.repeat);
|
|
199
|
+
if (!Number.isInteger(repeat) || repeat < 1 || repeat > SCORE_LIMITS.repeat) {
|
|
200
|
+
problems.push({ where: `${where}.repeat`, problem: `repeat is 1 to ${SCORE_LIMITS.repeat}.` });
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const playing = section.plays === undefined ? [...parts.keys()] : (Array.isArray(section.plays) ? section.plays : []).map((name) => text(name).toLowerCase());
|
|
204
|
+
for (const name of playing) if (!parts.has(name)) problems.push({ where: `${where}.plays`, problem: `"${name.slice(0, 24)}" is not one of the parts (${[...parts.keys()].join(", ")}).` });
|
|
205
|
+
// The written lines: a list of { part, notes }, which every model's tool schema can carry, or an object by part name.
|
|
206
|
+
const lines = (Array.isArray(section.lines) ? section.lines.map((entry) => [entry?.part, entry?.notes]) : isRecord(section.lines) ? Object.entries(section.lines) : [])
|
|
207
|
+
.map(([name, line]) => [text(name).toLowerCase(), line]);
|
|
208
|
+
const written = new Map();
|
|
209
|
+
for (const [name, line] of lines) {
|
|
210
|
+
const part = parts.get(name);
|
|
211
|
+
if (!part || part.role !== "line") {
|
|
212
|
+
problems.push({ where: `${where}.lines`, problem: `"${name.slice(0, 24)}" is not a line part. Only parts with role line take written notes.` });
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (text(line).length > SCORE_LIMITS.lineCharacters) {
|
|
216
|
+
problems.push({ where: `${where}.lines.${name}`, problem: "This line is too long. Write a shorter phrase and use repeat." });
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const parsed = parseLine(line, `${where}.lines.${name}`, problems);
|
|
220
|
+
if (!parsed) continue;
|
|
221
|
+
const room = symbols.length * beatsPerBar;
|
|
222
|
+
if (parsed.beats > room + 1e-6) {
|
|
223
|
+
problems.push({ where: `${where}.lines.${name}`, problem: `This line is ${round(parsed.beats)} beats long, but the section's ${symbols.length} bars hold ${room}. Shorten it by ${round(parsed.beats - room)} beats.` });
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
written.set(name, parsed.notes);
|
|
227
|
+
}
|
|
228
|
+
if (problems.length) continue;
|
|
229
|
+
const name = text(section.name).slice(0, 32) || `section ${index + 1}`;
|
|
230
|
+
for (let pass = 0; pass < repeat; pass += 1) {
|
|
231
|
+
const at = bar * beatsPerBar;
|
|
232
|
+
form.push({ name, bar, bars: symbols.length });
|
|
233
|
+
harmony.push(...symbols);
|
|
234
|
+
for (const partName of new Set(playing)) {
|
|
235
|
+
const part = parts.get(partName);
|
|
236
|
+
if (part.role === "chords") part.runs.push({ at, symbols });
|
|
237
|
+
else if (part.role === "bass") {
|
|
238
|
+
for (const [offset, symbol] of symbols.entries()) {
|
|
239
|
+
const [root] = chord(symbol, 0, 1, { part: "bass" });
|
|
240
|
+
part.notes.push(...bassNotes(part.style, root.pitch, at + offset * beatsPerBar, beatsPerBar, !/dim|aug|b5|#5|\+/u.test(symbol)));
|
|
241
|
+
}
|
|
242
|
+
} else if (part.role === "drums") {
|
|
243
|
+
const steps = symbols.length * beatsPerBar * 4;
|
|
244
|
+
for (const { voice, steps: grid } of part.grids) {
|
|
245
|
+
for (let step = 0; step < steps; step += 1) {
|
|
246
|
+
const mark = grid[step % grid.length];
|
|
247
|
+
if (VELOCITY[mark]) part.notes.push(hit(voice, at + step / 4, VELOCITY[mark]));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
for (const entry of written.get(partName) ?? []) part.notes.push(note(entry.pitch, at + entry.at, Math.max(0.1, entry.beats - 0.02), 0.5));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
bar += symbols.length;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (problems.length) return fail();
|
|
258
|
+
|
|
259
|
+
const seconds = (bar * beatsPerBar * 60) / tempo;
|
|
260
|
+
if (seconds < SCORE_LIMITS.seconds.min || seconds > SCORE_LIMITS.seconds.max) {
|
|
261
|
+
return { ok: false, problems: [{ where: "sections", problem: `The track runs ${Math.round(seconds)} seconds (${bar} bars at ${tempo} BPM); it can be ${SCORE_LIMITS.seconds.min} to ${SCORE_LIMITS.seconds.max}. ${seconds < SCORE_LIMITS.seconds.min ? "Add bars or use repeat." : "Take bars out."}` }] };
|
|
262
|
+
}
|
|
263
|
+
if (![...parts.values()].some((part) => part.role === "chords" ? part.runs.length : part.notes.length)) {
|
|
264
|
+
return { ok: false, problems: [{ where: "sections", problem: "No part plays a note. List the parts in each section's plays, and write lines for line parts." }] };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const assemble = (voicing) => song({
|
|
268
|
+
calibration: SONG_CALIBRATION_LATEST,
|
|
269
|
+
tempo,
|
|
270
|
+
meter: [beatsPerBar, 4],
|
|
271
|
+
bars: bar,
|
|
272
|
+
key,
|
|
273
|
+
tracks: [...parts.values()].map((part) => {
|
|
274
|
+
const mix = MIX[part.role];
|
|
275
|
+
return track({
|
|
276
|
+
name: part.name,
|
|
277
|
+
...instrumentOf(part.sound),
|
|
278
|
+
volume: Math.min(1, round(mix.volume * part.level)),
|
|
279
|
+
...(mix.reverb ? { reverb: mix.reverb } : {}),
|
|
280
|
+
notes: part.role === "chords" ? chordNotes(part.style, part.runs, beatsPerBar, voicing) : part.notes,
|
|
281
|
+
});
|
|
282
|
+
}),
|
|
283
|
+
master: MASTER,
|
|
284
|
+
});
|
|
285
|
+
const listen = (built) => lintSong(built, { config: { harmony } }).findings;
|
|
286
|
+
let best = null;
|
|
287
|
+
try {
|
|
288
|
+
for (const voicing of CHORD_VOICINGS) {
|
|
289
|
+
const built = JSON.parse(JSON.stringify(assemble(voicing)));
|
|
290
|
+
const findings = listen(built);
|
|
291
|
+
const clashes = findings.filter((finding) => finding.rule === "harmony.low-second").length;
|
|
292
|
+
if (!best || clashes < best.clashes) best = { built, findings, clashes };
|
|
293
|
+
if (clashes === 0) break;
|
|
294
|
+
}
|
|
295
|
+
// The renderer's own reading of the score: a score it refuses is a problem to fix, not a crash later.
|
|
296
|
+
scoreFromJSON(best.built);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
return { ok: false, problems: [{ where: "score", problem: String(error?.message ?? "The score could not be built.").slice(0, 240) }] };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const summary = {
|
|
302
|
+
tempo, key, beats_per_bar: beatsPerBar, bars: bar, seconds: Math.round(seconds),
|
|
303
|
+
parts: [...parts.values()].map((part) => ({ name: part.name, role: part.role, sound: part.sound, ...(part.style ? { style: part.style } : {}) })),
|
|
304
|
+
form: form.map((entry) => `${entry.name} (${entry.bars})`).join(", ").slice(0, 400),
|
|
305
|
+
chords: [...new Set(harmony)].slice(0, 24),
|
|
306
|
+
};
|
|
307
|
+
// What the lint heard: wrong notes first, then cautions. Advice the listener would not hear stays out.
|
|
308
|
+
const order = { error: 0, warn: 1 };
|
|
309
|
+
const notes = best.findings.filter((finding) => finding.severity in order)
|
|
310
|
+
.sort((left, right) => order[left.severity] - order[right.severity])
|
|
311
|
+
.slice(0, 6).map((finding) => ({
|
|
312
|
+
severity: finding.severity,
|
|
313
|
+
rule: finding.rule,
|
|
314
|
+
// The part and the bar as the agent counts them, from 1.
|
|
315
|
+
where: [finding.where?.track, Number.isInteger(finding.where?.bar) ? `bar ${finding.where.bar + 1}` : null].filter(Boolean).join(", ") || "score",
|
|
316
|
+
problem: String(finding.message).slice(0, 200),
|
|
317
|
+
}));
|
|
318
|
+
return { ok: true, score: best.built, seconds, bars: bar, summary, notes, wrong_notes: best.findings.filter((finding) => finding.severity === "error").length };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** The line a receipt and the agent keep in place of a text prompt: what the score is, in a sentence. */
|
|
322
|
+
export function describeScore(summary) {
|
|
323
|
+
const sounds = summary.parts.map((part) => part.sound).join(", ");
|
|
324
|
+
return `Written as a score: ${summary.key}, ${summary.tempo} BPM, ${summary.bars} bars. ${sounds}. Chords: ${summary.chords.join(" ")}.`.slice(0, 1_000);
|
|
325
|
+
}
|