accompy 0.0.0-stage → 0.3.24
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/LICENSE +21 -0
- package/README.md +84 -2
- package/dist/index.cjs +1041 -0
- package/dist/index.d.cts +331 -0
- package/dist/index.d.ts +331 -0
- package/dist/index.js +980 -0
- package/package.json +83 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,980 @@
|
|
|
1
|
+
// src/data.generated.ts
|
|
2
|
+
var DRUM_PATTERNS = { "swing": [{ "name": "swing_basic", "beats_per_bar": 4, "hits": [[0, 51, 90], [1, 51, 70], [2, 51, 85], [3, 51, 70], [1, 44, 80], [3, 44, 80]] }, { "name": "swing_brushes", "beats_per_bar": 4, "hits": [[0, 37, 60], [0.67, 42, 40], [1, 37, 50], [1.67, 42, 40], [2, 37, 60], [2.67, 42, 40], [3, 37, 50], [3.67, 42, 40]] }], "bossa": [{ "name": "bossa", "beats_per_bar": 4, "hits": [[0, 37, 70], [2, 37, 65], [0, 42, 60], [0.5, 42, 40], [1, 42, 50], [1.5, 42, 40], [2, 42, 60], [2.5, 42, 40], [3, 42, 50], [3.5, 42, 40], [0, 36, 80], [1.5, 36, 70], [3, 36, 75]] }], "rock": [{ "name": "rock_basic", "beats_per_bar": 4, "hits": [[0, 36, 110], [2, 36, 105], [1, 38, 100], [3, 38, 100], [0, 42, 80], [0.5, 42, 60], [1, 42, 80], [1.5, 42, 60], [2, 42, 80], [2.5, 42, 60], [3, 42, 80], [3.5, 42, 60]] }, { "name": "rock_heavy", "beats_per_bar": 4, "hits": [[0, 36, 120], [0.5, 36, 90], [1, 38, 110], [2, 36, 115], [2.75, 36, 85], [3, 38, 110], [3.5, 42, 70]] }], "funk": [{ "name": "funk", "beats_per_bar": 4, "hits": [[0, 36, 110], [0.75, 36, 85], [1.5, 36, 90], [2.5, 36, 100], [3.25, 36, 80], [0.5, 38, 40], [1, 38, 105], [2.25, 38, 35], [3, 38, 105], [3.75, 38, 45], [0, 42, 75], [0.25, 42, 50], [0.5, 42, 65], [0.75, 42, 50], [1, 46, 80], [1.25, 42, 50], [1.5, 42, 65], [1.75, 42, 50], [2, 42, 75], [2.25, 42, 50], [2.5, 42, 65], [2.75, 42, 50], [3, 46, 80], [3.25, 42, 50], [3.5, 42, 65], [3.75, 42, 50]] }], "ballad": [{ "name": "ballad", "beats_per_bar": 4, "hits": [[0, 36, 70], [2, 37, 60], [0, 42, 50], [1, 42, 40], [2, 42, 50], [3, 42, 40]] }], "latin": [{ "name": "latin", "beats_per_bar": 4, "hits": [[0, 75, 90], [0.75, 75, 85], [1.5, 75, 80], [2.5, 75, 90], [3.5, 75, 85], [0, 36, 90], [2.5, 36, 85], [0, 56, 70], [0.5, 56, 55], [1, 56, 65], [1.5, 56, 55], [2, 56, 70], [2.5, 56, 55], [3, 56, 65], [3.5, 56, 55]] }], "waltz": [{ "name": "waltz", "beats_per_bar": 3, "hits": [[0, 36, 90], [1, 42, 60], [2, 42, 60]] }], "blues": [{ "name": "blues_shuffle", "beats_per_bar": 4, "hits": [[0, 36, 95], [0, 42, 80], [0.67, 42, 60], [1, 42, 75], [1, 38, 90], [1.67, 42, 60], [2, 36, 90], [2, 42, 80], [2.67, 42, 60], [3, 42, 75], [3, 38, 90], [3.67, 42, 60]] }] };
|
|
3
|
+
var BASS_PATTERNS = { "swing": [{ "name": "walking", "notes": [[0, 0, 0.9, 100], [1, 4, 0.9, 90], [2, 7, 0.9, 95], [3, 11, 0.9, 85]] }], "bossa": [{ "name": "bossa", "notes": [[0, 0, 1.3, 90], [1.5, 7, 0.8, 75], [2.5, 0, 1.3, 85]] }], "rock": [{ "name": "rock", "notes": [[0, 0, 0.9, 100], [1, 0, 0.9, 90], [2, 0, 0.9, 100], [3, 0, 0.9, 90]] }, { "name": "rock_driving", "notes": [[0, 0, 0.45, 100], [0.5, 0, 0.45, 80], [1, 0, 0.45, 95], [1.5, 0, 0.45, 80], [2, 0, 0.45, 100], [2.5, 0, 0.45, 80], [3, 0, 0.45, 95], [3.5, 0, 0.45, 80]] }], "funk": [{ "name": "funk", "notes": [[0, 0, 0.4, 110], [0.75, 0, 0.2, 80], [1.25, 7, 0.3, 90], [1.75, 0, 0.2, 75], [2.5, 0, 0.4, 100], [3, 5, 0.3, 85], [3.5, 7, 0.3, 80]] }], "ballad": [{ "name": "ballad", "notes": [[0, 0, 2, 80], [2, 7, 2, 70]] }], "latin": [{ "name": "montuno", "notes": [[0, 0, 0.4, 95], [0.5, 7, 0.4, 80], [1.5, 0, 0.4, 90], [2, 7, 0.4, 85], [2.5, 0, 0.4, 90], [3.5, 5, 0.4, 80]] }], "waltz": [{ "name": "waltz", "notes": [[0, 0, 0.9, 95], [1, 4, 0.9, 75], [2, 7, 0.9, 75]] }], "blues": [{ "name": "blues_shuffle", "notes": [[0, 0, 0.6, 95], [0.67, 0, 0.3, 80], [1, 7, 0.6, 90], [1.67, 7, 0.3, 75], [2, 0, 0.6, 95], [2.67, 0, 0.3, 80], [3, 7, 0.6, 90], [3.67, 9, 0.3, 75]] }] };
|
|
4
|
+
var COMP_PATTERNS = { "swing": [{ "name": "swing_comp", "hits": [[1.5, 0.4, 75], [3.5, 0.4, 80]] }], "bossa": [{ "name": "bossa_comp", "hits": [[0, 0.3, 70], [1.5, 0.3, 65], [2.5, 0.3, 70], [3.5, 0.3, 60]] }], "rock": [], "funk": [], "ballad": [{ "name": "ballad_arp", "hits": [[0, 0.8, 60], [0.5, 0.8, 55], [1, 0.8, 50], [1.5, 0.8, 55], [2, 0.8, 60], [2.5, 0.8, 55], [3, 0.8, 50], [3.5, 0.8, 55]] }], "latin": [], "waltz": [{ "name": "waltz_comp", "hits": [[1, 0.9, 70], [2, 0.9, 70]] }], "blues": [] };
|
|
5
|
+
var DRUMS = { "KICK": 36, "SNARE": 38, "SIDE_STICK": 37, "CLOSED_HIHAT": 42, "OPEN_HIHAT": 46, "PEDAL_HIHAT": 44, "RIDE": 51, "RIDE_BELL": 53, "CRASH": 49, "LOW_TOM": 45, "MID_TOM": 47, "HIGH_TOM": 50, "COWBELL": 56, "CLAVES": 75, "SHAKER": 70 };
|
|
6
|
+
var RHYTHMIC_SKELETONS = { "whole_note": { "pattern": [4], "name": "Whole note", "beats_per_measure": 4, "styles": ["ballad", "pad", "ambient", "slow_swing", "worship", "drone"] }, "half_notes": { "pattern": [2, 2], "name": "Half notes", "beats_per_measure": 4, "styles": ["ballad", "hymn", "slow_rock", "ambient", "choral", "new_age"] }, "quarter_notes": { "pattern": [1, 1, 1, 1], "name": "Quarter notes", "beats_per_measure": 4, "styles": ["rock", "pop", "march", "country", "motown"] }, "four_on_the_floor": { "pattern": [1, 1, 1, 1], "name": "Four-on-the-floor", "beats_per_measure": 4, "styles": ["swing", "disco", "edm", "house", "country", "march", "polka", "techno", "pop", "motown"] }, "long_short_short": { "pattern": [2, 1, 1], "name": "Long-short-short", "beats_per_measure": 4, "styles": ["march", "polka", "country_rock", "folk", "bluegrass", "oom_pah"] }, "short_short_long": { "pattern": [1, 1, 2], "name": "Short-short-long", "beats_per_measure": 4, "styles": ["folk", "power_ballad", "pop_rock", "singer_songwriter", "anthem"] }, "short_long_short": { "pattern": [1, 2, 1], "name": "Short-long-short", "beats_per_measure": 4, "styles": ["pop", "soft_rock", "r_and_b", "soul", "gospel"] }, "dotted_half_quarter": { "pattern": [3, 1], "name": "Dotted half + quarter", "beats_per_measure": 4, "styles": ["ballad", "hymn", "slow_swing", "waltz_feel", "classical"] }, "quarter_dotted_half": { "pattern": [1, 3], "name": "Quarter + dotted half", "beats_per_measure": 4, "styles": ["ska", "reggae", "anticipation", "new_wave"] }, "charleston": { "pattern": [1.5, 1.5, 1], "name": "Charleston", "beats_per_measure": 4, "styles": ["swing", "jazz", "funk", "bebop", "big_band", "jump_blues", "dixieland", "jive"] }, "reverse_charleston": { "pattern": [1, 1.5, 1.5], "name": "Reverse Charleston", "beats_per_measure": 4, "styles": ["bebop", "modern_jazz", "hard_bop", "post_bop", "cool_jazz"] }, "displaced_charleston": { "pattern": [1.5, 1, 1.5], "name": "Displaced Charleston", "beats_per_measure": 4, "styles": ["latin_jazz", "afro_cuban", "fusion", "samba_jazz"] }, "straight_eighths": { "pattern": [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], "name": "Straight eighths", "beats_per_measure": 4, "styles": ["rock", "pop", "bossa_nova", "punk", "new_wave", "indie", "surf", "rockabilly"] }, "long_and_push": { "pattern": [2, 1, 0.5, 0.5], "name": "Long + push", "beats_per_measure": 4, "styles": ["funk", "r_and_b", "neo_soul", "gospel", "hip_hop"] }, "backbeat_pickup": { "pattern": [1, 1, 0.5, 0.5, 1], "name": "Backbeat with pickup", "beats_per_measure": 4, "styles": ["funk", "gospel", "soul", "motown", "blues_rock"] }, "offbeat_skank": { "pattern": [0.5, 1.5, 0.5, 1.5], "name": "Off-beat skank", "beats_per_measure": 4, "styles": ["reggae", "ska", "dub", "dancehall", "rocksteady", "two_tone"] }, "anticipated_two": { "pattern": [1, 0.5, 0.5, 1, 1], "name": "Anticipated 2", "beats_per_measure": 4, "styles": ["latin", "bossa_nova", "samba", "afro_cuban", "salsa"] }, "push_four": { "pattern": [1, 1, 1, 0.5, 0.5], "name": "Push on 4", "beats_per_measure": 4, "styles": ["jazz", "swing", "blues", "jump_blues", "soul_jazz"] }, "dotted_quarter_eighths": { "pattern": [1.5, 0.5, 1.5, 0.5], "name": "Dotted quarter + eighth", "beats_per_measure": 4, "styles": ["pop", "synth_pop", "disco", "euro_dance", "tropical"] }, "tresillo": { "pattern": [1.5, 1.5, 1], "name": "Tresillo", "beats_per_measure": 4, "styles": ["afro_cuban", "reggaeton", "new_orleans", "hip_hop", "dancehall", "samba", "rumba", "trap"] }, "habanera": { "pattern": [1.5, 1, 1.5], "name": "Habanera", "beats_per_measure": 4, "styles": ["habanera", "tango", "afro_cuban", "latin_pop", "reggaeton", "beguine"] }, "waltz_whole": { "pattern": [3], "name": "Dotted half", "beats_per_measure": 3, "styles": ["slow_waltz", "ballad", "ambient", "chorale"] }, "waltz_quarters": { "pattern": [1, 1, 1], "name": "Waltz quarters", "beats_per_measure": 3, "styles": ["waltz", "oom_pah", "viennese_waltz", "country_waltz", "mazurka", "landler"] }, "waltz_half_quarter": { "pattern": [2, 1], "name": "Half + quarter", "beats_per_measure": 3, "styles": ["waltz", "minuet", "folk_waltz", "country_waltz"] }, "waltz_quarter_half": { "pattern": [1, 2], "name": "Quarter + half", "beats_per_measure": 3, "styles": ["sarabande", "minuet", "slow_waltz", "classical"] }, "waltz_dotted_quarters": { "pattern": [1.5, 1.5], "name": "Dotted quarters (6/8 feel)", "beats_per_measure": 3, "styles": ["jig", "tarantella", "irish", "6_8_rock", "blues_shuffle"] }, "waltz_with_pickup": { "pattern": [1, 0.5, 0.5, 1], "name": "Waltz with pickup", "beats_per_measure": 3, "styles": ["viennese_waltz", "jazz_waltz", "show_tune"] }, "waltz_eighth_lead": { "pattern": [0.5, 0.5, 1, 1], "name": "Eighth-note lead-in", "beats_per_measure": 3, "styles": ["folk_waltz", "jazz_waltz", "musette"] } };
|
|
7
|
+
var CHORD_SYMBOL_REPLACEMENTS = [["maj7", "^7"], ["Maj7", "^7"], ["M7", "^7"], ["maj9", "^9"], ["Maj9", "^9"], ["M9", "^9"], ["maj", "^"], ["Maj", "^"], ["min7", "-7"], ["mi7", "-7"], ["mi9", "-9"], ["min", "-"], ["mi", "-"], ["m7", "-7"], ["m9", "-9"], ["m", "-"], ["dim7", "o7"], ["dim", "o"], ["\xB07", "o7"], ["\xB0", "o"], ["m7b5", "h7"], ["\xF87", "h7"], ["\xF8", "h"], ["sus2", "sus2"], ["add9", "add9"], ["add2", "add2"]];
|
|
8
|
+
var UNNORMALIZED_SYMBOLS = ["", "n", "N.C.", "NC", "%", "x"];
|
|
9
|
+
var SHARP_NOTES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
10
|
+
var FLAT_NOTES = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
|
|
11
|
+
var NOTE_TO_SEMITONE = [["C", 0], ["C#", 1], ["D", 2], ["D#", 3], ["E", 4], ["F", 5], ["F#", 6], ["G", 7], ["G#", 8], ["A", 9], ["A#", 10], ["B", 11], ["Db", 1], ["Eb", 3], ["Gb", 6], ["Ab", 8], ["Bb", 10], ["Fb", 4], ["E#", 5], ["Cb", 11], ["B#", 0]];
|
|
12
|
+
var FLAT_KEYS = ["Ab", "Bb", "Db", "Eb", "F", "Gb"];
|
|
13
|
+
var NO_CHORD_SYMBOLS = ["", "n", "N.C.", "NC"];
|
|
14
|
+
var FALLBACK_CHORD_NOTES = [48, 52, 55];
|
|
15
|
+
var DEFAULT_INSTRUMENTS = { "drums": true, "bass": true, "piano": true, "guitar": false };
|
|
16
|
+
var DEFAULT_VOLUMES = { "drums": 0.8, "bass": 0.9, "piano": 0.7 };
|
|
17
|
+
var DEFAULT_TRANSPOSE = -12;
|
|
18
|
+
var TONAL_ROOT_NOTES = [["C", 60], ["C#", 61], ["Db", 61], ["D", 62], ["D#", 63], ["Eb", 63], ["E", 64], ["F", 65], ["F#", 66], ["Gb", 66], ["G", 67], ["G#", 68], ["Ab", 68], ["A", 69], ["A#", 70], ["Bb", 70], ["B", 71], ["do", 60], ["do#", 61], ["reb", 61], ["re", 62], ["re#", 63], ["mib", 63], ["mi", 64], ["fa", 65], ["fa#", 66], ["solb", 66], ["sol", 67], ["sol#", 68], ["lab", 68], ["la", 69], ["la#", 70], ["sib", 70], ["si", 71]];
|
|
19
|
+
var TONAL_CHORD_QUALITY = [["", [0, 4, 7]], ["maj", [0, 4, 7]], ["M", [0, 4, 7]], ["m", [0, 3, 7]], ["min", [0, 3, 7]], ["dim", [0, 3, 6]], ["aug", [0, 4, 8]], ["7", [0, 4, 7, 10]], ["7M", [0, 4, 7, 11]], ["maj7", [0, 4, 7, 11]], ["min7", [0, 3, 7, 10]], ["minmaj7", [0, 3, 7, 11]], ["dim7", [0, 3, 6, 9]], ["hdim7", [0, 3, 6, 10]], ["aug7", [0, 4, 8, 10]], ["6", [0, 4, 7, 9]], ["min6", [0, 3, 7, 9]], ["9", [0, 4, 7, 10, 14]], ["maj9", [0, 4, 7, 11, 14]], ["min9", [0, 3, 7, 10, 14]], ["11", [0, 4, 7, 10, 14, 17]], ["maj11", [0, 4, 7, 11, 14, 17]], ["min11", [0, 3, 7, 10, 14, 17]], ["13", [0, 4, 7, 10, 14, 17, 21]], ["maj13", [0, 4, 7, 11, 14, 17, 21]], ["min13", [0, 3, 7, 10, 14, 17, 21]], ["sus", [0, 5, 7]], ["sus2", [0, 2, 7]], ["7sus4", [0, 5, 7, 10]], ["add9", [0, 4, 7, 14]], ["add2", [0, 2, 4, 7]], ["6add9", [0, 4, 7, 9, 14]], ["minadd9", [0, 3, 7, 14]], ["min6add9", [0, 3, 7, 9, 14]], ["minmaj9", [0, 3, 7, 11, 14]], ["5", [0, 7]], ["maj7b5", [0, 4, 6, 11]], ["maj7#5", [0, 4, 8, 11]], ["7b5", [0, 4, 6, 10]], ["min11b5", [0, 3, 6, 10, 14, 17]], ["7b9", [0, 4, 7, 10, 13]], ["7#9", [0, 4, 7, 10, 15]], ["7alt", [0, 4, 8, 10, 13, 15]], ["m6", [0, 3, 7, 9]], ["m7", [0, 3, 7, 10]], ["m9", [0, 3, 7, 10, 14]], ["m11", [0, 3, 7, 10, 14, 17]], ["m13", [0, 3, 7, 10, 14, 17, 21]], ["m7b5", [0, 3, 6, 10]], ["min7b5", [0, 3, 6, 10]], ["\xF87", [0, 3, 6, 10]], ["\xF8", [0, 3, 6, 10]], ["sus4", [0, 5, 7]], ["mmaj7", [0, 3, 7, 11]], ["mmaj9", [0, 3, 7, 11, 14]], ["madd9", [0, 3, 7, 14]], ["m6add9", [0, 3, 7, 9, 14]], ["m11b5", [0, 3, 6, 10, 14, 17]], ["7#5", [0, 4, 8, 10]], ["^", [0, 4, 7]], ["^7", [0, 4, 7, 11]], ["^9", [0, 4, 7, 11, 14]], ["-", [0, 3, 7]], ["-7", [0, 3, 7, 10]], ["-9", [0, 3, 7, 10, 14]], ["o", [0, 3, 6]], ["o7", [0, 3, 6, 9]], ["h", [0, 3, 6, 10]], ["h7", [0, 3, 6, 10]], ["+", [0, 4, 8]]];
|
|
20
|
+
var PY_WHITESPACE = [9, 10, 11, 12, 13, 28, 29, 30, 31, 32, 133, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288];
|
|
21
|
+
|
|
22
|
+
// src/py.ts
|
|
23
|
+
var WHITESPACE = new Set(PY_WHITESPACE);
|
|
24
|
+
var ValueError = class extends RangeError {
|
|
25
|
+
name = "ValueError";
|
|
26
|
+
};
|
|
27
|
+
var KeyError = class extends RangeError {
|
|
28
|
+
name = "KeyError";
|
|
29
|
+
};
|
|
30
|
+
var ZeroDivisionError = class extends RangeError {
|
|
31
|
+
name = "ZeroDivisionError";
|
|
32
|
+
};
|
|
33
|
+
function digitValue(d) {
|
|
34
|
+
const cp = d.codePointAt(0);
|
|
35
|
+
let start = cp;
|
|
36
|
+
while (/\p{Nd}/u.test(String.fromCodePoint(start - 1))) start--;
|
|
37
|
+
return (cp - start) % 10;
|
|
38
|
+
}
|
|
39
|
+
function isPySpace(ch) {
|
|
40
|
+
return WHITESPACE.has(ch.codePointAt(0) ?? -1);
|
|
41
|
+
}
|
|
42
|
+
function pyStrip(s) {
|
|
43
|
+
let start = 0;
|
|
44
|
+
let end = s.length;
|
|
45
|
+
while (start < end && isPySpace(s[start])) start++;
|
|
46
|
+
while (end > start && isPySpace(s[end - 1])) end--;
|
|
47
|
+
return s.slice(start, end);
|
|
48
|
+
}
|
|
49
|
+
function pySplit(s) {
|
|
50
|
+
const out = [];
|
|
51
|
+
let current = "";
|
|
52
|
+
for (const ch of s) {
|
|
53
|
+
if (isPySpace(ch)) {
|
|
54
|
+
if (current) out.push(current);
|
|
55
|
+
current = "";
|
|
56
|
+
} else current += ch;
|
|
57
|
+
}
|
|
58
|
+
if (current) out.push(current);
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
function pyInt(s) {
|
|
62
|
+
const t = pyStrip(s);
|
|
63
|
+
if (!/^[+-]?\p{Nd}+(_\p{Nd}+)*$/u.test(t)) throw new ValueError(`invalid literal for int(): '${s}'`);
|
|
64
|
+
let value = 0;
|
|
65
|
+
for (const ch of t.replace(/^[+-]/, "").replace(/_/g, "")) value = value * 10 + digitValue(ch);
|
|
66
|
+
return t.startsWith("-") ? -value : value;
|
|
67
|
+
}
|
|
68
|
+
function pyFloorDiv(a, b) {
|
|
69
|
+
if (b === 0) throw new ZeroDivisionError("division by zero");
|
|
70
|
+
if (Number.isInteger(a) && Number.isInteger(b)) return Math.floor(a / b);
|
|
71
|
+
let mod = a % b;
|
|
72
|
+
let div = (a - mod) / b;
|
|
73
|
+
if (mod && b < 0 !== mod < 0) {
|
|
74
|
+
mod += b;
|
|
75
|
+
div -= 1;
|
|
76
|
+
}
|
|
77
|
+
if (!div) return 0;
|
|
78
|
+
let floordiv = Math.floor(div);
|
|
79
|
+
if (div - floordiv > 0.5) floordiv += 1;
|
|
80
|
+
return floordiv;
|
|
81
|
+
}
|
|
82
|
+
function pyMod(a, b) {
|
|
83
|
+
return (a % b + b) % b;
|
|
84
|
+
}
|
|
85
|
+
function pyTrunc(x) {
|
|
86
|
+
return Math.trunc(x);
|
|
87
|
+
}
|
|
88
|
+
function pySum(values) {
|
|
89
|
+
let f = 0;
|
|
90
|
+
let c = 0;
|
|
91
|
+
for (const x of values) {
|
|
92
|
+
const t = f + x;
|
|
93
|
+
if (Math.abs(f) >= Math.abs(x)) c += f - t + x;
|
|
94
|
+
else c += x - t + f;
|
|
95
|
+
f = t;
|
|
96
|
+
}
|
|
97
|
+
return c && Number.isFinite(c) ? f + c : f;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/ireal.ts
|
|
101
|
+
var SCRAMBLE_PREFIX = "1r34LbKcu7";
|
|
102
|
+
var TIME_SIGNATURE_EXCEPTIONS = { "1,2": [12, 8] };
|
|
103
|
+
var [PLAIN_SONG_FIELDS, PLAIN_MUSIC_INDEX] = [6, 5];
|
|
104
|
+
var [KEY, FIELDS_BEFORE_MUSIC] = [-2, 6];
|
|
105
|
+
var WS = PY_WHITESPACE.map((cp) => `\\u{${cp.toString(16)}}`).join("");
|
|
106
|
+
var py = (src, flags = "") => new RegExp(
|
|
107
|
+
src.replace(/\\([sdw])/g, (_, c) => ({ s: `[${WS}]`, d: "\\p{Nd}", w: "[\\p{L}\\p{N}_]" })[c]),
|
|
108
|
+
"u" + flags
|
|
109
|
+
);
|
|
110
|
+
var sub = (s, src, rep) => s.replace(py(src, "g"), () => rep);
|
|
111
|
+
var ROOTS = "ABCDEFGn";
|
|
112
|
+
function utf8Decode(b) {
|
|
113
|
+
let out = "";
|
|
114
|
+
for (let i = 0; i < b.length; ) {
|
|
115
|
+
const c = b[i];
|
|
116
|
+
if (c < 128) {
|
|
117
|
+
out += String.fromCharCode(c);
|
|
118
|
+
i++;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
let need = 0, lo = 128, hi = 191, cp = 0;
|
|
122
|
+
if (c >= 194 && c <= 223) [need, cp] = [1, c & 31];
|
|
123
|
+
else if (c >= 224 && c <= 239) {
|
|
124
|
+
[need, cp] = [2, c & 15];
|
|
125
|
+
if (c === 224) lo = 160;
|
|
126
|
+
if (c === 237) hi = 159;
|
|
127
|
+
} else if (c >= 240 && c <= 244) {
|
|
128
|
+
[need, cp] = [3, c & 7];
|
|
129
|
+
if (c === 240) lo = 144;
|
|
130
|
+
if (c === 244) hi = 143;
|
|
131
|
+
}
|
|
132
|
+
let j = i + 1;
|
|
133
|
+
for (; need && j <= i + need; j++) {
|
|
134
|
+
const d = b[j];
|
|
135
|
+
if (d === void 0 || d < lo || d > hi) break;
|
|
136
|
+
cp = cp << 6 | d & 63;
|
|
137
|
+
[lo, hi] = [128, 191];
|
|
138
|
+
}
|
|
139
|
+
out += need && j === i + need + 1 ? String.fromCodePoint(cp) : "\uFFFD";
|
|
140
|
+
i = j;
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
function unquote(s) {
|
|
145
|
+
if (!s.includes("%")) return s;
|
|
146
|
+
return s.replace(/[\x00-\x7f]+/g, (run) => {
|
|
147
|
+
const [head, ...bits] = run.split("%");
|
|
148
|
+
const bytes = [...head].map((ch) => ch.charCodeAt(0));
|
|
149
|
+
for (const item of bits) {
|
|
150
|
+
const ok = /^[0-9A-Fa-f]{2}/.test(item);
|
|
151
|
+
if (ok) bytes.push(parseInt(item.slice(0, 2), 16));
|
|
152
|
+
else bytes.push(37);
|
|
153
|
+
for (const ch of ok ? item.slice(2) : item) bytes.push(ch.charCodeAt(0));
|
|
154
|
+
}
|
|
155
|
+
return utf8Decode(bytes);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function unscramble(scrambled) {
|
|
159
|
+
const swaps = [0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
|
|
160
|
+
const chars = Array.from(scrambled), n = chars.length;
|
|
161
|
+
let out = "", i = 0;
|
|
162
|
+
while (n - i > 50) {
|
|
163
|
+
const block = chars.slice(i, i + 50);
|
|
164
|
+
i += 50;
|
|
165
|
+
if (n - i >= 2) {
|
|
166
|
+
const r = [...block];
|
|
167
|
+
for (const k of swaps) [r[k], r[49 - k]] = [block[49 - k], block[k]];
|
|
168
|
+
out += r.join("");
|
|
169
|
+
} else out += block.join("");
|
|
170
|
+
}
|
|
171
|
+
return out + chars.slice(i).join("");
|
|
172
|
+
}
|
|
173
|
+
function cleanupChordString(s) {
|
|
174
|
+
const steps = [
|
|
175
|
+
["LZ|K|Z", "|"],
|
|
176
|
+
["cl", "x"],
|
|
177
|
+
["\\*\\s*\\*", ""],
|
|
178
|
+
["Y+", ""],
|
|
179
|
+
["XyQ|,", " "],
|
|
180
|
+
["\\|\\s*\\|", "|"],
|
|
181
|
+
["\\|\\s+", "|"],
|
|
182
|
+
["\\s+", " "]
|
|
183
|
+
];
|
|
184
|
+
for (const [src, rep] of steps) s = sub(s, src, rep);
|
|
185
|
+
let end = s.length;
|
|
186
|
+
while (end && isPySpace(s[end - 1])) end--;
|
|
187
|
+
return s.slice(0, end);
|
|
188
|
+
}
|
|
189
|
+
function dropSpans(text, start, end, stop = "") {
|
|
190
|
+
let out = "", i = 0;
|
|
191
|
+
for (let j, k; (j = text.indexOf(start, i)) >= 0 && (k = text.indexOf(end, j + 1)) >= 0; ) {
|
|
192
|
+
const inside = stop ? text.slice(j + 1, k).indexOf(stop) : -1;
|
|
193
|
+
if (inside >= 0) {
|
|
194
|
+
const cut = j + 1 + inside;
|
|
195
|
+
out += text.slice(i, cut + 1);
|
|
196
|
+
i = cut + 1;
|
|
197
|
+
} else {
|
|
198
|
+
out += text.slice(i, j);
|
|
199
|
+
i = k + 1;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return out + text.slice(i);
|
|
203
|
+
}
|
|
204
|
+
function removeAnnotations(s) {
|
|
205
|
+
s = dropSpans(dropSpans(s, "<", ">", "\n"), "(", ")").replaceAll('"', "");
|
|
206
|
+
const steps = [
|
|
207
|
+
["(?<!a)l(?!t)|f", ""],
|
|
208
|
+
["(?<!su)s(?!us)", ""],
|
|
209
|
+
["\\*\\w", ""],
|
|
210
|
+
["T\\d+", ""]
|
|
211
|
+
];
|
|
212
|
+
for (const [src, rep] of steps) s = sub(s, src, rep);
|
|
213
|
+
return s;
|
|
214
|
+
}
|
|
215
|
+
var removeMarkers = (s) => sub(s, "U|S|Q|N\\d", "");
|
|
216
|
+
function splitBars(s) {
|
|
217
|
+
const bars = [];
|
|
218
|
+
let opener = "", pos = 0;
|
|
219
|
+
const bar = (text, closes) => {
|
|
220
|
+
const ending = py("N(\\d)").exec(text);
|
|
221
|
+
return { text, opener, closes, ending: ending ? digitValue(ending[1]) : null };
|
|
222
|
+
};
|
|
223
|
+
for (const m of s.matchAll(/[|[\]{}]/g)) {
|
|
224
|
+
bars.push(bar(s.slice(pos, m.index), m[0] === "}"));
|
|
225
|
+
[opener, pos] = [m[0] === "{" || m[0] === "[" ? m[0] : "", m.index + 1];
|
|
226
|
+
}
|
|
227
|
+
bars.push(bar(s.slice(pos), false));
|
|
228
|
+
return bars;
|
|
229
|
+
}
|
|
230
|
+
function expandRepeats(bars) {
|
|
231
|
+
const n = bars.length;
|
|
232
|
+
const following = new Array(n + 1).fill(n);
|
|
233
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
234
|
+
const filled = bars[i].ending !== null || pyStrip(removeMarkers(bars[i].text)) !== "";
|
|
235
|
+
following[i] = filled ? i : following[i + 1];
|
|
236
|
+
}
|
|
237
|
+
const order = [];
|
|
238
|
+
const play = (items) => {
|
|
239
|
+
for (const item of items) order.push(item);
|
|
240
|
+
};
|
|
241
|
+
let region = 0, opened = null, body = [], ending = 0;
|
|
242
|
+
bars.forEach((bar, i) => {
|
|
243
|
+
if (bar.opener === "{") [region, opened, ending] = [i, i, 0];
|
|
244
|
+
else if (bar.opener === "[") region = i;
|
|
245
|
+
order.push(i);
|
|
246
|
+
if (!bar.closes) return;
|
|
247
|
+
const j = following[i + 1];
|
|
248
|
+
const nextEnding = j < n ? bars[j].ending : null;
|
|
249
|
+
if (ending && opened === null) {
|
|
250
|
+
if (nextEnding === ending + 1) {
|
|
251
|
+
play(body);
|
|
252
|
+
ending = nextEnding;
|
|
253
|
+
} else ending = 0;
|
|
254
|
+
} else {
|
|
255
|
+
const start = opened ?? region;
|
|
256
|
+
const endings = [];
|
|
257
|
+
for (let k = start; k <= i; k++) if (bars[k].ending !== null) endings.push(k);
|
|
258
|
+
body = [];
|
|
259
|
+
for (let k = start; k < (endings[0] ?? i + 1); k++) body.push(k);
|
|
260
|
+
if (endings.length) {
|
|
261
|
+
const text = bars[endings[0]].text, head = text.slice(0, py("N\\d").exec(text).index);
|
|
262
|
+
if (pyStrip(removeMarkers(head))) body.push({ head });
|
|
263
|
+
}
|
|
264
|
+
if (endings.length > 1) {
|
|
265
|
+
order.length -= i + 1 - endings[1];
|
|
266
|
+
endings.slice(1).forEach((a, e) => {
|
|
267
|
+
play(body);
|
|
268
|
+
for (let k = a; k < (endings[e + 2] ?? i + 1); k++) order.push(k);
|
|
269
|
+
});
|
|
270
|
+
} else play(body);
|
|
271
|
+
const last = endings.length ? bars[endings[endings.length - 1]].ending : null;
|
|
272
|
+
if (last !== null && nextEnding === last + 1) {
|
|
273
|
+
if (endings.length > 1) play(body);
|
|
274
|
+
ending = nextEnding;
|
|
275
|
+
} else ending = 0;
|
|
276
|
+
}
|
|
277
|
+
[region, opened] = [i + 1, null];
|
|
278
|
+
});
|
|
279
|
+
return order;
|
|
280
|
+
}
|
|
281
|
+
function fillCodas(bars, order) {
|
|
282
|
+
const texts = order.map((i) => typeof i === "number" ? bars[i].text : i);
|
|
283
|
+
const qs = bars.reduce((n, b) => n + [...b.text].filter((c) => c === "Q").length, 0);
|
|
284
|
+
if (qs > 2) throw new ValueError(`Could not parse codas: expected 0, 1 or 2 Qs, not ${qs}`);
|
|
285
|
+
const qBars = bars.flatMap((b, i) => b.text.includes("Q") ? [i] : []);
|
|
286
|
+
if (qs < 2 || qBars.length < 2) return texts;
|
|
287
|
+
const [p1, p2] = [order.indexOf(qBars[0]), order.indexOf(qBars[1])];
|
|
288
|
+
if (p1 >= p2) return texts;
|
|
289
|
+
const segnoBar = bars.findIndex((b) => b.text.includes("S"));
|
|
290
|
+
const segno = segnoBar >= 0 ? order.indexOf(segnoBar) : null;
|
|
291
|
+
const cut = (text, mark) => {
|
|
292
|
+
const k = text.indexOf(mark);
|
|
293
|
+
return k >= 0 ? [text.slice(0, k), text.slice(k + 1)] : [text, ""];
|
|
294
|
+
};
|
|
295
|
+
const toCoda = cut(texts[p1], "Q")[0];
|
|
296
|
+
let replay;
|
|
297
|
+
if (segno === null || segno > p1) replay = [...texts.slice(0, p1), toCoda];
|
|
298
|
+
else if (segno < p1) replay = [cut(texts[segno], "S")[1], ...texts.slice(segno + 1, p1), toCoda];
|
|
299
|
+
else replay = [cut(cut(texts[p1], "S")[1], "Q")[0]];
|
|
300
|
+
const [before, coda] = cut(texts[p2], "Q");
|
|
301
|
+
return [...texts.slice(0, p2), before, ...replay, coda, ...texts.slice(p2 + 1)];
|
|
302
|
+
}
|
|
303
|
+
function fillRepeatSigns(bars) {
|
|
304
|
+
const out = [];
|
|
305
|
+
for (const bar of bars) {
|
|
306
|
+
const sign = bar.length === 1 ? bar[0] : void 0;
|
|
307
|
+
if (sign === "x") out.push(...out.slice(-1));
|
|
308
|
+
else if (sign === "r") out.push(...out.length >= 2 ? out.slice(-2) : []);
|
|
309
|
+
else out.push(bar);
|
|
310
|
+
}
|
|
311
|
+
return out;
|
|
312
|
+
}
|
|
313
|
+
var barTokens = (bar) => pySplit(bar).flatMap((token) => token.split(/(?<!\/)(?=[A-GW])|(?=p)|(?<=p)/u).filter((piece) => piece));
|
|
314
|
+
function fillSlashes(bars) {
|
|
315
|
+
let last = "";
|
|
316
|
+
return bars.map((bar) => {
|
|
317
|
+
const chords = [];
|
|
318
|
+
for (let token of bar) {
|
|
319
|
+
if (token === "p") token = last;
|
|
320
|
+
else if (token.startsWith("W")) token = last ? token.length > 1 ? last.split("/")[0] + token.slice(1) : last : "";
|
|
321
|
+
if (token && ROOTS.includes(token[0])) last = token;
|
|
322
|
+
if (token) chords.push(token);
|
|
323
|
+
}
|
|
324
|
+
return chords;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
function flattenMeasures(chordString) {
|
|
328
|
+
const bars = splitBars(removeAnnotations(cleanupChordString(chordString)));
|
|
329
|
+
const played = [];
|
|
330
|
+
let carried = [];
|
|
331
|
+
for (const text of fillCodas(bars, expandRepeats(bars))) {
|
|
332
|
+
const tokens = barTokens(removeMarkers(typeof text === "string" ? text : text.head));
|
|
333
|
+
if (typeof text !== "string") carried = carried.concat(tokens);
|
|
334
|
+
else if (tokens.length) {
|
|
335
|
+
played.push(carried.concat(tokens));
|
|
336
|
+
carried = [];
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (carried.length) played.push(carried);
|
|
340
|
+
return fillSlashes(fillRepeatSigns(played));
|
|
341
|
+
}
|
|
342
|
+
function timeSignature(chordString) {
|
|
343
|
+
const m = py("T(\\d)(\\d)").exec(chordString);
|
|
344
|
+
if (!m) return [4, 4];
|
|
345
|
+
const ts = [digitValue(m[1]), digitValue(m[2])];
|
|
346
|
+
return TIME_SIGNATURE_EXCEPTIONS[ts.join(",")] ?? ts;
|
|
347
|
+
}
|
|
348
|
+
function* scrambledSongs(fields) {
|
|
349
|
+
const at = (i) => i >= 0 && i < fields.length ? fields[i] : "";
|
|
350
|
+
let previous = -1;
|
|
351
|
+
for (const [m, music] of fields.entries()) {
|
|
352
|
+
if (!music.startsWith(SCRAMBLE_PREFIX)) continue;
|
|
353
|
+
const standard = m - FIELDS_BEFORE_MUSIC;
|
|
354
|
+
const start = standard > previous + 1 && at(standard - 1) === "" && at(standard - 2) === "" ? standard : previous + 1;
|
|
355
|
+
previous = m;
|
|
356
|
+
const before = fields.slice(start, m);
|
|
357
|
+
let meta;
|
|
358
|
+
if (before.length >= FIELDS_BEFORE_MUSIC) meta = { title: at(start), composer: at(start + 1), key: at(m + KEY) };
|
|
359
|
+
else {
|
|
360
|
+
const [title = "", composer = "", , key = ""] = before.filter((f) => f);
|
|
361
|
+
meta = { title, composer, key };
|
|
362
|
+
}
|
|
363
|
+
yield [unscramble(music.slice(SCRAMBLE_PREFIX.length)), meta];
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function* plainSongs(fields) {
|
|
367
|
+
for (let s = 0; s + PLAIN_SONG_FIELDS <= fields.length; s += PLAIN_SONG_FIELDS)
|
|
368
|
+
yield [fields[s + PLAIN_MUSIC_INDEX], { title: fields[s], composer: fields[s + 1], key: fields[s + 3] }];
|
|
369
|
+
}
|
|
370
|
+
function decodeIrealUrl(url) {
|
|
371
|
+
let body = unquote(url.split('"', 1)[0]);
|
|
372
|
+
for (const scheme of ["irealbook://", "irealb://"])
|
|
373
|
+
if (body.startsWith(scheme)) {
|
|
374
|
+
body = body.slice(scheme.length);
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
const fields = body.split("=");
|
|
378
|
+
const songs = fields.some((f) => f.startsWith(SCRAMBLE_PREFIX)) ? scrambledSongs(fields) : plainSongs(fields);
|
|
379
|
+
const tunes = [];
|
|
380
|
+
for (const [music, meta] of songs) {
|
|
381
|
+
const chordString = cleanupChordString(music);
|
|
382
|
+
try {
|
|
383
|
+
tunes.push({ ...meta, timeSignature: timeSignature(chordString), measures: flattenMeasures(chordString) });
|
|
384
|
+
} catch (error) {
|
|
385
|
+
if (!(error instanceof ValueError)) throw error;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return tunes;
|
|
389
|
+
}
|
|
390
|
+
function parseIrealUrl(url) {
|
|
391
|
+
for (const tune of decodeIrealUrl(url)) {
|
|
392
|
+
const measures = tune.measures.filter((bar) => bar.length);
|
|
393
|
+
if (measures.length)
|
|
394
|
+
return {
|
|
395
|
+
measures,
|
|
396
|
+
title: tune.title || "Untitled",
|
|
397
|
+
composer: tune.composer,
|
|
398
|
+
key: tune.key || "C",
|
|
399
|
+
timeSignature: tune.timeSignature
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
throw new ValueError("No chords found in iReal Pro URL");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/chart.ts
|
|
406
|
+
function makeScore(measures, opts = {}, composer = "") {
|
|
407
|
+
return {
|
|
408
|
+
measures,
|
|
409
|
+
title: opts.title ?? "Untitled",
|
|
410
|
+
composer,
|
|
411
|
+
key: opts.key ?? "C",
|
|
412
|
+
timeSignature: opts.timeSignature ?? [4, 4]
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
var REPLACEMENTS_LONGEST_FIRST = [...CHORD_SYMBOL_REPLACEMENTS].sort((a, b) => b[0].length - a[0].length);
|
|
416
|
+
function rewriteSuffix(part) {
|
|
417
|
+
for (const [old, replacement] of REPLACEMENTS_LONGEST_FIRST) {
|
|
418
|
+
if (part.endsWith(old)) return part.slice(0, part.length - old.length) + replacement;
|
|
419
|
+
}
|
|
420
|
+
return part;
|
|
421
|
+
}
|
|
422
|
+
function normalizeChordSymbol(symbol) {
|
|
423
|
+
const s = pyStrip(symbol);
|
|
424
|
+
if (!s || UNNORMALIZED_SYMBOLS.includes(s)) return s;
|
|
425
|
+
return s.split("/").map(rewriteSuffix).join("/");
|
|
426
|
+
}
|
|
427
|
+
function parseChordString(chordString) {
|
|
428
|
+
const s = pyStrip(chordString);
|
|
429
|
+
if (!s.includes("|")) return pySplit(s).map((c) => [normalizeChordSymbol(c)]);
|
|
430
|
+
const measures = [];
|
|
431
|
+
let last = null;
|
|
432
|
+
for (const part of s.split("|")) {
|
|
433
|
+
const bar = pyStrip(part);
|
|
434
|
+
if (!bar) continue;
|
|
435
|
+
const normalized = [];
|
|
436
|
+
for (const token of pySplit(bar)) {
|
|
437
|
+
const chord = normalizeChordSymbol(token === "%" || token === "x" ? last || "C" : token);
|
|
438
|
+
normalized.push(chord);
|
|
439
|
+
last = chord;
|
|
440
|
+
}
|
|
441
|
+
if (normalized.length) measures.push(normalized);
|
|
442
|
+
}
|
|
443
|
+
return measures;
|
|
444
|
+
}
|
|
445
|
+
function parseTimeSig(ts) {
|
|
446
|
+
if (!ts) return [4, 4];
|
|
447
|
+
if (Array.isArray(ts)) return ts;
|
|
448
|
+
try {
|
|
449
|
+
const parts = ts.split("/");
|
|
450
|
+
if (parts.length < 2) return [4, 4];
|
|
451
|
+
return [pyInt(parts[0]), pyInt(parts[1])];
|
|
452
|
+
} catch (error) {
|
|
453
|
+
if (error instanceof ValueError) return [4, 4];
|
|
454
|
+
throw error;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function scoreFromChordSpecs(chords, opts = {}) {
|
|
458
|
+
const beatsPerBar = (opts.timeSignature ?? [4, 4])[0];
|
|
459
|
+
const measures = [];
|
|
460
|
+
let bar = [];
|
|
461
|
+
let barBeats = 0;
|
|
462
|
+
for (const [raw, beats] of chords) {
|
|
463
|
+
const chord = normalizeChordSymbol(raw);
|
|
464
|
+
if (chord === "" || chord === "n" || chord === "N.C." || chord === "NC") continue;
|
|
465
|
+
if (beats <= 0) continue;
|
|
466
|
+
const fullBars = pyFloorDiv(beats, beatsPerBar);
|
|
467
|
+
const rem = beats - fullBars * beatsPerBar;
|
|
468
|
+
for (let i = 0; i < fullBars; i++) measures.push([chord]);
|
|
469
|
+
if (rem) {
|
|
470
|
+
bar.push(chord);
|
|
471
|
+
barBeats += rem;
|
|
472
|
+
if (barBeats >= beatsPerBar - 1e-9) {
|
|
473
|
+
measures.push(bar);
|
|
474
|
+
bar = [];
|
|
475
|
+
barBeats = 0;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (bar.length) measures.push(bar);
|
|
480
|
+
return makeScore(measures, opts);
|
|
481
|
+
}
|
|
482
|
+
function scoreToChordSequence(score) {
|
|
483
|
+
const beatsPerBar = score.timeSignature[0];
|
|
484
|
+
const out = [];
|
|
485
|
+
for (const measure of score.measures) {
|
|
486
|
+
if (!measure.length) continue;
|
|
487
|
+
for (const symbol of measure) out.push([symbol, beatsPerBar / measure.length]);
|
|
488
|
+
}
|
|
489
|
+
return out;
|
|
490
|
+
}
|
|
491
|
+
function isScore(value) {
|
|
492
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && "measures" in value;
|
|
493
|
+
}
|
|
494
|
+
function ensureScore(chords, opts = {}) {
|
|
495
|
+
if (isScore(chords)) return chords;
|
|
496
|
+
if (typeof chords === "string") {
|
|
497
|
+
const s = pyStrip(chords);
|
|
498
|
+
if (s.startsWith("irealbook://") || s.startsWith("irealb://")) return parseIrealUrl(s);
|
|
499
|
+
return makeScore(parseChordString(s), opts);
|
|
500
|
+
}
|
|
501
|
+
if (Array.isArray(chords)) {
|
|
502
|
+
const items = chords;
|
|
503
|
+
const isMeasure = (x) => Array.isArray(x) && x.every((c) => typeof c === "string");
|
|
504
|
+
if (items.length && items.every(isMeasure)) {
|
|
505
|
+
return makeScore(items.map((m) => m.map(normalizeChordSymbol)), opts);
|
|
506
|
+
}
|
|
507
|
+
if (!items.length) return makeScore([], opts);
|
|
508
|
+
if (items.every((x) => typeof x === "string")) {
|
|
509
|
+
return makeScore(items.map((x) => [normalizeChordSymbol(x)]), opts);
|
|
510
|
+
}
|
|
511
|
+
const isSpec = (x) => Array.isArray(x) && x.length === 2 && typeof x[0] === "string" && ["number", "boolean"].includes(typeof x[1]);
|
|
512
|
+
if (items.every(isSpec)) {
|
|
513
|
+
return scoreFromChordSpecs(items.map(([c, b]) => [c, Number(b)]), opts);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
throw new TypeError(
|
|
517
|
+
"Unsupported chords input. Expected Score, chord string, iReal URL, list of (chord, beats) tuples, iterable of chord symbols, or measures list."
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
var NOTE_INDEX = new Map(NOTE_TO_SEMITONE);
|
|
521
|
+
function transposeNote(name, semitones, opts = {}) {
|
|
522
|
+
const index = NOTE_INDEX.get(name);
|
|
523
|
+
if (index === void 0) return name;
|
|
524
|
+
if (!Number.isInteger(semitones)) throw new TypeError("list indices must be integers or slices, not float");
|
|
525
|
+
const useFlat = opts.useFlat ?? semitones < 0;
|
|
526
|
+
return (useFlat ? FLAT_NOTES : SHARP_NOTES)[pyMod(index + semitones, 12)];
|
|
527
|
+
}
|
|
528
|
+
function transposeChord(chord, semitones, opts = {}) {
|
|
529
|
+
if (!chord || chord === "N.C." || chord === "NC") return chord;
|
|
530
|
+
const slash = chord.indexOf("/");
|
|
531
|
+
if (slash >= 0) {
|
|
532
|
+
return `${transposeChord(chord.slice(0, slash), semitones, opts)}/${transposeChord(chord.slice(slash + 1), semitones, opts)}`;
|
|
533
|
+
}
|
|
534
|
+
const match = /^([A-G][#b]?)([^\n]*)/.exec(chord);
|
|
535
|
+
if (!match) return chord;
|
|
536
|
+
return transposeNote(match[1], semitones, opts) + match[2];
|
|
537
|
+
}
|
|
538
|
+
function transposeScore(score, targetKey) {
|
|
539
|
+
const semitones = (NOTE_INDEX.get(targetKey) ?? 0) - (NOTE_INDEX.get(score.key) ?? 0);
|
|
540
|
+
const useFlat = targetKey.includes("b") || FLAT_KEYS.includes(targetKey);
|
|
541
|
+
return {
|
|
542
|
+
measures: score.measures.map((m) => m.map((c) => transposeChord(c, semitones, { useFlat }))),
|
|
543
|
+
title: `${score.title} (${targetKey})`,
|
|
544
|
+
composer: score.composer,
|
|
545
|
+
key: targetKey,
|
|
546
|
+
timeSignature: score.timeSignature
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// src/resolve.ts
|
|
551
|
+
var ROOTS2 = new Map(TONAL_ROOT_NOTES);
|
|
552
|
+
var QUALITIES = new Map(
|
|
553
|
+
TONAL_CHORD_QUALITY
|
|
554
|
+
);
|
|
555
|
+
var escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
556
|
+
var ROOT_RE = new RegExp(
|
|
557
|
+
`^(${[...ROOTS2.keys()].sort((a, b) => b.length - a.length).map(escape).join("|")})`,
|
|
558
|
+
"iu"
|
|
559
|
+
);
|
|
560
|
+
function parseRoot(chord) {
|
|
561
|
+
const match = ROOT_RE.exec(pyStrip(chord));
|
|
562
|
+
if (!match) throw new ValueError(`Unknown root note in chord: '${chord}'`);
|
|
563
|
+
const raw = match[1];
|
|
564
|
+
if (ROOTS2.has(raw)) return [raw, raw.length];
|
|
565
|
+
const lower = raw.toLowerCase();
|
|
566
|
+
if (ROOTS2.has(lower)) return [lower, raw.length];
|
|
567
|
+
for (const k of ROOTS2.keys()) if (k.toLowerCase() === lower) return [k, raw.length];
|
|
568
|
+
throw new ValueError(`Unknown root note: ${raw}`);
|
|
569
|
+
}
|
|
570
|
+
function simplifyQuality(qualityExtension) {
|
|
571
|
+
let qe = pyStrip(qualityExtension);
|
|
572
|
+
if (!qe) return "";
|
|
573
|
+
qe = qe.split("/")[0];
|
|
574
|
+
if (QUALITIES.has(qe)) return qe;
|
|
575
|
+
qe = qe.split("sus4").join("sus");
|
|
576
|
+
const qe2 = pyStrip(qe.replace(/[#b]\p{Nd}+/gu, "").replace(/add\p{Nd}+/gu, ""));
|
|
577
|
+
return QUALITIES.has(qe2) ? qe2 : qe;
|
|
578
|
+
}
|
|
579
|
+
function letterRootNotes(symbol) {
|
|
580
|
+
const match = /^([A-G][#b]?)([\s\S]*)$/.exec(pyStrip(symbol));
|
|
581
|
+
if (!match || !ROOTS2.has(match[1])) return null;
|
|
582
|
+
const intervals = QUALITIES.get(simplifyQuality(match[2]));
|
|
583
|
+
return intervals === void 0 ? null : intervals.map((i) => ROOTS2.get(match[1]) + i);
|
|
584
|
+
}
|
|
585
|
+
function tonalResolver(symbol, opts = {}) {
|
|
586
|
+
const transpose = opts.transpose ?? DEFAULT_TRANSPOSE;
|
|
587
|
+
let notes = letterRootNotes(symbol);
|
|
588
|
+
if (notes === null) {
|
|
589
|
+
const [root, rootLength] = parseRoot(symbol);
|
|
590
|
+
const quality = simplifyQuality(pyStrip(symbol).slice(rootLength));
|
|
591
|
+
const intervals = QUALITIES.get(quality);
|
|
592
|
+
if (intervals === void 0) throw new ValueError(`Unknown quality/extension: ${quality}`);
|
|
593
|
+
notes = intervals.map((i) => ROOTS2.get(root) + i);
|
|
594
|
+
}
|
|
595
|
+
return transpose ? notes.map((n) => n + transpose) : notes;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// src/patterns.ts
|
|
599
|
+
function builtin(style) {
|
|
600
|
+
const drums = DRUM_PATTERNS[style] ?? [];
|
|
601
|
+
const bass = BASS_PATTERNS[style] ?? [];
|
|
602
|
+
const comp = COMP_PATTERNS[style] ?? [];
|
|
603
|
+
return {
|
|
604
|
+
drums: drums.map((p) => ({
|
|
605
|
+
name: p.name,
|
|
606
|
+
beatsPerBar: p.beats_per_bar,
|
|
607
|
+
hits: p.hits.map(([beat, drum, velocity]) => ({ beat, drum, velocity }))
|
|
608
|
+
})),
|
|
609
|
+
bass: bass.map((p) => ({
|
|
610
|
+
name: p.name,
|
|
611
|
+
notes: p.notes.map(([beat, pitchOffset, duration, velocity]) => ({ beat, pitchOffset, duration, velocity }))
|
|
612
|
+
})),
|
|
613
|
+
comp: comp.map((p) => ({ name: p.name, hits: p.hits.map(([b, d, v]) => [b, d, v]) }))
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
var PatternRegistry = class {
|
|
617
|
+
#styles = /* @__PURE__ */ new Map();
|
|
618
|
+
/** A registry holding the builtin styles (`builtins: false` for an empty one). */
|
|
619
|
+
constructor({ builtins = true } = {}) {
|
|
620
|
+
if (builtins) for (const style of Object.keys(DRUM_PATTERNS)) this.#styles.set(style, builtin(style));
|
|
621
|
+
}
|
|
622
|
+
/** The style's patterns; throws KeyError (a RangeError) for an unknown style. */
|
|
623
|
+
getPatterns(style) {
|
|
624
|
+
const patterns = this.#styles.get(style);
|
|
625
|
+
if (patterns === void 0) {
|
|
626
|
+
throw new KeyError(`Unknown style: ${style}. Available: ${this.styles().join(", ")}`);
|
|
627
|
+
}
|
|
628
|
+
return patterns;
|
|
629
|
+
}
|
|
630
|
+
/** Add or replace a style. */
|
|
631
|
+
register(style, patterns) {
|
|
632
|
+
for (const key of ["drums", "bass", "comp"]) {
|
|
633
|
+
if (!Array.isArray(patterns[key])) throw new ValueError("Pattern dict must have keys: drums, bass, comp");
|
|
634
|
+
}
|
|
635
|
+
this.#styles.set(style, patterns);
|
|
636
|
+
}
|
|
637
|
+
/** Remove a style; returns whether it was there. */
|
|
638
|
+
unregister(style) {
|
|
639
|
+
return this.#styles.delete(style);
|
|
640
|
+
}
|
|
641
|
+
/** The style names, in registration order. */
|
|
642
|
+
styles() {
|
|
643
|
+
return [...this.#styles.keys()];
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
var registry;
|
|
647
|
+
function getPatternRegistry() {
|
|
648
|
+
registry ??= new PatternRegistry();
|
|
649
|
+
return registry;
|
|
650
|
+
}
|
|
651
|
+
function getPatterns(style) {
|
|
652
|
+
return getPatternRegistry().getPatterns(style);
|
|
653
|
+
}
|
|
654
|
+
function registerStyle(style, patterns) {
|
|
655
|
+
getPatternRegistry().register(style, patterns);
|
|
656
|
+
}
|
|
657
|
+
var STYLES = Object.freeze(Object.keys(DRUM_PATTERNS));
|
|
658
|
+
function selectPatterns(patterns, beatsPerBar) {
|
|
659
|
+
const drums = patterns.drums.find((p) => p.beatsPerBar === beatsPerBar) ?? patterns.drums[0] ?? null;
|
|
660
|
+
return [drums, patterns.bass[0] ?? null, patterns.comp[0] ?? null];
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// src/events.ts
|
|
664
|
+
var CHANNELS = Object.freeze({ drums: 9, bass: 0, piano: 1 });
|
|
665
|
+
function beatsToSeconds(beats, tempo) {
|
|
666
|
+
return beats * 60 / tempo;
|
|
667
|
+
}
|
|
668
|
+
var NO_CHORD = new Set(NO_CHORD_SYMBOLS);
|
|
669
|
+
var SEMITONES = new Map(NOTE_TO_SEMITONE);
|
|
670
|
+
var MINOR_THIRD = 3;
|
|
671
|
+
function fallbackChordNotes(symbol) {
|
|
672
|
+
const root = /^([A-Ga-g])([#b\u266f\u266d]?)/.exec(symbol);
|
|
673
|
+
if (!root) return [...FALLBACK_CHORD_NOTES];
|
|
674
|
+
const name = root[1].toUpperCase() + root[2].replace("\u266F", "#").replace("\u266D", "b");
|
|
675
|
+
const quality = symbol.slice(root[0].length);
|
|
676
|
+
const minor = quality.startsWith("-") || quality.startsWith("m") && !quality.startsWith("maj");
|
|
677
|
+
const [r, , f] = FALLBACK_CHORD_NOTES;
|
|
678
|
+
return [r, minor ? r + MINOR_THIRD : FALLBACK_CHORD_NOTES[1], f].map((n) => n + (SEMITONES.get(name) ?? 0));
|
|
679
|
+
}
|
|
680
|
+
var DRUM_DURATION = 0.25;
|
|
681
|
+
function drumEvents(pattern, start, duration, volume) {
|
|
682
|
+
return pattern.hits.filter((hit) => hit.beat < duration).map((hit) => ({
|
|
683
|
+
time: start + hit.beat,
|
|
684
|
+
channel: CHANNELS.drums,
|
|
685
|
+
note: hit.drum,
|
|
686
|
+
velocity: pyTrunc(hit.velocity * volume),
|
|
687
|
+
duration: DRUM_DURATION
|
|
688
|
+
}));
|
|
689
|
+
}
|
|
690
|
+
function pitchClasses(notes) {
|
|
691
|
+
return [...new Set([...notes].map((n) => (n % 12 + 12) % 12))].sort((a, b) => a - b);
|
|
692
|
+
}
|
|
693
|
+
function resolvePitchOffset({
|
|
694
|
+
rootPc,
|
|
695
|
+
chordPcs,
|
|
696
|
+
targetOffset
|
|
697
|
+
}) {
|
|
698
|
+
const target = targetOffset;
|
|
699
|
+
if (!chordPcs.length) return target;
|
|
700
|
+
const intervals = chordPcs.map((pc) => ((pc - rootPc) % 12 + 12) % 12);
|
|
701
|
+
if (intervals.includes(target)) return target;
|
|
702
|
+
if (target === 3 || target === 4) {
|
|
703
|
+
if (intervals.includes(3)) return 3;
|
|
704
|
+
if (intervals.includes(4)) return 4;
|
|
705
|
+
}
|
|
706
|
+
return target;
|
|
707
|
+
}
|
|
708
|
+
function bassPitch(root, chordNotes, pitchOffset) {
|
|
709
|
+
const bassRoot = (root % 12 + 12) % 12 + 36;
|
|
710
|
+
return bassRoot + resolvePitchOffset({ rootPc: bassRoot % 12, chordPcs: pitchClasses(chordNotes), targetOffset: pitchOffset });
|
|
711
|
+
}
|
|
712
|
+
function compVoicing(chordNotes) {
|
|
713
|
+
return chordNotes.length ? chordNotes.slice(0, 4).map((n) => (n % 12 + 12) % 12 + 60) : [60, 64, 67];
|
|
714
|
+
}
|
|
715
|
+
function bassEvents(pattern, start, duration, root, notes, volume) {
|
|
716
|
+
const out = [];
|
|
717
|
+
for (const note of pattern.notes) {
|
|
718
|
+
if (note.beat >= duration) continue;
|
|
719
|
+
const dur = Math.min(note.duration, Math.max(0, duration - note.beat));
|
|
720
|
+
if (dur <= 0) continue;
|
|
721
|
+
out.push({
|
|
722
|
+
time: start + note.beat,
|
|
723
|
+
channel: CHANNELS.bass,
|
|
724
|
+
note: bassPitch(root, notes, note.pitchOffset),
|
|
725
|
+
velocity: pyTrunc(note.velocity * volume),
|
|
726
|
+
duration: dur
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
return out;
|
|
730
|
+
}
|
|
731
|
+
function compEvents(pattern, start, duration, chordNotes, volume) {
|
|
732
|
+
const notes = compVoicing(chordNotes);
|
|
733
|
+
const out = [];
|
|
734
|
+
if (pattern === null || !pattern.hits.length) {
|
|
735
|
+
const velocity = pyTrunc(90 * volume);
|
|
736
|
+
for (let i = 0; i < Math.min(pyTrunc(duration), 4); i++) {
|
|
737
|
+
for (const note of notes) out.push({ time: start + i, channel: CHANNELS.piano, note, velocity, duration: 0.9 });
|
|
738
|
+
}
|
|
739
|
+
return out;
|
|
740
|
+
}
|
|
741
|
+
for (const [beat, hitDuration, hitVelocity] of pattern.hits) {
|
|
742
|
+
if (beat >= duration) continue;
|
|
743
|
+
const dur = Math.min(hitDuration, Math.max(0, duration - beat));
|
|
744
|
+
if (dur <= 0) continue;
|
|
745
|
+
const velocity = pyTrunc(hitVelocity * volume);
|
|
746
|
+
for (const note of notes) out.push({ time: start + beat, channel: CHANNELS.piano, note, velocity, duration: dur });
|
|
747
|
+
}
|
|
748
|
+
return out;
|
|
749
|
+
}
|
|
750
|
+
var volumeOf = (volumes, part, fallback) => volumes[part] ?? fallback;
|
|
751
|
+
var plays = (instruments, part) => instruments[part] ?? true;
|
|
752
|
+
function playChord(notes, [drums, bass, comp], start, duration, instruments, volumes) {
|
|
753
|
+
const root = notes.length ? notes[0] : 48;
|
|
754
|
+
const out = [];
|
|
755
|
+
if (plays(instruments, "drums") && drums) out.push(...drumEvents(drums, start, duration, volumeOf(volumes, "drums", 0.8)));
|
|
756
|
+
if (plays(instruments, "bass") && bass) {
|
|
757
|
+
out.push(...bassEvents(bass, start, duration, root, notes, volumeOf(volumes, "bass", 0.9)));
|
|
758
|
+
}
|
|
759
|
+
if (plays(instruments, "piano")) out.push(...compEvents(comp, start, duration, notes, volumeOf(volumes, "piano", 0.7)));
|
|
760
|
+
return out;
|
|
761
|
+
}
|
|
762
|
+
function chordEvents(chordNotes, opts = {}) {
|
|
763
|
+
const source = opts.patternSource ?? getPatternRegistry();
|
|
764
|
+
return playChord(
|
|
765
|
+
chordNotes,
|
|
766
|
+
selectPatterns(source.getPatterns(opts.style ?? "swing"), opts.beatsPerBar ?? 4),
|
|
767
|
+
opts.startBeat ?? 0,
|
|
768
|
+
opts.duration ?? 4,
|
|
769
|
+
opts.instruments ?? DEFAULT_INSTRUMENTS,
|
|
770
|
+
opts.volumes ?? DEFAULT_VOLUMES
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
function styleTimeSignature(source, style) {
|
|
774
|
+
return [source.getPatterns(style).drums[0]?.beatsPerBar || 4, 4];
|
|
775
|
+
}
|
|
776
|
+
function barChordSpans(beatsPerBar, nChords) {
|
|
777
|
+
if (nChords <= 0) return [];
|
|
778
|
+
if (nChords > beatsPerBar) {
|
|
779
|
+
const each = beatsPerBar / nChords;
|
|
780
|
+
return Array.from({ length: nChords }, (_, i) => [i * each, each]);
|
|
781
|
+
}
|
|
782
|
+
const base = Math.floor(beatsPerBar / nChords);
|
|
783
|
+
const extra = beatsPerBar % nChords;
|
|
784
|
+
const spans = [];
|
|
785
|
+
let offset = 0;
|
|
786
|
+
for (let i = 0; i < nChords; i++) {
|
|
787
|
+
const duration = base + (i < extra ? 1 : 0);
|
|
788
|
+
spans.push([offset, duration]);
|
|
789
|
+
offset += duration;
|
|
790
|
+
}
|
|
791
|
+
return spans;
|
|
792
|
+
}
|
|
793
|
+
function generateEvents(chords, opts = {}) {
|
|
794
|
+
const source = opts.patternSource ?? getPatternRegistry();
|
|
795
|
+
const style = opts.style ?? "swing";
|
|
796
|
+
const score = ensureScore(chords, { timeSignature: opts.timeSignature ?? styleTimeSignature(source, style) });
|
|
797
|
+
const resolve = opts.chordResolver ?? ((s) => tonalResolver(s));
|
|
798
|
+
const instruments = opts.instruments ?? DEFAULT_INSTRUMENTS;
|
|
799
|
+
const volumes = opts.volumes ?? DEFAULT_VOLUMES;
|
|
800
|
+
const beatsPerBar = score.timeSignature[0];
|
|
801
|
+
const patterns = selectPatterns(source.getPatterns(style), beatsPerBar);
|
|
802
|
+
const repeats = opts.repeats ?? 1;
|
|
803
|
+
if (!Number.isInteger(repeats)) throw new TypeError(`repeats must be an integer, got ${repeats}`);
|
|
804
|
+
const out = [];
|
|
805
|
+
for (let repeat = 0; repeat < repeats; repeat++) {
|
|
806
|
+
let beat = repeat * score.measures.length * beatsPerBar;
|
|
807
|
+
for (const measure of score.measures) {
|
|
808
|
+
const spans = barChordSpans(beatsPerBar, measure.length);
|
|
809
|
+
measure.forEach((symbol, i) => {
|
|
810
|
+
const [offset, perChord] = spans[i];
|
|
811
|
+
if (NO_CHORD.has(symbol)) return;
|
|
812
|
+
let notes;
|
|
813
|
+
try {
|
|
814
|
+
notes = resolve(symbol);
|
|
815
|
+
} catch {
|
|
816
|
+
notes = fallbackChordNotes(symbol);
|
|
817
|
+
}
|
|
818
|
+
out.push(...playChord(notes, patterns, beat + offset, perChord, instruments, volumes));
|
|
819
|
+
});
|
|
820
|
+
beat += beatsPerBar;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return out;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// src/skeletons.ts
|
|
827
|
+
var RHYTHMIC_SKELETONS2 = new Map(
|
|
828
|
+
Object.entries(RHYTHMIC_SKELETONS).map(([key, s]) => [
|
|
829
|
+
key,
|
|
830
|
+
{ pattern: [...s.pattern], name: s.name, beatsPerMeasure: s.beats_per_measure, styles: [...s.styles] }
|
|
831
|
+
])
|
|
832
|
+
);
|
|
833
|
+
function byName(name) {
|
|
834
|
+
for (const [key, s] of RHYTHMIC_SKELETONS2) if (s.name.toLowerCase() === name) return key;
|
|
835
|
+
return void 0;
|
|
836
|
+
}
|
|
837
|
+
function byStyle(style) {
|
|
838
|
+
return [...RHYTHMIC_SKELETONS2].filter(([, s]) => s.styles.includes(style)).map(([key]) => key);
|
|
839
|
+
}
|
|
840
|
+
function resolveSkeleton(skeleton) {
|
|
841
|
+
if (Array.isArray(skeleton)) return [...skeleton];
|
|
842
|
+
if (typeof skeleton !== "string") {
|
|
843
|
+
throw new TypeError(`skeleton must be a str, tuple, or list, got ${typeof skeleton}`);
|
|
844
|
+
}
|
|
845
|
+
const exact = RHYTHMIC_SKELETONS2.get(skeleton);
|
|
846
|
+
if (exact) return [...exact.pattern];
|
|
847
|
+
const lower = skeleton.toLowerCase();
|
|
848
|
+
const key = byName(lower) ?? byStyle(lower)[0];
|
|
849
|
+
if (key !== void 0) return [...RHYTHMIC_SKELETONS2.get(key).pattern];
|
|
850
|
+
throw new KeyError(`Unknown skeleton: '${skeleton}'`);
|
|
851
|
+
}
|
|
852
|
+
var EPSILON = 1e-9;
|
|
853
|
+
function groupIntoMeasures(chords, beatsPerMeasure) {
|
|
854
|
+
const measures = [];
|
|
855
|
+
let current = [];
|
|
856
|
+
let remaining = beatsPerMeasure;
|
|
857
|
+
for (const [symbol, duration] of chords) {
|
|
858
|
+
let left = duration;
|
|
859
|
+
while (left > EPSILON) {
|
|
860
|
+
const take = Math.min(left, remaining);
|
|
861
|
+
current.push([symbol, take]);
|
|
862
|
+
remaining -= take;
|
|
863
|
+
left -= take;
|
|
864
|
+
if (remaining < EPSILON) {
|
|
865
|
+
measures.push(current);
|
|
866
|
+
current = [];
|
|
867
|
+
remaining = beatsPerMeasure;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
if (current.length) measures.push(current);
|
|
872
|
+
return measures;
|
|
873
|
+
}
|
|
874
|
+
function expandMeasure(chords, starts, durations) {
|
|
875
|
+
const map = [];
|
|
876
|
+
let pos = 0;
|
|
877
|
+
for (const [symbol, dur] of chords) {
|
|
878
|
+
map.push([symbol, pos, pos + dur]);
|
|
879
|
+
pos += dur;
|
|
880
|
+
}
|
|
881
|
+
const out = [];
|
|
882
|
+
starts.forEach((strike, i) => {
|
|
883
|
+
const end = strike + durations[i];
|
|
884
|
+
for (const [symbol, chordStart, chordEnd] of map) {
|
|
885
|
+
const overlap = Math.min(end, chordEnd) - Math.max(strike, chordStart);
|
|
886
|
+
if (overlap > EPSILON) out.push([symbol, overlap]);
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
return out;
|
|
890
|
+
}
|
|
891
|
+
function applySkeleton(chords, skeleton) {
|
|
892
|
+
const pattern = resolveSkeleton(skeleton);
|
|
893
|
+
const starts = [];
|
|
894
|
+
let t = 0;
|
|
895
|
+
for (const dur of pattern) {
|
|
896
|
+
starts.push(t);
|
|
897
|
+
t += dur;
|
|
898
|
+
}
|
|
899
|
+
return groupIntoMeasures(chords, pySum(pattern)).flatMap((m) => expandMeasure(m, starts, pattern));
|
|
900
|
+
}
|
|
901
|
+
function registerSkeleton(key, pattern, opts = {}) {
|
|
902
|
+
RHYTHMIC_SKELETONS2.set(key, {
|
|
903
|
+
pattern: [...pattern],
|
|
904
|
+
name: opts.name || key,
|
|
905
|
+
beatsPerMeasure: opts.beatsPerMeasure ?? pySum(pattern),
|
|
906
|
+
styles: opts.styles ? [...opts.styles] : []
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
function listSkeletons(opts = {}) {
|
|
910
|
+
let keys = [...RHYTHMIC_SKELETONS2.keys()];
|
|
911
|
+
if (opts.beatsPerMeasure !== void 0) {
|
|
912
|
+
keys = keys.filter((k) => RHYTHMIC_SKELETONS2.get(k).beatsPerMeasure === opts.beatsPerMeasure);
|
|
913
|
+
}
|
|
914
|
+
if (opts.style !== void 0) {
|
|
915
|
+
const inStyle = new Set(byStyle(opts.style));
|
|
916
|
+
keys = keys.filter((k) => inStyle.has(k));
|
|
917
|
+
}
|
|
918
|
+
return keys;
|
|
919
|
+
}
|
|
920
|
+
export {
|
|
921
|
+
CHANNELS,
|
|
922
|
+
DRUMS,
|
|
923
|
+
KeyError,
|
|
924
|
+
PatternRegistry,
|
|
925
|
+
RHYTHMIC_SKELETONS2 as RHYTHMIC_SKELETONS,
|
|
926
|
+
STYLES,
|
|
927
|
+
ValueError,
|
|
928
|
+
ZeroDivisionError,
|
|
929
|
+
applySkeleton,
|
|
930
|
+
bassPitch,
|
|
931
|
+
beatsToSeconds,
|
|
932
|
+
chordEvents,
|
|
933
|
+
compVoicing,
|
|
934
|
+
ensureScore,
|
|
935
|
+
fallbackChordNotes,
|
|
936
|
+
generateEvents,
|
|
937
|
+
getPatternRegistry,
|
|
938
|
+
getPatterns,
|
|
939
|
+
listSkeletons,
|
|
940
|
+
normalizeChordSymbol,
|
|
941
|
+
parseChordString,
|
|
942
|
+
parseIrealUrl,
|
|
943
|
+
parseTimeSig,
|
|
944
|
+
pitchClasses,
|
|
945
|
+
registerSkeleton,
|
|
946
|
+
registerStyle,
|
|
947
|
+
resolvePitchOffset,
|
|
948
|
+
resolveSkeleton,
|
|
949
|
+
scoreFromChordSpecs,
|
|
950
|
+
scoreToChordSequence,
|
|
951
|
+
selectPatterns,
|
|
952
|
+
tonalResolver,
|
|
953
|
+
transposeChord,
|
|
954
|
+
transposeNote,
|
|
955
|
+
transposeScore
|
|
956
|
+
};
|
|
957
|
+
/*! Parts of accompy's iReal decoding derive from pyRealParser, under this licence:
|
|
958
|
+
|
|
959
|
+
MIT License
|
|
960
|
+
|
|
961
|
+
Copyright (c) 2018 Daniel Stephan
|
|
962
|
+
|
|
963
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
964
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
965
|
+
in the Software without restriction, including without limitation the rights
|
|
966
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
967
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
968
|
+
furnished to do so, subject to the following conditions:
|
|
969
|
+
|
|
970
|
+
The above copyright notice and this permission notice shall be included in all
|
|
971
|
+
copies or substantial portions of the Software.
|
|
972
|
+
|
|
973
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
974
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
975
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
976
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
977
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
978
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
979
|
+
SOFTWARE.
|
|
980
|
+
*/
|