@k-l-lambda/lilylet 0.1.70 → 0.1.71

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.
@@ -200,6 +200,68 @@ export class StaffLayout {
200
200
  }
201
201
  }
202
202
  export const parseStaffLayout = (code) => new StaffLayout(tokenize(code));
203
+ // ── Staff-layout serialization (inverse of parseStaffLayout) ──
204
+ // Reconstruct a layout string from a parsed StaffLayout by walking the group tree,
205
+ // so every staff slot and conjunction is preserved structurally (a regex strip of the
206
+ // ids would drop a BARE anonymous leaf — its empty token gets swallowed by whitespace).
207
+ //
208
+ // `anonymous` emits empty ids (the parser re-auto-names slots "1","2",… by position).
209
+ // `idMap` optionally overrides individual staff ids by their original id.
210
+ //
211
+ // Conjunction rendering: Solid → "-", Dashed → ".", Blank → " " ONLY when both sides
212
+ // are bracketed groups (the brackets self-delimit the slots); otherwise Blank → ","
213
+ // so an adjacent empty/bare leaf still tokenizes as its own slot.
214
+ const CONJ_CHAR = {
215
+ [StaffConjunctionType.Solid]: "-",
216
+ [StaffConjunctionType.Dashed]: ".",
217
+ [StaffConjunctionType.Blank]: ",",
218
+ };
219
+ export const serializeStaffLayout = (layout, options = {}) => {
220
+ const { anonymous = false, idMap } = options;
221
+ const isGrouped = (group) => group.type !== StaffGroupType.Default && !!group.subs;
222
+ const leafText = (id) => (anonymous ? "" : idMap ? idMap(id) : id);
223
+ // flat leaf index of a group's first / last staff (for the inter-child conjunction).
224
+ const firstLeafIndex = (group) => layout.staffIds.indexOf(groupHead(group));
225
+ const lastLeafIndex = (group) => layout.staffIds.indexOf(groupTail(group));
226
+ const sep = (conj, left, right) => {
227
+ if (conj !== StaffConjunctionType.Blank)
228
+ return CONJ_CHAR[conj];
229
+ // Blank: a space is safe only when both neighbours are bracketed (self-delimiting).
230
+ return isGrouped(left) && isGrouped(right) ? " " : ",";
231
+ };
232
+ const emit = (group) => {
233
+ if (!group.subs)
234
+ return leafText(group.staff); // Default leaf
235
+ const open = group.type === StaffGroupType.Brace ? "{" : group.type === StaffGroupType.Bracket ? "<" : group.type === StaffGroupType.Square ? "[" : "";
236
+ const close = group.type === StaffGroupType.Brace ? "}" : group.type === StaffGroupType.Bracket ? ">" : group.type === StaffGroupType.Square ? "]" : "";
237
+ let inner = "";
238
+ group.subs.forEach((sub, i) => {
239
+ inner += emit(sub);
240
+ if (i < group.subs.length - 1) {
241
+ const next = group.subs[i + 1];
242
+ const conj = layout.conjunctions[lastLeafIndex(sub)] ?? StaffConjunctionType.Blank;
243
+ inner += sep(conj, sub, next);
244
+ void firstLeafIndex; // (lastLeafIndex(sub) === firstLeafIndex(next) - 1)
245
+ }
246
+ });
247
+ return open + inner + close;
248
+ };
249
+ let out = emit(layout.group);
250
+ // A TRAILING bare anonymous leaf emits "" with nothing after it to delimit the slot
251
+ // (a leaf before a closing bracket is fine — the bracket gives it bounds; an internal
252
+ // one is flushed by the next separator). The tokenizer only flushes a final empty item
253
+ // if it carries bounds, so append one "," to materialize that last empty slot. This only
254
+ // arises when the OUTERMOST container is the Default sequence (no enclosing bracket) and
255
+ // its last child is a bare leaf; if the whole layout is wrapped in a bracket, the closing
256
+ // bracket already delimits the final leaf. The trailing conjunction is dropped on re-parse
257
+ // (conjunctions = items[0..n-1]), so it is harmless. Anonymous output only.
258
+ if (anonymous && layout.group.type === StaffGroupType.Default && layout.group.subs) {
259
+ const lastTop = layout.group.subs[layout.group.subs.length - 1];
260
+ if (!lastTop.subs && lastTop.staff !== undefined)
261
+ out += ",";
262
+ }
263
+ return out;
264
+ };
203
265
  // ── MEI staffGrp encoding (ported from FindLab staffLayout/encoding.js encodeMEI) ──
204
266
  // Recursively emit nested <staffGrp> with symbol (brace/bracket/square) and bar.thru,
205
267
  // with <staffDef n="..."> leaves keyed by staff index. nameDict maps a group key to a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k-l-lambda/lilylet",
3
- "version": "0.1.70",
3
+ "version": "0.1.71",
4
4
  "description": "Lilylet is a lilyopnd-like sheet music language designed for Markdown rendering and symbolic music representation in AIGC applications.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -580,27 +580,34 @@ const abcLayoutToStaves = (layout: ABC.StaffGroup[]): string | null => {
580
580
  return (node.items || []).every(isStaffLeaf);
581
581
  };
582
582
 
583
- const emit = (node: ABC.StaffGroup | string): string => {
583
+ // A square group maps to lilylet Bracket `<>` at the TOP level, but to lilylet Square
584
+ // `[]` when nested inside another group — e.g. ABC `[[1 2] 3 | 4]` → `<[1,2]3-4>`.
585
+ // A curly group always maps to Brace `{}`. `nested` is false for a top-level entry.
586
+ const emit = (node: ABC.StaffGroup | string, nested: boolean): string => {
584
587
  if (isStaffLeaf(node)) return firstVoice(node) || "";
585
588
 
586
589
  const group = node as ABC.StaffGroup;
587
- const open = group.bound === "curly" ? "{" : "<"; // square bracket <>, curly → brace {}
588
- const close = group.bound === "curly" ? "}" : ">";
590
+ const open = group.bound === "curly" ? "{" : (group.bound === "square" && nested) ? "[" : "<";
591
+ const close = group.bound === "curly" ? "}" : (group.bound === "square" && nested) ? "]" : ">";
589
592
 
590
593
  const items = group.items || [];
591
594
  let inner = "";
592
595
  items.forEach((item, i) => {
593
- inner += emit(item);
596
+ inner += emit(item, true);
594
597
  if (i < items.length - 1) {
595
- const conj = (item as ABC.StaffGroup).barThruAfter ? "-" : ",";
596
- inner += conj;
598
+ // A Blank separator (',') is only needed between two bare staff leaves; a
599
+ // grouped neighbour's bracket already delimits the slot, so suppress it there
600
+ // (giving `[1,2]3` not `[1,2],3`). A Solid join ('-', barThru) is always kept.
601
+ const next = items[i + 1];
602
+ if ((item as ABC.StaffGroup).barThruAfter) inner += "-";
603
+ else if (isStaffLeaf(item) && isStaffLeaf(next)) inner += ",";
597
604
  }
598
605
  });
599
606
  return `${open}${inner}${close}`;
600
607
  };
601
608
 
602
609
  const tops = layout.map((top, i) => {
603
- let s = emit(top);
610
+ let s = emit(top, false);
604
611
  // A bare top-level staff leaf (e.g. the `9` in `[ … ] 9 [ … ]`) still occupies a slot;
605
612
  // emit() already yields its id with no wrapper, which is the desired output.
606
613
  return { s, barThru: !!top.barThruAfter, isLast: i === layout.length - 1 };
@@ -0,0 +1,305 @@
1
+ // General MIDI program lookup: instrument name → GM program number (0–127).
2
+ //
3
+ // Verovio's MIDI export honors ONLY the numeric `@midi.instrnum` on an MEI
4
+ // <instrDef> (the GM-name attribute `@midi.instrname` is parsed but never used
5
+ // for MIDI). lilylet already carries human instrument names (from ABC voice
6
+ // names, MusicXML part names, etc.) in metadata.instruments; this table maps
7
+ // those names to GM programs so the MEI encoder can emit <instrDef midi.instrnum>
8
+ // and multi-instrument scores get distinct timbres instead of all-piano.
9
+ //
10
+ // The name set is seeded from the notagen dataset (Piano, Violins, Viola,
11
+ // Violoncellos, Oboe, Horn, Flute, Clarinet, Bassoon, Violin, Trombone,
12
+ // Timpani, Voice, Bass, Trumpet, Harp, Contrabasses, Vocal, Organ, …) plus
13
+ // common GM aliases, then matched through a normalizer that handles plurals
14
+ // ("Violins" → violin) and trailing part numbers ("Violin I", "Horn 2").
15
+
16
+ // Normalized name → GM program (0-based). Keys are lowercase, singular,
17
+ // whitespace-collapsed. Plural/number variants are resolved by the normalizer.
18
+ const GM_PROGRAMS: { [name: string]: number } = {
19
+ // Piano (0–7)
20
+ "piano": 0,
21
+ "acoustic grand piano": 0,
22
+ "grand piano": 0,
23
+ "bright acoustic piano": 1,
24
+ "electric piano": 4,
25
+ "harpsichord": 6,
26
+ "clavichord": 7,
27
+ "clavi": 7,
28
+ // Chromatic percussion (8–15)
29
+ "celesta": 8,
30
+ "glockenspiel": 9,
31
+ "music box": 10,
32
+ "vibraphone": 11,
33
+ "marimba": 12,
34
+ "xylophone": 13,
35
+ "tubular bells": 14,
36
+ "dulcimer": 15,
37
+ // Organ (16–23)
38
+ "organ": 19,
39
+ "hammond organ": 16,
40
+ "percussive organ": 17,
41
+ "rock organ": 18,
42
+ "church organ": 19,
43
+ "pipe organ": 19,
44
+ "reed organ": 20,
45
+ "accordion": 21,
46
+ "harmonica": 22,
47
+ // Guitar (24–31)
48
+ "guitar": 24,
49
+ "acoustic guitar": 24,
50
+ "nylon guitar": 24,
51
+ "steel guitar": 25,
52
+ "electric guitar": 27,
53
+ "guitarre": 24, // fr./de. guitar
54
+ "gitarre": 24, // de.
55
+ "chitarra": 24, // it.
56
+ // Bass (32–39) — orchestral "Bass" means double bass (Contrabass, 43); the
57
+ // electric/acoustic bass-guitar programs live here but are not the default.
58
+ "acoustic bass": 32,
59
+ "electric bass": 33,
60
+ "fretless bass": 35,
61
+ "basso": 43, // it. bass → double bass
62
+ "basse": 43, // fr.
63
+ "bassi": 43, // it. pl.
64
+ "bas": 43, // de./nl. abbrev
65
+ // Strings (40–47)
66
+ "violin": 40,
67
+ "viola": 41,
68
+ "cello": 42,
69
+ "violoncello": 42,
70
+ "contrabass": 43,
71
+ "double bass": 43,
72
+ "bass": 43,
73
+ "tremolo strings": 44,
74
+ "pizzicato strings": 45,
75
+ "harp": 46,
76
+ "orchestral harp": 46,
77
+ "timpani": 47,
78
+ // Ensemble (48–55)
79
+ "strings": 48,
80
+ "string ensemble": 48,
81
+ "string orchestra": 48,
82
+ "synth strings": 50,
83
+ "voice": 52,
84
+ "vocal": 52,
85
+ "voices": 52,
86
+ "choir": 52,
87
+ "choir aahs": 52,
88
+ "soprano": 52,
89
+ "alto": 52,
90
+ "tenor": 52,
91
+ "bass voice": 52,
92
+ "orchestra hit": 55,
93
+ // Brass (56–63)
94
+ "trumpet": 56,
95
+ "trombone": 57,
96
+ "tuba": 58,
97
+ "muted trumpet": 59,
98
+ "horn": 60,
99
+ "french horn": 60,
100
+ "brass": 61,
101
+ "brass section": 61,
102
+ // Reed (64–71)
103
+ "soprano sax": 64,
104
+ "alto sax": 65,
105
+ "tenor sax": 66,
106
+ "baritone sax": 67,
107
+ "saxophone": 66,
108
+ "sax": 66,
109
+ "oboe": 68,
110
+ "english horn": 69,
111
+ "cor anglais": 69,
112
+ "bassoon": 70,
113
+ "clarinet": 71,
114
+ // Pipe (72–79)
115
+ "piccolo": 72,
116
+ "flute": 73,
117
+ "recorder": 74,
118
+ "pan flute": 75,
119
+
120
+ // --- Foreign-language names, abbreviations and common spelling variants,
121
+ // harvested from the notagen corpus. Mapped to the nearest GM program.
122
+ // Keyboard
123
+ "pianoforte": 0,
124
+ "fortepiano": 0,
125
+ "klavier": 0,
126
+ "keyboard": 0,
127
+ "cembalo": 6, // it. harpsichord
128
+ "clavicembalo": 6,
129
+ "harpichord": 6, // misspelling
130
+ "organo": 19, // it. organ
131
+ "orgel": 19, // de. organ
132
+ // Strings (it./de./fr./variants)
133
+ "violino": 40,
134
+ "violini": 40,
135
+ "violine": 40, // de.
136
+ "violinen": 40,
137
+ "violon": 40, // fr.
138
+ "violons": 40,
139
+ "violn": 40, // abbrev/OCR variant
140
+ "violno": 40, // OCR variant
141
+ "viole": 41, // it. violas (also fr. "viole")
142
+ "bratsche": 41, // de. viola
143
+ "celli": 42,
144
+ "violoncelli": 42,
145
+ "violoncelle": 42, // fr.
146
+ "violoncelles": 42,
147
+ "violonchelo": 42, // es.
148
+ "soloncello": 42, // OCR variant of violoncello
149
+ "gambe": 42, // fr. viola da gamba
150
+ "gamba": 42, // viola da gamba ≈ cello
151
+ "viola da gamba": 42,
152
+ "contrabasso": 43, // it.
153
+ "contrabassi": 43,
154
+ "contrabbasso": 43, // it.
155
+ "contra-basso": 43,
156
+ "contrabajo": 43, // es.
157
+ "kontrabass": 43, // de.
158
+ "kontrabasse": 43, // de. pl.
159
+ "kontrabasso": 43,
160
+ "contrebasse": 43, // fr.
161
+ "violone": 43, // large bass viol ≈ contrabass
162
+ "arpa": 46, // it./es. harp
163
+ "harfe": 46, // de. harp
164
+ "pauken": 47, // de. timpani
165
+ // Voice (it./de./fr.)
166
+ "canto": 52, // it.
167
+ "coro": 52, // it. choir
168
+ "chorus": 52,
169
+ "chorale": 52,
170
+ "sopran": 52, // de.
171
+ "contralto": 52, // it. alto
172
+ "tenore": 52, // it.
173
+ "tenori": 52,
174
+ "gesang": 52, // de. voice
175
+ "singstimme": 52, // de. voice
176
+ "voce": 52, // it.
177
+ "voix": 52, // fr.
178
+ "chanto": 52, // OCR variant of canto
179
+ "women": 52, // women's voices
180
+ "contra-fagotto": 70, // hyphenated contrabassoon ≈ bassoon
181
+ // Brass
182
+ "tromboni": 57, // it. trombones
183
+ "posaune": 57, // de. trombone
184
+ "posaunen": 57,
185
+ "trombe": 56, // it. trumpets
186
+ "tromba": 56, // it. trumpet
187
+ "trompete": 56, // de. trumpet
188
+ "trompeten": 56,
189
+ "trompette": 56, // fr. trumpet
190
+ "cornetto": 56, // historical cornett ≈ trumpet
191
+ "cornettino": 56,
192
+ "corno": 60, // it. horn
193
+ "corni": 60, // it. horns
194
+ // Reed (it./de./fr.)
195
+ "oboi": 68, // it. oboes
196
+ "oboen": 68, // de.
197
+ "hautbois": 68, // fr. oboe
198
+ "corno inglese": 69, // it. english horn
199
+ "inglese": 69, // "corno inglese" trailing word fallback also covers it
200
+ "ingles": 69, // es. variant
201
+ "fagotto": 70, // it. bassoon
202
+ "fagotti": 70,
203
+ "fagott": 70, // de.
204
+ "fagotte": 70, // de. pl.
205
+ "fagot": 70, // es.
206
+ "basson": 70, // fr. bassoon
207
+ "bassons": 70,
208
+ "contrafagotto": 70, // it. contrabassoon ≈ bassoon timbre
209
+ "contrabassoon": 70,
210
+ "klarinette": 71, // de. clarinet
211
+ "clarinetto": 71, // it.
212
+ "clarinetti": 71,
213
+ "clarinette": 71, // fr.
214
+ // Pipe (it./de.)
215
+ "flauto": 73, // it. flute
216
+ "flauti": 73, // it. flutes
217
+ "flote": 73, // de. Flöte (diacritics stripped by the normalizer)
218
+ "floten": 73, // de. Flöten
219
+ "traverso": 73, // baroque transverse flute
220
+ "flauto traverso": 73,
221
+ };
222
+
223
+ // Normalize an instrument name for lookup: lowercase, turn literal "\n" escapes
224
+ // and real newlines into spaces, strip diacritics (Flöte→flote, Hautböis→...),
225
+ // drop a trailing part designator (roman numeral or arabic number — "Violin I",
226
+ // "Horn 2", "Oboe II"), collapse whitespace.
227
+ const normalizeInstrumentName = (raw: string): string => {
228
+ let s = raw.toLowerCase().trim();
229
+ s = s.replace(/\\n/g, " "); // literal backslash-n escape → space
230
+ s = s.normalize("NFD").replace(/[̀-ͯ]/g, ""); // strip diacritics
231
+ s = s.replace(/\s+/g, " ").trim();
232
+ s = s.replace(/\s+(?:[ivx]+|\d+)\.?$/i, "").trim();
233
+ return s;
234
+ };
235
+
236
+ // Choral single-letter voice-part abbreviations → "Voice" (GM 52). Matched ONLY
237
+ // against the whole name, never per-word: a bare "S"/"A"/"T"/"B" staff label in a
238
+ // chorale means Soprano/Alto/Tenor/Bass, but the same letters appear as key
239
+ // designators in "Clarinet in B", "Horn in F", "Trumpet in C" — so these must not
240
+ // enter GM_PROGRAMS where the word-scan would misread them.
241
+ const SATB_VOICE: { [letter: string]: number } = {
242
+ "s": 52,
243
+ "a": 52,
244
+ "t": 52,
245
+ "b": 52,
246
+ };
247
+
248
+ // Look up a single normalized name: exact match, else de-pluralized
249
+ // ("violins"→violin, "violoncellos"→violoncello, "contrabasses"→contrabass),
250
+ // else with a trailing attached part-number stripped ("violin1"→violin,
251
+ // "violino2"→violino).
252
+ const lookupNormalized = (norm: string): number | undefined => {
253
+ if (norm in GM_PROGRAMS)
254
+ return GM_PROGRAMS[norm];
255
+ // Try "-es" before "-s".
256
+ if (norm.endsWith("es")) {
257
+ const sing = norm.slice(0, -2);
258
+ if (sing in GM_PROGRAMS)
259
+ return GM_PROGRAMS[sing];
260
+ }
261
+ if (norm.endsWith("s")) {
262
+ const sing = norm.slice(0, -1);
263
+ if (sing in GM_PROGRAMS)
264
+ return GM_PROGRAMS[sing];
265
+ }
266
+ // Attached trailing digits ("violin1", "violino2"): strip and retry.
267
+ const deNum = norm.replace(/\d+$/, "");
268
+ if (deNum !== norm && deNum in GM_PROGRAMS)
269
+ return GM_PROGRAMS[deNum];
270
+ return undefined;
271
+ };
272
+
273
+ // Resolve an instrument name to a GM program number (0–127), or undefined if no
274
+ // confident match (caller then omits <instrDef>, leaving Verovio's default).
275
+ //
276
+ // Match priority: the full normalized string first (including the SATB
277
+ // single-letter voice abbreviations), then individual words from the last toward
278
+ // the first. Multi-word names ("Singstimme Voice", "First Violins", "Solo Flute")
279
+ // usually put the instrument at the end, so the trailing word is tried before
280
+ // earlier qualifier words. Each word attempt runs through the de-plural path
281
+ // (lookupNormalized); SATB letters are intentionally NOT part of the word scan.
282
+ export const gmProgramOf = (name: string | undefined | null): number | undefined => {
283
+ if (!name)
284
+ return undefined;
285
+
286
+ const norm = normalizeInstrumentName(name);
287
+ const direct = lookupNormalized(norm);
288
+ if (direct !== undefined)
289
+ return direct;
290
+
291
+ // Whole-name-only: a lone S/A/T/B is a chorale voice part.
292
+ if (norm in SATB_VOICE)
293
+ return SATB_VOICE[norm];
294
+
295
+ const words = norm.split(" ");
296
+ if (words.length > 1) {
297
+ for (let i = words.length - 1; i >= 0; i--) {
298
+ const hit = lookupNormalized(words[i]);
299
+ if (hit !== undefined)
300
+ return hit;
301
+ }
302
+ }
303
+
304
+ return undefined;
305
+ };