@dsebastien/music-notation 0.11.0 → 0.13.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.
@@ -0,0 +1,57 @@
1
+ import { type MusicalKey } from './keys.js';
2
+ import { type HarmonicFunction } from './numerals.js';
3
+ export declare const HARMONIZE_KINDS: readonly ["triads", "sevenths", "sus", "power"];
4
+ export type HarmonizeKind = (typeof HARMONIZE_KINDS)[number];
5
+ export interface HarmonizeOptions {
6
+ /** `triads` (default), `sevenths`, `sus` (sus4, else sus2), `power` (root and fifth). */
7
+ kind?: HarmonizeKind;
8
+ }
9
+ export interface HarmonyScale {
10
+ /** "A harmonic minor", "D dorian", "G major". */
11
+ name: string;
12
+ /** The scale's notes, as tonal spells them (G# harmonic minor's 7th is F##). */
13
+ notes: string[];
14
+ /**
15
+ * The key its numerals are written in: the key itself for major, minor and the modes, else
16
+ * the major or minor key of the scale's third (A harmonic minor: A minor).
17
+ */
18
+ key: MusicalKey;
19
+ }
20
+ export interface HarmonizedChord {
21
+ /** 1 to 7. */
22
+ degree: number;
23
+ /** The scale note it is built on. */
24
+ note: string;
25
+ /** "AmMaj7", "Fsus2", "E5". */
26
+ symbol: string;
27
+ /** "imaj7", "#viø7", "bVII", "I5". */
28
+ numeral: string;
29
+ /** "1mmaj⁷", "#6ø⁷". */
30
+ nashville: string;
31
+ function: HarmonicFunction;
32
+ }
33
+ export interface Harmonized extends HarmonyScale {
34
+ kind: HarmonizeKind;
35
+ chords: HarmonizedChord[];
36
+ /** Degrees without a chord of this kind (sus or power chords need a perfect fifth). */
37
+ skipped: {
38
+ degree: number;
39
+ note: string;
40
+ reason: string;
41
+ }[];
42
+ }
43
+ /**
44
+ * A scale to harmonize: a key (`G`, `Em`, `F# minor`, `D dorian`) or any 7-note scale tonal knows
45
+ * (`A harmonic minor`, `C melodic minor`, `A natural minor`, `C phrygian dominant`). An error
46
+ * says why it can't be one.
47
+ */
48
+ export declare function readHarmonyScale(text: string): HarmonyScale | {
49
+ error: string;
50
+ };
51
+ /**
52
+ * The chords of a 7-note scale, one per degree (see the module comment for numerals). Functions:
53
+ * the chord's function in `key` when it is tonic, subdominant or dominant, else by degree (major
54
+ * T S T S D T D, minor T S T S D S D), so a melodic minor's IV7 is a subdominant, not a secondary
55
+ * dominant. Null when the scale or the kind doesn't read.
56
+ */
57
+ export declare function harmonize(scale: string, options?: HarmonizeOptions): Harmonized | null;
@@ -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));
@@ -1613,5 +1708,5 @@ export {
1613
1708
  voicingCount
1614
1709
  };
1615
1710
 
1616
- //# debugId=0FDAEF7C01DCE45464756E2164756E21
1711
+ //# debugId=746B9A1294ADFAB664756E2164756E21
1617
1712
  //# sourceMappingURL=index.js.map