@dsebastien/music-notation 0.4.0 → 0.5.1

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
@@ -37,7 +37,7 @@ labels: intervals
37
37
  ```
38
38
  ````
39
39
 
40
- Chords in words or recognized from their frets, every way to play a chord, scale positions and modes:
40
+ Chords in words or recognized from their frets, every way to play a chord, triads, chord transitions, scale positions and modes:
41
41
 
42
42
  ````markdown
43
43
  ```chord
@@ -51,6 +51,16 @@ A Major
51
51
  voicings: 6
52
52
  ```
53
53
 
54
+ ```chord
55
+ G
56
+ triads: 1-3
57
+ ```
58
+
59
+ ```chord
60
+ from: C x32010
61
+ to: Am
62
+ ```
63
+
54
64
  ```fretboard
55
65
  A minor pentatonic
56
66
  positions: all
@@ -61,6 +71,12 @@ modes: C major
61
71
  ```
62
72
  ````
63
73
 
74
+ ![A Major's easiest voicings, and G triads on the three highest strings](docs/images/voicings.png)
75
+
76
+ ![G triads on the top strings, and the easiest ways from C to Am](docs/images/triads-transitions.png)
77
+
78
+ ![A tab with its player](docs/images/tab.png)
79
+
64
80
  Several diagrams side by side, separated by `---`, with shared settings first; vertical or horizontal, and left-handed:
65
81
 
66
82
  ````markdown
@@ -93,30 +109,37 @@ if (!result.ok) console.warn(result.error)
93
109
 
94
110
  - `renderBlock(language, source, host, options)` appends the rendering to `host` and never throws. It returns `{ ok, el, error, destroy() }`.
95
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.
96
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.
97
115
  - The renderers (`renderGuitarDiagram`, `renderKeyboard`, `renderFretboard`, `renderChart`), the parsers and the theory helpers (`parseChord`, `transposeChord`, `chordNoteSet`, `scaleNoteSet`…) are exported too.
98
116
  - Styles: `injectStyles(document)`, or ship `@dsebastien/music-notation/music-notation.css`. Colors follow Obsidian's theme variables, with fallbacks for any page.
99
117
 
100
118
  ## Obsidian Publish
101
119
 
102
- 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:
103
121
 
104
122
  ```js
105
123
  ;(() => {
106
- const VERSION = '0.1.0'
107
- const SELECTOR = ['chord', 'chords', 'keyboard', 'fretboard']
108
- .map((l) => `pre > code.language-${l}`)
109
- .join(', ')
110
- let requested = false
111
- const load = () => {
112
- if (requested || !document.querySelector(SELECTOR)) return
113
- requested = true
124
+ const VERSION = '0.5.0'
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)
114
133
  const script = document.createElement('script')
115
- script.src = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist/music-notation.publish.min.js`
134
+ script.src = `${CDN}/${file}`
116
135
  document.head.appendChild(script)
117
136
  }
118
- load()
119
- 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 })
120
143
  })()
121
144
  ```
122
145
 
@@ -124,7 +147,7 @@ The bundle renders every block, now and as you navigate, and hides a block's sou
124
147
 
125
148
  ## Credits and licenses
126
149
 
127
- 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.
128
151
 
129
152
  ## News & support
130
153
 
@@ -10,6 +10,11 @@ export interface BlockSource {
10
10
  export declare function parseBlockSource(source: string): BlockSource;
11
11
  /** "5-8", "5", "0 - 12" → [from, to]; null when invalid. */
12
12
  export declare function parseRange(text: string): [number, number] | null;
13
+ /**
14
+ * String numbers the way guitarists count them (1 = the highest): "1-3", "1 2 3", "2,4". Throws a
15
+ * message for the user when one is outside 1..count.
16
+ */
17
+ export declare function parseStrings(text: string, count: number): number[];
13
18
  /**
14
19
  * The parts of a block whose diagrams are separated by `---` lines, without the empty ones;
15
20
  * null when the block has no separator.
@@ -1,4 +1,5 @@
1
1
  import type { VoicingLookup } from '../voicings/index.js';
2
+ import type { PlayOptions } from '../playback/controls.js';
2
3
  import { type LayoutDefaults } from '../render/layout.js';
3
4
  import { type BlockSource } from './block-source.js';
4
5
  import type { Instrument, PropertyNames } from './properties.js';
@@ -17,6 +18,8 @@ export interface RenderOptions extends LayoutDefaults {
17
18
  propertyNames?: Partial<PropertyNames>;
18
19
  /** The note's instrument; from its tags when absent. */
19
20
  instrument?: Instrument;
21
+ /** A playback bar on chord, keyboard and fretboard diagrams (true for the defaults). */
22
+ play?: boolean | PlayOptions;
20
23
  }
21
24
  export type Renderer = (doc: Document, source: string, options: RenderOptions) => Element;
22
25
  /** Renders one diagram from a parsed block (the parts of a row share settings, see renderBlock). */
@@ -19,6 +19,10 @@ export interface GuitarDiagramOptions {
19
19
  orientation?: Orientation;
20
20
  /** Left-handed: mirrored (the low string on the right, or the nut on the right). */
21
21
  hand?: Hand;
22
+ /** Text in each string's dot instead of its finger (intervals, note names); null for none. */
23
+ labels?: (string | null)[];
24
+ /** Chord transitions: fingers that stay (anchor) or slide (guide), per string. */
25
+ marks?: ('anchor' | 'guide' | null)[];
22
26
  }
23
27
  /** "x02220", "x-10-12-12-11-10", "x 10 12 12 11 10" → frets; null when invalid. */
24
28
  export declare function parseFrets(text: string): (number | null)[] | null;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Chord transitions (#21): which voicing of the next chord the hand reaches most easily from the
3
+ * one it holds, and the voicings of a whole progression with the least movement. Fingers that stay
4
+ * on their fret (anchors) and slide along their string (guides) make changes clean.
5
+ */
6
+ import { type GeneratedVoicing } from './generate.js';
7
+ export interface TransitionStep {
8
+ voicing: GeneratedVoicing;
9
+ /** Strings (indexes, lowest first) whose finger stays on the same fret. */
10
+ anchors: number[];
11
+ /** Strings whose finger slides along the string to another fret. */
12
+ guides: number[];
13
+ /** Frets the hand moves along the neck (between the lowest fretted notes). */
14
+ shift: number;
15
+ /** How much the hand moves: fingers lifted and placed, slides, the shift. Lower is easier. */
16
+ movement: number;
17
+ /** What to do, in plain words. */
18
+ tips: string[];
19
+ }
20
+ export interface TransitionOptions {
21
+ /** Open-string pitches (MIDI), lowest string first; standard guitar by default. */
22
+ tuning?: number[];
23
+ /** The fingers of the voicing held (worked out when absent). */
24
+ fromFingers?: (string | null)[];
25
+ /** How many of the next chord's easiest voicings to consider (30 by default). */
26
+ candidates?: number;
27
+ }
28
+ /**
29
+ * The next chord's voicings, ranked by how easily the hand gets there from the one it holds:
30
+ * anchors and guides first, then a small move, then an easy shape. Empty when the chord is
31
+ * unknown.
32
+ */
33
+ export declare function transition(from: readonly (number | null)[], toChord: string, options?: TransitionOptions): TransitionStep[];
34
+ export interface ProgressionStep extends Partial<Omit<TransitionStep, 'voicing'>> {
35
+ chord: string;
36
+ voicing: GeneratedVoicing;
37
+ }
38
+ /**
39
+ * The voicing of each chord that makes the whole progression easiest: the shapes' ease and the
40
+ * moves between them, together (each chord's easiest 12 voicings are weighed). The first step has
41
+ * no move. Empty when a chord is unknown.
42
+ */
43
+ export declare function smoothProgression(chords: readonly string[], options?: {
44
+ tuning?: number[];
45
+ candidates?: number;
46
+ }): ProgressionStep[];
@@ -0,0 +1,43 @@
1
+ import { type GeneratedVoicing } from './generate.js';
2
+ export interface Triad extends GeneratedVoicing {
3
+ /** 0 root position, 1 first inversion (third in the bass), 2 second (fifth in the bass). */
4
+ inversion: number;
5
+ /** The chord tone on top, as triad charts say it: R, 3, b3, 5… */
6
+ top: string;
7
+ }
8
+ export interface TriadOptions {
9
+ /** Three adjacent strings (1 = the highest): [1, 2, 3], [2, 3, 4]… */
10
+ strings: number[];
11
+ /** One inversion (0, 1, 2) or all of them (the default). */
12
+ inversion?: number | 'any';
13
+ /** Open-string pitches (MIDI), lowest string first; standard guitar by default. */
14
+ tuning?: number[];
15
+ /** The highest fret used, 15 by default. */
16
+ maxFret?: number;
17
+ }
18
+ /** The inversion's name: "root position", "1st inversion", "2nd inversion". */
19
+ export declare const inversionName: (inversion: number) => string;
20
+ /**
21
+ * Close triads (the three notes within four frets) of a three-note chord (major, minor, diminished, augmented, sus) on a string set,
22
+ * up the neck (then root position first). Empty for other chords or a string set that isn't three
23
+ * adjacent strings.
24
+ */
25
+ export declare function triadVoicings(symbol: string, options: TriadOptions): Triad[];
26
+ export interface KeyTriad {
27
+ /** "I", "ii", "iii", "IV", "V", "vi", "vii°". */
28
+ numeral: string;
29
+ symbol: string;
30
+ triad: Triad;
31
+ }
32
+ /** "G major", "E minor" (natural minor): the tonic and the mode; null otherwise. */
33
+ export declare function parseKey(text: string): {
34
+ tonic: string;
35
+ minor: boolean;
36
+ } | null;
37
+ /**
38
+ * A key's seven triads on a string set, one inversion, moving up the neck: each one at or above
39
+ * the previous one when it can be. Null when the key can't be read.
40
+ */
41
+ export declare function keyTriads(key: string, options: Omit<TriadOptions, 'inversion'> & {
42
+ inversion?: number;
43
+ }): KeyTriad[] | null;
@@ -0,0 +1,2 @@
1
+ /** ChordPro for a chord sheet: chords placed into the lyrics at their column. */
2
+ export declare function chordSheetToChordPro(text: string): string;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * alphaTex for a plain-text tab; null when no tab lines are found (four strings or more, mostly
3
+ * dashes). The tuning is written when the string names aren't standard guitar.
4
+ */
5
+ export declare function textTabToAlphaTex(text: string): string | null;
@@ -15,5 +15,7 @@ export interface FretboardOptions {
15
15
  /** Only these notes (a scale position); the frets shown follow them. */
16
16
  cells?: Cell[];
17
17
  marks?: NoteMarks;
18
+ /** Only notes on these strings (indexes, lowest first): triads on a string set. */
19
+ strings?: number[];
18
20
  }
19
21
  export declare function renderFretboard(doc: Document, set: NoteSet, options?: FretboardOptions): SVGElement;
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ export type { RenderOptions, NotationLanguage } from './blocks/renderers.js';
4
4
  export { STYLES, injectStyles } from './styles.js';
5
5
  export { renderGuitarDiagram, parseFrets, parseFingers, baseFretOf, STANDARD_TUNING, type FrettedVoicing, type GuitarDiagramOptions } from './chord/guitar.js';
6
6
  export { generateVoicings, fingerVoicing, voicingShape, MAX_GENERATED_STRINGS, type GeneratedVoicing, type VoicingOptions, type VoicingShape } from './chord/generate.js';
7
+ export { sequenceOf, lengthOf, lightPlayer, pluckedString, pianoTone, DEFAULT_SEQUENCE, type NoteEvent, type NotePlayer, type PlayHandle, type PlayKind, type Sound, type SequenceOptions } from './playback/player.js';
8
+ export { attachPlayback, type PlayOptions } from './playback/controls.js';
9
+ export { transition, smoothProgression, type TransitionStep, type TransitionOptions, type ProgressionStep } from './chord/transitions.js';
10
+ export { triadVoicings, keyTriads, parseKey, inversionName, type Triad, type TriadOptions, type KeyTriad } from './chord/triads.js';
7
11
  export { renderKeyboard, parseKeyRange, keyRangeOf, type KeyboardOptions } from './keyboard/keyboard.js';
8
12
  export { renderFretboard, type FretboardOptions } from './fretboard/fretboard.js';
9
13
  export { parseTuning } from './fretboard/tuning.js';
@@ -19,3 +23,5 @@ export { detectChordFromFrets, detectChordFromPitches, type DetectedChord } from
19
23
  export { scaleName, modesOf, scaleTypes, characteristicIntervals, isScaleWithoutRoot } from './theory/scales.js';
20
24
  export { scalePosition, positionCount, CAGED_SHAPES, type Cell, type PositionSystem } from './fretboard/positions.js';
21
25
  export { blockFromProperties, instrumentOf, DEFAULT_PROPERTY_NAMES, type PropertyNames, type Instrument } from './blocks/properties.js';
26
+ export { textTabToAlphaTex } from './convert/text-tab.js';
27
+ export { chordSheetToChordPro } from './convert/chord-sheet.js';