@dsebastien/music-notation 0.10.1 → 0.12.0

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.
@@ -144,6 +144,102 @@ function titleWidth(title) {
144
144
  }
145
145
  return Math.ceil(width);
146
146
  }
147
+ var MAX_TITLE_LINES = 3;
148
+ var TITLE_LINE = 17;
149
+ function titleLineWidth(text) {
150
+ if (!text)
151
+ return 0;
152
+ let width = 8;
153
+ for (const ch of text) {
154
+ const code = ch.codePointAt(0);
155
+ width += code > 591 ? 15 : ch === " " || ".,:;'!|".includes(ch) ? 4 : "()[]-/".includes(ch) || "iljfrt".includes(ch) ? 5 : "mwMW".includes(ch) ? 12 : /[A-Z0-9#♯♭]/.test(ch) ? 9.5 : 7.8;
156
+ }
157
+ return Math.ceil(width);
158
+ }
159
+ function titleLines(title, width) {
160
+ const fits = (text) => titleLineWidth(text) <= width;
161
+ const squeeze = (text) => ({
162
+ text,
163
+ textLength: fits(text) ? null : Math.max(1, width - 8)
164
+ });
165
+ if (fits(title))
166
+ return [{ text: title, textLength: null }];
167
+ const phrases = title.split(/(?<=,)\s+|\s+(?=\()/).flatMap((phrase) => fits(phrase) ? [phrase] : phrase.split(/\s+/)).filter(Boolean);
168
+ const lines = [];
169
+ let line = "";
170
+ for (const phrase of phrases) {
171
+ const next = line ? `${line} ${phrase}` : phrase;
172
+ if (!line || fits(next))
173
+ line = next;
174
+ else {
175
+ lines.push(line);
176
+ line = phrase;
177
+ }
178
+ }
179
+ if (line)
180
+ lines.push(line);
181
+ if (lines.length > MAX_TITLE_LINES) {
182
+ const kept = lines.slice(0, MAX_TITLE_LINES - 1);
183
+ kept.push(lines.slice(MAX_TITLE_LINES - 1).join(" "));
184
+ return kept.map(squeeze);
185
+ }
186
+ return lines.map(squeeze);
187
+ }
188
+ function drawTitle(doc, root, lines, x, y) {
189
+ const NS = "http://www.w3.org/2000/svg";
190
+ const text = doc.createElementNS(NS, "text");
191
+ text.setAttribute("class", "mn-title");
192
+ text.setAttribute("x", String(x));
193
+ text.setAttribute("y", String(y));
194
+ text.setAttribute("text-anchor", "middle");
195
+ const squeeze = (el, line) => {
196
+ if (line.textLength === null)
197
+ return;
198
+ el.setAttribute("textLength", String(line.textLength));
199
+ el.setAttribute("lengthAdjust", "spacingAndGlyphs");
200
+ };
201
+ if (lines.length === 1) {
202
+ text.textContent = lines[0].text;
203
+ squeeze(text, lines[0]);
204
+ } else {
205
+ lines.forEach((line, i) => {
206
+ const span = doc.createElementNS(NS, "tspan");
207
+ span.setAttribute("x", String(x));
208
+ if (i > 0)
209
+ span.setAttribute("dy", String(TITLE_LINE));
210
+ span.textContent = i > 0 ? ` ${line.text}` : line.text;
211
+ squeeze(span, line);
212
+ text.appendChild(span);
213
+ });
214
+ }
215
+ root.setAttribute("data-mn-title-lines", String(lines.length));
216
+ root.appendChild(text);
217
+ return text;
218
+ }
219
+ function alignTitleLines(diagrams) {
220
+ const list = [...diagrams].filter((d) => d.hasAttribute("data-mn-title-lines"));
221
+ const most = Math.max(0, ...list.map((d) => Number(d.getAttribute("data-mn-title-lines"))));
222
+ for (const d of list) {
223
+ const missing = most - Number(d.getAttribute("data-mn-title-lines"));
224
+ if (missing <= 0)
225
+ continue;
226
+ const shift = missing * TITLE_LINE;
227
+ const doc = d.ownerDocument;
228
+ const g = doc.createElementNS("http://www.w3.org/2000/svg", "g");
229
+ g.setAttribute("class", "mn-title-pad");
230
+ g.setAttribute("transform", `translate(0 ${shift})`);
231
+ for (const child of [...d.children]) {
232
+ if (child.tagName.toLowerCase() !== "defs")
233
+ g.appendChild(child);
234
+ }
235
+ d.appendChild(g);
236
+ const [x, y, w, h] = (d.getAttribute("viewBox") ?? "0 0 0 0").split(/\s+/).map(Number);
237
+ d.setAttribute("viewBox", `${x} ${y} ${w} ${h + shift}`);
238
+ if (d.hasAttribute("height"))
239
+ d.setAttribute("height", String(Number(d.getAttribute("height")) + shift));
240
+ d.setAttribute("data-mn-title-lines", String(most));
241
+ }
242
+ }
147
243
  function neckFrame(n) {
148
244
  const vertical = n.orientation === "vertical";
149
245
  const left = n.hand === "left";
@@ -295,7 +391,7 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
295
391
  const fromNut = capo > 0 && options.fretsFrom === "nut";
296
392
  const capoFret = fromNut ? capo : null;
297
393
  const caption = capo && options.caption !== false ? capoCaption(capo, options.sounds) : undefined;
298
- const frame = neckFrame({
394
+ const layout = {
299
395
  orientation,
300
396
  hand: options.hand ?? "right",
301
397
  across: (strings - 1) * SPACING,
@@ -304,8 +400,13 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
304
400
  after: 10,
305
401
  low: vertical || base > 1 ? BASE : 10,
306
402
  high: vertical ? 14 : 10,
307
- top: options.title ? TITLE : 4,
308
- minWidth: Math.max(titleWidth(options.title), titleWidth(caption))
403
+ top: 4,
404
+ minWidth: titleWidth(caption)
405
+ };
406
+ const lines = options.title ? titleLines(options.title, neckFrame(layout).width) : [];
407
+ const frame = neckFrame({
408
+ ...layout,
409
+ top: lines.length ? TITLE + (lines.length - 1) * TITLE_LINE : 4
309
410
  });
310
411
  const across = (string) => string * SPACING;
311
412
  const along = (row) => row * ROW;
@@ -328,14 +429,8 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
328
429
  root.setAttribute("data-mn-capo", String(capo));
329
430
  root.setAttribute("data-mn-frets-from", fromNut ? "nut" : "capo");
330
431
  }
331
- if (options.title) {
332
- root.appendChild(svgText(doc, options.title, {
333
- class: "mn-title",
334
- x: frame.width / 2,
335
- y: 15,
336
- "text-anchor": "middle"
337
- }));
338
- }
432
+ if (lines.length)
433
+ drawTitle(doc, root, lines, frame.width / 2, 15);
339
434
  for (let row = 0;row <= rows; row++) {
340
435
  const cls = row === 0 && base === 1 && (!capo || fromNut) ? "mn-nut" : "mn-fret";
341
436
  root.appendChild(svgLine(doc, at(0, row), at(strings - 1, row), cls));
@@ -950,8 +1045,7 @@ function shapeOf(frets, info, targets, tuning) {
950
1045
  return "Triad";
951
1046
  const fretted = sounding.map((s) => frets[s]).filter((f) => f > 0);
952
1047
  const open = sounding.some((s) => frets[s] === 0);
953
- const top = sounding[sounding.length - 1] === frets.length - 1;
954
- if (open && top && sounding.length >= 4 && (!fretted.length || Math.max(...fretted) <= 4)) {
1048
+ if (open && sounding.length >= 4 && (!fretted.length || Math.max(...fretted) <= 4)) {
955
1049
  return "Open";
956
1050
  }
957
1051
  if (sameTuning(tuning, STANDARD_TUNING) && !open && fretted.length) {
@@ -977,9 +1071,97 @@ function shapeOf(frets, info, targets, tuning) {
977
1071
  }
978
1072
  return null;
979
1073
  }
1074
+ var OPEN_CHORD_FRETS = [...OPEN_CHORDS].map((t) => [...t].map((c) => c === "x" ? null : Number(c)));
1075
+ function stringsText(sounding, count) {
1076
+ const numbers = sounding.map((s) => count - s).sort((a, b) => a - b);
1077
+ const runs = [];
1078
+ for (let i = 0;i < numbers.length; ) {
1079
+ let j = i;
1080
+ while (j + 1 < numbers.length && numbers[j + 1] === numbers[j] + 1)
1081
+ j++;
1082
+ runs.push(i === j ? `${numbers[i]}` : `${numbers[i]}-${numbers[j]}`);
1083
+ i = j + 1;
1084
+ }
1085
+ return runs.join(", ");
1086
+ }
1087
+ function cagedPartOf(frets, sounding, info) {
1088
+ if (info.bass !== null || sounding.length < 3)
1089
+ return null;
1090
+ if (sounding.some((s) => frets[s] === 0))
1091
+ return null;
1092
+ for (const letter of ["E", "A", "C", "G", "D"]) {
1093
+ const target = chordInfo(letter + info.suffix);
1094
+ if (!target)
1095
+ continue;
1096
+ const tones = new Set(target.tones.map((t) => t.chroma));
1097
+ const base = ((info.root - chromaOf(letter)) % 12 + 12) % 12;
1098
+ for (const open of OPEN_CHORD_FRETS) {
1099
+ const strings = open.flatMap((f, s) => f === null ? [] : [s]);
1100
+ if (strings[0] !== CAGED_ROOT_STRING[letter] || strings.length <= sounding.length) {
1101
+ continue;
1102
+ }
1103
+ const chromas = strings.map((s) => (STANDARD_TUNING[s] + open[s]) % 12);
1104
+ if (chromas[0] !== chromaOf(letter))
1105
+ continue;
1106
+ if (!chromas.every((c) => tones.has(c)))
1107
+ continue;
1108
+ if (!target.tones.every((t) => !t.required || chromas.includes(t.chroma)))
1109
+ continue;
1110
+ for (const shift of [base, base + 12]) {
1111
+ if (shift === 0)
1112
+ continue;
1113
+ if (sounding.every((s) => open[s] !== null && frets[s] === open[s] + shift)) {
1114
+ return letter;
1115
+ }
1116
+ }
1117
+ }
1118
+ }
1119
+ return null;
1120
+ }
1121
+ function dropOf(pitches) {
1122
+ if (pitches.length !== 4 || new Set(pitches.map((p) => p % 12)).size !== 4)
1123
+ return null;
1124
+ const sorted = [...pitches].sort((a, b) => a - b);
1125
+ const raised = sorted[0] + 12;
1126
+ const close = [...sorted.slice(1), raised].sort((a, b) => a - b);
1127
+ if (close[3] - close[0] >= 12)
1128
+ return null;
1129
+ const fromTop = 3 - close.indexOf(raised);
1130
+ return fromTop === 1 ? 2 : fromTop === 2 ? 3 : null;
1131
+ }
1132
+ function isHendrixChord(frets, info, tuning) {
1133
+ if (!sameTuning(tuning, STANDARD_TUNING) || !isJazzGrip(frets) || frets[1] === null)
1134
+ return false;
1135
+ if (frets[0] !== null || (tuning[1] + frets[1]) % 12 !== info.root)
1136
+ return false;
1137
+ const chromas = new Set(info.tones.map((t) => t.chroma));
1138
+ return chromas.has((info.root + 3) % 12) && chromas.has((info.root + 4) % 12);
1139
+ }
1140
+ function otherNameOf(frets, info, tuning) {
1141
+ const sounding = frets.flatMap((f, s) => f === null ? [] : [s]);
1142
+ if (!sounding.length || tuning.length !== frets.length)
1143
+ return null;
1144
+ const strings = stringsText(sounding, frets.length);
1145
+ const standard = sameTuning(tuning, STANDARD_TUNING);
1146
+ const pitches = sounding.map((s) => tuning[s] + frets[s]);
1147
+ const letter = standard ? cagedPartOf(frets, sounding, info) : null;
1148
+ if (letter)
1149
+ return `${letter} shape (strings ${strings})`;
1150
+ const drop = dropOf(pitches);
1151
+ if (drop)
1152
+ return `Drop ${drop} (strings ${strings})`;
1153
+ const open = sounding.filter((s) => frets[s] === 0);
1154
+ if (open.length)
1155
+ return open.length === 1 && open[0] === sounding[0] ? "Open bass" : "Open strings";
1156
+ return `Strings ${strings}`;
1157
+ }
980
1158
  function voicingShape(frets, symbol, tuning = STANDARD_TUNING, _fingers) {
981
1159
  const info = chordInfo(symbol);
982
- return info ? shapeOf(frets, info, cagedTargets(info), tuning) : null;
1160
+ if (!info)
1161
+ return null;
1162
+ if (isHendrixChord(frets, info, tuning))
1163
+ return "Hendrix chord";
1164
+ return shapeOf(frets, info, cagedTargets(info), tuning) ?? otherNameOf(frets, info, tuning);
983
1165
  }
984
1166
  var KNOWN_SHAPES = new Set([
985
1167
  "C shape",
@@ -995,14 +1177,16 @@ var JAZZ_GRIPS = [
995
1177
  { rootString: 0, offsets: [0, null, -1, 0, -1, null] },
996
1178
  { rootString: 0, offsets: [0, null, 0, 1, 2, null] }
997
1179
  ];
998
- function distinctShapes(voicings, count = voicings.length) {
1180
+ function distinctShapes(voicings, count = voicings.length, tuning = STANDARD_TUNING) {
999
1181
  const sounding = (v) => v.frets.flatMap((f, s) => f === null ? [] : [s]);
1000
1182
  const copyOf = (v, w) => {
1001
1183
  const mine = sounding(v);
1002
1184
  const theirs = sounding(w);
1003
1185
  return mine.length < theirs.length && v.frets.length === w.frets.length && mine.every((s, i) => theirs[i] === s && w.frets[s] === v.frets[s]);
1004
1186
  };
1005
- const kept = voicings.filter((v) => !voicings.some((w) => w !== v && copyOf(v, w)));
1187
+ const standard = sameTuning(tuning, STANDARD_TUNING);
1188
+ const grip = (v) => standard && isJazzGrip(v.frets);
1189
+ const kept = voicings.filter((v) => !voicings.some((w) => w !== v && (copyOf(v, w) && !grip(v) || copyOf(w, v) && grip(w))));
1006
1190
  const lowest = (v) => {
1007
1191
  const fretted = v.frets.filter((f) => f !== null && f > 0);
1008
1192
  return fretted.length ? Math.min(...fretted) : 0;
@@ -1165,7 +1349,7 @@ function generateVoicings(symbol, options = {}) {
1165
1349
  baseFret: base,
1166
1350
  bassString,
1167
1351
  inversion: inBass < 0 ? null : inBass,
1168
- shape,
1352
+ shape: isHendrixChord(candidate, info, tuning) ? "Hendrix chord" : shape ?? otherNameOf(candidate, info, tuning),
1169
1353
  difficulty: Math.max(0, Math.round(difficulty * 10) / 10),
1170
1354
  reasons
1171
1355
  };
@@ -1524,5 +1708,5 @@ export {
1524
1708
  voicingCount
1525
1709
  };
1526
1710
 
1527
- //# debugId=085B9BAB5D2BA40664756E2164756E21
1711
+ //# debugId=746B9A1294ADFAB664756E2164756E21
1528
1712
  //# sourceMappingURL=index.js.map