@dsebastien/music-notation 0.16.0 → 0.18.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.
@@ -422,6 +422,8 @@ var SIGN = "(##|bb|#|b|♯|♭)?";
422
422
  var syllableLetter = (syllable) => SYLLABLE_LETTER[plain2(syllable)];
423
423
  var AFTER_ROOT = /^(?:$|[0-9/(+°øΔ^-]|m|M|a(?:dd|ug|lt)|sus|dim)/;
424
424
  var QUALITY_WORD = /^(?:[0-9(+°øΔ^]|m|M|a(?:dd|ug|lt)|sus|dim)/;
425
+ var WORD_QUALITY = /^(?:major|minor)$/i;
426
+ var joinsAsQuality = (word) => QUALITY_WORD.test(word) && !WORD_QUALITY.test(word);
425
427
  var reads = (letters) => midiOf(letters) !== null || /^[A-G]/.test(letters) && parseChord(letters) !== null;
426
428
  function rootToken(token, naming, h, solfegeLine = false) {
427
429
  const b = naming === "german" ? /^B(?![#♯b♭x])(.*)$/.exec(token) : null;
@@ -492,7 +494,7 @@ function read(text, naming, options, changes) {
492
494
  let out = text.normalize("NFC").replace(new RegExp(`(^|[\\s,/|])${SYLLABLE}\\s+(dièse|diese|bémol|bemol)(?=$|[\\s,|])`, "g"), (_, before, syllable, sign) => `${before}${syllable}${/^d/.test(sign) ? "#" : "b"}`);
493
495
  if (options.chord) {
494
496
  const joined = new RegExp(`^\\s*${SYLLABLE}${SIGN}\\s+(\\S+)\\s*$`).exec(out);
495
- if (joined && QUALITY_WORD.test(joined[3])) {
497
+ if (joined && joinsAsQuality(joined[3])) {
496
498
  const word = `${joined[1]}${joined[2] ?? ""}${joined[3]}`;
497
499
  const letters = rootToken(word, naming, h) ?? letterPiece(word, naming, h);
498
500
  if (letters !== word) {
@@ -505,7 +507,7 @@ function read(text, naming, options, changes) {
505
507
  for (let i = 0;i + 2 < words.length; i += 2) {
506
508
  const head = words[i];
507
509
  const next = words[i + 2];
508
- if (!syllableOnly(head) || !QUALITY_WORD.test(next))
510
+ if (!syllableOnly(head) || !joinsAsQuality(next))
509
511
  continue;
510
512
  if (naming !== "solfege" && reads(head))
511
513
  continue;
@@ -1818,10 +1820,10 @@ var STACKED = {
1818
1820
  "3M 5P 7M": "maj7",
1819
1821
  "3M 5P 7m": "7",
1820
1822
  "3m 5P 7m": "m7",
1821
- "3m 5P 7M": "mMaj7",
1823
+ "3m 5P 7M": "m(maj7)",
1822
1824
  "3m 5d 7m": "m7b5",
1823
1825
  "3m 5d 7d": "dim7",
1824
- "3M 5A 7M": "maj7#5",
1826
+ "3M 5A 7M": "+maj7",
1825
1827
  "3M 5A 7m": "7#5",
1826
1828
  "3M 5d 7m": "7b5"
1827
1829
  };
@@ -1850,14 +1852,18 @@ function numeralQuality(suffix) {
1850
1852
  case "aug":
1851
1853
  return { lower: false, mark: "+" };
1852
1854
  case "mMaj7":
1853
- return { lower: true, mark: "maj7" };
1855
+ case "m(maj7)":
1856
+ return { lower: true, mark: "(maj7)" };
1857
+ case "maj7#5":
1858
+ case "+maj7":
1859
+ return { lower: false, mark: "+maj7" };
1854
1860
  }
1855
1861
  if (/^m(?!aj)/.test(suffix))
1856
1862
  return { lower: true, mark: suffix.slice(1) };
1857
1863
  return { lower: false, mark: suffix };
1858
1864
  }
1859
1865
  function nashvilleQuality(suffix) {
1860
- return { dim: "°", dim7: "°7", m7b5: "ø7", aug: "+" }[suffix] ?? (suffix === "mMaj7" ? "mmaj7" : suffix);
1866
+ return { dim: "°", dim7: "°7", m7b5: "ø7", aug: "+" }[suffix] ?? (suffix === "mMaj7" ? "m(maj7)" : suffix === "maj7#5" ? "+maj7" : suffix);
1861
1867
  }
1862
1868
  function degreeOf(tonic, note, minorKey = false) {
1863
1869
  if (chromaOf(note) === null || chromaOf(tonic) === null)
@@ -4217,6 +4223,11 @@ function lightPlayer(win, random = Math.random) {
4217
4223
  } catch {}
4218
4224
  }
4219
4225
  cleanup = win.setTimeout(finish, 80);
4226
+ },
4227
+ setVolume(next) {
4228
+ if (stopped || !Number.isFinite(next))
4229
+ return;
4230
+ gain.gain.setTargetAtTime(Math.min(1, Math.max(0, next)), ctx.currentTime, 0.015);
4220
4231
  }
4221
4232
  };
4222
4233
  }
@@ -4366,6 +4377,32 @@ function followNotes(win, drawing, targets, events, wait = START_WAIT) {
4366
4377
  };
4367
4378
  }
4368
4379
 
4380
+ // src/playback/live-volume.ts
4381
+ var listeners = new Set;
4382
+ var level = (value) => typeof value === "number" && Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : null;
4383
+ function volumeChanged() {
4384
+ for (const listener of [...listeners]) {
4385
+ try {
4386
+ listener();
4387
+ } catch {}
4388
+ }
4389
+ }
4390
+ function followVolume(read, handles, start) {
4391
+ let last = level(start);
4392
+ const listener = () => {
4393
+ const now = level(read());
4394
+ if (now === null || now === last)
4395
+ return;
4396
+ last = now;
4397
+ for (const h of handles())
4398
+ h.setVolume?.(now);
4399
+ };
4400
+ listeners.add(listener);
4401
+ return () => {
4402
+ listeners.delete(listener);
4403
+ };
4404
+ }
4405
+
4369
4406
  // src/strum/play.ts
4370
4407
  function scoreOf(pattern, voicing) {
4371
4408
  const changes = pattern.slots.filter((s) => s.chord);
@@ -4474,14 +4511,16 @@ function runPasses(make, player, settings, timers, onEnd, follow) {
4474
4511
  let stopped = false;
4475
4512
  let timer;
4476
4513
  let pass = 0;
4514
+ const handles = [];
4515
+ const unfollow = followVolume(() => settings().volume, () => handles, settings().volume);
4477
4516
  let anchor = null;
4478
4517
  let elapsed = 0;
4479
- const handles = [];
4480
4518
  const followers = [];
4481
4519
  const finish = () => {
4482
4520
  if (stopped)
4483
4521
  return;
4484
4522
  stopped = true;
4523
+ unfollow();
4485
4524
  onEnd?.();
4486
4525
  };
4487
4526
  const schedule = (looping) => {
@@ -4550,6 +4589,7 @@ function runPasses(make, player, settings, timers, onEnd, follow) {
4550
4589
  return {
4551
4590
  stop() {
4552
4591
  stopped = true;
4592
+ unfollow();
4553
4593
  timers.clearTimeout(timer);
4554
4594
  for (const h of handles)
4555
4595
  h.stop();
@@ -5105,6 +5145,7 @@ function renderFretboard(doc, set, options = {}) {
5105
5145
  // src/playback/bar-parts.ts
5106
5146
  var ICONS = {
5107
5147
  play: "M4 2.5v11l9-5.5z",
5148
+ pause: "M4 3h3v10H4zM9 3h3v10H9z",
5108
5149
  stop: "M4 4h8v8H4z",
5109
5150
  all: "M1.5 2.5v11l6.5-5.5zM8 2.5v11l6.5-5.5z",
5110
5151
  loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
@@ -6197,17 +6238,32 @@ function diagramGroup(doc, win, diagrams, options, how) {
6197
6238
  sawStart(player);
6198
6239
  follower.heard();
6199
6240
  };
6241
+ const volume = volumeOf(options.volume);
6242
+ let handle = null;
6243
+ const unfollow = followVolume(() => volumeOf(options.volume), () => handle ? [handle] : [], volume);
6200
6244
  try {
6201
- return player.play(events, {
6245
+ handle = player.play(events, {
6202
6246
  sound: soundOf(item),
6203
- volume: volumeOf(options.volume),
6204
- onEnd,
6247
+ volume,
6248
+ onEnd: () => {
6249
+ unfollow();
6250
+ onEnd();
6251
+ },
6205
6252
  onStart
6206
6253
  });
6207
6254
  } catch (error) {
6255
+ unfollow();
6208
6256
  follower.stop();
6209
6257
  throw error;
6210
6258
  }
6259
+ const played = handle;
6260
+ return {
6261
+ stop() {
6262
+ unfollow();
6263
+ played.stop();
6264
+ },
6265
+ setVolume: (next) => played.setVolume?.(next)
6266
+ };
6211
6267
  };
6212
6268
  try {
6213
6269
  return run(playerFor(here, options));
@@ -6699,6 +6755,7 @@ var DIRECTIONS = {
6699
6755
  ArrowUp: [0, -1],
6700
6756
  ArrowDown: [0, 1]
6701
6757
  };
6758
+ var TOUCH = ["touchstart", "touchend"];
6702
6759
  var RESULT_CLASS = { right: "mn-pick-right", wrong: "mn-pick-wrong" };
6703
6760
  var num = (el, name) => Number(el.getAttribute(name));
6704
6761
  var optional = (el, name) => el.hasAttribute(name) ? num(el, name) : null;
@@ -6863,6 +6920,8 @@ function attachPick(diagram, options = {}) {
6863
6920
  }
6864
6921
  for (const type of ["pointerdown", "mousedown"])
6865
6922
  diagram.addEventListener(type, onPress);
6923
+ for (const type of TOUCH)
6924
+ diagram.addEventListener(type, onPress, { passive: true });
6866
6925
  diagram.addEventListener("click", onClick);
6867
6926
  diagram.addEventListener("keydown", onKey);
6868
6927
  const groups = new Set(targets.map((t) => t.parentElement));
@@ -6976,6 +7035,8 @@ function attachPick(diagram, options = {}) {
6976
7035
  marks = null;
6977
7036
  for (const type of ["pointerdown", "mousedown"])
6978
7037
  diagram.removeEventListener(type, onPress);
7038
+ for (const type of TOUCH)
7039
+ diagram.removeEventListener(type, onPress);
6979
7040
  diagram.removeEventListener("click", onClick);
6980
7041
  diagram.removeEventListener("keydown", onKey);
6981
7042
  for (const el of targets) {
@@ -8604,21 +8665,12 @@ var FINGER_NAMES = {
8604
8665
  };
8605
8666
  var ordinal2 = (n) => `${n}${n % 100 >= 11 && n % 100 <= 13 ? "th" : ["th", "st", "nd", "rd"][n % 10] ?? "th"}`;
8606
8667
  var MAX_SLIDE = 3;
8607
- function stringName(tuning, s) {
8608
- const name = noteOf(tuning[s]).replace(/-?\d+$/, "");
8609
- const same = tuning.filter((p) => noteOf(p).replace(/-?\d+$/, "") === name).length;
8610
- if (same < 2)
8611
- return `the ${name} string`;
8612
- if (same > 2)
8613
- return `the ${ordinal2(tuning.length - s)} string (${name})`;
8614
- const lowest = tuning.findIndex((p) => noteOf(p).replace(/-?\d+$/, "") === name);
8615
- return `the ${s === lowest ? "low" : "high"} ${name} string`;
8616
- }
8668
+ var namesOf2 = (tuning, spelled) => stringNames(tuning, tuning.length, spelled?.length === tuning.length ? spelled : undefined);
8617
8669
  var lowestFret2 = (frets) => {
8618
8670
  const fretted = frets.filter((f) => f !== null && f > 0);
8619
8671
  return fretted.length ? Math.min(...fretted) : 0;
8620
8672
  };
8621
- function move(from, fromFingers, to, tuning) {
8673
+ function move(from, fromFingers, to, names) {
8622
8674
  const anchors = [];
8623
8675
  const guides = [];
8624
8676
  let movement = 0;
@@ -8651,14 +8703,14 @@ function move(from, fromFingers, to, tuning) {
8651
8703
  if (seen.has(f))
8652
8704
  continue;
8653
8705
  seen.add(f);
8654
- tips.push(`Keep your ${fingerName(f)} on ${stringName(tuning, s)}'s ${ordinal2(to.frets[s])} fret.`);
8706
+ tips.push(`Keep your ${fingerName(f)} on ${names[s]}'s ${ordinal2(to.frets[s])} fret.`);
8655
8707
  }
8656
8708
  for (const s of guides) {
8657
8709
  const f = to.fingers[s];
8658
8710
  if (seen.has(f))
8659
8711
  continue;
8660
8712
  seen.add(f);
8661
- tips.push(`Slide your ${fingerName(f)} along ${stringName(tuning, s)} from fret ${from[s]} to ${to.frets[s]}.`);
8713
+ tips.push(`Slide your ${fingerName(f)} along ${names[s]} from fret ${from[s]} to ${to.frets[s]}.`);
8662
8714
  }
8663
8715
  if (shift >= 2) {
8664
8716
  const up = lowestFret2(to.frets) > lowestFret2(from);
@@ -8675,8 +8727,9 @@ function transition(from, toChord, options = {}) {
8675
8727
  if (from.length !== tuning.length)
8676
8728
  return [];
8677
8729
  const fromFingers = options.fromFingers ?? fingerVoicing(from) ?? from.map(() => null);
8730
+ const names = namesOf2(tuning, options.tuningNames);
8678
8731
  const candidates = generateVoicings(toChord, { tuning }).slice(0, options.candidates ?? 30);
8679
- return candidates.map((voicing) => ({ voicing, ...move(from, fromFingers, voicing, tuning) })).sort((a, b) => score(a, a.voicing) - score(b, b.voicing));
8732
+ return candidates.map((voicing) => ({ voicing, ...move(from, fromFingers, voicing, names) })).sort((a, b) => score(a, a.voicing) - score(b, b.voicing));
8680
8733
  }
8681
8734
  var DATABASE_VOICINGS = 6;
8682
8735
  var HIGH_POSITION = 5;
@@ -8728,6 +8781,7 @@ function candidatesOf(symbol, tuning, count, voicings) {
8728
8781
  var sameTuning4 = (a, b) => a.length === b.length && a.every((p, i) => p === b[i]);
8729
8782
  function smoothProgression(chords, options = {}) {
8730
8783
  const tuning = options.tuning ?? STANDARD_TUNING;
8784
+ const names = namesOf2(tuning, options.tuningNames);
8731
8785
  const count = options.candidates ?? 12;
8732
8786
  const cache = new Map;
8733
8787
  const lists = chords.map((c) => {
@@ -8748,7 +8802,7 @@ function smoothProgression(chords, options = {}) {
8748
8802
  let best = Infinity;
8749
8803
  let from = 0;
8750
8804
  prev.forEach((p, j) => {
8751
- const step = move(p.voicing.frets, p.voicing.fingers, voicing, tuning);
8805
+ const step = move(p.voicing.frets, p.voicing.fingers, voicing, names);
8752
8806
  const total = costs[j] + step.movement + SHAPE_WEIGHT * cost;
8753
8807
  if (total < best) {
8754
8808
  best = total;
@@ -8771,7 +8825,7 @@ function smoothProgression(chords, options = {}) {
8771
8825
  return {
8772
8826
  chord: chords[i],
8773
8827
  voicing,
8774
- ...move(previous.frets, previous.fingers, voicing, tuning)
8828
+ ...move(previous.frets, previous.fingers, voicing, names)
8775
8829
  };
8776
8830
  });
8777
8831
  }
@@ -9005,6 +9059,8 @@ function noteOn(key, degree, accidental, diminished) {
9005
9059
  function suffixOf(mark, lower) {
9006
9060
  let s = mark.replace(/♭/g, "b").replace(/♯/g, "#");
9007
9061
  s = s.replace(/^o/, "°").replace(/^Δ7?$/, "maj7").replace(/^M7$/, "maj7");
9062
+ if (lower)
9063
+ s = s.replace(/^\((maj\d*)\)$/, "$1").replace(/^m(?=[Mm]aj)/, "").replace(/^Maj/, "maj");
9008
9064
  if (s === "°" || s === "dim")
9009
9065
  return "dim";
9010
9066
  if (s === "°7" || s === "dim7")
@@ -9026,8 +9082,10 @@ function suffixOf(mark, lower) {
9026
9082
  return `m${s}`;
9027
9083
  }
9028
9084
  var isDiminished = (suffix) => /^(dim|m7b5)/.test(suffix);
9085
+ var USUAL = { mMaj7: "m(maj7)", "maj7#5": "+maj7" };
9029
9086
  function chordFrom(root, suffix) {
9030
- const symbol = `${root}${canonicalSuffix(suffix)}`;
9087
+ const canonical = canonicalSuffix(suffix);
9088
+ const symbol = `${root}${USUAL[canonical] ?? canonical}`;
9031
9089
  return parseChord(symbol) && chordNoteSet(symbol) ? symbol : null;
9032
9090
  }
9033
9091
  function bassOf(key, text) {
@@ -9520,7 +9578,8 @@ var SHARED = [
9520
9578
  "tempo",
9521
9579
  "bpm",
9522
9580
  "strum",
9523
- "harmonize"
9581
+ "harmonize",
9582
+ "caption"
9524
9583
  ];
9525
9584
  var KIND_WORDS = {
9526
9585
  triad: "triads",
@@ -9740,7 +9799,7 @@ function guitarVoicings(symbols, tuningText, voicings) {
9740
9799
  var fretsText = (list) => list.some((f) => f !== null && f > 9) ? list.map((f) => f === null ? "x" : f).join("-") : list.map((f) => f === null ? "x" : f).join("");
9741
9800
  function diagramBlock(block, lines) {
9742
9801
  const fields = new Map(lines);
9743
- for (const key of ["orientation", "hand", "labels", "tuning"]) {
9802
+ for (const key of ["orientation", "hand", "labels", "tuning", "caption"]) {
9744
9803
  const value = field(block, key);
9745
9804
  if (value !== undefined)
9746
9805
  fields.set(key, value);
@@ -9806,6 +9865,10 @@ function renderSection(doc, section, options, renderChord) {
9806
9865
  }
9807
9866
  const piano = instrument !== "guitar";
9808
9867
  const diagrams = yes(field(block, "diagrams"), "diagrams", true);
9868
+ const caption = field(block, "caption")?.trim();
9869
+ if (caption && !["fingering", "none", "no", "off", "false"].includes(caption.toLowerCase())) {
9870
+ throw new Error(`caption: use fingering (the fingering in words) or none, got "${caption}"`);
9871
+ }
9809
9872
  const functions = yes(field(block, "functions"), "functions", false);
9810
9873
  const leading = field(block, "voice-leading");
9811
9874
  const led = yes(leading, "voice-leading", false);
@@ -10808,6 +10871,8 @@ var renderChord = (doc, block, options) => {
10808
10871
  }
10809
10872
  const sounding = symbol && capo ? soundingChord(symbol, capo) : symbol;
10810
10873
  const sounds = soundsField(field2(block, "sounds"), symbol, capo);
10874
+ if (capo && sounding && soundsInTitle(field2(block, "sounds")) && !field2(block, "title"))
10875
+ title = `${sounding} (${title ?? symbol} shape)`;
10811
10876
  const root = sounding ? chordNoteSet(sounding)?.root ?? rootOf2(sounding) : null;
10812
10877
  const strings = tuning ?? (voicing.frets.length === 6 ? STANDARD_TUNING : undefined);
10813
10878
  const labels = dotLabels(field2(block, "labels"), sounding, shape, strings && capo ? capoTuning(strings, capo) : tuning);
@@ -10857,10 +10922,17 @@ function captioned(doc, diagram, sentences) {
10857
10922
  figure.append(diagram, html(doc, "figcaption", "mn-fingering", sentences.join(" ")));
10858
10923
  return figure;
10859
10924
  }
10925
+ var soundsInTitle = (text) => /^title$/i.test(text?.trim() ?? "");
10860
10926
  function soundsField(text, shape, capo) {
10861
10927
  const value = text?.trim();
10862
10928
  if (!value || /^(no|false|off)$/i.test(value))
10863
10929
  return;
10930
+ if (soundsInTitle(value)) {
10931
+ if (capo && (!shape || !chordNoteSet(shape))) {
10932
+ throw new Error("sounds: title needs a chord the library knows: add its symbol (e.g. symbol: A)");
10933
+ }
10934
+ return;
10935
+ }
10864
10936
  const auto = /^(yes|true|on|auto)$/i.test(value);
10865
10937
  if (!capo) {
10866
10938
  if (auto)
@@ -12351,5 +12423,5 @@ export {
12351
12423
  renderAll
12352
12424
  };
12353
12425
 
12354
- //# debugId=45178CB1E7CD5EC664756E2164756E21
12426
+ //# debugId=EC1DD34B4CF525E764756E2164756E21
12355
12427
  //# sourceMappingURL=index.js.map