scribbletune 5.1.2 → 5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scribbletune",
3
- "version": "5.1.2",
3
+ "version": "5.2.0",
4
4
  "description": "Create music with JavaScript and Node.js!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -35,17 +35,17 @@
35
35
  "bump-patch": "npm version patch --no-git-tag-version"
36
36
  },
37
37
  "dependencies": {
38
- "harmonics": "^1.1.2",
39
- "jsmidgen": "~0.1.5"
38
+ "@scribbletune/midi": "^1.0.0",
39
+ "harmonics": "^1.1.2"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/jest": "^27.0.1",
43
43
  "@types/node": "^12.19.15",
44
- "@typescript-eslint/eslint-plugin": "^4.31.1",
45
- "@typescript-eslint/parser": "^4.31.1",
44
+ "@typescript-eslint/eslint-plugin": "^6.21.0",
45
+ "@typescript-eslint/parser": "^6.21.0",
46
46
  "cross-env": "^7.0.3",
47
47
  "dts-bundle": "^0.7.3",
48
- "eslint": "^7.32.0",
48
+ "eslint": "^8.57.1",
49
49
  "eslint-config-prettier": "^8.3.0",
50
50
  "eslint-loader": "^4.0.2",
51
51
  "eslint-plugin-prettier": "^4.0.0",
@@ -55,7 +55,7 @@
55
55
  "ts-loader": "^9.2.5",
56
56
  "ts-node": "^10.2.0",
57
57
  "typedoc": "^0.21.5",
58
- "typescript": "^4.4.3",
58
+ "typescript": "^5.9.3",
59
59
  "webpack": "^5.53.0",
60
60
  "webpack-cli": "^4.8.0"
61
61
  },
@@ -64,12 +64,10 @@
64
64
  "url": "git://github.com/scribbletune/scribbletune.git"
65
65
  },
66
66
  "keywords": [
67
- "javascript",
68
67
  "music",
69
68
  "music theory",
70
69
  "midi",
71
70
  "web audio api",
72
- "jsmidgen",
73
71
  "tone.js",
74
72
  "tonal",
75
73
  "harmonics"
@@ -1,588 +0,0 @@
1
- var O = {}, B;
2
- function Q() {
3
- return B || (B = 1, function(e) {
4
- (() => {
5
- var r = { 413: (s) => {
6
- s.exports = JSON.parse('{"5th":"100000010000","M7#5sus4":"100001001001","7#5sus4":"100001001010","sus4":"100001010000","M7sus4":"100001010001","7sus4":"100001010010","7no5":"100010000010","aug":"100010001000","M7b6":"100010001001","maj7#5":"100010001001","7#5":"100010001010","7b13":"100010001010","M":"100010010000","maj7":"100010010001","7th":"100010010010","6th":"100010010100","7add6":"100010010110","7b6":"100010011010","Mb5":"100010100000","M7b5":"100010100001","7b5":"100010100010","maj#4":"100010110001","7#11":"100010110010","M6#11":"100010110100","7#11b13":"100010111010","m#5":"100100001000","mb6M7":"100100001001","m7#5":"100100001010","m":"100100010000","m/ma7":"100100010001","m7":"100100010010","m6":"100100010100","mMaj7b6":"100100011001","dim":"100100100000","oM7":"100100100001","m7b5":"100100100010","dim7":"100100100100","o7M7":"100100100101","4th":"100101000010","madd4":"100101010000","m7add11":"100101010010","+add#9":"100110001000","7#5#9":"100110001010","7#9":"100110010010","13#9":"100110010110","7#9b13":"100110011010","maj7#9#11":"100110110001","7#9#11":"100110110010","13#9#11":"100110110110","7#9#11b13":"100110111010","sus2":"101000010000","M9#5sus4":"101001001001","sus24":"101001010000","M9sus4":"101001010001","11th":"101001010010","9sus4":"101001010010","13sus4":"101001010110","9no5":"101010000010","13no5":"101010000110","M#5add9":"101010001000","maj9#5":"101010001001","9#5":"101010001010","9b13":"101010001010","Madd9":"101010010000","maj9":"101010010001","9th":"101010010010","6/9":"101010010100","maj13":"101010010101","M7add13":"101010010101","13th":"101010010110","M9b5":"101010100001","9b5":"101010100010","13b5":"101010100110","9#5#11":"101010101010","maj9#11":"101010110001","9#11":"101010110010","69#11":"101010110100","M13#11":"101010110101","13#11":"101010110110","9#11b13":"101010111010","m9#5":"101100001010","madd9":"101100010000","mM9":"101100010001","m9":"101100010010","m69":"101100010100","m13":"101100010110","mMaj9b6":"101100011001","m9b5":"101100100010","m11A":"101101001010","m11":"101101010010","b9sus":"110001010010","11b9":"110001010010","7sus4b9b13":"110001011010","alt7":"110010000010","7#5b9":"110010001010","Maddb9":"110010010000","M7b9":"110010010001","7b9":"110010010010","13b9":"110010010110","7b9b13":"110010011010","7#5b9#11":"110010101010","7b9#11":"110010110010","13b9#11":"110010110110","7b9b13#11":"110010111010","mb6b9":"110100001000","7b9#9":"110110010010"}');
7
- }, 722: (s) => {
8
- s.exports = JSON.parse(`{"major pentatonic":"101010010100","ionian pentatonic":"100011010001","mixolydian pentatonic":"100011010010","ritusen":"101001010100","egyptian":"101001010010","neopolitan major pentatonic":"100011100010","vietnamese 1":"100101011000","pelog":"110100011000","kumoijoshi":"110001011000","hirajoshi":"101100011000","iwato":"110001100010","in-sen":"110001010010","lydian pentatonic":"100010110001","malkos raga":"100101001010","locrian pentatonic":"100101100010","minor pentatonic":"100101010010","minor six pentatonic":"100101010100","flat three pentatonic":"101100010100","flat six pentatonic":"101010011000","scriabin":"110010010100","whole tone pentatonic":"100010101010","lydian #5P pentatonic":"100010101001","lydian dominant pentatonic":"100010110010","minor #7M pentatonic":"100101010001","super locrian pentatonic":"100110100010","minor hexatonic":"101101010001","augmented":"100110011001","major blues":"101110010100","piongio":"101001010110","prometheus neopolitan":"110010100110","prometheus":"101010100110","mystery #1":"110010101010","six tone symmetric":"110011001100","whole tone":"101010101010","messiaen's mode #5":"110001110001","minor blues":"100101110010","locrian major":"101011101010","double harmonic lydian":"110010111001","harmonic minor":"101101011001","altered":"110110101010","locrian #2":"101101101010","mixolydian b6":"101011011010","lydian dominant":"101010110110","lydian":"101010110101","lydian augmented":"101010101101","dorian b2":"110101010110","melodic minor":"101101010101","locrian":"110101101010","ultralocrian":"110110101100","locrian 6":"110101100110","augmented heptatonic":"100111011001","romanian minor":"101100110110","dorian #4":"101100110110","lydian diminished":"101100110101","phrygian":"110101011010","leading whole tone":"101010101011","lydian minor":"101010111010","phrygian dominant":"110011011010","balinese":"110101011001","neopolitan major":"110101010101","aeolian":"101101011010","harmonic major":"101011011001","double harmonic major":"110011011001","dorian":"101101010110","hungarian minor":"101100111001","hungarian major":"100110110110","oriental":"110011100110","flamenco":"110110110010","todi raga":"110100111001","mixolydian":"101011010110","persian":"110011101001","major":"101011010101","enigmatic":"110010101011","major augmented":"101011001101","lydian #9":"100110110101","messiaen's mode #4":"111001111001","purvi raga":"110011111001","spanish heptatonic":"110111011010","bebop":"101011010111","bebop minor":"101111010110","bebop major":"101011011101","bebop locrian":"110101111010","minor bebop":"101101011011","diminished":"101101101101","ichikosucho":"101011110101","minor six diminished":"101101011101","half-whole diminished":"110110110110","kafi raga":"100111010111","messiaen's mode #6":"101011101011","composite blues":"101111110110","messiaen's mode #3":"101110111011","messiaen's mode #7":"111101111101","chromatic":"111111111111","ionian":"101011010101","minor":"101101011010"}`);
9
- } }, a = {};
10
- function h(s) {
11
- var c = a[s];
12
- if (c !== void 0) return c.exports;
13
- var u = a[s] = { exports: {} };
14
- return r[s](u, u.exports, h), u.exports;
15
- }
16
- h.d = (s, c) => {
17
- for (var u in c) h.o(c, u) && !h.o(s, u) && Object.defineProperty(s, u, { enumerable: !0, get: c[u] });
18
- }, h.o = (s, c) => Object.prototype.hasOwnProperty.call(s, c), h.r = (s) => {
19
- typeof Symbol < "u" && Symbol.toStringTag && Object.defineProperty(s, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(s, "__esModule", { value: !0 });
20
- };
21
- var o = {};
22
- (() => {
23
- h.r(o), h.d(o, { chord: () => f, chords: () => n, inlineChord: () => t, scale: () => m, scales: () => d });
24
- var s = h(722), c = h(413), u = function(p) {
25
- var b = p.scale, v = p.chord, y = b || v, M = b ? "scale" : "chord";
26
- if (typeof y != "string") throw new Error("".concat(y, " is not a valid input for ").concat(M));
27
- var w, T, A = y.indexOf(" ");
28
- A === -1 ? (w = y.slice(1), T = y[0], y[1] !== "b" && y[1] !== "#" || (w = y.slice(2), T += y[1])) : (w = y.slice(A === -1 ? 1 : A + 1), T = y.slice(0, A));
29
- var K = function(x) {
30
- return { "C#": "Db", "D#": "Eb", "F#": "Gb", "G#": "Ab", "A#": "Bb" }[x.toUpperCase()] || x.charAt(0).toUpperCase() + x.slice(1);
31
- }(T.replace(/\d/g, "")), F = T.replace(/\D/g, "") !== "" ? +T.replace(/\D/g, "") : 4;
32
- if (isNaN(F)) throw new Error("".concat(T[0], " does not have a valid octave"));
33
- if (!s[w] && !c[w]) throw new Error("".concat(y, " is not a valid ").concat(M));
34
- for (var J = function(x, N) {
35
- var k = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"], q = k.map(function(C) {
36
- return C + N;
37
- }), W = k.map(function(C) {
38
- return C + (N + 1);
39
- }), U = q.concat(W);
40
- return U.slice(U.indexOf(x + N));
41
- }(K, F), j = [], _ = 0, D = 0, S = b ? s : c; _ < S[w].length; ) S[w][_] === "1" && j.push(J[D]), _++, D++;
42
- return j;
43
- }, t = function(p) {
44
- var b, v, y = "b9sus", M = 4;
45
- return p.includes(y) ? (v = y, b = p.slice(0, p.indexOf(y))) : (b = p[0], v = p.slice(1), p[1] !== "b" && p[1] !== "#" || (b += p[1], v = p.slice(2))), p.includes("_") && (M = +p.split("_")[1], v = v.slice(0, v.indexOf("_"))), u({ chord: b + M + " " + v });
46
- }, n = function() {
47
- return Object.keys(c);
48
- }, d = function() {
49
- return Object.keys(s);
50
- }, f = function(p) {
51
- return u({ chord: p });
52
- }, m = function(p) {
53
- return u({ scale: p });
54
- };
55
- })();
56
- var i = e;
57
- for (var l in o) i[l] = o[l];
58
- o.__esModule && Object.defineProperty(i, "__esModule", { value: !0 });
59
- })();
60
- }(O)), O;
61
- }
62
- var g = Q();
63
- const R = (e) => /^[a-gA-G](?:#|b)?\d$/.test(e), X = (e) => (e = JSON.stringify(e.split("")), e = e.replace(/,"\[",/g, ", ["), e = e.replace(/"\[",/g, "["), e = e.replace(/,"\]"/g, "]"), JSON.parse(e)), Z = (e, r = !0) => {
64
- const a = e.length - 1;
65
- return e.forEach((h, o) => {
66
- if (o >= a)
67
- return;
68
- const i = r ? (
69
- // Pick random number from idx+1 to lastIndex (Modified algorithm, (N-1)! combinations)
70
- // Math.random -> [0, 1) -> [0, lastIndex-idx ) --floor-> [0, lastIndex-idx-1]
71
- // rnd = [0, lastIndex-idx-1] + 1 + idx = [1 + idx, lastIndex]
72
- // (Original algorithm would pick rnd = [idx, lastIndex], thus any element could arrive back into its slot)
73
- Math.floor(Math.random() * (a - o)) + 1 + o
74
- ) : (
75
- // Pick random number from idx to lastIndex (Unmodified Richard Durstenfeld, N! combinations)
76
- Math.floor(Math.random() * (a + 1 - o)) + o
77
- );
78
- e[o] = e[i], e[i] = h;
79
- }), e;
80
- }, E = (e) => e.length > 1 ? e[Math.round(Math.random())] : e[0], L = () => !!Math.round(Math.random()), t0 = (e) => typeof e.message == "string", e0 = (e) => {
81
- let r, a, h, o;
82
- try {
83
- r = g.inlineChord(e);
84
- } catch (i) {
85
- h = i;
86
- }
87
- try {
88
- a = g.chord(e.replace(/_/g, " "));
89
- } catch (i) {
90
- o = i;
91
- }
92
- if (!h && !o) {
93
- if (r.toString() !== a.toString())
94
- throw new Error(`Chord ${e} cannot decode, guessing ${r} or ${a}`);
95
- return r;
96
- }
97
- return h ? o ? g.chord(e) : a : r;
98
- }, P = (e) => {
99
- if (typeof e == "string" && R(e))
100
- return [e];
101
- if (Array.isArray(e))
102
- return e.forEach((r) => {
103
- if (Array.isArray(r))
104
- r.forEach((a) => {
105
- if (typeof a != "string" || !R(a))
106
- throw new TypeError("array of arrays must comprise valid notes");
107
- });
108
- else if (typeof r != "string" || !R(r))
109
- throw new TypeError("array must comprise valid notes");
110
- }), e;
111
- if (!Array.isArray(e)) {
112
- const r = e0(e);
113
- if (r && r.length)
114
- return r;
115
- }
116
- throw new Error(`Chord ${e} not found`);
117
- }, V = (e = 1) => Math.round(Math.random() * e), n0 = () => ({
118
- notes: ["C4"],
119
- pattern: "x",
120
- shuffle: !1,
121
- sizzle: !1,
122
- sizzleReps: 1,
123
- arpegiate: !1,
124
- subdiv: "4n",
125
- amp: 100,
126
- accentLow: 70,
127
- randomNotes: null,
128
- offlineRendering: !1
129
- }), H = {
130
- "1m": 2048,
131
- "2m": 4096,
132
- "3m": 6144,
133
- "4m": 8192,
134
- "1n": 512,
135
- "2n": 256,
136
- "4n": 128,
137
- "8n": 64,
138
- "16n": 32
139
- }, u0 = (e) => {
140
- if (e = { ...n0(), ...e || {} }, typeof e.notes == "string" && (e.notes = e.notes.replace(/\s{2,}/g, " "), e.notes = e.notes.split(" ")), e.notes = e.notes ? e.notes.map(P) : [], /[^x\-_[\]R]/.test(e.pattern))
141
- throw new TypeError(
142
- `pattern can only comprise x - _ [ ] R, found ${e.pattern}`
143
- );
144
- e.shuffle && (e.notes = Z(e.notes)), e.randomNotes && typeof e.randomNotes == "string" && (e.randomNotes = e.randomNotes.replace(/\s{2,}/g, " ").split(/\s/)), e.randomNotes && (e.randomNotes = e.randomNotes.map(
145
- P
146
- ));
147
- const r = [];
148
- let a = 0;
149
- const h = (o, i, l) => {
150
- let s = 0;
151
- o.forEach((c, u) => {
152
- var t;
153
- if (typeof c == "string") {
154
- let n = null;
155
- if (c === "-" || (c === "R" && V() && // Use 1/2 probability for R to pick from param.notes
156
- e.randomNotes && e.randomNotes.length > 0 ? n = e.randomNotes[V(e.randomNotes.length - 1)] : e.notes && (n = e.notes[a])), (c === "x" || c === "R") && a++, (c === "x" || c === "-" || c === "R") && (r.push({
157
- note: n,
158
- length: i,
159
- level: c === "R" && !e.randomNotes ? e.accentLow : e.amp
160
- }), s += i), c === "_" && r.length && (r[r.length - 1].length += i, s += i), l && s !== l && u === o.length - 1) {
161
- const d = Math.abs(
162
- l - s
163
- ), f = r[r.length - 1];
164
- f.length > d ? f.length = f.length - d : f.length = f.length + d;
165
- }
166
- a === ((t = e.notes) == null ? void 0 : t.length) && (a = 0);
167
- }
168
- if (Array.isArray(c)) {
169
- let n = !1;
170
- (c.length % 2 !== 0 || i % 2 !== 0) && (n = !0), h(
171
- c,
172
- Math.round(i / c.length),
173
- n && i
174
- ), s += i;
175
- }
176
- });
177
- };
178
- if (h(
179
- X(e.pattern),
180
- H[e.subdiv] || H["4n"],
181
- !1
182
- ), e.sizzle) {
183
- const o = [], i = e.sizzle === !0 ? "sin" : e.sizzle, l = r.length, s = e.amp, c = e.sizzleReps, u = s / (l / c);
184
- if (i === "sin" || i === "cos")
185
- for (let t = 0; t < l; t++) {
186
- const n = Math[i](t * Math.PI / (l / c)) * s;
187
- o.push(Math.round(Math.abs(n)));
188
- }
189
- if (i === "rampUp") {
190
- let t = 0;
191
- for (let n = 0; n < l; n++)
192
- n % (l / c) === 0 ? t = 0 : t = t + u, o.push(Math.round(Math.abs(t)));
193
- }
194
- if (i === "rampDown") {
195
- let t = s;
196
- for (let n = 0; n < l; n++)
197
- n % (l / c) === 0 ? t = s : t = t - u, o.push(Math.round(Math.abs(t)));
198
- }
199
- for (let t = 0; t < o.length; t++)
200
- r[t].level = o[t] ? o[t] : 1;
201
- }
202
- if (e.accent) {
203
- if (/[^x-]/.test(e.accent))
204
- throw new TypeError("Accent can only have x and - characters");
205
- let o = 0;
206
- for (const i of r) {
207
- let l = e.accent[o] === "x" ? e.amp : e.accentLow;
208
- e.sizzle && (l = (i.level + l) / 2), i.level = Math.round(l), o = o + 1, o === e.accent.length && (o = 0);
209
- }
210
- }
211
- return r;
212
- }, p0 = (e) => {
213
- const r = {
214
- ionian: ["I", "ii", "iii", "IV", "V", "vi", "vii°"],
215
- dorian: ["i", "ii", "III", "IV", "v", "vi°", "VII"],
216
- phrygian: ["i", "II", "III", "iv", "v°", "VI", "vii"],
217
- lydian: ["I", "II", "iii", "iv°", "V", "vi", "vii"],
218
- mixolydian: ["I", "ii", "iii°", "IV", "v", "vi", "VII"],
219
- aeolian: ["i", "ii°", "III", "iv", "v", "VI", "VII"],
220
- locrian: ["i°", "II", "iii", "iv", "V", "VI", "vii"],
221
- "melodic minor": ["i", "ii", "III+", "IV", "V", "vi°", "vii°"],
222
- "harmonic minor": ["i", "ii°", "III+", "iv", "V", "VI", "vii°"]
223
- };
224
- return r.major = r.ionian, r.minor = r.aeolian, r[e] || [];
225
- }, i0 = {
226
- i: 0,
227
- ii: 1,
228
- iii: 2,
229
- iv: 3,
230
- v: 4,
231
- vi: 5,
232
- vii: 6
233
- }, o0 = (e) => {
234
- const r = e.replace(/\W/g, "");
235
- let a = "M";
236
- return r.toLowerCase() === r && (a = "m"), e.indexOf("°") > -1 ? a + "7b5" : e.indexOf("+") > -1 ? a + "#5" : e.indexOf("7") > -1 ? a === "M" ? "maj7" : "m7" : a;
237
- }, m0 = (e, r) => {
238
- const a = e.split(" ");
239
- a[0].match(/\d/) || (a[0] += "4", e = a.join(" "));
240
- const h = g.scale(e);
241
- return r.replace(/\s*,+\s*/g, " ").split(" ").map((l) => {
242
- const s = o0(l), c = i0[l.replace(/\W|\d/g, "").toLowerCase()], u = h[c], t = u.replace(/\D+/, "");
243
- return u.replace(/\d/, "") + s + "_" + t;
244
- }).toString().replace(/,/g, " ");
245
- }, Y = ({ T: e, P: r, D: a }) => (h = 4) => {
246
- const o = [];
247
- o.push(E(e));
248
- let i = 1;
249
- for (i < h - 1 && (o.push(E(r)), i++), i < h - 1 && L() && (o.push(E(r)), i++), i < h - 1 && (o.push(E(a)), i++), i < h - 1 && (o.push(E(r)), i++), i < h - 1 && (o.push(E(a)), i++), i < h - 1 && L() && (o.push(E(r)), i++); i < h; )
250
- o.push(E(a)), i++;
251
- return o;
252
- }, r0 = Y({ T: ["I", "vi"], P: ["ii", "IV"], D: ["V"] }), s0 = Y({ T: ["i", "VI"], P: ["ii", "iv"], D: ["V"] }), y0 = (e, r = 4) => e === "major" || e === "M" ? r0(r) : e === "minor" || e === "m" ? s0(r) : [], a0 = 4, z = (e, r) => {
253
- const a = (l) => {
254
- if (!l)
255
- throw new Error("Empty element");
256
- const s = l.replace(/\d/, ""), c = l.replace(/\D/g, "") || a0;
257
- if (!s)
258
- throw new Error("Incorrect note");
259
- return s + (+c + 1);
260
- }, h = e.map(a), o = h.map(a);
261
- return [...e, ...h, ...o].slice(0, r);
262
- }, v0 = (e) => {
263
- let r = [];
264
- const a = {
265
- count: 4,
266
- order: "0123",
267
- chords: ""
268
- };
269
- if (typeof e == "string")
270
- a.chords = e;
271
- else {
272
- if (e.order && e.order.match(/\D/g))
273
- throw new TypeError("Invalid value for order");
274
- if (e.count > 8 || e.count < 2)
275
- throw new TypeError("Invalid value for count");
276
- e.count && !e.order && (a.order = Array.from(Array(e.count).keys()).join("")), Object.assign(a, e);
277
- }
278
- if (typeof a.chords == "string")
279
- a.chords.split(" ").forEach((o, i) => {
280
- try {
281
- const l = z(g.inlineChord(o), a.count), s = a.order.split("").map((c) => l[c]);
282
- r = [...r, ...s];
283
- } catch {
284
- throw new Error(
285
- `Cannot decode chord ${i + 1} "${o}" in given "${a.chords}"`
286
- );
287
- }
288
- });
289
- else if (Array.isArray(a.chords))
290
- a.chords.forEach((h, o) => {
291
- try {
292
- const i = z(h, a.count), l = a.order.split("").map((s) => i[s]);
293
- r = [...r, ...l];
294
- } catch (i) {
295
- throw new Error(
296
- `${t0(i) ? i.message : i} in chord ${o + 1} "${h}"`
297
- );
298
- }
299
- });
300
- else
301
- throw new TypeError("Invalid value for chords");
302
- return r;
303
- }, c0 = {};
304
- var I = { exports: {} }, l0 = I.exports, $;
305
- function h0() {
306
- return $ || ($ = 1, function(e, r) {
307
- var a = {};
308
- (function(h) {
309
- var o = h.DEFAULT_VOLUME = 90;
310
- h.DEFAULT_DURATION = 128, h.DEFAULT_CHANNEL = 0;
311
- var i = {
312
- midi_letter_pitches: { a: 21, b: 23, c: 12, d: 14, e: 16, f: 17, g: 19 },
313
- /**
314
- * Convert a symbolic note name (e.g. "c4") to a numeric MIDI pitch (e.g.
315
- * 60, middle C).
316
- *
317
- * @param {string} n - The symbolic note name to parse.
318
- * @returns {number} The MIDI pitch that corresponds to the symbolic note
319
- * name.
320
- */
321
- midiPitchFromNote: function(t) {
322
- var n = /([a-g])(#+|b+)?([0-9]+)$/i.exec(t), d = n[1].toLowerCase(), f = n[2] || "", m = parseInt(n[3], 10);
323
- return 12 * m + i.midi_letter_pitches[d] + (f.substr(0, 1) == "#" ? 1 : -1) * f.length;
324
- },
325
- /**
326
- * Ensure that the given argument is converted to a MIDI pitch. Note that
327
- * it may already be one (including a purely numeric string).
328
- *
329
- * @param {string|number} p - The pitch to convert.
330
- * @returns {number} The resulting numeric MIDI pitch.
331
- */
332
- ensureMidiPitch: function(t) {
333
- return typeof t == "number" || !/[^0-9]/.test(t) ? parseInt(t, 10) : i.midiPitchFromNote(t);
334
- },
335
- midi_pitches_letter: { 12: "c", 13: "c#", 14: "d", 15: "d#", 16: "e", 17: "f", 18: "f#", 19: "g", 20: "g#", 21: "a", 22: "a#", 23: "b" },
336
- midi_flattened_notes: { "a#": "bb", "c#": "db", "d#": "eb", "f#": "gb", "g#": "ab" },
337
- /**
338
- * Convert a numeric MIDI pitch value (e.g. 60) to a symbolic note name
339
- * (e.g. "c4").
340
- *
341
- * @param {number} n - The numeric MIDI pitch value to convert.
342
- * @param {boolean} [returnFlattened=false] - Whether to prefer flattened
343
- * notes to sharpened ones. Optional, default false.
344
- * @returns {string} The resulting symbolic note name.
345
- */
346
- noteFromMidiPitch: function(t, p) {
347
- var d = 0, f = t, m, p = p || !1;
348
- return t > 23 && (d = Math.floor(t / 12) - 1, f = t - d * 12), m = i.midi_pitches_letter[f], p && m.indexOf("#") > 0 && (m = i.midi_flattened_notes[m]), m + d;
349
- },
350
- /**
351
- * Convert beats per minute (BPM) to microseconds per quarter note (MPQN).
352
- *
353
- * @param {number} bpm - A number in beats per minute.
354
- * @returns {number} The number of microseconds per quarter note.
355
- */
356
- mpqnFromBpm: function(t) {
357
- var n = Math.floor(6e7 / t), d = [];
358
- do
359
- d.unshift(n & 255), n >>= 8;
360
- while (n);
361
- for (; d.length < 3; )
362
- d.push(0);
363
- return d;
364
- },
365
- /**
366
- * Convert microseconds per quarter note (MPQN) to beats per minute (BPM).
367
- *
368
- * @param {number} mpqn - The number of microseconds per quarter note.
369
- * @returns {number} A number in beats per minute.
370
- */
371
- bpmFromMpqn: function(t) {
372
- var n = t;
373
- if (typeof t[0] < "u") {
374
- n = 0;
375
- for (var d = 0, f = t.length - 1; f >= 0; ++d, --f)
376
- n |= t[d] << f;
377
- }
378
- return Math.floor(6e7 / t);
379
- },
380
- /**
381
- * Converts an array of bytes to a string of hexadecimal characters. Prepares
382
- * it to be converted into a base64 string.
383
- *
384
- * @param {Array} byteArray - Array of bytes to be converted.
385
- * @returns {string} Hexadecimal string, e.g. "097B8A".
386
- */
387
- codes2Str: function(t) {
388
- return String.fromCharCode.apply(null, t);
389
- },
390
- /**
391
- * Converts a string of hexadecimal values to an array of bytes. It can also
392
- * add remaining "0" nibbles in order to have enough bytes in the array as the
393
- * `finalBytes` parameter.
394
- *
395
- * @param {string} str - string of hexadecimal values e.g. "097B8A"
396
- * @param {number} [finalBytes] - Optional. The desired number of bytes
397
- * (not nibbles) that the returned array should contain.
398
- * @returns {Array} An array of nibbles.
399
- */
400
- str2Bytes: function(t, n) {
401
- if (n)
402
- for (; t.length / 2 < n; )
403
- t = "0" + t;
404
- for (var d = [], f = t.length - 1; f >= 0; f = f - 2) {
405
- var m = f === 0 ? t[f] : t[f - 1] + t[f];
406
- d.unshift(parseInt(m, 16));
407
- }
408
- return d;
409
- },
410
- /**
411
- * Translates number of ticks to MIDI timestamp format, returning an array
412
- * of bytes with the time values. MIDI has a very particular way to express
413
- * time; take a good look at the spec before ever touching this function.
414
- *
415
- * @param {number} ticks - Number of ticks to be translated.
416
- * @returns {number} Array of bytes that form the MIDI time value.
417
- */
418
- translateTickTime: function(t) {
419
- for (var n = t & 127; t = t >> 7; )
420
- n <<= 8, n |= t & 127 | 128;
421
- for (var d = []; d.push(n & 255), n & 128; )
422
- n >>= 8;
423
- return d;
424
- }
425
- }, l = function(t) {
426
- if (!this) return new l(t);
427
- t && (t.type !== null || t.type !== void 0) && (t.channel !== null || t.channel !== void 0) && (t.param1 !== null || t.param1 !== void 0) && (this.setTime(t.time), this.setType(t.type), this.setChannel(t.channel), this.setParam1(t.param1), this.setParam2(t.param2));
428
- };
429
- l.NOTE_OFF = 128, l.NOTE_ON = 144, l.AFTER_TOUCH = 160, l.CONTROLLER = 176, l.PROGRAM_CHANGE = 192, l.CHANNEL_AFTERTOUCH = 208, l.PITCH_BEND = 224, l.prototype.setTime = function(t) {
430
- this.time = i.translateTickTime(t || 0);
431
- }, l.prototype.setType = function(t) {
432
- if (t < l.NOTE_OFF || t > l.PITCH_BEND)
433
- throw new Error("Trying to set an unknown event: " + t);
434
- this.type = t;
435
- }, l.prototype.setChannel = function(t) {
436
- if (t < 0 || t > 15)
437
- throw new Error("Channel is out of bounds.");
438
- this.channel = t;
439
- }, l.prototype.setParam1 = function(t) {
440
- this.param1 = t;
441
- }, l.prototype.setParam2 = function(t) {
442
- this.param2 = t;
443
- }, l.prototype.toBytes = function() {
444
- var t = [], n = this.type | this.channel & 15;
445
- return t.push.apply(t, this.time), t.push(n), t.push(this.param1), this.param2 !== void 0 && this.param2 !== null && t.push(this.param2), t;
446
- };
447
- var s = function(t) {
448
- if (!this) return new s(t);
449
- this.setTime(t.time), this.setType(t.type), this.setData(t.data);
450
- };
451
- s.SEQUENCE = 0, s.TEXT = 1, s.COPYRIGHT = 2, s.TRACK_NAME = 3, s.INSTRUMENT = 4, s.LYRIC = 5, s.MARKER = 6, s.CUE_POINT = 7, s.CHANNEL_PREFIX = 32, s.END_OF_TRACK = 47, s.TEMPO = 81, s.SMPTE = 84, s.TIME_SIG = 88, s.KEY_SIG = 89, s.SEQ_EVENT = 127, s.prototype.setTime = function(t) {
452
- this.time = i.translateTickTime(t || 0);
453
- }, s.prototype.setType = function(t) {
454
- this.type = t;
455
- }, s.prototype.setData = function(t) {
456
- this.data = t;
457
- }, s.prototype.toBytes = function() {
458
- if (!this.type)
459
- throw new Error("Type for meta-event not specified.");
460
- var t = [];
461
- if (t.push.apply(t, this.time), t.push(255, this.type), Array.isArray(this.data))
462
- t.push(this.data.length), t.push.apply(t, this.data);
463
- else if (typeof this.data == "number")
464
- t.push(1, this.data);
465
- else if (this.data !== null && this.data !== void 0) {
466
- t.push(this.data.length);
467
- var n = this.data.split("").map(function(d) {
468
- return d.charCodeAt(0);
469
- });
470
- t.push.apply(t, n);
471
- } else
472
- t.push(0);
473
- return t;
474
- };
475
- var c = function(t) {
476
- if (!this) return new c(t);
477
- var n = t || {};
478
- this.events = n.events || [];
479
- };
480
- c.START_BYTES = [77, 84, 114, 107], c.END_BYTES = [0, 255, 47, 0], c.prototype.addEvent = function(t) {
481
- return this.events.push(t), this;
482
- }, c.prototype.addNoteOn = c.prototype.noteOn = function(t, n, d, f) {
483
- return this.events.push(new l({
484
- type: l.NOTE_ON,
485
- channel: t,
486
- param1: i.ensureMidiPitch(n),
487
- param2: f || o,
488
- time: d || 0
489
- })), this;
490
- }, c.prototype.addNoteOff = c.prototype.noteOff = function(t, n, d, f) {
491
- return this.events.push(new l({
492
- type: l.NOTE_OFF,
493
- channel: t,
494
- param1: i.ensureMidiPitch(n),
495
- param2: f || o,
496
- time: d || 0
497
- })), this;
498
- }, c.prototype.addNote = c.prototype.note = function(t, n, d, f, m) {
499
- return this.noteOn(t, n, f, m), d && this.noteOff(t, n, d, m), this;
500
- }, c.prototype.addChord = c.prototype.chord = function(t, n, d, f) {
501
- if (!Array.isArray(n) && !n.length)
502
- throw new Error("Chord must be an array of pitches");
503
- return n.forEach(function(m) {
504
- this.noteOn(t, m, 0, f);
505
- }, this), n.forEach(function(m, p) {
506
- p === 0 ? this.noteOff(t, m, d) : this.noteOff(t, m);
507
- }, this), this;
508
- }, c.prototype.setInstrument = c.prototype.instrument = function(t, n, d) {
509
- return this.events.push(new l({
510
- type: l.PROGRAM_CHANGE,
511
- channel: t,
512
- param1: n,
513
- time: d || 0
514
- })), this;
515
- }, c.prototype.setTempo = c.prototype.tempo = function(t, n) {
516
- return this.events.push(new s({
517
- type: s.TEMPO,
518
- data: i.mpqnFromBpm(t),
519
- time: n || 0
520
- })), this;
521
- }, c.prototype.toBytes = function() {
522
- var t = 0, n = [], d = c.START_BYTES, f = c.END_BYTES, m = function(b) {
523
- var v = b.toBytes();
524
- t += v.length, n.push.apply(n, v);
525
- };
526
- this.events.forEach(m), t += f.length;
527
- var p = i.str2Bytes(t.toString(16), 4);
528
- return d.concat(p, n, f);
529
- };
530
- var u = function(t) {
531
- if (!this) return new u(t);
532
- var n = t || {};
533
- if (n.ticks) {
534
- if (typeof n.ticks != "number")
535
- throw new Error("Ticks per beat must be a number!");
536
- if (n.ticks <= 0 || n.ticks >= 32768 || n.ticks % 1 !== 0)
537
- throw new Error("Ticks per beat must be an integer between 1 and 32767!");
538
- }
539
- this.ticks = n.ticks || 128, this.tracks = n.tracks || [];
540
- };
541
- u.HDR_CHUNKID = "MThd", u.HDR_CHUNK_SIZE = "\0\0\0", u.HDR_TYPE0 = "\0\0", u.HDR_TYPE1 = "\0", u.prototype.addTrack = function(t) {
542
- return t ? (this.tracks.push(t), this) : (t = new c(), this.tracks.push(t), t);
543
- }, u.prototype.toBytes = function() {
544
- var t = this.tracks.length.toString(16), n = u.HDR_CHUNKID + u.HDR_CHUNK_SIZE;
545
- return parseInt(t, 16) > 1 ? n += u.HDR_TYPE1 : n += u.HDR_TYPE0, n += i.codes2Str(i.str2Bytes(t, 2)), n += String.fromCharCode(this.ticks / 256, this.ticks % 256), this.tracks.forEach(function(d) {
546
- n += i.codes2Str(d.toBytes());
547
- }), n;
548
- }, h.Util = i, h.File = u, h.Track = c, h.Event = l, h.MetaEvent = s;
549
- })(a), e !== null ? e.exports = a : r !== null ? r = a : l0.Midi = a;
550
- }(I, I.exports)), I.exports;
551
- }
552
- var G = h0();
553
- const b0 = (e, r = "music.mid", a) => {
554
- const o = f0(e, a).toBytes();
555
- if (r === null)
556
- return o;
557
- if (r.endsWith(".mid") || (r = r + ".mid"), typeof window < "u" && window.URL && window.URL.createObjectURL)
558
- return d0(o, r);
559
- c0.writeFileSync(r, o, "binary"), console.log(`MIDI file generated: ${r}.`);
560
- }, d0 = (e, r) => {
561
- const a = new Uint8Array(e.length);
562
- for (let i = 0; i < e.length; i++) {
563
- const l = e.charCodeAt(i);
564
- a[i] = l;
565
- }
566
- const h = new Blob([a], { type: "audio/midi" }), o = document.createElement("a");
567
- return o.href = typeof window < "u" && typeof window.URL < "u" && typeof window.URL.createObjectURL < "u" && window.URL.createObjectURL(h) || "", o.download = r, o.innerText = "Download MIDI file", o;
568
- }, f0 = (e, r) => {
569
- const a = new G.File(), h = new G.Track();
570
- typeof r == "number" && h.setTempo(r), a.addTrack(h);
571
- for (const o of e) {
572
- const i = o.level || 127;
573
- o.note ? typeof o.note == "string" ? (h.noteOn(0, o.note, o.length, i), h.noteOff(0, o.note, o.length, i)) : h.addChord(0, o.note, o.length, i) : h.noteOff(0, "", o.length);
574
- }
575
- return a;
576
- }, g0 = g.chord, E0 = g.chords, w0 = g.scale, T0 = g.scales;
577
- export {
578
- v0 as arp,
579
- g0 as chord,
580
- E0 as chords,
581
- u0 as clip,
582
- p0 as getChordDegrees,
583
- m0 as getChordsByProgression,
584
- b0 as midi,
585
- y0 as progression,
586
- w0 as scale,
587
- T0 as scales
588
- };