@dsebastien/music-notation 0.9.0 → 0.10.1

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.
@@ -7,6 +7,11 @@ export declare function svg(doc: Document, tag: string, attrs?: Attributes, chil
7
7
  export declare function svgText(doc: Document, text: string, attrs?: Attributes): SVGElement;
8
8
  /** A <text> vertically centered on (x, y), for the 10–11px labels of diagrams. */
9
9
  export declare function svgLabel(doc: Document, text: string, [x, y]: [number, number], cls: string, anchor?: 'start' | 'middle' | 'end'): SVGElement;
10
+ /**
11
+ * The text in a note dot of radius `r`: a label of three characters or more (bb7, #11, b13) is
12
+ * smaller and squeezed to the dot's inner width, so it never passes the dot's edge (#73).
13
+ */
14
+ export declare function dotLabel(doc: Document, text: string, point: [number, number], r: number): SVGElement;
10
15
  /** A <line> between two points. */
11
16
  export declare function svgLine(doc: Document, [x1, y1]: [number, number], [x2, y2]: [number, number], cls: string): SVGElement;
12
17
  /** An HTML element with a class and optional text. */
@@ -38,6 +38,12 @@ export interface StrumPattern {
38
38
  /** Open strings for playback (`tuning:`), as written; null for standard guitar. */
39
39
  tuning: string | null;
40
40
  title: string | null;
41
+ /**
42
+ * How strums written alone were read when the hand's motion placed them and the bar ends with
43
+ * whole beats of no strum (`D U D U` in triplets: `D - U D - U` and 2 empty beats), with what
44
+ * to write instead; null otherwise (#73).
45
+ */
46
+ note: string | null;
41
47
  }
42
48
  /** Bounds that keep a pattern quick to draw and to hear. */
43
49
  export declare const MAX_STRUM_BARS = 16;
@@ -17,4 +17,4 @@ export declare function renderStrum(doc: Document, pattern: StrumPattern, option
17
17
  * option's on a standard guitar, else generated ones, for the block's `tuning:`). Throws a
18
18
  * message for the user.
19
19
  */
20
- export declare function renderStrumBlock(doc: Document, source: string, options: RenderOptions): SVGElement;
20
+ export declare function renderStrumBlock(doc: Document, source: string, options: RenderOptions): Element;
@@ -60,11 +60,17 @@ export interface TabSource {
60
60
  tex: string;
61
61
  /** Lines of the block before the alphaTex (settings, blank lines), for error positions. */
62
62
  texLine: number;
63
+ /**
64
+ * Whether `tex` was converted from a plain-text tab written in the block (a tab pasted from
65
+ * the web, #71): `textTabToAlphaTex`, in memory, the block's text unchanged.
66
+ */
67
+ fromText: boolean;
63
68
  }
64
69
  /**
65
70
  * A tab block's own settings (lines `staves:`, `file:`, `tracks:` at the top, `//` comments
66
- * allowed among them), then its alphaTex. ```tab blocks show tablature, ```score blocks standard
67
- * notation, unless `staves:` says otherwise. Throws a message for the user.
71
+ * allowed among them), then its alphaTex, or a plain-text tab, converted (#71). ```tab blocks show
72
+ * tablature, ```score blocks standard notation, unless `staves:` says otherwise. Throws a message
73
+ * for the user.
68
74
  */
69
75
  export declare function parseTabSource(language: 'tab' | 'score', source: string): TabSource;
70
76
  /** The part of alphaTab's score read and set to hide its default dynamics. */
package/dist/tab/index.js CHANGED
@@ -22,6 +22,15 @@ function svgText(doc, text, attrs = {}) {
22
22
  function svgLabel(doc, text, [x, y], cls, anchor = "middle") {
23
23
  return svgText(doc, text, { class: cls, x, y: y + 4, "text-anchor": anchor });
24
24
  }
25
+ function dotLabel(doc, text, point, r) {
26
+ const label = svgLabel(doc, text, point, "mn-dot-text");
27
+ if ([...text].length >= 3) {
28
+ label.classList.add("mn-dot-text-long");
29
+ label.setAttribute("textLength", String(2 * r - 2));
30
+ label.setAttribute("lengthAdjust", "spacingAndGlyphs");
31
+ }
32
+ return label;
33
+ }
25
34
  function svgLine(doc, [x1, y1], [x2, y2], cls) {
26
35
  return svg(doc, "line", { class: cls, x1, y1, x2, y2 });
27
36
  }
@@ -135,11 +144,20 @@ var isFlat = (name) => /^[A-Ga-g][b♭]/.test(name.trim());
135
144
  var NAMED = new Map(Object.entries({
136
145
  standard: "E2 A2 D3 G3 B3 E4",
137
146
  "drop d": "D2 A2 D3 G3 B3 E4",
147
+ "drop c": "C2 G2 C3 F3 A3 D4",
148
+ "drop b": "B1 F#2 B2 E3 G#3 C#4",
149
+ "drop a": "A1 E2 A2 D3 F#3 B3",
150
+ "double drop d": "D2 A2 D3 G3 B3 D4",
138
151
  dadgad: "D2 A2 D3 G3 A3 D4",
139
152
  "open g": "D2 G2 D3 G3 B3 D4",
140
153
  "open d": "D2 A2 D3 F#3 A3 D4",
141
154
  "open e": "E2 B2 E3 G#3 B3 E4",
155
+ "open c": "C2 G2 C3 G3 C4 E4",
156
+ "open a": "E2 A2 E3 A3 C#4 E4",
142
157
  "half step down": "Eb2 Ab2 Db3 Gb3 Bb3 Eb4",
158
+ "whole step down": "D2 G2 C3 F3 A3 D4",
159
+ "modal c6": "C2 A2 C3 G3 C4 E4",
160
+ c6: "C2 A2 C3 G3 C4 E4",
143
161
  bass: "E1 A1 D2 G2",
144
162
  ukulele: "G4 C4 E4 A4"
145
163
  }));
@@ -540,6 +558,19 @@ function onGuitar(midi) {
540
558
  }
541
559
  return { tuning, string: index + 1 };
542
560
  }
561
+ function placedOnGuitar(tuning) {
562
+ if (tuning.length === 1) {
563
+ const { tuning: six, string } = onGuitar(tuning[0]);
564
+ return { tuning: six, strings: [string] };
565
+ }
566
+ const standard = BY_COUNT[6];
567
+ if (tuning.length > 5)
568
+ return null;
569
+ const at = tuning.map((m) => standard.indexOf(m));
570
+ if (at[0] < 0 || at.some((i, k) => i !== at[0] + k))
571
+ return null;
572
+ return { tuning: [...standard], strings: at.map((i) => i + 1) };
573
+ }
543
574
  var drumBeatAt = (drums) => (padded, col) => {
544
575
  const beat = [];
545
576
  padded.forEach((line, i) => {
@@ -615,6 +646,8 @@ function positionsOf(cols, width, pad, meter = COMMON) {
615
646
  [pad, width - pad],
616
647
  [pad, width]
617
648
  ];
649
+ for (let start = 1;start < pad; start++)
650
+ spans.push([start, width - start]);
618
651
  if (cols[0] > pad)
619
652
  spans.push([0, width]);
620
653
  for (const [slack, ties] of [
@@ -641,7 +674,7 @@ function restsFrom(at, meter, end = meter.length) {
641
674
  }
642
675
  return out;
643
676
  }
644
- function sketchRhythmOf(cols, meter = COMMON) {
677
+ function sketchRhythmOf(cols, meter = COMMON, legato = []) {
645
678
  const gaps = cols.slice(1).map((c, i) => c - cols[i]);
646
679
  const counts = new Map;
647
680
  for (const g of gaps)
@@ -649,7 +682,18 @@ function sketchRhythmOf(cols, meter = COMMON) {
649
682
  const unit = [...counts].reduce((best, e) => !best || e[1] > best[1] || e[1] === best[1] && e[0] < best[0] ? e : best, null)?.[0];
650
683
  for (const eighth of [2, 1]) {
651
684
  const lengths = cols.map((_, i) => eighth * (i < gaps.length ? Math.max(1, Math.round(gaps[i] / unit)) : 1));
652
- const total = lengths.reduce((a, b) => a + b, 0);
685
+ let total = lengths.reduce((a, b) => a + b, 0);
686
+ if (eighth === 2)
687
+ for (const i of legato) {
688
+ if (total <= meter.length)
689
+ break;
690
+ if (i + 1 >= lengths.length || lengths[i] !== 2)
691
+ continue;
692
+ const next = Math.max(1, lengths[i + 1] - 1);
693
+ total -= 1 + lengths[i + 1] - next;
694
+ lengths[i] = 1;
695
+ lengths[i + 1] = next;
696
+ }
653
697
  if (total > meter.length)
654
698
  continue;
655
699
  return [
@@ -659,6 +703,8 @@ function sketchRhythmOf(cols, meter = COMMON) {
659
703
  }
660
704
  return cols.map((_, beat) => ({ beat, sixteenths: 1 }));
661
705
  }
706
+ var LEGATO = new Set(["h", "sl"]);
707
+ var legatoOf = (beats) => beats.flatMap((b, i) => b.notes.some((n) => n.effects.some((e) => LEGATO.has(e))) ? [i] : []);
662
708
  function rhythmOf(m, typical, meter) {
663
709
  const bar = meter.length;
664
710
  const span = m.part === "whole" ? m.width : typical;
@@ -675,7 +721,7 @@ function rhythmOf(m, typical, meter) {
675
721
  }
676
722
  const at = positionsOf(cols, span, m.pad, meter);
677
723
  if (!at) {
678
- const sketch = sketchRhythmOf(m.beats.map((b) => b.col), meter);
724
+ const sketch = sketchRhythmOf(m.beats.map((b) => b.col), meter, legatoOf(m.beats));
679
725
  if (m.part !== "end")
680
726
  return sketch;
681
727
  const notes = sketch.filter((e) => e.beat >= 0);
@@ -835,6 +881,42 @@ function measuresOf(rows, typical) {
835
881
  });
836
882
  return out;
837
883
  }
884
+ function splitUnbarred(measures, meter) {
885
+ return measures.flatMap((m) => {
886
+ if (m.part !== "whole" || m.beats.length < 2)
887
+ return [m];
888
+ const cols = m.beats.map((b) => b.col);
889
+ if (positionsOf(cols, m.width, m.pad, meter))
890
+ return [m];
891
+ const gaps = cols.slice(1).map((c, i) => c - cols[i]);
892
+ const unit = mostFrequent(gaps).value;
893
+ const eighths = gaps.reduce((sum, g) => sum + Math.max(1, Math.round(g / unit)), 1);
894
+ const perBar = meter.length / 2;
895
+ const span = unit * perBar;
896
+ if (eighths <= perBar || m.width - m.pad < span * 1.5)
897
+ return [m];
898
+ const count = Math.floor((cols.at(-1) - m.pad) / span) + 1;
899
+ const pieces = Array.from({ length: count }, () => []);
900
+ for (const b of m.beats) {
901
+ let k = Math.min(count - 1, Math.max(0, Math.floor((b.col - m.pad) / span)));
902
+ while (k < count - 1 && b.col >= m.pad + (k + 1) * span)
903
+ k++;
904
+ while (k > 0 && b.col < m.pad + k * span)
905
+ k--;
906
+ pieces[k].push({ notes: b.notes, col: b.col - k * span });
907
+ }
908
+ return pieces.map((beats, k) => {
909
+ return {
910
+ beats,
911
+ width: m.pad + span,
912
+ pad: m.pad,
913
+ part: "whole",
914
+ pickup: false,
915
+ text: k === count - 1 ? m.text : null
916
+ };
917
+ });
918
+ });
919
+ }
838
920
  var quoted = (text) => `"${text.replace(/[\\"]/g, (c) => `\\${c}`)}"`;
839
921
  var withEffects = (tex, effects) => effects.length ? `${tex}{${effects.join(" ")}}` : tex;
840
922
  function beatTex(notes, effects) {
@@ -892,9 +974,9 @@ function textTabToAlphaTex(text) {
892
974
  if (!first)
893
975
  return null;
894
976
  let tuning = first.kind === "strings" ? tuningOf(first.names) : null;
895
- const lone = tuning?.length === 1 ? onGuitar(tuning[0]) : null;
896
- if (lone)
897
- tuning = lone.tuning;
977
+ const placed = tuning ? placedOnGuitar(tuning) : null;
978
+ if (placed)
979
+ tuning = placed.tuning;
898
980
  const rows = [];
899
981
  if (first.kind === "drums") {
900
982
  for (const s of systems)
@@ -902,7 +984,7 @@ function textTabToAlphaTex(text) {
902
984
  rows.push(rowOf(s.lines, barsOf(s.lines, drumBeatAt(s.drums)), s.texts, false));
903
985
  } else {
904
986
  const same = JSON.stringify(tuningOf(first.names));
905
- const strings = lone ? [lone.string] : [];
987
+ const strings = placed?.strings ?? [];
906
988
  for (const s of systems) {
907
989
  if (s.kind !== "strings" || JSON.stringify(tuningOf(s.names)) !== same)
908
990
  continue;
@@ -912,7 +994,7 @@ function textTabToAlphaTex(text) {
912
994
  }
913
995
  const typical = typicalWidth(rows);
914
996
  const meter = meterOf(rows, typical);
915
- const bars = measuresOf(rows, typical).map((m) => ({
997
+ const bars = splitUnbarred(measuresOf(rows, typical), meter).map((m) => ({
916
998
  beats: m.beats.map((b) => b.notes),
917
999
  rhythm: rhythmOf(m, typical ?? 0, meter),
918
1000
  text: m.text,
@@ -1355,7 +1437,8 @@ function parseTabSource(language, source) {
1355
1437
  file: null,
1356
1438
  tracks: null,
1357
1439
  tex: "",
1358
- texLine: 0
1440
+ texLine: 0,
1441
+ fromText: false
1359
1442
  };
1360
1443
  let i = 0;
1361
1444
  for (;i < lines.length; i++) {
@@ -1391,6 +1474,11 @@ function parseTabSource(language, source) {
1391
1474
  `).trimEnd();
1392
1475
  if (!out.file && !out.tex)
1393
1476
  throw new Error("Write the music in alphaTex, or give a file: [[Song.gp]]");
1477
+ const converted = out.file ? null : textTabToAlphaTex(out.tex);
1478
+ if (converted !== null) {
1479
+ out.tex = converted;
1480
+ out.fromText = true;
1481
+ }
1394
1482
  return out;
1395
1483
  }
1396
1484
  function messageOf(error, lineOffset) {
@@ -1401,7 +1489,7 @@ function messageOf(error, lineOffset) {
1401
1489
  continue;
1402
1490
  const first = items.find((d) => d.severity === 2 && d.message);
1403
1491
  if (first) {
1404
- const at = first.start ? `line ${first.start.line + lineOffset}, column ${first.start.col}: ` : "";
1492
+ const at = first.start && lineOffset !== null ? `line ${first.start.line + lineOffset}, column ${first.start.col}: ` : "";
1405
1493
  return `${at}${first.message}`;
1406
1494
  }
1407
1495
  }
@@ -1787,7 +1875,7 @@ function renderTab(language, source, host, options) {
1787
1875
  settle({ ok: true, error: null });
1788
1876
  });
1789
1877
  current.error.on((e) => {
1790
- const message = messageOf(e, parsed.texLine);
1878
+ const message = parsed.fromText ? `Couldn't draw this text tab: ${messageOf(e, null)}` : messageOf(e, parsed.texLine);
1791
1879
  if (settled)
1792
1880
  bar?.failed(message);
1793
1881
  else
@@ -1998,5 +2086,5 @@ export {
1998
2086
  textTabToAlphaTex
1999
2087
  };
2000
2088
 
2001
- //# debugId=64A5A150256EB96064756E2164756E21
2089
+ //# debugId=7EA581924096D5C364756E2164756E21
2002
2090
  //# sourceMappingURL=index.js.map