@dsebastien/music-notation 0.5.1 → 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,8 +109,8 @@ 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.
@@ -121,7 +121,7 @@ Add this to your site's `publish.js`. It loads the bundle only on pages that hav
121
121
 
122
122
  ```js
123
123
  ;(() => {
124
- const VERSION = '0.5.0'
124
+ const VERSION = '0.5.1'
125
125
  const CDN = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist`
126
126
  const selector = (languages) => languages.map((l) => `pre > code.language-${l}`).join(', ')
127
127
  const BLOCKS = selector(['chord', 'chords', 'keyboard', 'fretboard'])
@@ -147,7 +147,7 @@ The bundle renders every block, now and as you navigate, and hides a block's sou
147
147
 
148
148
  ## Credits and licenses
149
149
 
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.
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.
151
151
 
152
152
  ## News & support
153
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
  });
@@ -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 (3px); the thumb is pulled up to its middle below. */
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,10 +3738,10 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3684
3738
  appearance: none;
3685
3739
  width: 9px;
3686
3740
  height: 9px;
3687
- /* Centered on the 3px track. Hosts may move thumbs (Obsidian: relative, top -6px). */
3741
+ /* Hosts may move thumbs (Obsidian: relative, top -6px): the thumb fills the track instead. */
3688
3742
  position: static;
3689
3743
  top: 0;
3690
- margin-top: -3px;
3744
+ margin: 0;
3691
3745
  border: none;
3692
3746
  border-radius: 50%;
3693
3747
  background: var(--mn-accent);
@@ -3700,15 +3754,15 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3700
3754
  }
3701
3755
 
3702
3756
  .mn-play input[type='range']::-moz-range-track {
3703
- height: 3px;
3757
+ height: 9px;
3704
3758
  border: none;
3705
- border-radius: 2px;
3706
- background: var(--mn-faint);
3759
+ background: transparent;
3707
3760
  }
3708
3761
 
3709
3762
  .mn-play input[type='range']::-moz-range-thumb {
3710
3763
  width: 9px;
3711
3764
  height: 9px;
3765
+ margin: 0;
3712
3766
  border: none;
3713
3767
  border-radius: 50%;
3714
3768
  background: var(--mn-accent);
@@ -3877,6 +3931,12 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
3877
3931
  font-weight: 600;
3878
3932
  }
3879
3933
 
3934
+ .mn-svg .mn-key-finger {
3935
+ font-size: 10px;
3936
+ fill: var(--mn-fg);
3937
+ font-weight: 600;
3938
+ }
3939
+
3880
3940
  /* Several diagrams in one block: a row that wraps, or a grid of at most \`columns\` columns that
3881
3941
  also wraps on narrow screens rather than shrinking diagrams to nothing. */
3882
3942
  .mn-block .mn-row {
@@ -4285,5 +4345,5 @@ export {
4285
4345
  voicingShape
4286
4346
  };
4287
4347
 
4288
- //# debugId=2791EC7C679EEE4064756E2164756E21
4348
+ //# debugId=D55A05CAF6873D7B64756E2164756E21
4289
4349
  //# sourceMappingURL=index.js.map