@dsebastien/music-notation 0.24.0 → 0.25.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.
@@ -112,7 +112,10 @@ export declare function playerFor(win: Window, options: PlayOptions, el?: Elemen
112
112
  * guitar, so an app that names none gets exactly the player it gave.
113
113
  */
114
114
  export declare function toned(player: NotePlayer, options: PlayOptions, el?: Element | null): NotePlayer;
115
- /** The guitar that plays for an element: its block's `guitar:` line, else the app's or page's. */
115
+ /**
116
+ * The guitar that plays for an element: the one its reader chose in a bar's Sound menu, else its
117
+ * block's `guitar:` line, else the app's or page's.
118
+ */
116
119
  export declare function guitarFor(options: PlayOptions, el?: Element | null): GuitarTone | null;
117
120
  /** The diagram's own playback data, set by its renderer. */
118
121
  export interface DiagramData {
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Which guitar plays the diagrams (#103): a steel-string acoustic (the default), a nylon-string
3
3
  * classical guitar, a clean electric or an overdriven electric. The reader's side chooses (a
4
- * page's or an app's `guitar`); a block may say `guitar:`. It changes the guitar sound only: a
5
- * piano stays a piano, and tabs and scores keep the instruments their tracks name.
4
+ * page's or an app's `guitar`); a block may say `guitar:`; the reader may choose another one in a
5
+ * play bar's Sound menu. It changes the guitar sound only: a piano stays a piano, and tabs and
6
+ * scores keep the instruments their tracks name.
6
7
  */
7
8
  /** The guitars, the default first. */
8
9
  export declare const GUITAR_TONES: readonly ["steel", "nylon", "clean", "overdrive"];
@@ -23,5 +24,12 @@ export declare function blockGuitar(source: string, chart?: boolean): {
23
24
  };
24
25
  /** The guitar of the block an element is in (`data-mn-guitar`, set from its `guitar:` line). */
25
26
  export declare function guitarWish(el: Element | null | undefined): GuitarTone | null;
27
+ /** The guitar a reader chose on a play bar of the block an element is in; null without one. */
28
+ export declare function guitarChoice(el: Element | null | undefined): GuitarTone | null;
29
+ /**
30
+ * Keeps the guitar a reader chose on a play bar: on the block an element is in, so every bar and
31
+ * diagram of the block plays it, else on the element itself. It lasts as long as the drawing.
32
+ */
33
+ export declare function chooseGuitar(el: Element, guitar: GuitarTone): void;
26
34
  /** A page's guitar (`play: { guitar }` in `window.musicNotationOptions`), when it names one. */
27
35
  export declare function pageGuitar(options: unknown): GuitarTone | undefined;
@@ -6,4 +6,9 @@ export declare function barsFromDrawing(drawing: Element): {
6
6
  bars: ProgressionBar[];
7
7
  marks: Element[][];
8
8
  };
9
- export declare function progressionBarFor(doc: Document, win: Window, drawing: Element, options: PlayOptions, diagrams?: DiagramGroup | null): () => void;
9
+ export declare function progressionBarFor(doc: Document, win: Window, drawing: Element, options: PlayOptions, diagrams?: DiagramGroup | null,
10
+ /**
11
+ * Gives the group of the drawing's diagrams again, after the chart was drawn again (#45): the
12
+ * caller frees the old one.
13
+ */
14
+ regroup?: () => DiagramGroup | null): () => void;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The Sound menu of the play bars (#103): the four guitars, then the piano. A guitar chosen here
3
+ * comes before the block's `guitar:` line and the app's or page's guitar, for the block the bar
4
+ * is in, until it is drawn again. Nothing is stored.
5
+ */
6
+ import type { Translator } from '../i18n/translate.js';
7
+ import { type PlayOptions } from './controls.js';
8
+ import { type GuitarTone } from './guitar-tone.js';
9
+ import type { Sound } from './player.js';
10
+ /** An entry of the menu: a guitar, or the piano. */
11
+ export type SoundChoice = GuitarTone | 'piano';
12
+ export interface SoundMenu {
13
+ el: HTMLSelectElement;
14
+ /** Shows what plays now: the piano, or the guitar in effect (an app's setting may have changed). */
15
+ show(): void;
16
+ }
17
+ /**
18
+ * The menu of a bar that plays `played` (the drawing, or each diagram's wrapper). `sound` is the
19
+ * bar's sound now; `set` gives it the instrument chosen.
20
+ */
21
+ export declare function soundMenu(doc: Document, tr: Translator, options: PlayOptions, played: () => Element[], sound: () => Sound, set: (sound: Sound) => void): SoundMenu;
@@ -516,6 +516,7 @@ var enParse = {
516
516
  "chords.chart.capo": "Capo {capo}",
517
517
  "chords.chart.tempo": "{bpm} BPM",
518
518
  "chords.chart.transposed": "Transposed {amount}",
519
+ "chords.chart.simplified": "Easier chords",
519
520
  "chords.chart.sounds": "{title} (sounds {sounding})",
520
521
  "chords.chart.moreNotDrawn": "{count} more chords not drawn (at most {max})",
521
522
  "chords.chart.missing": "No diagram for {names}: give the shape with {how}",
@@ -530,6 +531,7 @@ var enParse = {
530
531
  "chords.define.fingersCount": "{at}: fingers has {fingers} values for {strings} strings: give one per string (0 for none)",
531
532
  "chords.define.reaches": "{at}: the shape reaches fret {top}: frets go up to {max}",
532
533
  "chords.directive.diagrams": "{at}: use on, off, top or bottom",
534
+ "chords.directive.simplify": "{at}: use yes or no",
533
535
  "chord.shape.open": "Open",
534
536
  "chord.shape.caged": "{letter} shape",
535
537
  "chord.shape.cagedStrings": "{letter} shape (strings {strings})",
@@ -569,7 +571,15 @@ var enPlay = {
569
571
  "playback.click": "Click",
570
572
  "playback.sound": "Sound",
571
573
  "playback.guitar": "Guitar",
574
+ "playback.guitar.steel": "Guitar (steel)",
575
+ "playback.guitar.nylon": "Guitar (nylon)",
576
+ "playback.guitar.clean": "Guitar (clean)",
577
+ "playback.guitar.overdrive": "Guitar (overdrive)",
572
578
  "playback.piano": "Piano",
579
+ "playback.simplify": "Simplify",
580
+ "playback.simplified": "Simplified",
581
+ "playback.simplify.hint": "Show easier chords",
582
+ "playback.simplified.hint": "Show the chords as written",
573
583
  "playback.tempo": "Tempo (BPM)",
574
584
  "playback.unit.bpm": "BPM",
575
585
  "playback.unit.ms": "ms",
@@ -1071,6 +1081,7 @@ var frParse = {
1071
1081
  "chords.chart.capo": "Capodastre {capo}",
1072
1082
  "chords.chart.tempo": "{bpm} BPM",
1073
1083
  "chords.chart.transposed": "Transposé de {amount}",
1084
+ "chords.chart.simplified": "Accords simplifiés",
1074
1085
  "chords.chart.sounds": "{title} (sonne {sounding})",
1075
1086
  "chords.chart.moreNotDrawn": "{count} accords de plus non dessinés (au plus {max})",
1076
1087
  "chords.chart.missing": "Pas de diagramme pour {names} : donnez la forme avec {how}",
@@ -1085,6 +1096,7 @@ var frParse = {
1085
1096
  "chords.define.fingersCount": "{at} : fingers a {fingers} valeurs pour {strings} cordes : donnez-en une par corde (0 pour aucune)",
1086
1097
  "chords.define.reaches": "{at} : la forme atteint la case {top} : les cases vont jusqu'à {max}",
1087
1098
  "chords.directive.diagrams": "{at} : utilisez on, off, top ou bottom",
1099
+ "chords.directive.simplify": "{at} : utilisez yes ou no",
1088
1100
  "chord.shape.open": "Accord ouvert",
1089
1101
  "chord.shape.caged": "forme {letter}",
1090
1102
  "chord.shape.cagedStrings": "forme {letter} (cordes {strings})",
@@ -1124,7 +1136,15 @@ var frPlay = {
1124
1136
  "playback.click": "Clic",
1125
1137
  "playback.sound": "Son",
1126
1138
  "playback.guitar": "Guitare",
1139
+ "playback.guitar.steel": "Guitare (folk)",
1140
+ "playback.guitar.nylon": "Guitare (classique)",
1141
+ "playback.guitar.clean": "Guitare (son clair)",
1142
+ "playback.guitar.overdrive": "Guitare (son saturé)",
1127
1143
  "playback.piano": "Piano",
1144
+ "playback.simplify": "Simplifier",
1145
+ "playback.simplified": "Simplifié",
1146
+ "playback.simplify.hint": "Afficher des accords plus faciles",
1147
+ "playback.simplified.hint": "Afficher les accords écrits",
1128
1148
  "playback.tempo": "Tempo (BPM)",
1129
1149
  "playback.unit.bpm": "BPM",
1130
1150
  "playback.unit.ms": "ms",
@@ -3737,6 +3757,17 @@ function parseDirective(line) {
3737
3757
  return null;
3738
3758
  return { name: m[0].toLowerCase(), value: rest.replace(/^\s*:?/, "").trim() };
3739
3759
  }
3760
+ var SWITCHES = new Map([
3761
+ ["", true],
3762
+ ["yes", true],
3763
+ ["on", true],
3764
+ ["true", true],
3765
+ ["1", true],
3766
+ ["no", false],
3767
+ ["off", false],
3768
+ ["false", false],
3769
+ ["0", false]
3770
+ ]);
3740
3771
  function parseChordPro(source) {
3741
3772
  const song = {
3742
3773
  title: null,
@@ -3751,6 +3782,8 @@ function parseChordPro(source) {
3751
3782
  definitions: [],
3752
3783
  diagrams: null,
3753
3784
  instrument: null,
3785
+ simplify: false,
3786
+ capoAuto: false,
3754
3787
  warnings: []
3755
3788
  };
3756
3789
  let section = { kind: "none", label: null, lines: [] };
@@ -3779,8 +3812,15 @@ function parseChordPro(source) {
3779
3812
  } else if (key === "title" || key === "subtitle" || key === "artist" || key === "key" || key === "tempo" || key === "time") {
3780
3813
  song[key] = value || null;
3781
3814
  } else if (key === "capo") {
3815
+ song.capoAuto = value.trim().toLowerCase() === "auto";
3782
3816
  const capo = Number(value);
3783
3817
  song.capo = Number.isInteger(capo) && capo > 0 ? capo : null;
3818
+ } else if (key === "simplify") {
3819
+ const wish = SWITCHES.get(value.trim().toLowerCase());
3820
+ if (wish !== undefined)
3821
+ song.simplify = wish;
3822
+ else
3823
+ song.warnings.push(t("chords.directive.simplify", { at: `{simplify: ${value}}` }));
3784
3824
  } else if (key === "transpose") {
3785
3825
  const semitones = Number(value);
3786
3826
  if (Number.isInteger(semitones))
@@ -5523,6 +5563,13 @@ function blockGuitar(source, chart = false) {
5523
5563
  function guitarWish(el) {
5524
5564
  return guitarToneOf(el?.closest("[data-mn-guitar]")?.getAttribute("data-mn-guitar"));
5525
5565
  }
5566
+ var CHOICE = "data-mn-guitar-choice";
5567
+ function guitarChoice(el) {
5568
+ return guitarToneOf(el?.closest(`[${CHOICE}]`)?.getAttribute(CHOICE));
5569
+ }
5570
+ function chooseGuitar(el, guitar) {
5571
+ (el.closest(".mn-block") ?? el).setAttribute(CHOICE, guitar);
5572
+ }
5526
5573
  function pageGuitar(options) {
5527
5574
  if (!options || typeof options !== "object" || !("play" in options))
5528
5575
  return;
@@ -6954,6 +7001,44 @@ function volumeOf(volume) {
6954
7001
  return typeof value === "number" && Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : DEFAULT_VOLUME;
6955
7002
  }
6956
7003
 
7004
+ // src/playback/sound-menu.ts
7005
+ function soundMenu(doc, tr, options, played, sound, set) {
7006
+ const el = html(doc, "select", "mn-play-sound");
7007
+ const label = tr("playback.sound");
7008
+ el.setAttribute("aria-label", label);
7009
+ el.title = label;
7010
+ const names = {
7011
+ steel: tr("playback.guitar.steel"),
7012
+ nylon: tr("playback.guitar.nylon"),
7013
+ clean: tr("playback.guitar.clean"),
7014
+ overdrive: tr("playback.guitar.overdrive"),
7015
+ piano: tr("playback.piano")
7016
+ };
7017
+ for (const value of [...GUITAR_TONES, "piano"]) {
7018
+ const option = html(doc, "option", undefined, names[value]);
7019
+ option.value = value;
7020
+ el.appendChild(option);
7021
+ }
7022
+ const show = () => {
7023
+ el.value = sound() === "piano" ? "piano" : guitarFor(options, played()[0]) ?? DEFAULT_GUITAR_TONE;
7024
+ };
7025
+ el.addEventListener("change", () => {
7026
+ const guitar = GUITAR_TONES.find((tone) => tone === el.value);
7027
+ if (!guitar && el.value !== "piano")
7028
+ return show();
7029
+ if (guitar)
7030
+ for (const element of played())
7031
+ chooseGuitar(element, guitar);
7032
+ const next = guitar ? "guitar" : "piano";
7033
+ if (next !== sound())
7034
+ set(next);
7035
+ });
7036
+ el.addEventListener("pointerdown", show);
7037
+ el.addEventListener("focus", show);
7038
+ show();
7039
+ return { el, show };
7040
+ }
7041
+
6957
7042
  // src/playback/app-bar.ts
6958
7043
  var NONE = { tempoChanged() {}, free() {} };
6959
7044
  function handToApp(options, bar, info) {
@@ -8040,7 +8125,7 @@ function timedBarFor(doc, win, drawing, options, piece) {
8040
8125
  settings.bpm = tempoOf(n);
8041
8126
  app.tempoChanged(settings.bpm);
8042
8127
  }, compound ? tr("playback.dotted") : undefined).field);
8043
- bar.appendChild(choice(doc, "mn-play-sound", tr("playback.sound"), ["guitar", "piano"], settings.sound, { guitar: tr("playback.guitar"), piano: tr("playback.piano") }, (value) => settings.sound = value === "piano" ? "piano" : "guitar"));
8128
+ bar.appendChild(soundMenu(doc, tr, options, () => [wrap], () => settings.sound, (value) => settings.sound = value).el);
8044
8129
  if (options.copy !== false)
8045
8130
  bar.append(...copyImageButton(doc, win, () => wrap.closest(".mn-block") ?? wrap));
8046
8131
  const freeFull = addFullScreen(doc, bar, wrap.closest(".mn-block"), options.fullScreen);
@@ -8108,11 +8193,12 @@ var strumLabels = (tr) => ({
8108
8193
  arpeggio: tr("playback.strum.arpeggio"),
8109
8194
  pattern: tr("playback.strum.pattern")
8110
8195
  });
8111
- function progressionBarFor(doc, win, drawing, options, diagrams = null) {
8112
- const { bars, marks } = barsFromDrawing(drawing);
8196
+ function progressionBarFor(doc, win, drawing, options, diagrams = null, regroup) {
8197
+ let { bars, marks } = barsFromDrawing(drawing);
8113
8198
  if (!bars.length)
8114
8199
  return () => {};
8115
8200
  const tr = translator();
8201
+ const language = currentLanguage();
8116
8202
  const time = drawing.getAttribute("data-mn-time");
8117
8203
  let beatsPerBar = 4;
8118
8204
  let beatUnit = 4;
@@ -8150,14 +8236,17 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
8150
8236
  playing = null;
8151
8237
  show();
8152
8238
  };
8153
- if (diagrams) {
8239
+ const wire = () => {
8240
+ if (!diagrams)
8241
+ return;
8154
8242
  diagrams.onPlay = stop;
8155
8243
  diagrams.stopOthers = () => {
8156
8244
  const was = playing !== null;
8157
8245
  stop();
8158
8246
  return was;
8159
8247
  };
8160
- }
8248
+ };
8249
+ wire();
8161
8250
  const start = () => {
8162
8251
  stop();
8163
8252
  if (!drawing.isConnected)
@@ -8198,10 +8287,49 @@ function progressionBarFor(doc, win, drawing, options, diagrams = null) {
8198
8287
  if (playing)
8199
8288
  start();
8200
8289
  }));
8201
- bar.appendChild(choice(doc, "mn-play-sound", tr("playback.sound"), ["guitar", "piano"], settings.sound, { guitar: tr("playback.guitar"), piano: tr("playback.piano") }, (value) => {
8202
- settings.sound = value === "piano" ? "piano" : "guitar";
8290
+ bar.appendChild(soundMenu(doc, tr, options, () => [drawing], () => settings.sound, (value) => {
8291
+ settings.sound = value;
8203
8292
  diagrams?.setSound(settings.sound);
8204
- }));
8293
+ }).el);
8294
+ const redraw = chartRedrawOf(drawing);
8295
+ if (redraw) {
8296
+ const simplify = html(doc, "button", "mn-play-simplify");
8297
+ simplify.type = "button";
8298
+ const shown = () => {
8299
+ const on = drawing.hasAttribute("data-mn-simplified");
8300
+ simplify.textContent = tr(on ? "playback.simplified" : "playback.simplify");
8301
+ simplify.title = tr(on ? "playback.simplified.hint" : "playback.simplify.hint");
8302
+ simplify.setAttribute("aria-pressed", String(on));
8303
+ simplify.classList.toggle("mn-on", on);
8304
+ };
8305
+ simplify.addEventListener("click", () => {
8306
+ stop();
8307
+ diagrams?.stop();
8308
+ const on = !drawing.hasAttribute("data-mn-simplified");
8309
+ const naming = namingOf(drawing.closest("[data-mn-naming]")?.getAttribute("data-mn-naming"));
8310
+ let next;
8311
+ try {
8312
+ next = withLanguage(language, () => naming ? withNaming(naming, () => redraw(on)) : redraw(on));
8313
+ } catch {
8314
+ return;
8315
+ }
8316
+ for (const child of [...drawing.children])
8317
+ if (child !== bar)
8318
+ child.remove();
8319
+ bar.before(...next.children);
8320
+ drawing.toggleAttribute("data-mn-simplified", next.hasAttribute("data-mn-simplified"));
8321
+ ({ bars, marks } = barsFromDrawing(drawing));
8322
+ if (regroup) {
8323
+ diagrams = regroup();
8324
+ wire();
8325
+ if (settings.sound !== (piano ? "piano" : "guitar"))
8326
+ diagrams?.setSound(settings.sound);
8327
+ }
8328
+ shown();
8329
+ });
8330
+ shown();
8331
+ bar.appendChild(simplify);
8332
+ }
8205
8333
  if (options.copy !== false)
8206
8334
  bar.append(...copyImageButton(doc, win, () => drawing.closest(".mn-block") ?? drawing));
8207
8335
  const freeFull = addFullScreen(doc, bar, drawing.closest(".mn-block"), options.fullScreen);
@@ -9114,8 +9242,8 @@ function blockBarFor(doc, win, block, diagrams, options) {
9114
9242
  bar.appendChild(tempo.field);
9115
9243
  if (strum)
9116
9244
  bar.appendChild(strum.field);
9117
- const sound = choice(doc, "mn-play-sound", tr("playback.sound"), ["guitar", "piano"], made.sound, { guitar: tr("playback.guitar"), piano: tr("playback.piano") }, (value) => group.setSound(value === "piano" ? "piano" : "guitar"));
9118
- bar.appendChild(sound);
9245
+ const sound = soundMenu(doc, tr, options, () => items.map((item) => item.wrap), () => made.sound, (value) => group.setSound(value));
9246
+ bar.appendChild(sound.el);
9119
9247
  if (options.copy !== false)
9120
9248
  bar.append(...copyImageButton(doc, win, (whole) => whole || !several ? block : diagramImage(made.selected.wrap)));
9121
9249
  const freeFull = addFullScreen(doc, bar, block, options.fullScreen);
@@ -9136,7 +9264,7 @@ function blockBarFor(doc, win, block, diagrams, options) {
9136
9264
  strum.field.hidden = !(several && item.data.kind === "chord");
9137
9265
  strum.show(made.strumOf(made.sound));
9138
9266
  }
9139
- sound.value = made.sound;
9267
+ sound.show();
9140
9268
  }
9141
9269
  });
9142
9270
  return () => {
@@ -9171,7 +9299,7 @@ function toned(player, options, el) {
9171
9299
  return tone ? withGuitar(player, tone) : player;
9172
9300
  }
9173
9301
  function guitarFor(options, el) {
9174
- const wish = guitarWish(el);
9302
+ const wish = guitarChoice(el) ?? guitarWish(el);
9175
9303
  if (wish)
9176
9304
  return wish;
9177
9305
  try {
@@ -9374,9 +9502,17 @@ function attachPlaybackIn(el, options) {
9374
9502
  for (const [block, diagrams] of byBlock)
9375
9503
  stops.push(blockBarFor(doc, win, block, diagrams, options));
9376
9504
  for (const drawing of timed) {
9377
- const diagrams = byDrawing.get(drawing);
9378
- const group = diagrams?.length ? diagramGroup(doc, win, diagrams, options, { bar: false }).group : null;
9379
- const bar = progressionBarFor(doc, win, drawing, options, group);
9505
+ const groupOf = (diagrams) => diagrams?.length ? diagramGroup(doc, win, diagrams, options, { bar: false }).group : null;
9506
+ let group = groupOf(byDrawing.get(drawing));
9507
+ const regroup = () => {
9508
+ group?.destroy();
9509
+ group = groupOf([...drawing.querySelectorAll("svg[data-mn-pitches]")].flatMap((diagram) => {
9510
+ const data = diagram.hasAttribute("data-mn-pick") ? null : dataOf(diagram);
9511
+ return data ? [{ diagram, data }] : [];
9512
+ }));
9513
+ return group;
9514
+ };
9515
+ const bar = progressionBarFor(doc, win, drawing, options, group, regroup);
9380
9516
  stops.push(() => {
9381
9517
  bar();
9382
9518
  group?.destroy();
@@ -10173,6 +10309,186 @@ function renderChordsUsed(doc, song, options) {
10173
10309
  return root;
10174
10310
  }
10175
10311
 
10312
+ // src/chord/simplify.ts
10313
+ var easiest = new Map;
10314
+ var KEPT = 512;
10315
+ function easiestOf(symbol, options) {
10316
+ const key = `${options.tuning?.join(",") ?? ""}|${symbol}`;
10317
+ const known = easiest.get(key);
10318
+ if (known !== undefined)
10319
+ return known;
10320
+ const generation = options.tuning ? { tuning: options.tuning } : {};
10321
+ const first = generateVoicings(symbol, generation)[0];
10322
+ const found = first ? { frets: [...first.frets], difficulty: first.difficulty, open: first.shape === "Open" } : null;
10323
+ if (easiest.size >= KEPT)
10324
+ easiest.delete(easiest.keys().next().value);
10325
+ easiest.set(key, found);
10326
+ return found;
10327
+ }
10328
+ function plainerSuffixes(symbol) {
10329
+ const info = chordInfo(symbol);
10330
+ if (!info)
10331
+ return [];
10332
+ const has = (interval) => info.members.some((m) => m.interval === interval);
10333
+ const third = has("3M") ? "major" : has("3m") ? "minor" : null;
10334
+ const diminished = third === "minor" && has("5d");
10335
+ const pick = (...found) => found.find(([i]) => has(i))?.[1] ?? null;
10336
+ const seventh = third !== "minor" ? pick(["7M", "maj7"], ["7m", "7"]) : diminished ? pick(["7d", "dim7"], ["7m", "m7b5"]) : pick(["7m", "m7"]);
10337
+ const steps = [];
10338
+ if (seventh)
10339
+ steps.push(seventh);
10340
+ if (seventh === "m7b5")
10341
+ steps.push("m7");
10342
+ if (seventh === "dim7")
10343
+ steps.push("dim");
10344
+ else if (!(diminished && !seventh))
10345
+ steps.push(third === "minor" ? "m" : "");
10346
+ return steps.filter((suffix, i) => suffix !== info.suffix && steps.indexOf(suffix) === i);
10347
+ }
10348
+ function plainerChords(symbol) {
10349
+ const parts = splitChordSymbol(symbol);
10350
+ if (!parts)
10351
+ return [];
10352
+ const own = `${parts.root}${canonicalSuffix(parts.suffix)}`;
10353
+ const bass = parts.bass ? `/${parts.bass}` : "";
10354
+ const found = bass ? [`${parts.root}${parts.suffix}`] : [];
10355
+ for (const suffix of plainerSuffixes(symbol)) {
10356
+ if (bass)
10357
+ found.push(`${parts.root}${suffix}${bass}`);
10358
+ found.push(`${parts.root}${suffix}`);
10359
+ }
10360
+ return found.filter((chord, i) => chord !== own + bass && found.indexOf(chord) === i);
10361
+ }
10362
+ function simplifyChord(symbol, options = {}) {
10363
+ const own = easiestOf(symbol.trim(), options);
10364
+ const limit = own ? own.difficulty : Infinity;
10365
+ return plainerChords(symbol.trim()).map((chord, order) => ({ chord, order, voicing: easiestOf(chord, options) })).filter((c) => c.voicing !== null && c.voicing.difficulty < limit).sort((a, b) => a.voicing.difficulty - b.voicing.difficulty || a.order - b.order).map((c) => ({
10366
+ symbol: c.chord,
10367
+ frets: [...c.voicing.frets],
10368
+ difficulty: c.voicing.difficulty
10369
+ }));
10370
+ }
10371
+ function easierChord(symbol, options = {}) {
10372
+ return simplifyChord(symbol, options)[0]?.symbol ?? symbol.trim();
10373
+ }
10374
+ var UNPLAYABLE = 12;
10375
+ var CAPO_COST = 0.5;
10376
+ function shapeName(chord, capo) {
10377
+ if (!capo)
10378
+ return chord;
10379
+ const flat = transposeChord(chord, -capo, true);
10380
+ const sharp = transposeChord(chord, -capo, false);
10381
+ if (flat === sharp)
10382
+ return flat;
10383
+ return /^[A-G]b/.test(chord) ? flat : sharp;
10384
+ }
10385
+ function capoChoices(chords, options = {}) {
10386
+ const distinct = [...new Set(chords.map((c) => c.trim()))].filter((c) => chordInfo(c) !== null);
10387
+ if (!distinct.length)
10388
+ return [];
10389
+ const max = Math.min(12, Math.max(0, Math.round(options.maxCapo ?? 7) || 0));
10390
+ const choices = [];
10391
+ for (let capo = 0;capo <= max; capo++) {
10392
+ const shapes = distinct.map((chord) => {
10393
+ const shape = shapeName(chord, capo);
10394
+ const voicing = easiestOf(shape, options);
10395
+ return {
10396
+ chord,
10397
+ shape,
10398
+ frets: voicing ? [...voicing.frets] : null,
10399
+ open: voicing?.open ?? false,
10400
+ difficulty: voicing?.difficulty ?? UNPLAYABLE
10401
+ };
10402
+ });
10403
+ choices.push({
10404
+ capo,
10405
+ shapes,
10406
+ open: shapes.filter((s) => s.open).length,
10407
+ difficulty: shapes.reduce((sum, s) => sum + s.difficulty, 0)
10408
+ });
10409
+ }
10410
+ const effort = (c) => c.difficulty + CAPO_COST * c.capo;
10411
+ return choices.sort((a, b) => b.open - a.open || effort(a) - effort(b) || a.capo - b.capo);
10412
+ }
10413
+ function bestCapo(chords, options = {}) {
10414
+ return capoChoices(chords, options)[0] ?? null;
10415
+ }
10416
+
10417
+ // src/chords/easier.ts
10418
+ function withSuffixOf(written, easier) {
10419
+ const own = splitChordSymbol(written);
10420
+ const other = splitChordSymbol(easier);
10421
+ if (!own || !other)
10422
+ return written;
10423
+ return `${own.root}${other.suffix}${other.bass && own.bass ? `/${own.bass}` : ""}`;
10424
+ }
10425
+ function easierSong(song, shownOf, semitones, options) {
10426
+ const same = { song, easier: [], canSimplify: false, capo: null, choice: null };
10427
+ if (!options.guitar)
10428
+ return same;
10429
+ const names = chordsUsed(song);
10430
+ const found = names.filter((written) => !findDefinition(song.definitions, written)).map((written) => {
10431
+ const from = shownOf(written).trim();
10432
+ return { written, from, to: easierChord(from) };
10433
+ }).filter((c) => c.to !== c.from);
10434
+ const canSimplify = found.length > 0;
10435
+ const simpler = new Map;
10436
+ if (options.simplify)
10437
+ for (const c of found)
10438
+ simpler.set(c.written, withSuffixOf(c.written, c.to));
10439
+ const easier = options.simplify ? found.map(({ from, to }) => ({ from, to })) : [];
10440
+ const simplified = (name) => simpler.get(name.trim()) ?? name;
10441
+ const copy = (rename) => song.sections.map((section) => ({
10442
+ ...section,
10443
+ lines: section.lines.map((l) => {
10444
+ if (l.kind === "lyrics")
10445
+ return {
10446
+ kind: "lyrics",
10447
+ segments: l.segments.map((s) => s.chord === null ? s : { ...s, chord: rename(s.chord, true) })
10448
+ };
10449
+ const d = l.kind === "chord" ? l.definition : null;
10450
+ if (d && !d.frets && !d.keys && !d.copy)
10451
+ return {
10452
+ kind: "chord",
10453
+ definition: { ...d, name: rename(d.name, d.transposable) }
10454
+ };
10455
+ return l;
10456
+ })
10457
+ }));
10458
+ const best = song.capoAuto ? bestCapo(names.map((written) => shownOf(simplified(written)).trim())) : null;
10459
+ if (!best || best.capo === 0) {
10460
+ if (!simpler.size)
10461
+ return { ...same, canSimplify, choice: best };
10462
+ return {
10463
+ song: { ...song, sections: copy(simplified) },
10464
+ easier,
10465
+ canSimplify,
10466
+ capo: null,
10467
+ choice: best
10468
+ };
10469
+ }
10470
+ const shapes = new Map(best.shapes.map((s) => [s.chord, s.shape]));
10471
+ const shape = (name, transposable) => {
10472
+ if (!/^[A-G]/.test(name.trim()))
10473
+ return name;
10474
+ const sounding = transposable ? shownOf(simplified(name)).trim() : simplified(name).trim();
10475
+ return shapes.get(sounding) ?? transposeChord(sounding, -best.capo);
10476
+ };
10477
+ return {
10478
+ song: {
10479
+ ...song,
10480
+ capo: best.capo,
10481
+ transpose: song.transpose - semitones,
10482
+ key: song.key ? transposeKey(song.key, semitones) : null,
10483
+ sections: copy(shape)
10484
+ },
10485
+ easier,
10486
+ canSimplify,
10487
+ capo: best.capo,
10488
+ choice: best
10489
+ };
10490
+ }
10491
+
10176
10492
  // src/chords/chart.ts
10177
10493
  var PLACEMENTS = new Set(["top", "bottom", "off"]);
10178
10494
  function lineBeats(segments, playable, beatsPerBar, perPulse) {
@@ -10271,6 +10587,36 @@ function transposerOf(song, extra) {
10271
10587
  chord: (symbol) => transposeChord(symbol, semitones, spelling)
10272
10588
  };
10273
10589
  }
10590
+ function easierPlan(song, options, guitar) {
10591
+ const written = transposerOf(song, options.transpose);
10592
+ return easierSong(song, written.chord, written.semitones, {
10593
+ simplify: options.simplify ?? song.simplify ?? false,
10594
+ guitar
10595
+ });
10596
+ }
10597
+ function easierChart(given, options = {}) {
10598
+ const instrument = given.instrument ?? (options.instrument === "piano" ? "piano" : "guitar");
10599
+ const asked = { ...given, capoAuto: options.capo ?? given.capoAuto ?? false };
10600
+ const plan = easierPlan(asked, options, instrument === "guitar");
10601
+ const renames = new Map;
10602
+ asked.sections.forEach((section, i) => section.lines.forEach((line, j) => {
10603
+ const other = plan.song.sections[i]?.lines[j];
10604
+ if (line.kind !== "lyrics" || other?.kind !== "lyrics")
10605
+ return;
10606
+ line.segments.forEach((segment, k) => {
10607
+ const now = other.segments[k]?.chord;
10608
+ if (segment.chord !== null && now != null && now !== segment.chord)
10609
+ renames.set(segment.chord, now);
10610
+ });
10611
+ }));
10612
+ return {
10613
+ easier: plan.easier,
10614
+ capo: plan.choice,
10615
+ renames,
10616
+ transpose: plan.song.transpose,
10617
+ key: plan.song.key
10618
+ };
10619
+ }
10274
10620
  function meterOf(song) {
10275
10621
  let beatsPerBar = 4;
10276
10622
  let beatUnit = 4;
@@ -10279,7 +10625,9 @@ function meterOf(song) {
10279
10625
  ({ beatsPerBar, beatUnit } = parseTime(time));
10280
10626
  return { beatsPerBar, beatUnit, perPulse: isCompound(beatsPerBar, beatUnit) ? 3 : 1 };
10281
10627
  }
10282
- function chartChords(song, options = {}) {
10628
+ function chartChords(given, options = {}) {
10629
+ const instrument = given.instrument ?? (options.instrument === "piano" ? "piano" : "guitar");
10630
+ const { song } = easierPlan(given, options, instrument === "guitar");
10283
10631
  const { chord } = transposerOf(song, options.transpose);
10284
10632
  const { beatsPerBar, beatUnit, perPulse } = meterOf(song);
10285
10633
  const playable = (written) => {
@@ -10323,11 +10671,19 @@ function chartChords(song, options = {}) {
10323
10671
  tempo: song.tempo && Number.isFinite(tempo) && tempo >= 20 && tempo <= 240 ? tempo : null
10324
10672
  };
10325
10673
  }
10326
- function renderChart(doc, song, options = {}) {
10674
+ var redraws = new WeakMap;
10675
+ function chartRedrawOf(chart) {
10676
+ return redraws.get(chart) ?? null;
10677
+ }
10678
+ function renderChart(doc, given, options = {}) {
10679
+ const instrument = given.instrument ?? (options.instrument === "piano" ? "piano" : "guitar");
10680
+ const plan = easierPlan(given, options, instrument === "guitar");
10681
+ const song = plan.song;
10327
10682
  const { semitones, key, spelling, chord } = transposerOf(song, options.transpose);
10683
+ const transposed = transposerOf(given, options.transpose).semitones;
10328
10684
  const meter = meterOf(song);
10329
10685
  const drawing = {
10330
- instrument: song.instrument ?? (options.instrument === "piano" ? "piano" : "guitar"),
10686
+ instrument,
10331
10687
  ...options.voicings ? { voicings: options.voicings } : {},
10332
10688
  ...options.orientation ? { orientation: options.orientation } : {},
10333
10689
  ...options.hand ? { hand: options.hand } : {}
@@ -10340,6 +10696,10 @@ function renderChart(doc, song, options = {}) {
10340
10696
  ...spelling === undefined ? {} : { spelling }
10341
10697
  });
10342
10698
  const root = html(doc, "div", "mn-chart");
10699
+ if (plan.easier.length)
10700
+ root.setAttribute("data-mn-simplified", "");
10701
+ if (plan.canSimplify)
10702
+ redraws.set(root, (simplify) => renderChart(doc, given, { ...options, simplify }));
10343
10703
  root.setAttribute("data-mn-progression-play", "");
10344
10704
  root.setAttribute("data-mn-sound", drawing.instrument === "piano" ? "piano" : "guitar");
10345
10705
  const time = timeOf(song.time);
@@ -10393,7 +10753,8 @@ function renderChart(doc, song, options = {}) {
10393
10753
  song.capo ? t("chords.chart.capo", { capo: song.capo }) : null,
10394
10754
  song.tempo ? t("chords.chart.tempo", { bpm: song.tempo }) : null,
10395
10755
  song.time ? song.time : null,
10396
- semitones % 12 ? t("chords.chart.transposed", { amount: `${semitones > 0 ? "+" : ""}${semitones}` }) : null
10756
+ transposed % 12 ? t("chords.chart.transposed", { amount: `${transposed > 0 ? "+" : ""}${transposed}` }) : null,
10757
+ plan.easier.length ? t("chords.chart.simplified") : null
10397
10758
  ].filter((m) => m !== null);
10398
10759
  if (meta.length)
10399
10760
  root.appendChild(html(doc, "div", "mn-chart-meta", meta.join(" · ")));
@@ -15244,5 +15605,5 @@ export {
15244
15605
  renderAll
15245
15606
  };
15246
15607
 
15247
- //# debugId=A605C3830CDF43CA64756E2164756E21
15608
+ //# debugId=054081EECF4A18AD64756E2164756E21
15248
15609
  //# sourceMappingURL=index.js.map