@dsebastien/music-notation 0.1.3 → 0.2.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.
- package/README.md +14 -6
- package/dist/blocks/block-source.d.ts +5 -0
- package/dist/blocks/renderers.d.ts +11 -1
- package/dist/chord/guitar.d.ts +5 -0
- package/dist/fretboard/fretboard.d.ts +5 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +452 -158
- package/dist/index.js.map +11 -10
- package/dist/keyboard/keyboard.d.ts +3 -0
- package/dist/music-notation.css +80 -0
- package/dist/music-notation.publish.min.js +84 -3
- package/dist/publish/auto.d.ts +2 -0
- package/dist/publish/index.js +368 -158
- package/dist/publish/index.js.map +11 -10
- package/dist/render/dom.d.ts +4 -0
- package/dist/render/layout.d.ts +57 -0
- package/dist/render/render-block.d.ts +4 -2
- package/dist/voicings/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orientation and hand of diagrams, and the frame that maps a neck (guitar diagram, fretboard)
|
|
3
|
+
* to x/y. Renderers place things along the neck and across the strings, never in x/y directly,
|
|
4
|
+
* so text stays upright in every orientation (nothing is rotated or mirrored).
|
|
5
|
+
*/
|
|
6
|
+
export type Orientation = 'vertical' | 'horizontal';
|
|
7
|
+
export type Hand = 'left' | 'right';
|
|
8
|
+
/** Defaults used when a block doesn't say (Music Practice settings, a Publish site's options). */
|
|
9
|
+
export interface LayoutDefaults {
|
|
10
|
+
/** Per block language; piano chords (`chord` blocks for piano) follow `keyboard`. */
|
|
11
|
+
orientation?: Partial<Record<'chord' | 'keyboard' | 'fretboard', Orientation>>;
|
|
12
|
+
/** Left-handed guitar diagrams and fretboards. Keyboards ignore it. */
|
|
13
|
+
hand?: Hand;
|
|
14
|
+
}
|
|
15
|
+
export declare function parseOrientation(text: string): Orientation;
|
|
16
|
+
export declare function parseHand(text: string): Hand;
|
|
17
|
+
/** Keeps only valid layout defaults from untrusted input (a page's `window.musicNotationOptions`). */
|
|
18
|
+
export declare function layoutDefaults(value: unknown): LayoutDefaults;
|
|
19
|
+
/** Width a title needs (14px semi-bold), so a narrow diagram doesn't clip it. Errs on the wide side. */
|
|
20
|
+
export declare function titleWidth(title?: string): number;
|
|
21
|
+
export interface NeckLayout {
|
|
22
|
+
orientation: Orientation;
|
|
23
|
+
hand: Hand;
|
|
24
|
+
/** Distance from the low string to the high string. */
|
|
25
|
+
across: number;
|
|
26
|
+
/** Length of the neck shown, from its start (the nut side) to its end. */
|
|
27
|
+
along: number;
|
|
28
|
+
/** Margins: before the start of the neck, after its end, beyond the low and the high string. */
|
|
29
|
+
before: number;
|
|
30
|
+
after: number;
|
|
31
|
+
low: number;
|
|
32
|
+
high: number;
|
|
33
|
+
/** Space above everything, for the title. */
|
|
34
|
+
top: number;
|
|
35
|
+
/** The diagram is widened (and centered) to at least this width. */
|
|
36
|
+
minWidth?: number;
|
|
37
|
+
}
|
|
38
|
+
export interface Box {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
width: number;
|
|
42
|
+
height: number;
|
|
43
|
+
}
|
|
44
|
+
export interface NeckFrame {
|
|
45
|
+
width: number;
|
|
46
|
+
height: number;
|
|
47
|
+
/** x/y of the point `across` from the low string towards the high one, `along` from the neck's start. */
|
|
48
|
+
at(across: number, along: number): [number, number];
|
|
49
|
+
/** The rectangle between two opposite corners, given in neck coordinates. */
|
|
50
|
+
box(across1: number, along1: number, across2: number, along2: number): Box;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Vertical: the nut on top, the low string on the left (on the right when left-handed), as chord
|
|
54
|
+
* diagrams are drawn. Horizontal: the high string on top, as in tabs, the nut on the left (on the
|
|
55
|
+
* right when left-handed).
|
|
56
|
+
*/
|
|
57
|
+
export declare function neckFrame(n: NeckLayout): NeckFrame;
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
import { type NotationLanguage, type RenderOptions } from '../blocks/renderers.js';
|
|
3
3
|
export declare const LANGUAGES: NotationLanguage[];
|
|
4
4
|
export declare const isNotationLanguage: (language: string) => language is NotationLanguage;
|
|
5
|
+
/** More diagrams than this in one block is an error, so a page stays quick to draw. */
|
|
6
|
+
export declare const MAX_DIAGRAMS = 32;
|
|
5
7
|
export interface RenderResult {
|
|
6
8
|
ok: boolean;
|
|
7
9
|
/** The block's container, appended to the host. */
|
|
8
10
|
el: HTMLElement;
|
|
9
|
-
/** The message shown when ok is false. */
|
|
11
|
+
/** The message shown when ok is false (every failing diagram's, for a row). */
|
|
10
12
|
error: string | null;
|
|
11
13
|
/** Removes what was rendered. */
|
|
12
14
|
destroy(): void;
|
|
13
15
|
}
|
|
14
|
-
export declare function renderBlock(language: NotationLanguage, source: string, host: HTMLElement,
|
|
16
|
+
export declare function renderBlock(language: NotationLanguage, source: string, host: HTMLElement, given?: RenderOptions): RenderResult;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"/**\n * Pitch classes and note names. A pitch class is a chroma: 0 = C … 11 = B. Names accept ASCII\n * (`#`, `b`) and Unicode (`♯`, `♭`) accidentals.\n */\n\nexport const SHARP_NAMES = [\n 'C',\n 'C#',\n 'D',\n 'D#',\n 'E',\n 'F',\n 'F#',\n 'G',\n 'G#',\n 'A',\n 'A#',\n 'B'\n] as const\nexport const FLAT_NAMES = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'] as const\n\nconst LETTER_CHROMA: Record<string, number> = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 }\n\n/** \"C#\", \"Db\", \"D♭\", \"E\" → 0..11; null when it isn't a note name. */\nexport function chromaOf(name: string): number | null {\n const m = /^([A-Ga-g])([#♯b♭]*)$/.exec(name.trim())\n if (!m) return null\n let chroma = LETTER_CHROMA[m[1]!.toUpperCase()]!\n for (const accidental of m[2]!) chroma += accidental === '#' || accidental === '♯' ? 1 : -1\n return ((chroma % 12) + 12) % 12\n}\n\n/** A note with its octave, scientific pitch notation (\"C4\" = MIDI 60); null when invalid. */\nexport function midiOf(note: string): number | null {\n const m = /^([A-Ga-g][#♯b♭]*)(-?\\d)$/.exec(note.trim())\n if (!m) return null\n const chroma = chromaOf(m[1]!)\n if (chroma === null) return null\n // B#3 is C4 and Cb4 is B3: the letter decides the octave, the accidental can cross it.\n const letter = LETTER_CHROMA[m[1]![0]!.toUpperCase()]!\n const shift = Math.round((chroma - letter) / 12)\n return (Number(m[2]) + 1) * 12 + chroma - shift * 12\n}\n\n/** Name of a pitch class, with sharps or flats. Non-integers are rounded. */\nexport const nameOf = (chroma: number, preferFlats = false): string =>\n (preferFlats ? FLAT_NAMES : SHARP_NAMES)[(((Math.round(chroma) || 0) % 12) + 12) % 12]!\n\nexport interface ChordSymbolParts {\n root: string\n /** Everything between the root and the bass: \"m7\", \"6/9\", \"\". */\n suffix: string\n bass: string | null\n}\n\n/**\n * \"F♯m7/C#\" → root F#, suffix m7, bass C#. A slash starts a bass only when a note name follows\n * (\"C6/9\" has no bass). ♯ and ♭ become # and b.\n */\nexport function splitChordSymbol(symbol: string): ChordSymbolParts | null {\n const normalized = symbol.trim().replace(/♯/g, '#').replace(/♭/g, 'b')\n const m = /^([A-G][#b]?)(.*?)(?:\\/([A-G][#b]?))?$/.exec(normalized)\n return m ? { root: m[1]!, suffix: m[2]!, bass: m[3] ?? null } : null\n}\n\n/** \"C4\" for MIDI 60. */\nexport const noteOf = (midi: number, preferFlats = false): string =>\n `${nameOf(midi, preferFlats)}${Math.floor(midi / 12) - 1}`\n\n/** Whether a name is spelled with a flat (\"Bb\", \"E♭\"). */\nexport const isFlat = (name: string): boolean => /^[A-Ga-g][b♭]/.test(name.trim())\n",
|
|
6
6
|
"/**\n * Chord suffixes are written many ways (\"Δ7\", \"M7\", \"maj7\"). One spelling per chord, the one tonal\n * and the voicing data understand, so every spelling the symbol parser accepts also draws.\n */\nconst CANONICAL = new Map<string, string>([\n ['M', ''],\n ['maj', ''],\n ['min', 'm'],\n ['-', 'm'],\n ['M7', 'maj7'],\n ['Δ', 'maj7'],\n ['Δ7', 'maj7'],\n ['M9', 'maj9'],\n ['Δ9', 'maj9'],\n ['min6', 'm6'],\n ['-6', 'm6'],\n ['min7', 'm7'],\n ['-7', 'm7'],\n ['min9', 'm9'],\n ['-9', 'm9'],\n ['ø', 'm7b5'],\n ['ø7', 'm7b5'],\n ['m7-5', 'm7b5'],\n ['mmaj7', 'mMaj7'],\n ['mM7', 'mMaj7'],\n ['m(maj7)', 'mMaj7'],\n ['-(maj7)', 'mMaj7'],\n ['°', 'dim'],\n ['°7', 'dim7'],\n ['+', 'aug'],\n ['sus', 'sus4'],\n ['add2', 'add9'],\n ['69', '6/9']\n])\n\nexport const canonicalSuffix = (suffix: string): string => CANONICAL.get(suffix) ?? suffix\n",
|
|
7
|
-
"/**\n * Guitar voicings for chord symbols, from @tombatossals/chords-db (MIT,
|
|
7
|
+
"/**\n * Guitar voicings for chord symbols, from @tombatossals/chords-db (MIT, about 2,000 voicings, 1,800 of them by symbol). A\n * separate entry point: the data adds about 230 KB, so only consumers that want it pay for it.\n */\nimport db from '@tombatossals/chords-db/lib/guitar.json'\nimport type { FrettedVoicing } from '../chord/guitar.js'\nimport { chromaOf, splitChordSymbol } from '../theory/pitch.js'\nimport { canonicalSuffix } from '../theory/suffix.js'\n\n/** Finds the n-th voicing (0-based) of a chord symbol; null when unknown. */\nexport type VoicingLookup = (symbol: string, index: number) => FrettedVoicing | null\n\n/** The database's own key names: sharps for C# and F#, flats elsewhere. */\nconst DB_KEYS = ['C', 'Csharp', 'D', 'Eb', 'E', 'F', 'Fsharp', 'G', 'Ab', 'A', 'Bb', 'B']\n\n/** The database's bass names in slash-chord suffixes. */\nconst DB_BASSES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B']\n\n/** Canonical suffix → the database's name, where they differ. */\nconst DB_SUFFIXES: Record<string, string> = {\n '': 'major',\n 'm': 'minor',\n 'mMaj7': 'mmaj7',\n '6/9': '69'\n}\n\nexport const lookupGuitarVoicing: VoicingLookup = (symbol, index) => {\n const parts = splitChordSymbol(symbol)\n if (!parts) return null\n const chroma = chromaOf(parts.root)\n if (chroma === null) return null\n const canonical = canonicalSuffix(parts.suffix)\n const suffix = Object.prototype.hasOwnProperty.call(DB_SUFFIXES, canonical)\n ? DB_SUFFIXES[canonical]!\n : canonical\n // Slash chords are stored as \"/E\" (major) and \"m/B\" (minor) suffixes.\n const bassChroma = parts.bass ? chromaOf(parts.bass) : null\n const bass = bassChroma === null ? null : DB_BASSES[bassChroma]!\n const wanted = bass\n ? `${suffix === 'major' ? '' : suffix === 'minor' ? 'm' : suffix}/${bass}`\n : suffix\n const chords = db.chords[DB_KEYS[chroma]!] ?? []\n const chord = chords.find((c) => c.suffix === wanted)\n const position = chord?.positions[index]\n if (!position) return null\n const absolute = (f: number) => (f < 0 ? null : f === 0 ? 0 : f + position.baseFret - 1)\n return {\n frets: position.frets.map(absolute),\n fingers: position.fingers.map((f) => (f > 0 ? String(f) : null)),\n baseFret: position.baseFret\n }\n}\n\n/** How many voicings the database has for a symbol. */\nexport function voicingCount(symbol: string): number {\n let n = 0\n while (lookupGuitarVoicing(symbol, n)) n++\n return n\n}\n"
|
|
8
8
|
],
|
|
9
9
|
"mappings": ";;;;;;;;;;AAKO,IAAM,cAAc;AAAA,EACvB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ;AACO,IAAM,aAAa,CAAC,KAAK,MAAM,KAAK,MAAM,KAAK,KAAK,MAAM,KAAK,MAAM,KAAK,MAAM,GAAG;AAE1F,IAAM,gBAAwC,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG;AAGnF,SAAS,Q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": "@dsebastien/music-notation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Chord diagrams, piano keyboards, fretboards and ChordPro charts rendered as SVG and HTML, with no framework. Works in Obsidian, on Obsidian Publish and on any web page.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Sébastien Dubois",
|