@dsebastien/music-notation 0.5.0 → 0.5.2

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.
package/README.md CHANGED
@@ -109,32 +109,37 @@ if (!result.ok) console.warn(result.error)
109
109
 
110
110
  - `renderBlock(language, source, host, options)` appends the rendering to `host` and never throws. It returns `{ ok, el, error, destroy() }`.
111
111
  - `@dsebastien/music-notation/voicings` adds about 1,800 guitar voicings, found by chord symbol (`chord` blocks with only a symbol). It's a separate entry point because the data weighs about 230 KB.
112
- - **Hear the diagrams**: with the `play` option, a playback bar on each diagram (on hover): chords strum, scales go up and down, a click plays a note.
113
- - **Tabs and scores**: `@dsebastien/music-notation/tab` draws `tab and `score blocks (alphaTex, or Guitar Pro and MusicXML files) with [alphaTab](https://alphatab.net), with a player; it also converts score files, text tabs and chord sheets, and exports Guitar Pro 7 and MIDI.
112
+ - **Hear the diagrams**: the `play` option adds a playback bar to each diagram, shown on hover. Chords strum, scales go up and down, and a click plays a note.
113
+ - **Tabs and scores**: `@dsebastien/music-notation/tab` draws `tab` and `score` blocks (alphaTex, or Guitar Pro and MusicXML files) with [alphaTab](https://alphatab.net), and adds a player. It also converts score files, text tabs and chord sheets, and exports Guitar Pro 7 and MIDI.
114
114
  - `generateVoicings(symbol, options)` finds every playable voicing of a chord on any tuning, easiest first, with fingers and shape names; `fingerVoicing(frets)` fingers any voicing.
115
115
  - The renderers (`renderGuitarDiagram`, `renderKeyboard`, `renderFretboard`, `renderChart`), the parsers and the theory helpers (`parseChord`, `transposeChord`, `chordNoteSet`, `scaleNoteSet`…) are exported too.
116
116
  - Styles: `injectStyles(document)`, or ship `@dsebastien/music-notation/music-notation.css`. Colors follow Obsidian's theme variables, with fallbacks for any page.
117
117
 
118
118
  ## Obsidian Publish
119
119
 
120
- Add this to your site's `publish.js`. It loads the bundle only on pages that have notation blocks:
120
+ Add this to your site's `publish.js`. It loads the bundle only on pages that have notation blocks, and the tab bundle (with alphaTab) only on pages with tabs or scores:
121
121
 
122
122
  ```js
123
123
  ;(() => {
124
- const VERSION = '0.4.0'
125
- const SELECTOR = ['chord', 'chords', 'keyboard', 'fretboard']
126
- .map((l) => `pre > code.language-${l}`)
127
- .join(', ')
128
- let requested = false
129
- const load = () => {
130
- if (requested || !document.querySelector(SELECTOR)) return
131
- requested = true
124
+ const VERSION = '0.5.1'
125
+ const CDN = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist`
126
+ const selector = (languages) => languages.map((l) => `pre > code.language-${l}`).join(', ')
127
+ const BLOCKS = selector(['chord', 'chords', 'keyboard', 'fretboard'])
128
+ const TABS = selector(['tab', 'score'])
129
+ const requested = new Set()
130
+ const load = (file) => {
131
+ if (requested.has(file)) return
132
+ requested.add(file)
132
133
  const script = document.createElement('script')
133
- script.src = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist/music-notation.publish.min.js`
134
+ script.src = `${CDN}/${file}`
134
135
  document.head.appendChild(script)
135
136
  }
136
- load()
137
- new MutationObserver(load).observe(document.body, { childList: true, subtree: true })
137
+ const check = () => {
138
+ if (document.querySelector(BLOCKS)) load('music-notation.publish.min.js')
139
+ if (document.querySelector(TABS)) load('music-notation.tab.min.js')
140
+ }
141
+ check()
142
+ new MutationObserver(check).observe(document.body, { childList: true, subtree: true })
138
143
  })()
139
144
  ```
140
145
 
@@ -142,7 +147,7 @@ The bundle renders every block, now and as you navigate, and hides a block's sou
142
147
 
143
148
  ## Credits and licenses
144
149
 
145
- MIT. Built on [tonal](https://github.com/tonaljs/tonal) (MIT) for music theory and [chords-db](https://github.com/tombatossals/chords-db) (MIT) for guitar voicings.
150
+ MIT. Built on [tonal](https://github.com/tonaljs/tonal) (MIT) for music theory and [chords-db](https://github.com/tombatossals/chords-db) (MIT) for guitar voicings. Tabs and scores use [alphaTab](https://alphatab.net) (MPL-2.0), which the app passes in. The tab bundle loads it from jsDelivr.
146
151
 
147
152
  ## News & support
148
153
 
package/dist/index.js CHANGED
@@ -559,7 +559,7 @@ var CANONICAL = new Map([
559
559
  var canonicalSuffix = (suffix) => CANONICAL.get(suffix) ?? suffix;
560
560
 
561
561
  // src/theory/scales.ts
562
- import { Scale } from "tonal";
562
+ import { Note, Scale } from "tonal";
563
563
  var ALIASES2 = new Map([
564
564
  ["natural minor", "minor"],
565
565
  ["blues", "minor blues"],
@@ -603,6 +603,19 @@ function modesOf(text) {
603
603
  return null;
604
604
  return modes.map(([tonic, mode]) => `${tonic} ${mode}`);
605
605
  }
606
+ var DOUBLE_ACCIDENTAL = /^[A-G](##|bb)/;
607
+ var SUPERLOCRIAN = ["1P", "2m", "3m", "4d", "5d", "6m", "7m"];
608
+ function scaleSpelling(scale) {
609
+ const hasDouble = (notes) => notes.some((n) => DOUBLE_ACCIDENTAL.test(n));
610
+ if (!hasDouble(scale.notes))
611
+ return { notes: scale.notes, intervals: scale.intervals };
612
+ if (scale.type === "altered" && scale.tonic) {
613
+ const notes = SUPERLOCRIAN.map((interval) => Note.transpose(scale.tonic, interval));
614
+ if (!hasDouble(notes))
615
+ return { notes, intervals: SUPERLOCRIAN };
616
+ }
617
+ return { notes: scale.notes, intervals: scale.intervals };
618
+ }
606
619
  var scaleTypes = () => Scale.names();
607
620
 
608
621
  // src/theory/note-set.ts
@@ -696,7 +709,8 @@ function scaleNoteSet(name) {
696
709
  const scale = Scale2.get(scaleName(name));
697
710
  if (scale.empty || !scale.tonic)
698
711
  return null;
699
- const set = fromNames(name.trim(), scale.notes, scale.intervals);
712
+ const { notes, intervals } = scaleSpelling(scale);
713
+ const set = fromNames(name.trim(), notes, intervals);
700
714
  if (set)
701
715
  set.scaleType = scale.type;
702
716
  return set;
@@ -860,6 +874,7 @@ var WHITE_H = 104;
860
874
  var BLACK_W = 14;
861
875
  var BLACK_H = 64;
862
876
  var TITLE3 = 22;
877
+ var FINGER_ROW = 16;
863
878
  var isBlack = (midi) => BLACK.has((midi % 12 + 12) % 12);
864
879
  function parseKeyRange(text) {
865
880
  const parts = text.split(/\s*-\s*(?=[A-Ga-g])/);
@@ -888,10 +903,13 @@ function renderKeyboard(doc, set, options = {}) {
888
903
  const vertical = options.orientation === "vertical";
889
904
  const top = options.title ? TITLE3 : 0;
890
905
  const length = whites.length * WHITE_W;
891
- const content = (vertical ? WHITE_H : length) + 2;
906
+ const label = options.label ?? "notes";
907
+ const fingerRow = label !== "fingers" && [...options.fingers?.keys() ?? []].some((midi) => midi >= from && midi <= to) ? FINGER_ROW : 0;
908
+ const depth = WHITE_H + fingerRow;
909
+ const content = (vertical ? depth : length) + 2;
892
910
  const width = Math.max(content, titleWidth(options.title));
893
911
  const shift = (width - content) / 2;
894
- const height = top + (vertical ? length : WHITE_H) + 2;
912
+ const height = top + (vertical ? length : depth) + 2;
895
913
  const at = (along, depth) => vertical ? [shift + 1 + depth, top + 1 + length - along] : [shift + 1 + along, top + 1 + depth];
896
914
  const key = (along, size, depth) => {
897
915
  const [x1, y1] = at(along, 0);
@@ -903,7 +921,6 @@ function renderKeyboard(doc, set, options = {}) {
903
921
  height: Math.abs(y2 - y1)
904
922
  };
905
923
  };
906
- const label = options.label ?? "notes";
907
924
  const root = svgRoot(doc, width, height, "mn-keyboard", `${options.title ?? `Keyboard: ${set.title}`}${describeMarks(set, options.marks)}`);
908
925
  if (options.title) {
909
926
  root.appendChild(svgText(doc, options.title, {
@@ -953,6 +970,15 @@ function renderKeyboard(doc, set, options = {}) {
953
970
  }));
954
971
  caption(black, at(along + BLACK_W / 2, BLACK_H - 13), true);
955
972
  });
973
+ if (fingerRow) {
974
+ for (const [midi, finger] of options.fingers) {
975
+ if (midi < from || midi > to)
976
+ continue;
977
+ const whitesBelow = whites.filter((w) => w < midi).length;
978
+ const along = isBlack(midi) ? whitesBelow * WHITE_W : whitesBelow * WHITE_W + WHITE_W / 2;
979
+ root.appendChild(svgLabel(doc, finger, at(along, WHITE_H + FINGER_ROW / 2 - 1), "mn-key-finger"));
980
+ }
981
+ }
956
982
  const played = [];
957
983
  for (let midi = from;midi <= to; midi++)
958
984
  if (memberOf(set, midi))
@@ -1167,6 +1193,8 @@ var TONAL_SUFFIXES = new Map([
1167
1193
  ["M9", "maj9"],
1168
1194
  ["M7", "maj7"],
1169
1195
  ["M69", "6/9"],
1196
+ ["7add6", "13"],
1197
+ ["m7add6", "m13"],
1170
1198
  ["m69", "m6/9"],
1171
1199
  ["Msus2", "sus2"],
1172
1200
  ["Msus4", "sus4"],
@@ -1184,6 +1212,8 @@ function normalize(symbol) {
1184
1212
  bass = rest.slice(slash + 1);
1185
1213
  rest = rest.slice(0, slash);
1186
1214
  }
1215
+ if (!TONAL_SUFFIXES.has(rest) && rest.endsWith("no5"))
1216
+ rest = rest.slice(0, -3);
1187
1217
  const suffix = canonicalSuffix(TONAL_SUFFIXES.get(rest) ?? rest);
1188
1218
  if (!COMMON.includes(suffix) && !EXTENDED.has(suffix))
1189
1219
  return null;
@@ -1191,10 +1221,20 @@ function normalize(symbol) {
1191
1221
  return parseChord(out) ? out : null;
1192
1222
  }
1193
1223
  var USUAL_ROOTS = new Set(["C", "C#", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"]);
1224
+ function bassCost(root, quality, bass) {
1225
+ if (!bass)
1226
+ return 0;
1227
+ const chroma = chromaOf(bass);
1228
+ const tone = Chord2.get(`${root}${quality}`).notes.find((note) => chromaOf(note) === chroma);
1229
+ if (tone)
1230
+ return tone === bass ? 0 : 1;
1231
+ const flats = keyPrefersFlats(`${root}${quality.startsWith("m") && !quality.startsWith("maj") ? "m" : ""}`);
1232
+ return bass.includes("b") === flats || !/[#b]/.test(bass) ? 0 : 1;
1233
+ }
1194
1234
  var rank = (symbol) => {
1195
- const m = /^([A-G][#b]*)(.*?)(\/[A-G][#b]*)?$/.exec(symbol);
1235
+ const m = /^([A-G][#b]*)(.*?)(?:\/([A-G][#b]*))?$/.exec(symbol);
1196
1236
  const common = COMMON.indexOf(m[2]);
1197
- return ((common < 0 ? COMMON.length + 5 : common) + (m[3] ? 10 : 0)) * 2 + (USUAL_ROOTS.has(m[1]) ? 0 : 1);
1237
+ return ((common < 0 ? COMMON.length + 5 : common) + (m[3] ? 10 : 0)) * 4 + (USUAL_ROOTS.has(m[1]) ? 0 : 2) + bassCost(m[1], m[2], m[3]);
1198
1238
  };
1199
1239
  function soundOf(symbol) {
1200
1240
  const m = /^([A-G][#b]*)(.*?)(?:\/([A-G][#b]*))?$/.exec(symbol);
@@ -1214,7 +1254,7 @@ function detectChordFromPitches(pitches) {
1214
1254
  return null;
1215
1255
  const readings = new Set;
1216
1256
  for (const flats of [false, true]) {
1217
- for (const symbol of Chord2.detect(namesOf(pitches, flats))) {
1257
+ for (const symbol of Chord2.detect(namesOf(pitches, flats), { assumePerfectFifth: true })) {
1218
1258
  const normalized = normalize(symbol);
1219
1259
  if (normalized)
1220
1260
  readings.add(normalized);
@@ -1718,21 +1758,28 @@ var renderKeyboardBlock = (doc, block, options) => {
1718
1758
  const range = field(block, "range");
1719
1759
  handOf(block, options);
1720
1760
  const fingering = field(block, "fingering");
1761
+ const keys = range ? rangeOf(range) : undefined;
1721
1762
  let fingers;
1722
1763
  if (fingering && set.root !== null && !set.pitches) {
1723
- const start = 60 + set.root;
1724
- set.pitches = [
1764
+ let start = 60 + set.root;
1765
+ if (keys) {
1766
+ const first = keys[0] + ((set.root - keys[0]) % 12 + 12) % 12;
1767
+ start = first <= keys[1] ? first : keys[0] - ((keys[0] - set.root) % 12 + 12) % 12;
1768
+ }
1769
+ const octave = [
1725
1770
  ...set.members.map((m) => start + (m.chroma - set.root + 12) % 12),
1726
1771
  start + 12
1727
- ];
1728
- fingers = fingersFor(set.pitches, fingering);
1772
+ ].sort((a, b) => a - b);
1773
+ fingers = fingersFor(octave, fingering);
1774
+ if (!keys)
1775
+ set.pitches = octave;
1729
1776
  }
1730
1777
  return renderKeyboard(doc, set, {
1731
1778
  playAs: !set.scaleType && set.root !== null ? "chord" : "scale",
1732
1779
  title: field(block, "title"),
1733
1780
  label: labelField(block, fingers ? "fingers" : "notes", fingers !== undefined),
1734
1781
  fingers,
1735
- range: keyRange(range ? rangeOf(range) : fingers && set.pitches ? [set.pitches[0], set.pitches[set.pitches.length - 1]] : keyRangeOf(set)),
1782
+ range: keyRange(keys ? keys : fingers && set.pitches ? [set.pitches[0], set.pitches[set.pitches.length - 1]] : keyRangeOf(set)),
1736
1783
  orientation: orientationOf(block, options.orientation?.keyboard),
1737
1784
  marks: marksOf(block, set)
1738
1785
  });
@@ -1972,7 +2019,7 @@ function lightFor(win) {
1972
2019
  return player;
1973
2020
  }
1974
2021
  function playerFor(win, options) {
1975
- const given = typeof options.player === "function" ? options.player() : options.player;
2022
+ const given = typeof options.player === "function" ? options.player(win) : options.player;
1976
2023
  return given ?? lightFor(win);
1977
2024
  }
1978
2025
  function dataOf(diagram) {
@@ -3625,6 +3672,13 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3625
3672
  cursor: pointer;
3626
3673
  }
3627
3674
 
3675
+ /* The opened list is a native popup on the page's background, not the bar's: its options take
3676
+ the page's colors, not the bar's inherited text color (light text on white in dark themes). */
3677
+ .mn-play select option {
3678
+ color: var(--text-normal, #222);
3679
+ background: var(--background-primary, #fff);
3680
+ }
3681
+
3628
3682
  .mn-play button:hover,
3629
3683
  .mn-play select:hover,
3630
3684
  .mn-play button:focus-visible,
@@ -3666,16 +3720,16 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3666
3720
  all: unset;
3667
3721
  appearance: none;
3668
3722
  width: 44px;
3669
- /* The input is the track: WebKit then centers the thumb on it. */
3670
- height: 3px;
3723
+ /* As tall as the thumb, so no host or browser has to center it: the visible 3px track is a
3724
+ centered line drawn by the background. */
3725
+ height: 9px;
3671
3726
  margin: 0;
3672
- border-radius: 2px;
3673
- background: var(--mn-faint);
3727
+ background: linear-gradient(var(--mn-faint), var(--mn-faint)) center / 100% 3px no-repeat;
3674
3728
  cursor: pointer;
3675
3729
  }
3676
3730
 
3677
3731
  .mn-play input[type='range']::-webkit-slider-runnable-track {
3678
- height: 3px;
3732
+ height: 9px;
3679
3733
  border: none;
3680
3734
  background: transparent;
3681
3735
  }
@@ -3684,23 +3738,31 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3684
3738
  appearance: none;
3685
3739
  width: 9px;
3686
3740
  height: 9px;
3687
- margin-top: 0;
3741
+ /* Hosts may move thumbs (Obsidian: relative, top -6px): the thumb fills the track instead. */
3742
+ position: static;
3743
+ top: 0;
3744
+ margin: 0;
3688
3745
  border: none;
3689
3746
  border-radius: 50%;
3690
3747
  background: var(--mn-accent);
3691
3748
  box-shadow: none;
3692
3749
  }
3693
3750
 
3751
+ /* Hosts ring a focused thumb (Obsidian): only for keyboard focus here, not after a click. */
3752
+ .mn-play input[type='range']:focus:not(:focus-visible)::-webkit-slider-thumb {
3753
+ box-shadow: none;
3754
+ }
3755
+
3694
3756
  .mn-play input[type='range']::-moz-range-track {
3695
- height: 3px;
3757
+ height: 9px;
3696
3758
  border: none;
3697
- border-radius: 2px;
3698
- background: var(--mn-faint);
3759
+ background: transparent;
3699
3760
  }
3700
3761
 
3701
3762
  .mn-play input[type='range']::-moz-range-thumb {
3702
3763
  width: 9px;
3703
3764
  height: 9px;
3765
+ margin: 0;
3704
3766
  border: none;
3705
3767
  border-radius: 50%;
3706
3768
  background: var(--mn-accent);
@@ -3869,6 +3931,12 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3869
3931
  font-weight: 600;
3870
3932
  }
3871
3933
 
3934
+ .mn-svg .mn-key-finger {
3935
+ font-size: 10px;
3936
+ fill: var(--mn-fg);
3937
+ font-weight: 600;
3938
+ }
3939
+
3872
3940
  /* Several diagrams in one block: a row that wraps, or a grid of at most \`columns\` columns that
3873
3941
  also wraps on narrow screens rather than shrinking diagrams to nothing. */
3874
3942
  .mn-block .mn-row {
@@ -4277,5 +4345,5 @@ export {
4277
4345
  voicingShape
4278
4346
  };
4279
4347
 
4280
- //# debugId=38B8A991D9E8CE0564756E2164756E21
4348
+ //# debugId=D55A05CAF6873D7B64756E2164756E21
4281
4349
  //# sourceMappingURL=index.js.map