midi-audio-player 2.0.0 → 2.0.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/dist/index.js +35 -37
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +4 -4
- package/dist/index.mjs.map +3 -3
- package/dist/midi-audio-player.js +35 -37
- package/dist/midi-audio-player.min.js +4 -4
- package/package.json +12 -5
- package/src/midiaudioplayer.js +3 -4
- package/CHANGELOG.md +0 -26
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
██║ ╚═╝ ██║██║██████╔╝██║██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝██║ ███████╗██║ ██║ ██║ ███████╗██║ ██║
|
|
8
8
|
╚═╝ ╚═╝╚═╝╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
|
|
9
9
|
|
|
10
|
-
Version: 2.0.
|
|
11
|
-
Build: 2026-05-
|
|
10
|
+
Version: 2.0.1
|
|
11
|
+
Build: 2026-05-31 02:44:14
|
|
12
12
|
Author: Maxime Larrivée-Roy <mlarriveeroy@gmail.com>
|
|
13
13
|
Github: https://github.com/webaudiofonts/midi-audio-player/
|
|
14
|
-
Website: https://webaudiofonts.
|
|
14
|
+
Website: https://webaudiofonts.com/midiaudioplayer/
|
|
15
15
|
|
|
16
16
|
*/
|
|
17
17
|
(() => {
|
|
@@ -1213,7 +1213,7 @@
|
|
|
1213
1213
|
};
|
|
1214
1214
|
|
|
1215
1215
|
// node_modules/webaudiofontplayer/dist/index.js
|
|
1216
|
-
var WebAudioFontPlayer = class {
|
|
1216
|
+
var WebAudioFontPlayer = class _WebAudioFontPlayer {
|
|
1217
1217
|
#audioCtx = null;
|
|
1218
1218
|
#compressor = null;
|
|
1219
1219
|
#preset = null;
|
|
@@ -1228,24 +1228,33 @@
|
|
|
1228
1228
|
#sustain = false;
|
|
1229
1229
|
#pitchBendValue = 8192;
|
|
1230
1230
|
#notesWaitingForSustain = /* @__PURE__ */ new Set();
|
|
1231
|
-
constructor(preset, audioCtx, compressor = null) {
|
|
1231
|
+
constructor(preset, audioCtx, compressor = null, callback = null) {
|
|
1232
1232
|
this.#audioCtx = audioCtx;
|
|
1233
1233
|
this.#compressor = compressor;
|
|
1234
|
-
this.#preset = preset;
|
|
1235
1234
|
this.#mainGain = this.#audioCtx.createGain();
|
|
1236
1235
|
this.#mainGain.gain.setValueAtTime(this.#volumeValue, this.#audioCtx.currentTime);
|
|
1237
1236
|
this.#expressionGain = this.#audioCtx.createGain();
|
|
1238
1237
|
this.#expressionGain.gain.setValueAtTime(this.#expressionValue, this.#audioCtx.currentTime);
|
|
1239
1238
|
this.#mainGain.connect(this.#expressionGain);
|
|
1240
1239
|
this.#expressionGain.connect(this.#compressor ? this.#compressor.input : this.#audioCtx.destination);
|
|
1241
|
-
this
|
|
1240
|
+
this.setPreset(preset).then(() => {
|
|
1241
|
+
if (typeof callback === "function") callback();
|
|
1242
|
+
});
|
|
1242
1243
|
}
|
|
1243
1244
|
get preset() {
|
|
1244
1245
|
return this.#preset;
|
|
1245
1246
|
}
|
|
1246
1247
|
set preset(preset) {
|
|
1248
|
+
this.setPreset(preset);
|
|
1249
|
+
}
|
|
1250
|
+
static load(preset, audioCtx, compressor = null) {
|
|
1251
|
+
return new Promise((resolve) => {
|
|
1252
|
+
const player = new _WebAudioFontPlayer(preset, audioCtx, compressor, () => resolve(player));
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
async setPreset(preset) {
|
|
1247
1256
|
this.#preset = preset;
|
|
1248
|
-
this.#preset.zones.map((zone) => this.#adjustZone(zone));
|
|
1257
|
+
await Promise.all(this.#preset.zones.map((zone) => this.#adjustZone(zone)));
|
|
1249
1258
|
}
|
|
1250
1259
|
close() {
|
|
1251
1260
|
const now = this.#audioCtx.currentTime;
|
|
@@ -1380,30 +1389,24 @@
|
|
|
1380
1389
|
break;
|
|
1381
1390
|
}
|
|
1382
1391
|
}
|
|
1383
|
-
#adjustZone(zone) {
|
|
1384
|
-
if (zone.buffer) return
|
|
1392
|
+
async #adjustZone(zone) {
|
|
1393
|
+
if (zone.buffer) return zone;
|
|
1385
1394
|
zone.delay = 0;
|
|
1386
1395
|
if (zone.file) {
|
|
1387
|
-
const
|
|
1388
|
-
const
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
(error) => {
|
|
1398
|
-
console.error("Audio decoding error:", error);
|
|
1399
|
-
console.warn(this.#preset);
|
|
1400
|
-
return false;
|
|
1401
|
-
}
|
|
1402
|
-
);
|
|
1396
|
+
const binary = atob(zone.file);
|
|
1397
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
1398
|
+
try {
|
|
1399
|
+
zone.buffer = await this.#audioCtx.decodeAudioData(bytes.buffer);
|
|
1400
|
+
this.#applyZoneParameters(zone);
|
|
1401
|
+
} catch (error) {
|
|
1402
|
+
console.error("Audio decoding error:", error);
|
|
1403
|
+
console.warn(this.#preset);
|
|
1404
|
+
return false;
|
|
1405
|
+
}
|
|
1403
1406
|
} else {
|
|
1404
1407
|
this.#applyZoneParameters(zone);
|
|
1405
|
-
return zone;
|
|
1406
1408
|
}
|
|
1409
|
+
return zone;
|
|
1407
1410
|
}
|
|
1408
1411
|
#applyZoneParameters(zone) {
|
|
1409
1412
|
zone.loopStart = this.#numValue(zone.loopStart, 0);
|
|
@@ -1495,9 +1498,7 @@
|
|
|
1495
1498
|
return envelope;
|
|
1496
1499
|
}
|
|
1497
1500
|
#findZone(pitch) {
|
|
1498
|
-
|
|
1499
|
-
if (zone) this.#adjustZone(zone);
|
|
1500
|
-
return zone;
|
|
1501
|
+
return this.#preset.zones.findLast((z) => pitch >= z.keyRangeLow && pitch <= z.keyRangeHigh);
|
|
1501
1502
|
}
|
|
1502
1503
|
#limitVolume(v) {
|
|
1503
1504
|
const requestedVolume = v ? 1 * v : 0.5;
|
|
@@ -1963,7 +1964,7 @@
|
|
|
1963
1964
|
if (!presetInfo) throw new Error("Invalid preset: ".concat(presetId));
|
|
1964
1965
|
this.#presetMap[presetInfo.program] = presetInfo;
|
|
1965
1966
|
const preset = await this.getPreset(presetId);
|
|
1966
|
-
this.#players[channel].preset
|
|
1967
|
+
await this.#players[channel].setPreset(preset);
|
|
1967
1968
|
this.#setupChange();
|
|
1968
1969
|
}
|
|
1969
1970
|
async load(content, setup) {
|
|
@@ -2414,12 +2415,9 @@
|
|
|
2414
2415
|
this.#players[channel].setPitchBend?.(event.value);
|
|
2415
2416
|
break;
|
|
2416
2417
|
case "Program Change":
|
|
2417
|
-
if (
|
|
2418
|
-
// (this.#opts.presetAuto || this.#opts.presetRandom) &&
|
|
2419
|
-
event.value >= 0 && event.value <= 127 && this.#instruments[event.value + 1] !== void 0 && event.channel != 10
|
|
2420
|
-
) {
|
|
2418
|
+
if (event.value >= 0 && event.value <= 127 && this.#instruments[event.value + 1] !== void 0 && event.channel != 10) {
|
|
2421
2419
|
if (this.#players[channel].preset?.program !== event.value + 1) {
|
|
2422
|
-
this.#players[channel].
|
|
2420
|
+
this.#players[channel].setPreset(this.#instruments[event.value + 1]);
|
|
2423
2421
|
}
|
|
2424
2422
|
}
|
|
2425
2423
|
break;
|
|
@@ -2525,7 +2523,7 @@
|
|
|
2525
2523
|
if (!this.#players[event.channel]) return;
|
|
2526
2524
|
if (event.channel == 10 || event.value > 127 || event.value < 0) break;
|
|
2527
2525
|
if (this.#players[event.channel] !== void 0 && this.#players[event.channel].preset.program != event.value + 1)
|
|
2528
|
-
this.#players[event.channel].
|
|
2526
|
+
this.#players[event.channel].setPreset(this.#instruments[event.value + 1]);
|
|
2529
2527
|
break;
|
|
2530
2528
|
case "Karaoke Event":
|
|
2531
2529
|
if (event.tick < this.tick - this.secondsToTicks(10)) return;
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
██║ ╚═╝ ██║██║██████╔╝██║██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝██║ ███████╗██║ ██║ ██║ ███████╗██║ ██║
|
|
8
8
|
╚═╝ ╚═╝╚═╝╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
|
|
9
9
|
|
|
10
|
-
Version: 2.0.
|
|
11
|
-
Build: 2026-05-
|
|
10
|
+
Version: 2.0.1
|
|
11
|
+
Build: 2026-05-31 02:44:14
|
|
12
12
|
Author: Maxime Larrivée-Roy <mlarriveeroy@gmail.com>
|
|
13
13
|
Github: https://github.com/webaudiofonts/midi-audio-player/
|
|
14
|
-
Website: https://webaudiofonts.
|
|
14
|
+
Website: https://webaudiofonts.com/midiaudioplayer/
|
|
15
15
|
|
|
16
16
|
*/
|
|
17
|
-
(()=>{function B(r,s){if(!(r instanceof s))throw new TypeError("Cannot call a class as a function")}function j(r,s){for(var t=0;t<s.length;t++){var e=s[t];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(r,e.key,e)}}function V(r,s,t){return s&&j(r.prototype,s),t&&j(r,t),Object.defineProperty(r,"prototype",{writable:!1}),r}var A={VERSION:"2.0.17",NOTES:[],HEADER_CHUNK_LENGTH:14,CIRCLE_OF_FOURTHS:["C","F","Bb","Eb","Ab","Db","Gb","Cb","Fb","Bbb","Ebb","Abb"],CIRCLE_OF_FIFTHS:["C","G","D","A","E","B","F#","C#","G#","D#","A#","E#"]},q=[["C"],["C#","Db"],["D"],["D#","Eb"],["E"],["F"],["F#","Gb"],["G"],["G#","Ab"],["A"],["A#","Bb"],["B"]],H=0,W=function(s){q.forEach(function(t){t.forEach(function(e){return A.NOTES[H]=e+s}),H++})};for(R=-1;R<=9;R++)W(R);var R,w=(function(){function r(){B(this,r)}return V(r,null,[{key:"byteToHex",value:function(t){return("0"+t.toString(16)).slice(-2)}},{key:"bytesToHex",value:function(t){var e=[];return t.forEach(function(i){return e.push(r.byteToHex(i))}),e.join("")}},{key:"hexToNumber",value:function(t){return parseInt(t,16)}},{key:"bytesToNumber",value:function(t){return r.hexToNumber(r.bytesToHex(t))}},{key:"bytesToLetters",value:function(t){var e=[];return t.forEach(function(i){return e.push(String.fromCharCode(i))}),e.join("")}},{key:"decToBinary",value:function(t){return(t>>>0).toString(2)}},{key:"getVarIntLength",value:function(t){for(var e=t[0],i=1;e>=128;)e=t[i],i++;return i}},{key:"readVarInt",value:function(t){var e=0;return t.forEach(function(i){var a=i;a&128?(e+=a&127,e<<=7):e+=a}),e}},{key:"atob",value:(function(s){function t(e){return s.apply(this,arguments)}return t.toString=function(){return s.toString()},t})(function(s){return typeof atob=="function"?atob(s):Buffer.from(s,"base64").toString("binary")})}]),r})(),G=(function(){function r(s,t){B(this,r),this.enabled=!0,this.eventIndex=0,this.pointer=0,this.lastTick=0,this.lastStatus=null,this.index=s,this.data=t,this.delta=0,this.runningDelta=0,this.events=[];var e=this.data.subarray(this.data.length-3,this.data.length);if(!(e[0]===255&&e[1]===47&&e[2]===0))throw"Invalid MIDI file; Last three bytes of track "+this.index+"must be FF 2F 00 to mark end of track"}return V(r,[{key:"reset",value:function(){return this.enabled=!0,this.eventIndex=0,this.pointer=0,this.lastTick=0,this.lastStatus=null,this.delta=0,this.runningDelta=0,this}},{key:"enable",value:function(){return this.enabled=!0,this}},{key:"disable",value:function(){return this.enabled=!1,this}},{key:"setEventIndexByTick",value:function(t){t=t||0;for(var e=0;e<this.events.length;e++)if(this.events[e].tick>=t)return this.eventIndex=e,this}},{key:"getCurrentByte",value:function(){return this.data[this.pointer]}},{key:"getDeltaByteCount",value:function(){return w.getVarIntLength(this.data.subarray(this.pointer))}},{key:"getDelta",value:function(){return w.readVarInt(this.data.subarray(this.pointer,this.pointer+this.getDeltaByteCount()))}},{key:"handleEvent",value:function(t,e){if(e=e||!1,e){var i=t-this.lastTick,a=this.getDelta(),n=i>=a;if(this.pointer<this.data.length&&(e||n)){var o=this.parseEvent();if(this.enabled)return o}}else{for(var h=[];this.events[this.eventIndex]&&this.events[this.eventIndex].tick<=t;)this.enabled&&h.push(this.events[this.eventIndex]),this.eventIndex++;if(h.length>0)return h}return null}},{key:"getStringData",value:function(t){var e=w.getVarIntLength(this.data.subarray(t+2)),i=w.readVarInt(this.data.subarray(t+2,t+2+e)),a=w.bytesToLetters(this.data.subarray(t+2+e,t+2+e+i));return a}},{key:"parseEvent",value:function(){var t=this.pointer+this.getDeltaByteCount(),e={},i=this.getDeltaByteCount();if(e.track=this.index+1,e.delta=this.getDelta(),this.lastTick=this.lastTick+e.delta,this.runningDelta+=e.delta,e.tick=this.runningDelta,e.byteIndex=this.pointer,this.data[t]==255){switch(this.data[t+1]){case 0:e.name="Sequence Number";break;case 1:e.name="Text Event",e.string=this.getStringData(t);break;case 2:e.name="Copyright Notice";break;case 3:e.name="Sequence/Track Name",e.string=this.getStringData(t);break;case 4:e.name="Instrument Name",e.string=this.getStringData(t);break;case 5:e.name="Lyric",e.string=this.getStringData(t);break;case 6:e.name="Marker",e.string=this.getStringData(t);break;case 7:e.name="Cue Point",e.string=this.getStringData(t);break;case 9:e.name="Device Name",e.string=this.getStringData(t);break;case 32:e.name="MIDI Channel Prefix";break;case 33:e.name="MIDI Port",e.data=w.bytesToNumber([this.data[t+3]]);break;case 47:e.name="End of Track";break;case 81:e.name="Set Tempo",e.data=Math.round(6e7/w.bytesToNumber(this.data.subarray(t+3,t+6))),this.tempo=e.data;break;case 84:e.name="SMTPE Offset";break;case 88:e.name="Time Signature",e.data=this.data.subarray(t+3,t+7),e.timeSignature=""+e.data[0]+"/"+Math.pow(2,e.data[1]);break;case 89:e.name="Key Signature",e.data=this.data.subarray(t+3,t+5);var a=e.data[0]>127?e.data[0]-256:e.data[0];a>=0?e.keySignature=A.CIRCLE_OF_FIFTHS[a]:e.keySignature=A.CIRCLE_OF_FOURTHS[Math.abs(a)],e.data[1]==0?e.keySignature+=" Major":e.data[1]==1&&(e.keySignature+=" Minor");break;case 127:e.name="Sequencer-Specific Meta-event";break;default:e.name="Unknown: "+this.data[t+1].toString(16);break}var n=w.getVarIntLength(this.data.subarray(t+2)),o=w.readVarInt(this.data.subarray(t+2,t+2+n));this.pointer+=i+2+n+o}else if(this.data[t]===240){e.name="Sysex";var h=w.getVarIntLength(this.data.subarray(t+1)),l=w.readVarInt(this.data.subarray(t+1,t+1+h));e.data=this.data.subarray(t+1+h,t+1+h+l),this.pointer+=i+1+h+l}else if(this.data[t]===247){e.name="Sysex (escape)";var c=w.getVarIntLength(this.data.subarray(t+1)),u=w.readVarInt(this.data.subarray(t+1,t+1+c));e.data=this.data.subarray(t+1+c,t+1+c+u),this.pointer+=i+1+c+u}else if(this.data[t]<128)if(e.running=!0,e.noteNumber=this.data[t],e.noteName=A.NOTES[this.data[t]],e.velocity=this.data[t+1],this.lastStatus<=143)e.name="Note off",e.channel=this.lastStatus-128+1,this.pointer+=i+2;else if(this.lastStatus<=159)e.name="Note on",e.channel=this.lastStatus-144+1,this.pointer+=i+2;else if(this.lastStatus<=175)e.name="Polyphonic Key Pressure",e.channel=this.lastStatus-160+1,e.note=A.NOTES[this.data[t]],e.pressure=this.data[t+1],this.pointer+=i+2;else if(this.lastStatus<=191)e.name="Controller Change",e.channel=this.lastStatus-176+1,e.number=this.data[t],e.value=this.data[t+1],this.pointer+=i+2;else if(this.lastStatus<=207)e.name="Program Change",e.channel=this.lastStatus-192+1,e.value=this.data[t+1],this.pointer+=i+1;else if(this.lastStatus<=223)e.name="Channel Key Pressure",e.channel=this.lastStatus-208+1,this.pointer+=i+1;else if(this.lastStatus<=239)e.name="Pitch Bend",e.channel=this.lastStatus-224+1,e.value=(this.data[t+1]&127)<<7|this.data[t]&127,this.pointer+=i+2;else throw"Unknown event (running): ".concat(this.lastStatus);else if(this.lastStatus=this.data[t],this.data[t]<=143)e.name="Note off",e.channel=this.lastStatus-128+1,e.noteNumber=this.data[t+1],e.noteName=A.NOTES[this.data[t+1]],e.velocity=Math.round(this.data[t+2]/127*100),this.pointer+=i+3;else if(this.data[t]<=159)e.name="Note on",e.channel=this.lastStatus-144+1,e.noteNumber=this.data[t+1],e.noteName=A.NOTES[this.data[t+1]],e.velocity=Math.round(this.data[t+2]/127*100),this.pointer+=i+3;else if(this.data[t]<=175)e.name="Polyphonic Key Pressure",e.channel=this.lastStatus-160+1,e.note=A.NOTES[this.data[t+1]],e.pressure=this.data[t+2],this.pointer+=i+3;else if(this.data[t]<=191)e.name="Controller Change",e.channel=this.lastStatus-176+1,e.number=this.data[t+1],e.value=this.data[t+2],this.pointer+=i+3;else if(this.data[t]<=207)e.name="Program Change",e.channel=this.lastStatus-192+1,e.value=this.data[t+1],this.pointer+=i+2;else if(this.data[t]<=223)e.name="Channel Key Pressure",e.channel=this.lastStatus-208+1,this.pointer+=i+2;else if(this.data[t]<=239)e.name="Pitch Bend",e.channel=this.lastStatus-224+1,e.value=(this.data[t+2]&127)<<7|this.data[t+1]&127,this.pointer+=i+3;else throw"Unknown event: ".concat(this.data[t]);return this.delta+=e.delta,this.events.push(e),e}},{key:"endOfTrack",value:function(){return this.data[this.pointer+1]==255&&this.data[this.pointer+2]==47&&this.data[this.pointer+3]==0}}]),r})();Uint8Array.prototype.forEach||Object.defineProperty(Uint8Array.prototype,"forEach",{value:Array.prototype.forEach});var Q=(function(){function r(s,t){B(this,r),this.sampleRate=5,this.startTime=0,this.buffer=t||null,this.midiChunksByteLength=null,this.division,this.format,this.setTimeoutId=!1,this.scheduledTime=0,this.tracks=[],this.instruments=[],this.defaultTempo=120,this.tempo=null,this.startTick=0,this.tick=0,this.lastTick=null,this.inLoop=!1,this.totalTicks=0,this.events=[],this.totalEvents=0,this.tempoMap=[],this.eventListeners={},typeof s=="function"&&this.on("midiEvent",s)}return V(r,[{key:"loadFile",value:function(t){throw"loadFile is only supported on Node.js"}},{key:"loadArrayBuffer",value:function(t){return this.buffer=new Uint8Array(t),this.fileLoaded()}},{key:"loadDataUri",value:function(t){for(var e=w.atob(t.split(",")[1]),i=new Uint8Array(e.length),a=0;a<e.length;a++)i[a]=e.charCodeAt(a);return this.buffer=i,this.fileLoaded()}},{key:"getFilesize",value:function(){return this.buffer?this.buffer.length:0}},{key:"fileLoaded",value:function(){if(!this.validate())throw"Invalid MIDI file; should start with MThd";return this.defaultTempo=120,this.setTempo(this.defaultTempo).getDivision().getFormat().getTracks().dryRun()}},{key:"validate",value:function(){return w.bytesToLetters(this.buffer.subarray(0,4))==="MThd"}},{key:"getFormat",value:function(){return this.format=w.bytesToNumber(this.buffer.subarray(8,10)),this}},{key:"getTracks",value:function(){this.tracks=[];for(var t=0;t<this.buffer.length;){if(w.bytesToLetters(this.buffer.subarray(t,t+4))=="MTrk"){var e=w.bytesToNumber(this.buffer.subarray(t+4,t+8));this.tracks.push(new G(this.tracks.length,this.buffer.subarray(t+8,t+8+e)))}t+=w.bytesToNumber(this.buffer.subarray(t+4,t+8))+8}var i=0;return this.tracks.forEach(function(a){i+=8+a.data.length}),this.midiChunksByteLength=A.HEADER_CHUNK_LENGTH+i,this}},{key:"enableTrack",value:function(t){return this.tracks[t-1].enable(),this}},{key:"disableTrack",value:function(t){return this.tracks[t-1].disable(),this}},{key:"getDivision",value:function(){return this.division=w.bytesToNumber(this.buffer.subarray(12,A.HEADER_CHUNK_LENGTH)),this}},{key:"playLoop",value:function(t){this.inLoop||(this.inLoop=!0,this.tick=this.getCurrentTick(),this.tracks.forEach(function(e,i){if(!t&&this.endOfFile())this.stop(),this.triggerPlayerEvent("endOfFile");else{var a=e.handleEvent(this.tick,t);if(t&&a)a.hasOwnProperty("name")&&a.name==="Set Tempo"&&this.setTempo(a.data),a.hasOwnProperty("name")&&a.name==="Program Change"&&(this.instruments.includes(a.value)||this.instruments.push(a.value));else if(a){var n=Array.isArray(a)?a:[a];n.forEach(function(o){o.hasOwnProperty("name")&&o.name==="Set Tempo"&&this.setTempo(o.data),this.emitEvent(o)},this)}}},this),!t&&this.isPlaying()&&this.triggerPlayerEvent("playing",{tick:this.tick}),this.inLoop=!1)}},{key:"setTempo",value:function(t){return this.tempo=t,this}},{key:"setStartTime",value:function(t){return this.startTime=t,this}},{key:"play",value:function(){if(this.isPlaying())throw"Already playing...";return this.startTime||(this.startTime=new Date().getTime()),this.scheduledTime=Date.now(),this.schedulePlayLoop(this.sampleRate),this}},{key:"schedulePlayLoop",value:function(t){var e=this;this.setTimeoutId=setTimeout(function(){if(e.playLoop(),e.setTimeoutId!==!1){e.scheduledTime+=e.sampleRate;var i=Date.now()-e.scheduledTime;e.schedulePlayLoop(Math.max(0,e.sampleRate-i))}},t)}},{key:"pause",value:function(){return clearTimeout(this.setTimeoutId),this.setTimeoutId=!1,this.scheduledTime=0,this.startTick=this.tick,this.startTime=0,this}},{key:"stop",value:function(){return clearTimeout(this.setTimeoutId),this.setTimeoutId=!1,this.scheduledTime=0,this.startTick=0,this.startTime=0,this.resetTracks(),this}},{key:"skipToTick",value:function(t){this.stop(),this.startTick=t;for(var e=this.tempoMap.length-1;e>=0;e--)if(this.tempoMap[e].tick<=t){this.setTempo(this.tempoMap[e].tempo);break}return this.collectStateAtTick(t).forEach(function(i){this.emitEvent(i)},this),this.tracks.forEach(function(i){i.setEventIndexByTick(t)}),this}},{key:"collectStateAtTick",value:function(t){var e={};return this.events.forEach(function(i){i.forEach(function(a){if(!(a.tick>=t)){var n;a.name==="Program Change"?n="pc:"+a.channel:a.name==="Controller Change"?n="cc:"+a.channel+":"+a.number:a.name==="Pitch Bend"&&(n="pb:"+a.channel),n&&(e[n]=a)}})}),Object.keys(e).map(function(i){return e[i]})}},{key:"skipToPercent",value:function(t){if(t<0||t>100)throw"Percent must be number between 1 and 100.";return this.skipToTick(Math.round(t/100*this.totalTicks)),this}},{key:"skipToSeconds",value:function(t){var e=this.getSongTime();if(t<0||t>e)throw t+" seconds not within song time of "+e;return this.skipToTick(this.secondsToTicks(t)),this}},{key:"isPlaying",value:function(){return this.setTimeoutId!==!1}},{key:"dryRun",value:function(){for(this.resetTracks();!this.endOfFile();)this.playLoop(!0);return this.events=this.getEvents(),this.totalEvents=this.getTotalEvents(),this.totalTicks=this.getTotalTicks(),this.buildTempoMap(),this.startTick=0,this.startTime=0,this.resetTracks(),this.triggerPlayerEvent("fileLoaded",this),this}},{key:"resetTracks",value:function(){return this.tracks.forEach(function(t){return t.reset()}),this}},{key:"getEvents",value:function(){return this.tracks.map(function(t){return t.events})}},{key:"getTotalTicks",value:function(){return Math.max.apply(null,this.tracks.map(function(t){return t.delta}))}},{key:"getTotalEvents",value:function(){return this.tracks.reduce(function(t,e){return{events:{length:t.events.length+e.events.length}}},{events:{length:0}}).events.length}},{key:"buildTempoMap",value:function(){var t=[];return this.events.forEach(function(e){e.forEach(function(i){i.name==="Set Tempo"&&t.push({tick:i.tick,tempo:i.data})})}),t.sort(function(e,i){return e.tick-i.tick}),this.tempoMap=[{tick:0,tempo:this.defaultTempo}],t.forEach(function(e){var i=this.tempoMap[this.tempoMap.length-1];e.tick===i.tick?i.tempo=e.tempo:this.tempoMap.push({tick:e.tick,tempo:e.tempo})},this),this}},{key:"ticksToSeconds",value:function(t,e){for(var i=0,a=t,n=0;n<this.tempoMap.length;n++){var o=this.tempoMap[n],h=n+1<this.tempoMap.length?this.tempoMap[n+1].tick:e;if(!(h<=t)){var l=Math.max(o.tick,t),c=Math.min(h,e);if(l>=e)break;var u=c-l;i+=u/this.division/o.tempo*60,a=c}}if(a<e){var m=this.tempoMap[this.tempoMap.length-1];i+=(e-a)/this.division/m.tempo*60}return i}},{key:"secondsToTicks",value:function(t){for(var e=t,i=0,a=0;a<this.tempoMap.length;a++){var n=this.tempoMap[a],o=a+1<this.tempoMap.length?this.tempoMap[a+1].tick:1/0,h=o-n.tick,l=h/this.division/n.tempo*60;if(e<=l)return i=n.tick+Math.round(e/60*n.tempo*this.division),i;e-=l,i=o}return this.totalTicks}},{key:"getSongTime",value:function(){return this.ticksToSeconds(0,this.totalTicks)}},{key:"getSongTimeRemaining",value:function(){return Math.round(this.ticksToSeconds(this.getCurrentTick(),this.totalTicks))}},{key:"getSongPercentRemaining",value:function(){return Math.round(this.getSongTimeRemaining()/this.getSongTime()*100)}},{key:"bytesProcessed",value:function(){return A.HEADER_CHUNK_LENGTH+this.tracks.length*8+this.tracks.reduce(function(t,e){return{pointer:t.pointer+e.pointer}},{pointer:0}).pointer}},{key:"eventsPlayed",value:function(){return this.tracks.reduce(function(t,e){return{eventIndex:t.eventIndex+e.eventIndex}},{eventIndex:0}).eventIndex}},{key:"endOfFile",value:function(){return this.isPlaying()?this.totalTicks-this.tick<=0:this.bytesProcessed()>=this.midiChunksByteLength}},{key:"getCurrentTick",value:function(){if(!this.startTime)return this.startTick;var t=(new Date().getTime()-this.startTime)/1e3,e=this.ticksToSeconds(0,this.startTick);return this.secondsToTicks(e+t)}},{key:"emitEvent",value:function(t){return this.triggerPlayerEvent("midiEvent",t),this}},{key:"on",value:function(t,e){return this.eventListeners.hasOwnProperty(t)||(this.eventListeners[t]=[]),this.eventListeners[t].push(e),this}},{key:"triggerPlayerEvent",value:function(t,e){return this.eventListeners.hasOwnProperty(t)&&this.eventListeners[t].forEach(function(i){return i(e||{})}),this}}]),r})(),U={Player:Q,Utils:w,Constants:A};var _=class{#s=null;#r=null;#t=null;#n=[];#a=.05;#h=1e-6;#o=2;#e=null;#l=.7;#u=1;#c=null;#m=!1;#d=8192;#g=new Set;constructor(r,s,t=null){this.#s=s,this.#r=t,this.#t=r,this.#e=this.#s.createGain(),this.#e.gain.setValueAtTime(this.#l,this.#s.currentTime),this.#c=this.#s.createGain(),this.#c.gain.setValueAtTime(this.#u,this.#s.currentTime),this.#e.connect(this.#c),this.#c.connect(this.#r?this.#r.input:this.#s.destination),this.#t.zones.map(e=>this.#p(e))}get preset(){return this.#t}set preset(r){this.#t=r,this.#t.zones.map(s=>this.#p(s))}close(){let r=this.#s.currentTime;this.#n.forEach(s=>{try{s.gain.cancelScheduledValues(0),s.audioBufferSourceNode&&(s.audioBufferSourceNode.stop(r),s.audioBufferSourceNode.disconnect(),s.audioBufferSourceNode=null)}catch{}try{s.disconnect()}catch{}}),this.#n=[],this.#g.clear();try{this.#e.disconnect()}catch{}try{this.#c.disconnect()}catch{}this.#e=null,this.#c=null,this.#t=null,this.#r=null,this.#s=null}queueWaveTable(r,s,t,e,i){this.#s.state==="suspended"&&this.#s.resume().catch(()=>{});let a=this.#E(e),n=this.#x(Math.round(s));if(!n?.buffer)return null;let o=n.originalPitch-100*n.coarseTune-n.fineTune,h=s*100,l=(this.#d-8192)/8192*this.#o*100,c=h-o+l,u=Math.pow(2,c/1200),m=Math.max(r,this.#s.currentTime),f=t+this.#a,d=n.loopStart>=1&&n.loopStart<n.loopEnd;d||(f=Math.min(f,n.buffer.duration/u));let p=this.#i();this.#k(p,n,a,m,f,t);let g=this.#s.createBufferSource();if(g.buffer=n.buffer,g.playbackRate.setValueAtTime(u,0),i?.length>0&&(g.playbackRate.setValueAtTime(u,m),i.forEach(y=>{let T=(s+y.delta)*100-o+l,x=Math.pow(2,T/1200);g.playbackRate.linearRampToValueAtTime(x,m+y.when)})),g.loop=d,d){let y=n.delay??0;g.loopStart=n.loopStart/n.sampleRate+y,g.loopEnd=n.loopEnd/n.sampleRate+y}return g.connect(p),g.start(m,n.delay??0),g.stop(m+f),p.audioBufferSourceNode=g,p.when=m,p.duration=f,p.pitch=s,p.baseDetune=o,p}async cancelQueue(){this.#n.forEach(r=>{r.gain.cancelScheduledValues(0),r.gain.setValueAtTime(this.#h,this.#s.currentTime),r.when=-1;try{r.audioBufferSourceNode?.disconnect()}catch{}})}isSustainActive(){return this.#m}registerSustainNote(r){this.#g.add(r)}setPitchBend(r){this.#d=r;let s=r-8192,t=s>=0?s/8191*this.#o:s/8192*this.#o,e=this.#s.currentTime;this.#n.forEach(i=>{if(i.audioBufferSourceNode&&i.when+i.duration>e){let a=i.pitch*100,n=i.baseDetune,o=t*100,h=a-n+o,l=Math.pow(2,h/1200);i.audioBufferSourceNode.playbackRate.cancelScheduledValues(e),i.audioBufferSourceNode.playbackRate.setTargetAtTime(l,e,.015)}})}setController(r,s){let t=this.#s.currentTime,e=Math.max(0,Math.min(127,s))/127;switch(r){case 7:this.#l=e,this.#e.gain.setTargetAtTime(this.#l,t,.05);break;case 11:this.#u=e,this.#c.gain.setTargetAtTime(this.#u,t,.03);break;case 64:this.#m=s>=64,this.#m||(this.#g.forEach(i=>i()),this.#g.clear());break}}#p(r){if(r.buffer)return Promise.resolve(r);if(r.delay=0,r.file){let s=atob(r.file),t=new Uint8Array(s.length);for(let e=0;e<s.length;e++)t[e]=s.charCodeAt(e);this.#s.decodeAudioData(t.buffer,e=>(r.buffer=e,this.#T(r),r),e=>(console.error("Audio decoding error:",e),console.warn(this.#t),!1))}else return this.#T(r),r}#T(r){r.loopStart=this.#y(r.loopStart,0),r.loopEnd=this.#y(r.loopEnd,0),r.coarseTune=this.#y(r.coarseTune,0),r.fineTune=this.#y(r.fineTune,0),r.originalPitch=this.#y(r.originalPitch,6e3)}#k(r,s,t,e,i,a){r.gain.setValueAtTime(this.#h,this.#s.currentTime);let n=Math.min(a,i-this.#a),o=s.ahdsr&&s.ahdsr.length>0?s.ahdsr:[{duration:0,volume:1},{duration:n,volume:1}];r.gain.cancelScheduledValues(e);let h=(o[0]?.volume??1)*t;r.gain.linearRampToValueAtTime(this.#b(h),e+.002);let l=0,c=o[0]?.volume??1;for(let u of o){let{duration:m,volume:f}=u;if(m<=0)continue;let d=n-l;if(m>d){let p=d/m,g=c+p*(f-c);r.gain.exponentialRampToValueAtTime(this.#b(t*g),e+n);break}l+=m,c=f,r.gain.exponentialRampToValueAtTime(this.#b(t*c),e+l)}r.gain.exponentialRampToValueAtTime(this.#h,e+n+this.#a)}#i(r){let s=r||this.#e,t=this.#s.currentTime,e=this.#n.find(i=>i.target===s&&t>i.when+i.duration+.05);if(!e&&this.#n.length>=64){let i=this.#n.filter(a=>a.target===s);i.length>0&&(i.sort((a,n)=>a.when-n.when),e=i[0])}if(e){if(e.audioBufferSourceNode){try{e.audioBufferSourceNode.stop(0),e.audioBufferSourceNode.disconnect()}catch{}e.audioBufferSourceNode=null}e.gain.cancelScheduledValues(0),e.gain.setValueAtTime(this.#h,t)}else e=this.#s.createGain(),e.gain.value=0,e.target=s,e.connect(s),this.#n.push(e);return e.cancel=(i=!1)=>{let a=this.#s.currentTime;if(i&&e.audioBufferSourceNode){try{e.audioBufferSourceNode.stop(0),e.audioBufferSourceNode.disconnect()}catch{}e.audioBufferSourceNode=null}e.when+e.duration>a&&(e.gain.cancelScheduledValues(0),e.gain.setTargetAtTime(this.#h,a,i?.005:.02),e.when=a+1e-5,e.duration=0)},e}#x(r){let s=this.#t.zones.findLast(t=>r>=t.keyRangeLow&&r<=t.keyRangeHigh+1);return s&&this.#p(s),s}#E(r){let s=r?1*r:.5;return Math.min(s,.8)}#b(r){return r>this.#h?r:this.#h}#y(r,s){return typeof r=="number"?r:s}};typeof window<"u"&&(window.WebAudioFontPlayer=_);var $=_;var D=class r{#s=null;#r=null;#t=null;#n=null;#a=null;#h=null;#o=null;#e=0;#l=new Map;static#u=[32,64,128,256,512,1024,2048,4096,8192,16384];static#c=new Map([[32,.7],[64,.8],[128,.9],[256,1],[512,1.1],[1024,1.2],[2048,1.4],[4096,1.6],[8192,1.8],[16384,2]]);constructor(s,t,e){this.#t=s,this.#s=this.#t.createGain();let i=this.#s;[32,64,128,256,512,1024,2048,4096,8192,16384].forEach(n=>{i=this.#m(i,n);let o=n<1e3?n:n/1024+"k";this["band".concat(o)]=i}),this.#e=e,this.#h=this.#t.createConvolver(),this.#o=this.#t.createGain(),this.#o.gain.setValueAtTime(e,this.#t.currentTime),this.#d(1.5,2),this.#n=this.#t.createDynamicsCompressor(),this.#n.threshold.setValueAtTime(-10,this.#t.currentTime),this.#n.ratio.setValueAtTime(20,this.#t.currentTime),this.#n.attack.setValueAtTime(.001,this.#t.currentTime),this.#n.release.setValueAtTime(.1,this.#t.currentTime),this.#n.knee.setValueAtTime(0,this.#t.currentTime),this.#a=this.#t.createAnalyser(),this.#a.fftSize=256,this.#a.smoothingTimeConstant=.6,this.#r=this.#t.createGain(),this.#r.gain.setValueAtTime(t,this.#t.currentTime),i.connect(this.#r),this.#r.connect(this.#n),this.#r.connect(this.#h),this.#h.connect(this.#o),this.#n.connect(this.#a),this.#o.connect(this.#a),this.#a.connect(this.#t.destination)}get eqFrequencies(){return r.#u}get analyser(){return this.#a||null}get input(){return this.#s}get reverb(){return this.#e}set reverb(s){this.#e=Math.max(0,Math.min(1,s)),this.#o.gain.setTargetAtTime(this.#e,this.#t.currentTime,.1)}get masterVolume(){return this.#r.gain.value}set masterVolume(s){let t=Math.max(0,Math.min(1,s)),e=Math.pow(t,2);this.#r.gain.setTargetAtTime(e,this.#t.currentTime,.01)}killReverbTail(){let s=this.#t.currentTime;this.#o.gain.cancelScheduledValues(s),this.#o.gain.setValueAtTime(0,s)}restoreReverb(){this.reverb=this.#e}setEQ(s,t=.04){let e=this.#t.currentTime,i=12;for(let[a,n]of Object.entries(s)){let o=Number(a),h=this.#l.get(o);if(!h)continue;let l=Math.max(-i,Math.min(i,n));h.filter.gain.setTargetAtTime(l,e,t),h.gain=l}}getEQ(){let s={};for(let[t,e]of this.#l)s[t]=e.gain;return s}resetEQ(s=.04){let t={};for(let e of r.#u)t[e]=0;this.setEQ(t,s)}setEQPreset(s){let t={flat:{32:0,64:0,128:0,256:0,512:0,1024:0,2048:0,4096:0,8192:0,16384:0},bass:{32:7,64:6,128:4,256:2,512:0,1024:-1,2048:-1,4096:0,8192:0,16384:0},treble:{32:0,64:0,128:0,256:0,512:0,1024:1,2048:3,4096:5,8192:7,16384:8},vocal:{32:-3,64:-2,128:0,256:2,512:4,1024:5,2048:4,4096:2,8192:1,16384:0},loudness:{32:6,64:4,128:1,256:0,512:-1,1024:-1,2048:0,4096:2,8192:4,16384:5},classical:{32:4,64:3,128:2,256:0,512:0,1024:0,2048:0,4096:2,8192:3,16384:4},jazz:{32:4,64:3,128:1,256:0,512:-1,1024:-1,2048:0,4096:1,8192:3,16384:4},electronic:{32:6,64:5,128:2,256:-1,512:-2,1024:-1,2048:2,4096:4,8192:5,16384:6}},e=t[s];if(!e)throw new Error('Preset EQ unkown: "'.concat(s,'". Avaiables: ').concat(Object.keys(t).join(", ")));this.setEQ(e)}#m(s,t){let e=this.#t.createBiquadFilter();e.type="peaking",e.frequency.setValueAtTime(t,this.#t.currentTime),e.gain.setValueAtTime(0,this.#t.currentTime);let i=r.#c.get(t)??1;return e.Q.setValueAtTime(i,this.#t.currentTime),this.#l.set(t,{filter:e,gain:0}),s.connect(e),e}#d(s,t){let e=this.#t.sampleRate,i=e*s,a=this.#t.createBuffer(2,i,e),o=Math.floor(.015*e);for(let h=0;h<a.numberOfChannels;h++){let l=a.getChannelData(h),c=0,u=h===1?Math.floor(.002*e):0;for(let m=0;m<i;m++){if(m<o){l[m]=0;continue}let f=(m-o)/e,d=Math.exp(-f*(t/s)),p=Math.max(.01,.2*Math.exp(-f*2.5));c=(Math.random()*2-1)*p+c*(1-p);let y=c*d;f<.04&&(m%123===0||m%234===0)&&(y+=(Math.random()*2-1)*.2*(.04-f)/.04),m+u<i?l[m+u]=y:l[m]=y}}this.#h.buffer=a}};var Z="MidiAudioPlayer",P="KeyValues";var M=null,F=1;async function O(r=F){return M&&r!==F&&(M.close(),M=null,F=r),M||new Promise((s,t)=>{let e=indexedDB.open(Z,r);e.onupgradeneeded=i=>{let a=i.target.result;a.objectStoreNames.contains(P)||a.createObjectStore(P)},e.onsuccess=i=>{M=i.target.result,s(M)},e.onerror=i=>t(i.target.error)})}var J={async setVersion(r){await O(r)},async setItem(r,s,t=!1){let e=await O(),i=s,a=!1;if(t){let o=JSON.stringify(s),l=new Blob([o]).stream().pipeThrough(new CompressionStream("gzip"));i=await new Response(l).arrayBuffer(),a=!0}let n={data:i,isCompressed:a};return new Promise((o,h)=>{let u=e.transaction(P,"readwrite").objectStore(P).put(n,r);u.onsuccess=()=>o(),u.onerror=()=>h(u.error)})},async getItem(r){let s=await O(),t=await new Promise((e,i)=>{let o=s.transaction(P,"readonly").objectStore(P).get(r);o.onsuccess=()=>e(o.result),o.onerror=()=>i(o.error)});if(!t)return null;if(t.isCompressed){let i=new Blob([t.data]).stream().pipeThrough(new DecompressionStream("gzip")),n=await new Response(i).text();return JSON.parse(n)}return t.data},async removeItem(r){let s=await O();return new Promise((t,e)=>{let n=s.transaction(P,"readwrite").objectStore(P).delete(r);n.onsuccess=()=>t(),n.onerror=()=>e(n.error)})},async clear(){let r=await O();return new Promise((s,t)=>{let a=r.transaction(P,"readwrite").objectStore(P).clear();a.onsuccess=()=>s(),a.onerror=()=>t(a.error)})}},I=J;var z=(r,s,t)=>Math.min(Math.max(r,s),t),L=class r extends U.Player{static ENDPOINT="https://webaudiofonts.com/presets/";static DEFAULT_PRESET=-1;static REFERENCE_GAIN=.15;static KARAOKE_CHANNEL=0;#s=null;#r=null;#t=null;#n=null;#a={};#h={};#o={};#e={};#l={};#u={};#c={};#m=null;#d=null;#g=null;#p=null;#T=!1;#k="";#i={endpoint:r.ENDPOINT,volume:.6,reverb:.3,onEndFile:null,localCache:!0,presetRandom:!1,karaoke:!1,karaokeDelay:0,muteExpression:!1,maxCharPerLine:48,eqPreset:"flat",preferred:[],presets:[]};constructor(s={}){super(),this.#i={...this.#i,...s},this.#g=this.#x(),this.#r=new(window.AudioContext||window.webkitAudioContext),this.#t=new D(this.#r,this.#i.volume,this.#i.reverb),this.#t.setEQPreset(this.#i.eqPreset),this.#i.karaoke&&this.#C("intro")}get catalog(){return this.getCatalog()}get channels(){return this.#e}get channelStates(){return this.#h}get volume(){return this.#i.volume}set volume(s){this.#i.volume=z(s,0,1),this.#t.masterVolume=this.#i.volume}get volumes(){return this.#u}get reverb(){return this.#t.reverb}set reverb(s){this.#t.reverb=s}get muteExpression(){return this.#i.muteExpression}set muteExpression(s){this.#i.muteExpression=!!s}get eqFrequencies(){return this.#t.eqFrequencies}get eq(){return this.#t.getEQ()}getEQ(){return this.#t.getEQ()}setEQ(s){this.#t.setEQ(s)}setEQPreset(s){this.#t.setEQPreset(s)}setChannelVolume(s,t){this.#u[s]=t,this.#E()}async#x(){await Promise.all(this.#i.presets.map(async s=>{let t=await this.findPreset(s);t&&(this.#c[t.program]=t)}))}async findPreset(s){let t=null;return(await this.getCategories()).some(i=>{if(i.instruments.some(a=>{if(t=a.presets.find(n=>n.id==s),t)return t.category=i.name,t.instrument=a.name,t.program=a.program,!0}),t)return!0}),t}async close(){Object.keys(this.#e).forEach(s=>this.#e[s].close()),await this.#r.close()}async getCatalog(){if(this.#s)return this.#s;let s=this.#i.localCache?await sessionStorage.getItem("waf_catalog"):null;if(s)this.#s=JSON.parse(s);else{this.#f("Downloading catalog...");let i=await fetch("".concat(this.#i.endpoint,"catalog.json"));if(!i.ok)throw new Error("Impossible to download catalog: ".concat(i.status));this.#s=await i.json(),this.#i.localCache&&await sessionStorage.setItem("waf_catalog",JSON.stringify(this.#s))}let t=new Date(this.#s.updatedAt).getTime();return(await I.getItem("waf_catalog_version")||1)<t&&(await I.clear(),I.setItem("waf_catalog_version",t)),this.#s}async getCategories(){return(await this.getCatalog()).categories}async getProgramInstruments(s){let t=await this.getCategories(),e=[];return await Promise.all(t.map(async i=>i.instruments.filter(a=>a.program==s).forEach(a=>{a.presets.forEach(n=>{n.instrument=i.name+" / "+a.name,e.push(n)})}))),e}async getPreset(s){try{if(typeof s=="object")return s;let t="waf_preset_".concat(s),e=this.#i.localCache?await I.getItem(t):null;if(e)return JSON.parse(e);this.#f("Downloading preset ".concat(s,"..."));let a=await(await fetch("".concat(r.ENDPOINT).concat(s,".json"))).json();if(a.zones===void 0)throw console.error("Invalid preset: ".concat($id)),new Error("Invalid preset: ".concat($id));return this.#i.localCache&&await I.setItem(t,JSON.stringify(a),!0),a}catch{throw console.error("Invalid preset: ".concat(s)),new Error("Invalid preset: ".concat(s))}}async loadPreset(s,t){let e=await this.findPreset(s);if(!e)throw new Error("Invalid preset: ".concat(s));this.#c[e.program]=e;let i=await this.getPreset(s);this.#e[t].preset=i,this.#E()}async load(s,t){typeof s=="string"&&(this.#f("Downloading song..."),s=await(await fetch(s)).arrayBuffer()),typeof t=="string"&&(this.#f("Downloading setup..."),t=await(await fetch(t)).json()),this.#m=await this.hashBuffer(s),await this.#g,this.isPlaying()&&this.stop(),this.#v(),await Promise.all(Object.values(this.#e).map(async o=>o.close())),this.#e={},this.#o={},this.#a={},this.#k="",this.#f("Loading buffer...");try{await this.loadArrayBuffer(s)}catch{await this.loadArrayBuffer(await this.#R(s))}this.#f("Loading instruments..."),this.#l=await this.#N(),this.#h=Object.keys(this.#l).reduce((o,h)=>({...o,[h]:!1}),{}),this.#u=Object.keys(this.#l).reduce((o,h)=>({...o,[h]:1}),{}),t?.volumes!==void 0&&await Promise.all(Object.keys(t.volumes).map(async o=>{this.#u[o]!==void 0&&(this.#u[o]=t.volumes[o])}));let e=new Set,i={};t?.presets!==void 0&&await Promise.all(Object.keys(t.presets).map(async o=>{let h=await this.findPreset(t.presets[o]);h&&(i[o]=await this.getPreset(h.id),e.add(h.program))}));let a=await this.#M();Object.values(this.#l).length||this.#f("Error: no instrument found");let n=Promise.all([...a].map(async o=>{if(e.has(o))return;let h=null;this.#c[o]!==void 0?h=await this.getPreset(this.#c[o].id):this.#i.presetRandom?h=await this.#I(o):h=await this.#L(o),this.#o[o]=h}));this.#i.karaoke&&(this.#f("Generating karaoke frames..."),this.#p=null,await this.#V(),this.#k&&this.#C("title",this.#k)),this.#f("Trim midi events..."),this.#B(),queueMicrotask(()=>this.triggerPlayerEvent("computed")),await n,await Promise.all(Object.keys(this.#l).map(async o=>{this.#e[o]&&this.#e[o].close(),i[o]!==void 0?this.#e[o]=await this.#A(i[o]):this.#e[o]=await this.#A(this.#o[this.#l[o]])})),this.#f("Initializing instrument states..."),await this.#y(),await this.triggerPlayerEvent("presetsLoaded",this.#o),await this.#E(),this.#f("Player ready")}async getSongSetup(){let s={hash:this.#m,presets:{},volumes:{}};return Object.keys(this.#e).map(async t=>s.presets[t]=this.#e[t].preset.id),s.volumes=this.#u,s}async getTrainingPresets(){return await Promise.all(Object.values(this.#c).map(async s=>s.id))}async play(s=null){if(this.#r.state==="suspended")try{await this.#r.resume()}catch{return!1}return s&&await this.load(s),await Promise.all(Object.keys(this.#e).map(async t=>await this.#e[t]?.cancelQueue())),this.#t.restoreReverb(),this.isPlaying()||(this.startTime||(this.startTime=new Date().getTime()),this.scheduledTime=Date.now(),this.schedulePlayLoop(this.sampleRate)),!0}async pause(){await super.pause(),this.#t.killReverbTail(),await this.#v(),await Promise.all(Object.keys(this.#e).map(async s=>await this.#e[s]?.cancelQueue()))}async stop(s=!1){return await super.stop(),this.setTimeoutId=!1,s||(this.#t.killReverbTail(),await Promise.all(Object.keys(this.#e).map(async t=>await this.#e[t]?.cancelQueue()))),await this.#v(),this.#i.karaoke&&this.#C("intro"),this}getRealTimeVolume(){let s=this.#t.analyser,t=new Uint8Array(s.frequencyBinCount);s.getByteFrequencyData(t);let e=0;for(let i=0;i<t.length;i++)e+=t[i];return e/(t.length*100)}getSongTimeRemaining(){return this.ticksToSeconds(this.getCurrentTick(),this.totalTicks)}async skipToSeconds(s){let t=this.getSongTime();if(s<0||s>t)throw s+" seconds not within song time of "+t;return await this.skipToTick(this.secondsToTicks(s)),this}async generateWaveformSVG(s=1e3){if(!this.totalTicks||!this.events)return"";let t=new Array(s).fill(0),e=this.totalTicks/s,i=this.events.flatMap((f,d)=>f.map(p=>({...p,computedChannel:p.channel!==void 0?p.channel:d}))).filter(f=>f.name==="Controller Change"||f.name==="Program Change"||f.name==="Note on"&&f.velocity>0).sort((f,d)=>f.tick-d.tick),a=new Map,n=new Map;i.forEach(f=>{let d=Math.floor(f.tick/e);if(d>=s)return;let p=f.computedChannel;if(a.has(p)||a.set(p,100),n.has(p)||n.set(p,127),f.name==="Controller Change")f.number===7?a.set(p,f.value):f.number===11&&n.set(p,f.value);else if(f.name==="Note on"){let g=a.get(p)/127,y=n.get(p)/127,k=f.velocity*g*y;t[d]+=k}});let o=t.reduce((f,d)=>isNaN(d)?f:d>f?d:f,0),h=o>0?t.map(f=>isNaN(f)?0:f/o):t.fill(0),l=s,c=l/5,u=h.map((f,d)=>{let p=d,g=Math.max(0,Math.min(c,c-f*c));return"".concat(p,",").concat(g.toFixed(2))}),m="M 0,".concat(c," L ").concat(u.join(" L ")," L ").concat(l,",").concat(c);return'<svg class="midiaudioplayer-waveform" viewBox="0 0 '.concat(l," ").concat(c,'" preserveAspectRatio="none"><path d="').concat(m,'" fill="none" stroke-linecap="round" stroke-linejoin="round" /></svg>')}async hashBuffer(s,t="SHA-256"){let e=await crypto.subtle.digest(t,s);return Array.from(new Uint8Array(e)).map(a=>a.toString(16).padStart(2,"0")).join("")}async#E(){this.#d&&clearTimeout(this.#d),this.#d=setTimeout(async()=>{let s=await this.getSongSetup();queueMicrotask(()=>this.triggerPlayerEvent("setupChange",s))},1e3)}async triggerPlayerEvent(s,t){s!="fileLoaded"&&(s=="computed"?(this.#n=await this.#F(),super.triggerPlayerEvent(s,{title:this.#k,karaoke:this.#T,vocalChannel:this.#n,tempo:this.tempo,division:this.division,duration:this.getSongTime(),sampleRate:this.sampleRate,totalTicks:this.totalTicks,totalEvents:this.totalEvents,channels:await this.#l})):s=="endOfFile"&&this.#i.karaoke?queueMicrotask(()=>super.triggerPlayerEvent(s,t)):super.triggerPlayerEvent(s,t))}async playLoop(s){if(this.inLoop)return;if(!s&&this.endOfFile()&&this.tick>0){await this.stop(!0),this.tick=0,this.triggerPlayerEvent("endOfFile");return}this.inLoop=!0,this.tick=this.getCurrentTick();let t=this.tracks.length;for(let e=0;e<t;e++){let i=this.tracks[e].handleEvent(this.tick,s);if(!i)continue;let a=i.constructor===Array,n=a?i.length:1;for(let o=0;o<n;o++){let h=a?i[o]:i,{name:l,data:c,value:u}=h;l==="Set Tempo"&&this.setTempo(c),s?l==="Program Change"&&!this.instruments.includes(u)&&this.instruments.push(u):this.emitEvent(h)}}!s&&this.isPlaying()&&this.triggerPlayerEvent("playing",{tick:this.tick}),this.inLoop=!1}schedulePlayLoop(s){this.setTimeoutId=setTimeout(()=>{if(this.setTimeoutId===!1)return;this.playLoop();let t=this.#r.currentTime;this._lastAudioTime||(this._lastAudioTime=t);let e=t-this._lastAudioTime;this._lastAudioTime=t;let i=this.sampleRate/1e3,a=e-i,n=Math.max(0,this.sampleRate-a*1e3);this.schedulePlayLoop(n)},s)}emitEvent(s){this.#D(s)}ticksToSeconds(s,t){if(t===void 0&&(t=s,s=0),s>=t)return 0;let e=0,i=this.tempoMap.length,a=60/this.division,n=0,o=i-1,h=0;for(;n<=o;){let c=n+o>>1;this.tempoMap[c].tick<=s?(h=c,n=c+1):o=c-1}let l=s;for(let c=h;c<i;c++){let u=this.tempoMap[c],m=c+1<i?this.tempoMap[c+1].tick:t;if(m<=s)continue;let f=Math.max(u.tick,s),d=Math.min(m,t);if(f>=t)break;e+=(d-f)/u.tempo*a,l=d}if(l<t){let c=this.tempoMap[i-1];e+=(t-l)/c.tempo*a}return e}secondsToTicks(s){let t=s,e=this.tempoMap.length,i=60/this.division;for(let a=0;a<e;a++){let n=this.tempoMap[a],l=((a+1<e?this.tempoMap[a+1].tick:1/0)-n.tick)/n.tempo*i;if(t<=l)return n.tick+Math.round(t*n.tempo/i);t-=l}return this.totalTicks}getTickBeforeSeconds(s,t){if(s<=0)return 0;let e=this.ticksToSeconds(0,s),i=Math.max(0,e-t);return this.secondsToTicks(i)}async skipToTick(s){let t=Math.max(0,Math.min(s,this.totalTicks||0)),e=this.isPlaying();if(this.#v(),Object.keys(this.channels).forEach(i=>this.channels[i]?.cancelQueue?.()),e&&super.pause(),this.startTick=t,this.tick=t,this.tempoMap&&this.tempoMap.length>0){for(let i=this.tempoMap.length-1;i>=0;i--)if(this.tempoMap[i].tick<=t){this.setTempo(this.tempoMap[i].tempo);break}}try{let i=[],a=[],n=[],o=[];this.#b(t).forEach(h=>{let l=h.channel;if(!((l===void 0||!this.channels[l])&&h.name!=="Karaoke Event"))switch(h.name){case"Controller Change":i[h.channel]=h;break;case"Program Change":a[h.channel]=h;break;case"Pitch Bend":n[h.channel]=h;break;case"Karaoke Event":o[h.channel]=h;break}}),i.forEach(h=>this.emitEvent(h)),a.forEach(h=>this.emitEvent(h)),n.forEach(h=>this.emitEvent(h)),o.forEach(h=>this.triggerPlayerEvent("karaoke",{type:h.type,tick:h.tick,html:h.text}))}catch(i){console.warn("Chase MIDI Error:",i),this.#f("Chase MIDI Error:",i)}return this.tracks&&this.tracks.length>0&&this.tracks.forEach((i,a)=>{let n=this.events[a];if(n&&n.length>0){let o=0,h=n.length-1,l=n.length;for(;o<=h;){let c=o+h>>1;n[c].tick>=t?(l=c,h=c-1):o=c+1}i.eventIndex=l}else typeof i.setEventIndexByTick=="function"&&i.setEventIndexByTick(t)}),e?this.play():this.triggerPlayerEvent("playing",{tick:t}),this}#b(s){let t={};if(!this.events)return[];for(let e=0;e<this.events.length;e++){let i=this.events[e];if(!i||i.length===0)continue;let a=0,n=i.length-1,o=i.length;for(;a<=n;){let h=a+n>>1;i[h].tick>=s?(o=h,n=h-1):a=h+1}for(let h=0;h<o;h++){let l=i[h],c;l.name==="Program Change"?c="pc:"+l.channel:l.name==="Controller Change"?c="cc:"+l.channel+":"+l.number:l.name==="Pitch Bend"?c="pb:"+l.channel:l.name==="Karaoke Event"&&(c="ke:"+l.channel),c&&(t[c]=l)}}return Object.keys(t).map(e=>t[e])}async#y(){this.events&&this.#b(1).forEach(s=>{let t=s.channel;if(this.#e[t])switch(s.name){case"Controller Change":this.#e[t].setController(s.number,s.value);break;case"Pitch Bend":this.#e[t].setPitchBend?.(s.value);break;case"Program Change":s.value>=0&&s.value<=127&&this.#o[s.value+1]!==void 0&&s.channel!=10&&this.#e[t].preset?.program!==s.value+1&&(this.#e[t].preset=this.#o[s.value+1]);break}})}async#N(){let s={},t=new Set;return this.events.forEach(e=>{e.forEach(i=>{if(i.name==="Program Change"&&i.value>=0&&i.value<=127){if(s[i.channel])return;i.channel==10?s[i.channel]=-1:s[i.channel]=i.value+1}else i.name==="Note on"&&i.channel==10?(s[i.channel]=-1,t.add(10)):i.name==="Note on"&&t.add(i.channel)})}),Object.keys(s).forEach(e=>{t.has(Number(e))||delete s[e]}),s}async#M(){let s=new Set;return this.events.forEach(t=>{t.forEach(e=>{e.name==="Program Change"&&e.value>=0&&e.value<=127?s.add(e.channel==10?-1:e.value+1):e.name==="Note on"&&e.channel==10&&s.add(-1)})}),s}async#I(s){let t=await this.getProgramInstruments(s);if(!t.length)return null;let e=null;return this.#i.preferred.some(i=>{let a=new RegExp("_".concat(i,"$"),"i"),n=t.filter(o=>a.test(o.id));if(n.length)return e=n[Math.floor(Math.random()*n.length)],!0}),e||(e=t[Math.floor(Math.random()*t.length)]),this.#c[s]=e,await this.getPreset(e.id)}async#L(s){let t=await this.getProgramInstruments(s);if(!t.length)return null;let e=null;return this.#i.preferred.some(i=>{let a=new RegExp("_".concat(i,"$"),"i");if(e=t.find(n=>a.test(n.id)),e)return!0}),e||(e=t[0]),this.#c[s]=e,await this.getPreset(e.id)}async#A(s){return new $(s,this.#r,this.#t)}async#D(s){if(this.isPlaying())switch(s.name){case"Note on":if(s.tick<this.tick-100||s.noteNumber===void 0||s.channel==this.#n&&this.#i.muteExpression)return;if(s.velocity>0&&s.velocity<=127){if(this.#S(s.channel,s.noteNumber),this.#u[s.channel]==0)return;let t=s.velocity/127,e=r.REFERENCE_GAIN*Math.pow(t,2)*this.#u[s.channel],i=this.#e[s.channel]?.queueWaveTable(0,s.noteNumber,2,e);i&&this.#O(s.channel,s.noteNumber,i)}else this.#S(s.channel,s.noteNumber);break;case"Note off":if(s.noteNumber===void 0)return;this.#S(s.channel,s.noteNumber);break;case"Controller Change":this.#e[s.channel]?.setController(s.number,s.value);break;case"Pitch Bend":this.#e[s.channel]?.setPitchBend?.(s.value);break;case"Program Change":return;case"Karaoke Event":if(s.tick<this.tick-this.secondsToTicks(10))return;this.triggerPlayerEvent("karaoke",{type:s.type,tick:s.tick,html:s.text});break}}#O(s,t,e){this.#a[s]||(this.#a[s]=new Map),this.#a[s].set(t,e),this.#w();let i=(e.duration||0)*1e3;e.cleanupTimer=setTimeout(()=>{this.#a[s]?.get(t)===e&&(this.#a[s].delete(t),this.#w())},i+50)}#S(s,t){let e=this.#e[s],i=this.#a[s]?.get(t);if(i){i.cleanupTimer&&clearTimeout(i.cleanupTimer);let a=()=>{this.#a[s]?.delete(t),this.#w()};e&&e.isSustainActive()?e.registerSustainNote(()=>i.cancel(!1)):i.cancel(!1),a()}}#v(){Object.keys(this.#a).forEach(s=>{this.#a[s].forEach((t,e)=>{t&&(t.cleanupTimer&&clearTimeout(t.cleanupTimer),t.cancel&&t.cancel(!0)),this.#a[s]?.delete(e)})}),this.#w()}async#w(){let s=!1,t={};Object.keys(this.#e).forEach(e=>{let i=!!(this.#a[e]?.size&&this.#a[e].size>0);t[e]=i,this.#h[e]!==i&&(s=!0)}),s&&(this.#h=t,this.triggerPlayerEvent("channelState",this.#h))}async#R(s){let t=new Uint8Array(s),e=new DataView(s);if(String.fromCharCode(...t.slice(0,4))!=="MThd")throw new Error("Invalid MIDI file (MThd missing)");let a=e.getUint32(4),n=e.getUint16(8),o=e.getUint16(10),h=e.getUint16(12),l=[255,47,0],c=[],u=8+a;for(;u<t.length&&!(u+8>t.length);){let k=String.fromCharCode(...t.slice(u,u+4)),T=e.getUint32(u+4),x=u+8,S=x+T;if(k!=="MTrk"){let C=Math.min(S,t.length);c.push({tag:k,data:t.slice(u,C),repaired:!1}),u=S;continue}let E=c.filter(C=>C.tag==="MTrk").length+1,b=Math.min(T,t.length-x),v=t.slice(x,x+b),N=v.slice(-3);if(N[0]===255&&N[1]===47&&N[2]===0&&b===T)c.push({tag:k,data:v,repaired:!1});else{let C;if(b<T){let Y=T-b,K=v.slice(-2);K[0]===255&&K[1]===47?(C=new Uint8Array(v.length+1),C.set(v),C[v.length]=0):(C=new Uint8Array(v.length+3),C.set(v),C.set(l,v.length))}else C=new Uint8Array(v.length+3),C.set(v),C.set(l,v.length);c.push({tag:k,data:C,repaired:!0})}u=S}let m=c.filter(k=>k.tag==="MTrk").length,f=14+c.reduce((k,T)=>k+8+T.data.length,0),d=new Uint8Array(f),p=new DataView(d.buffer);d.set([77,84,104,100],0),p.setUint32(4,6),p.setUint16(8,n),p.setUint16(10,m),p.setUint16(12,h);let g=14;for(let k of c){let T=k.tag.split("").map(x=>x.charCodeAt(0));d.set(T,g),p.setUint32(g+4,k.data.length),d.set(k.data,g+8),g+=8+k.data.length}let y=c.filter(k=>k.repaired).length;return d.buffer}#B(){if(!this.events||this.events.length===0)return;let s=1/0,t=0;if(this.events.forEach(o=>{o.forEach(h=>{(h.name==="Note on"||h.name==="Note off")&&(h.tick<s&&(s=h.tick),h.tick>t&&(t=h.tick))})}),s===1/0)return;let e=[];this.events.forEach((o,h)=>{o.forEach(l=>{let c=l.name==="Program Change"||l.name==="Controller Change"||l.name==="Pitch Bend"||l.name==="Set Tempo";l.tick<s&&c&&e.push({event:l,trackIdx:h})})});let i=Object.fromEntries(this.events.map((o,h)=>[h,[]])),a=new Set;for(let o=e.length-1;o>=0;o--){let{event:h,trackIdx:l}=e[o],c=h.channel!==void 0?h.channel:"track-".concat(l),u=null;if(h.name==="Program Change"?u="pc:".concat(c):h.name==="Controller Change"?u="cc:".concat(c,":").concat(h.number):h.name==="Pitch Bend"?u="pb:".concat(c):h.name==="Set Tempo"&&(u="tempo"),u&&!a.has(u)){a.add(u);let m={...h,tick:0};i[l].push(m)}}let n=this.events.map((o,h)=>{let l=[];return o.forEach(u=>{let m=u.name==="Program Change"||u.name==="Controller Change"||u.name==="Pitch Bend"||u.name==="Set Tempo",f=u.name==="Text Event"||u.name==="Lyric Event"||u.name==="Track Name"||u.name==="Karaoke Event";if(u.tick<s)!m&&(f||h===0)&&(u.tick=0,l.push(u));else{u.tick=u.tick-s;let d=t-s;u.tick>d&&(u.tick=d),l.push(u)}}),[...i[h]||[],...l].sort((u,m)=>u.tick-m.tick)});this.events=n,this.totalTicks=t-s,typeof this.computeTempoMap=="function"&&this.computeTempoMap()}async#P(){if(this.#p)return this.#p;let s={language:"",title:"",paragraphs:[]},t=null,e=0;if(this.events.forEach(l=>{let c=l.filter(m=>m.name==="Text Event"||m.name==="Lyric Event"||m.name==="Cue Point"||m.name==="Marker"||m.name==="Track Name"),u=c.filter(m=>{let f=m.string||m.text||"";return f&&!f.startsWith("@")}).length;u>e&&(e=u,t=c)}),!t||t.length===0)return s;let i=t.sort((l,c)=>l.tick-c.tick),a=[],n=[],o=[],h=0;for(i.forEach(l=>{let c=this.#K(l.string||"");if(!c||/^Track-/i.test(c.trim())||/^Piste/i.test(c.trim())||c.trim()===""||l.tick===0&&c.length>20)return;if(c.startsWith("@L")){s.language=c.substring(2).trim();return}if(c.startsWith("@T")){s.title+=(s.title?" / ":"")+c.substring(2).trim();return}if(c.startsWith("@")||c.startsWith("(")||c.startsWith("PART")||/^\d+\s+\d+/.test(c.trim()))return;if(/^(Verse|Chorus|Bridge|Break|Intro|End\.)/i.test(c.trim())){let y=c.startsWith("\\")||c.startsWith("/"),k=o.length===0||l.tick-h>500;if((y||k)&&(o.length>0&&(n.push({tick:o[0].tick,blocks:o}),o=[]),n.length>0)){for(;n.length>4;){let T=n.splice(0,4);a.push({tick:T[0].tick,lines:T})}n.length>0&&(a.push({tick:n[0].tick,lines:n}),n=[])}return}let u=!1;if(o.length>0){let k=o[o.length-1].text,T=c.trimLeft();if(T.length>0){let x=/^[A-Z]/.test(T)||/^'[A-Z]/.test(T),S=/^[A-Z]/.test(k.trim())||/^'[A-Z]/.test(k.trim());x&&!k.endsWith(" ")&&k.trim()!="o"&&!S&&l.tick>h&&(u=!0),c.startsWith('"')&&k.endsWith('"')&&(u=!0),c.startsWith('"')&&(k.endsWith(")")||k.endsWith(')"'))&&(u=!0),T.startsWith('"')&&k.trimRight().endsWith(")")&&(u=!0)}}let m=c.startsWith("\\"),f=c.startsWith("/")||u;(m||f)&&(c.startsWith("\\")||c.startsWith("/"))&&(c=c.substring(1)),c=c.replace(/[\r\n]/g,"");let d=!1;h>0&&l.tick>h&&this.ticksToSeconds(h,l.tick)>2.5&&(d=!0);let g=o.reduce((y,k)=>y+k.text.length,0)+c.length>this.#i.maxCharPerLine;if((f||m||d||g)&&(o.length>0&&(n.push({tick:o[0].tick,blocks:o}),o=[]),n.length>0)){if(m||d){for(;n.length>4;){let y=n.splice(0,4);a.push({tick:y[0].tick,lines:y})}n.length>0&&(a.push({tick:n[0].tick,lines:n}),n=[])}else if(n.length>=6){let y=n.splice(0,4);a.push({tick:y[0].tick,lines:y})}}c.length>0&&(o.push({text:c,tick:l.tick}),h=l.tick)}),o.length>0&&n.push({tick:o[0].tick,blocks:o});n.length>4;){let l=n.splice(0,4);a.push({tick:l[0].tick,lines:l})}return n.length>0&&a.push({tick:n[0].tick,lines:n}),a=a.filter(l=>!(l.lines.length==1&&l.lines[0].blocks.length==1&&["intro","outro","sfx","solo","chorus","verse","bridge","break","end"].includes(l.lines[0].blocks[0].text.toLowerCase().trim()))),a.length<=2&&(a=[]),s.paragraphs=a,this.#p=s,s}async#V(){let s=await this.#P();if(!s.paragraphs.length){this.#T=!1,this.events[r.KARAOKE_CHANNEL].push({text:'<span class="karaoke-intro"></span>',name:"Karaoke Event",type:"intro",tick:0,channel:r.KARAOKE_CHANNEL}),this.events[r.KARAOKE_CHANNEL]=this.events[r.KARAOKE_CHANNEL].sort((u,m)=>u.tick-m.tick);return}this.#T=!0,this.#k=s.title;let t=0,e=this.secondsToTicks(this.#i.karaokeDelay),i=this.secondsToTicks(3),a=this.secondsToTicks(5),n=this.secondsToTicks(7),o=this.secondsToTicks(10),h=[];s.paragraphs.forEach((u,m)=>{u.lines.forEach((f,d)=>{f.blocks.forEach(p=>{h.push({block:p,lineIdx:d,paraIdx:m,paragraph:u,fastLinesText:u.lines.map(g=>g.blocks.map(y=>y.text).join(""))})})})});let l=[];s.paragraphs.forEach((u,m)=>{let f=this.getTickBeforeSeconds(u.tick,5);f<t&&(f=t+(u.tick-t)/2),m===0&&f<20&&(f=20),l[m]=f;let p=u.lines.map(g=>g.blocks.map(y=>y.text).join("")).map(g=>'<span class="karaoke-coming">'.concat(g,"</span>")).join("<br/>");if(this.events[r.KARAOKE_CHANNEL].push({text:p,name:"Karaoke Event",type:"lyric",tick:f,channel:r.KARAOKE_CHANNEL}),u.lines.length>0){let g=u.lines[u.lines.length-1];g.blocks.length>0&&(t=g.blocks[g.blocks.length-1].tick)}}),(l[0]||0)>25&&this.events[r.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:5,channel:r.KARAOKE_CHANNEL}),h.forEach((u,m)=>{let f=u.block,d=u.lineIdx,p=u.paraIdx,g=u.paragraph,y=u.fastLinesText,k=(x=!1)=>g.lines.map((S,E)=>{if(E<d)return'<span class="karaoke-played">'.concat(y[E],"</span>");if(E>d)return'<span class="karaoke-coming">'.concat(y[E],"</span>");let b="";return S.blocks.forEach(v=>{let N="coming";x||v.tick<f.tick?N="played":v.tick===f.tick&&(N="playing"),b+='<span class="karaoke-'.concat(N,'">').concat(v.text,"</span>")}),b}).join("<br>");this.events[r.KARAOKE_CHANNEL].push({text:k(!1),name:"Karaoke Event",type:"lyric",tick:f.tick-e,channel:r.KARAOKE_CHANNEL});let T=h[m+1];if(T){let x=T.block.tick-f.tick;if(x>i){let S=f.tick+i,E=f.tick+n,b=x>o&&p>0;if(T.paraIdx!==p){let v=l[T.paraIdx];S>=v&&(S=v-1),b&&(E>=v||v-E<i)&&(b=!1)}S>f.tick&&this.events[r.KARAOKE_CHANNEL].push({text:k(!0),name:"Karaoke Event",type:"lyric",tick:S-e,channel:r.KARAOKE_CHANNEL}),b&&E>S&&this.events[r.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:E-e,channel:r.KARAOKE_CHANNEL})}}t=f.tick}),this.totalTicks-t>this.secondsToTicks(5)?this.events[r.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:t+this.secondsToTicks(5),channel:r.KARAOKE_CHANNEL}):this.events[r.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:this.totalTicks-1,channel:r.KARAOKE_CHANNEL}),this.events[r.KARAOKE_CHANNEL]=this.events[r.KARAOKE_CHANNEL].sort((u,m)=>u.tick-m.tick)}async#F(){let s=await this.#P();if(!s?.paragraphs?.length)return null;let t=s.paragraphs.flatMap(l=>l.lines.flatMap(c=>c.blocks.map(u=>u.tick)));if(t.length===0)return null;let e=this.division?this.division/2:48,i=48,a=84,n=Object.keys(this.#l).map(Number).filter(l=>l!==10),o=null,h=-1/0;for(let l of n){let c=this.events.flatMap(E=>E.filter(b=>b.name==="Note on"&&b.velocity>0&&b.channel===l));if(c.length===0)continue;let m=t.filter(E=>c.some(b=>Math.abs(b.tick-E)<=e)).length/t.length,d=c.filter(E=>E.noteNumber>=i&&E.noteNumber<=a).length/c.length;if(d<.3)continue;let p=[...c].sort((E,b)=>E.tick-b.tick),g=this.division/8||6,k=1-p.filter((E,b)=>b>0&&Math.abs(E.tick-p[b-1].tick)<g).length/Math.max(c.length-1,1),T=c.length/Math.max(t.length,1),x=T<.3?T/.3:T>5?Math.max(0,1-(T-5)/10):1,S=m*.45+d*.35+k*.15+x*.05;S>h&&(h=S,o=l)}return h>=.4?o:null}#K(s){if(!s)return"";let t=new Uint8Array(s.length);for(let a=0;a<s.length;a++)t[a]=s.charCodeAt(a)&255;let i=new TextDecoder("windows-1252").decode(t);return i=i.replace(/ÿ/g,""),i=i.replace(/’/g,"'"),i=i.replace(/`/g,"'"),i}#C(s="clear",t=""){let e='<span class="karaoke-'.concat(s,'">').concat(t.replace(/\s\/\s/g,"<br>"),"</span>");this.#i.karaoke&&(s=="title"?queueMicrotask(()=>this.triggerPlayerEvent("karaoke",{type:s,title:t,html:e})):queueMicrotask(()=>this.triggerPlayerEvent("karaoke",{type:s,html:e})))}#f(s,t=!1){queueMicrotask(()=>this.triggerPlayerEvent("logs",s))}};typeof window<"u"&&(window.MidiAudioPlayer=L);var ut=L;})();
|
|
17
|
+
(()=>{function B(f,e){if(!(f instanceof e))throw new TypeError("Cannot call a class as a function")}function j(f,e){for(var t=0;t<e.length;t++){var s=e[t];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(f,s.key,s)}}function V(f,e,t){return e&&j(f.prototype,e),t&&j(f,t),Object.defineProperty(f,"prototype",{writable:!1}),f}var P={VERSION:"2.0.17",NOTES:[],HEADER_CHUNK_LENGTH:14,CIRCLE_OF_FOURTHS:["C","F","Bb","Eb","Ab","Db","Gb","Cb","Fb","Bbb","Ebb","Abb"],CIRCLE_OF_FIFTHS:["C","G","D","A","E","B","F#","C#","G#","D#","A#","E#"]},W=[["C"],["C#","Db"],["D"],["D#","Eb"],["E"],["F"],["F#","Gb"],["G"],["G#","Ab"],["A"],["A#","Bb"],["B"]],H=0,G=function(e){W.forEach(function(t){t.forEach(function(s){return P.NOTES[H]=s+e}),H++})};for(R=-1;R<=9;R++)G(R);var R,w=(function(){function f(){B(this,f)}return V(f,null,[{key:"byteToHex",value:function(t){return("0"+t.toString(16)).slice(-2)}},{key:"bytesToHex",value:function(t){var s=[];return t.forEach(function(i){return s.push(f.byteToHex(i))}),s.join("")}},{key:"hexToNumber",value:function(t){return parseInt(t,16)}},{key:"bytesToNumber",value:function(t){return f.hexToNumber(f.bytesToHex(t))}},{key:"bytesToLetters",value:function(t){var s=[];return t.forEach(function(i){return s.push(String.fromCharCode(i))}),s.join("")}},{key:"decToBinary",value:function(t){return(t>>>0).toString(2)}},{key:"getVarIntLength",value:function(t){for(var s=t[0],i=1;s>=128;)s=t[i],i++;return i}},{key:"readVarInt",value:function(t){var s=0;return t.forEach(function(i){var a=i;a&128?(s+=a&127,s<<=7):s+=a}),s}},{key:"atob",value:(function(e){function t(s){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t})(function(e){return typeof atob=="function"?atob(e):Buffer.from(e,"base64").toString("binary")})}]),f})(),Q=(function(){function f(e,t){B(this,f),this.enabled=!0,this.eventIndex=0,this.pointer=0,this.lastTick=0,this.lastStatus=null,this.index=e,this.data=t,this.delta=0,this.runningDelta=0,this.events=[];var s=this.data.subarray(this.data.length-3,this.data.length);if(!(s[0]===255&&s[1]===47&&s[2]===0))throw"Invalid MIDI file; Last three bytes of track "+this.index+"must be FF 2F 00 to mark end of track"}return V(f,[{key:"reset",value:function(){return this.enabled=!0,this.eventIndex=0,this.pointer=0,this.lastTick=0,this.lastStatus=null,this.delta=0,this.runningDelta=0,this}},{key:"enable",value:function(){return this.enabled=!0,this}},{key:"disable",value:function(){return this.enabled=!1,this}},{key:"setEventIndexByTick",value:function(t){t=t||0;for(var s=0;s<this.events.length;s++)if(this.events[s].tick>=t)return this.eventIndex=s,this}},{key:"getCurrentByte",value:function(){return this.data[this.pointer]}},{key:"getDeltaByteCount",value:function(){return w.getVarIntLength(this.data.subarray(this.pointer))}},{key:"getDelta",value:function(){return w.readVarInt(this.data.subarray(this.pointer,this.pointer+this.getDeltaByteCount()))}},{key:"handleEvent",value:function(t,s){if(s=s||!1,s){var i=t-this.lastTick,a=this.getDelta(),o=i>=a;if(this.pointer<this.data.length&&(s||o)){var n=this.parseEvent();if(this.enabled)return n}}else{for(var r=[];this.events[this.eventIndex]&&this.events[this.eventIndex].tick<=t;)this.enabled&&r.push(this.events[this.eventIndex]),this.eventIndex++;if(r.length>0)return r}return null}},{key:"getStringData",value:function(t){var s=w.getVarIntLength(this.data.subarray(t+2)),i=w.readVarInt(this.data.subarray(t+2,t+2+s)),a=w.bytesToLetters(this.data.subarray(t+2+s,t+2+s+i));return a}},{key:"parseEvent",value:function(){var t=this.pointer+this.getDeltaByteCount(),s={},i=this.getDeltaByteCount();if(s.track=this.index+1,s.delta=this.getDelta(),this.lastTick=this.lastTick+s.delta,this.runningDelta+=s.delta,s.tick=this.runningDelta,s.byteIndex=this.pointer,this.data[t]==255){switch(this.data[t+1]){case 0:s.name="Sequence Number";break;case 1:s.name="Text Event",s.string=this.getStringData(t);break;case 2:s.name="Copyright Notice";break;case 3:s.name="Sequence/Track Name",s.string=this.getStringData(t);break;case 4:s.name="Instrument Name",s.string=this.getStringData(t);break;case 5:s.name="Lyric",s.string=this.getStringData(t);break;case 6:s.name="Marker",s.string=this.getStringData(t);break;case 7:s.name="Cue Point",s.string=this.getStringData(t);break;case 9:s.name="Device Name",s.string=this.getStringData(t);break;case 32:s.name="MIDI Channel Prefix";break;case 33:s.name="MIDI Port",s.data=w.bytesToNumber([this.data[t+3]]);break;case 47:s.name="End of Track";break;case 81:s.name="Set Tempo",s.data=Math.round(6e7/w.bytesToNumber(this.data.subarray(t+3,t+6))),this.tempo=s.data;break;case 84:s.name="SMTPE Offset";break;case 88:s.name="Time Signature",s.data=this.data.subarray(t+3,t+7),s.timeSignature=""+s.data[0]+"/"+Math.pow(2,s.data[1]);break;case 89:s.name="Key Signature",s.data=this.data.subarray(t+3,t+5);var a=s.data[0]>127?s.data[0]-256:s.data[0];a>=0?s.keySignature=P.CIRCLE_OF_FIFTHS[a]:s.keySignature=P.CIRCLE_OF_FOURTHS[Math.abs(a)],s.data[1]==0?s.keySignature+=" Major":s.data[1]==1&&(s.keySignature+=" Minor");break;case 127:s.name="Sequencer-Specific Meta-event";break;default:s.name="Unknown: "+this.data[t+1].toString(16);break}var o=w.getVarIntLength(this.data.subarray(t+2)),n=w.readVarInt(this.data.subarray(t+2,t+2+o));this.pointer+=i+2+o+n}else if(this.data[t]===240){s.name="Sysex";var r=w.getVarIntLength(this.data.subarray(t+1)),c=w.readVarInt(this.data.subarray(t+1,t+1+r));s.data=this.data.subarray(t+1+r,t+1+r+c),this.pointer+=i+1+r+c}else if(this.data[t]===247){s.name="Sysex (escape)";var h=w.getVarIntLength(this.data.subarray(t+1)),l=w.readVarInt(this.data.subarray(t+1,t+1+h));s.data=this.data.subarray(t+1+h,t+1+h+l),this.pointer+=i+1+h+l}else if(this.data[t]<128)if(s.running=!0,s.noteNumber=this.data[t],s.noteName=P.NOTES[this.data[t]],s.velocity=this.data[t+1],this.lastStatus<=143)s.name="Note off",s.channel=this.lastStatus-128+1,this.pointer+=i+2;else if(this.lastStatus<=159)s.name="Note on",s.channel=this.lastStatus-144+1,this.pointer+=i+2;else if(this.lastStatus<=175)s.name="Polyphonic Key Pressure",s.channel=this.lastStatus-160+1,s.note=P.NOTES[this.data[t]],s.pressure=this.data[t+1],this.pointer+=i+2;else if(this.lastStatus<=191)s.name="Controller Change",s.channel=this.lastStatus-176+1,s.number=this.data[t],s.value=this.data[t+1],this.pointer+=i+2;else if(this.lastStatus<=207)s.name="Program Change",s.channel=this.lastStatus-192+1,s.value=this.data[t+1],this.pointer+=i+1;else if(this.lastStatus<=223)s.name="Channel Key Pressure",s.channel=this.lastStatus-208+1,this.pointer+=i+1;else if(this.lastStatus<=239)s.name="Pitch Bend",s.channel=this.lastStatus-224+1,s.value=(this.data[t+1]&127)<<7|this.data[t]&127,this.pointer+=i+2;else throw"Unknown event (running): ".concat(this.lastStatus);else if(this.lastStatus=this.data[t],this.data[t]<=143)s.name="Note off",s.channel=this.lastStatus-128+1,s.noteNumber=this.data[t+1],s.noteName=P.NOTES[this.data[t+1]],s.velocity=Math.round(this.data[t+2]/127*100),this.pointer+=i+3;else if(this.data[t]<=159)s.name="Note on",s.channel=this.lastStatus-144+1,s.noteNumber=this.data[t+1],s.noteName=P.NOTES[this.data[t+1]],s.velocity=Math.round(this.data[t+2]/127*100),this.pointer+=i+3;else if(this.data[t]<=175)s.name="Polyphonic Key Pressure",s.channel=this.lastStatus-160+1,s.note=P.NOTES[this.data[t+1]],s.pressure=this.data[t+2],this.pointer+=i+3;else if(this.data[t]<=191)s.name="Controller Change",s.channel=this.lastStatus-176+1,s.number=this.data[t+1],s.value=this.data[t+2],this.pointer+=i+3;else if(this.data[t]<=207)s.name="Program Change",s.channel=this.lastStatus-192+1,s.value=this.data[t+1],this.pointer+=i+2;else if(this.data[t]<=223)s.name="Channel Key Pressure",s.channel=this.lastStatus-208+1,this.pointer+=i+2;else if(this.data[t]<=239)s.name="Pitch Bend",s.channel=this.lastStatus-224+1,s.value=(this.data[t+2]&127)<<7|this.data[t+1]&127,this.pointer+=i+3;else throw"Unknown event: ".concat(this.data[t]);return this.delta+=s.delta,this.events.push(s),s}},{key:"endOfTrack",value:function(){return this.data[this.pointer+1]==255&&this.data[this.pointer+2]==47&&this.data[this.pointer+3]==0}}]),f})();Uint8Array.prototype.forEach||Object.defineProperty(Uint8Array.prototype,"forEach",{value:Array.prototype.forEach});var Z=(function(){function f(e,t){B(this,f),this.sampleRate=5,this.startTime=0,this.buffer=t||null,this.midiChunksByteLength=null,this.division,this.format,this.setTimeoutId=!1,this.scheduledTime=0,this.tracks=[],this.instruments=[],this.defaultTempo=120,this.tempo=null,this.startTick=0,this.tick=0,this.lastTick=null,this.inLoop=!1,this.totalTicks=0,this.events=[],this.totalEvents=0,this.tempoMap=[],this.eventListeners={},typeof e=="function"&&this.on("midiEvent",e)}return V(f,[{key:"loadFile",value:function(t){throw"loadFile is only supported on Node.js"}},{key:"loadArrayBuffer",value:function(t){return this.buffer=new Uint8Array(t),this.fileLoaded()}},{key:"loadDataUri",value:function(t){for(var s=w.atob(t.split(",")[1]),i=new Uint8Array(s.length),a=0;a<s.length;a++)i[a]=s.charCodeAt(a);return this.buffer=i,this.fileLoaded()}},{key:"getFilesize",value:function(){return this.buffer?this.buffer.length:0}},{key:"fileLoaded",value:function(){if(!this.validate())throw"Invalid MIDI file; should start with MThd";return this.defaultTempo=120,this.setTempo(this.defaultTempo).getDivision().getFormat().getTracks().dryRun()}},{key:"validate",value:function(){return w.bytesToLetters(this.buffer.subarray(0,4))==="MThd"}},{key:"getFormat",value:function(){return this.format=w.bytesToNumber(this.buffer.subarray(8,10)),this}},{key:"getTracks",value:function(){this.tracks=[];for(var t=0;t<this.buffer.length;){if(w.bytesToLetters(this.buffer.subarray(t,t+4))=="MTrk"){var s=w.bytesToNumber(this.buffer.subarray(t+4,t+8));this.tracks.push(new Q(this.tracks.length,this.buffer.subarray(t+8,t+8+s)))}t+=w.bytesToNumber(this.buffer.subarray(t+4,t+8))+8}var i=0;return this.tracks.forEach(function(a){i+=8+a.data.length}),this.midiChunksByteLength=P.HEADER_CHUNK_LENGTH+i,this}},{key:"enableTrack",value:function(t){return this.tracks[t-1].enable(),this}},{key:"disableTrack",value:function(t){return this.tracks[t-1].disable(),this}},{key:"getDivision",value:function(){return this.division=w.bytesToNumber(this.buffer.subarray(12,P.HEADER_CHUNK_LENGTH)),this}},{key:"playLoop",value:function(t){this.inLoop||(this.inLoop=!0,this.tick=this.getCurrentTick(),this.tracks.forEach(function(s,i){if(!t&&this.endOfFile())this.stop(),this.triggerPlayerEvent("endOfFile");else{var a=s.handleEvent(this.tick,t);if(t&&a)a.hasOwnProperty("name")&&a.name==="Set Tempo"&&this.setTempo(a.data),a.hasOwnProperty("name")&&a.name==="Program Change"&&(this.instruments.includes(a.value)||this.instruments.push(a.value));else if(a){var o=Array.isArray(a)?a:[a];o.forEach(function(n){n.hasOwnProperty("name")&&n.name==="Set Tempo"&&this.setTempo(n.data),this.emitEvent(n)},this)}}},this),!t&&this.isPlaying()&&this.triggerPlayerEvent("playing",{tick:this.tick}),this.inLoop=!1)}},{key:"setTempo",value:function(t){return this.tempo=t,this}},{key:"setStartTime",value:function(t){return this.startTime=t,this}},{key:"play",value:function(){if(this.isPlaying())throw"Already playing...";return this.startTime||(this.startTime=new Date().getTime()),this.scheduledTime=Date.now(),this.schedulePlayLoop(this.sampleRate),this}},{key:"schedulePlayLoop",value:function(t){var s=this;this.setTimeoutId=setTimeout(function(){if(s.playLoop(),s.setTimeoutId!==!1){s.scheduledTime+=s.sampleRate;var i=Date.now()-s.scheduledTime;s.schedulePlayLoop(Math.max(0,s.sampleRate-i))}},t)}},{key:"pause",value:function(){return clearTimeout(this.setTimeoutId),this.setTimeoutId=!1,this.scheduledTime=0,this.startTick=this.tick,this.startTime=0,this}},{key:"stop",value:function(){return clearTimeout(this.setTimeoutId),this.setTimeoutId=!1,this.scheduledTime=0,this.startTick=0,this.startTime=0,this.resetTracks(),this}},{key:"skipToTick",value:function(t){this.stop(),this.startTick=t;for(var s=this.tempoMap.length-1;s>=0;s--)if(this.tempoMap[s].tick<=t){this.setTempo(this.tempoMap[s].tempo);break}return this.collectStateAtTick(t).forEach(function(i){this.emitEvent(i)},this),this.tracks.forEach(function(i){i.setEventIndexByTick(t)}),this}},{key:"collectStateAtTick",value:function(t){var s={};return this.events.forEach(function(i){i.forEach(function(a){if(!(a.tick>=t)){var o;a.name==="Program Change"?o="pc:"+a.channel:a.name==="Controller Change"?o="cc:"+a.channel+":"+a.number:a.name==="Pitch Bend"&&(o="pb:"+a.channel),o&&(s[o]=a)}})}),Object.keys(s).map(function(i){return s[i]})}},{key:"skipToPercent",value:function(t){if(t<0||t>100)throw"Percent must be number between 1 and 100.";return this.skipToTick(Math.round(t/100*this.totalTicks)),this}},{key:"skipToSeconds",value:function(t){var s=this.getSongTime();if(t<0||t>s)throw t+" seconds not within song time of "+s;return this.skipToTick(this.secondsToTicks(t)),this}},{key:"isPlaying",value:function(){return this.setTimeoutId!==!1}},{key:"dryRun",value:function(){for(this.resetTracks();!this.endOfFile();)this.playLoop(!0);return this.events=this.getEvents(),this.totalEvents=this.getTotalEvents(),this.totalTicks=this.getTotalTicks(),this.buildTempoMap(),this.startTick=0,this.startTime=0,this.resetTracks(),this.triggerPlayerEvent("fileLoaded",this),this}},{key:"resetTracks",value:function(){return this.tracks.forEach(function(t){return t.reset()}),this}},{key:"getEvents",value:function(){return this.tracks.map(function(t){return t.events})}},{key:"getTotalTicks",value:function(){return Math.max.apply(null,this.tracks.map(function(t){return t.delta}))}},{key:"getTotalEvents",value:function(){return this.tracks.reduce(function(t,s){return{events:{length:t.events.length+s.events.length}}},{events:{length:0}}).events.length}},{key:"buildTempoMap",value:function(){var t=[];return this.events.forEach(function(s){s.forEach(function(i){i.name==="Set Tempo"&&t.push({tick:i.tick,tempo:i.data})})}),t.sort(function(s,i){return s.tick-i.tick}),this.tempoMap=[{tick:0,tempo:this.defaultTempo}],t.forEach(function(s){var i=this.tempoMap[this.tempoMap.length-1];s.tick===i.tick?i.tempo=s.tempo:this.tempoMap.push({tick:s.tick,tempo:s.tempo})},this),this}},{key:"ticksToSeconds",value:function(t,s){for(var i=0,a=t,o=0;o<this.tempoMap.length;o++){var n=this.tempoMap[o],r=o+1<this.tempoMap.length?this.tempoMap[o+1].tick:s;if(!(r<=t)){var c=Math.max(n.tick,t),h=Math.min(r,s);if(c>=s)break;var l=h-c;i+=l/this.division/n.tempo*60,a=h}}if(a<s){var m=this.tempoMap[this.tempoMap.length-1];i+=(s-a)/this.division/m.tempo*60}return i}},{key:"secondsToTicks",value:function(t){for(var s=t,i=0,a=0;a<this.tempoMap.length;a++){var o=this.tempoMap[a],n=a+1<this.tempoMap.length?this.tempoMap[a+1].tick:1/0,r=n-o.tick,c=r/this.division/o.tempo*60;if(s<=c)return i=o.tick+Math.round(s/60*o.tempo*this.division),i;s-=c,i=n}return this.totalTicks}},{key:"getSongTime",value:function(){return this.ticksToSeconds(0,this.totalTicks)}},{key:"getSongTimeRemaining",value:function(){return Math.round(this.ticksToSeconds(this.getCurrentTick(),this.totalTicks))}},{key:"getSongPercentRemaining",value:function(){return Math.round(this.getSongTimeRemaining()/this.getSongTime()*100)}},{key:"bytesProcessed",value:function(){return P.HEADER_CHUNK_LENGTH+this.tracks.length*8+this.tracks.reduce(function(t,s){return{pointer:t.pointer+s.pointer}},{pointer:0}).pointer}},{key:"eventsPlayed",value:function(){return this.tracks.reduce(function(t,s){return{eventIndex:t.eventIndex+s.eventIndex}},{eventIndex:0}).eventIndex}},{key:"endOfFile",value:function(){return this.isPlaying()?this.totalTicks-this.tick<=0:this.bytesProcessed()>=this.midiChunksByteLength}},{key:"getCurrentTick",value:function(){if(!this.startTime)return this.startTick;var t=(new Date().getTime()-this.startTime)/1e3,s=this.ticksToSeconds(0,this.startTick);return this.secondsToTicks(s+t)}},{key:"emitEvent",value:function(t){return this.triggerPlayerEvent("midiEvent",t),this}},{key:"on",value:function(t,s){return this.eventListeners.hasOwnProperty(t)||(this.eventListeners[t]=[]),this.eventListeners[t].push(s),this}},{key:"triggerPlayerEvent",value:function(t,s){return this.eventListeners.hasOwnProperty(t)&&this.eventListeners[t].forEach(function(i){return i(s||{})}),this}}]),f})(),U={Player:Z,Utils:w,Constants:P};var _=class ${#s=null;#r=null;#t=null;#n=[];#a=.05;#h=1e-6;#o=2;#e=null;#l=.7;#u=1;#c=null;#m=!1;#d=8192;#p=new Set;constructor(e,t,s=null,i=null){this.#s=t,this.#r=s,this.#e=this.#s.createGain(),this.#e.gain.setValueAtTime(this.#l,this.#s.currentTime),this.#c=this.#s.createGain(),this.#c.gain.setValueAtTime(this.#u,this.#s.currentTime),this.#e.connect(this.#c),this.#c.connect(this.#r?this.#r.input:this.#s.destination),this.setPreset(e).then(()=>{typeof i=="function"&&i()})}get preset(){return this.#t}set preset(e){this.setPreset(e)}static load(e,t,s=null){return new Promise(i=>{let a=new $(e,t,s,()=>i(a))})}async setPreset(e){this.#t=e,await Promise.all(this.#t.zones.map(t=>this.#y(t)))}close(){let e=this.#s.currentTime;this.#n.forEach(t=>{try{t.gain.cancelScheduledValues(0),t.audioBufferSourceNode&&(t.audioBufferSourceNode.stop(e),t.audioBufferSourceNode.disconnect(),t.audioBufferSourceNode=null)}catch{}try{t.disconnect()}catch{}}),this.#n=[],this.#p.clear();try{this.#e.disconnect()}catch{}try{this.#c.disconnect()}catch{}this.#e=null,this.#c=null,this.#t=null,this.#r=null,this.#s=null}queueWaveTable(e,t,s,i,a){this.#s.state==="suspended"&&this.#s.resume().catch(()=>{});let o=this.#E(i),n=this.#x(Math.round(t));if(!n?.buffer)return null;let r=n.originalPitch-100*n.coarseTune-n.fineTune,c=t*100,h=(this.#d-8192)/8192*this.#o*100,l=c-r+h,m=Math.pow(2,l/1200),u=Math.max(e,this.#s.currentTime),d=s+this.#a,p=n.loopStart>=1&&n.loopStart<n.loopEnd;p||(d=Math.min(d,n.buffer.duration/m));let y=this.#i();this.#g(y,n,o,u,d,s);let k=this.#s.createBufferSource();if(k.buffer=n.buffer,k.playbackRate.setValueAtTime(m,0),a?.length>0&&(k.playbackRate.setValueAtTime(m,u),a.forEach(g=>{let S=(t+g.delta)*100-r+h,x=Math.pow(2,S/1200);k.playbackRate.linearRampToValueAtTime(x,u+g.when)})),k.loop=p,p){let g=n.delay??0;k.loopStart=n.loopStart/n.sampleRate+g,k.loopEnd=n.loopEnd/n.sampleRate+g}return k.connect(y),k.start(u,n.delay??0),k.stop(u+d),y.audioBufferSourceNode=k,y.when=u,y.duration=d,y.pitch=t,y.baseDetune=r,y}async cancelQueue(){this.#n.forEach(e=>{e.gain.cancelScheduledValues(0),e.gain.setValueAtTime(this.#h,this.#s.currentTime),e.when=-1;try{e.audioBufferSourceNode?.disconnect()}catch{}})}isSustainActive(){return this.#m}registerSustainNote(e){this.#p.add(e)}setPitchBend(e){this.#d=e;let t=e-8192,s=t>=0?t/8191*this.#o:t/8192*this.#o,i=this.#s.currentTime;this.#n.forEach(a=>{if(a.audioBufferSourceNode&&a.when+a.duration>i){let o=a.pitch*100,n=a.baseDetune,r=s*100,c=o-n+r,h=Math.pow(2,c/1200);a.audioBufferSourceNode.playbackRate.cancelScheduledValues(i),a.audioBufferSourceNode.playbackRate.setTargetAtTime(h,i,.015)}})}setController(e,t){let s=this.#s.currentTime,i=Math.max(0,Math.min(127,t))/127;switch(e){case 7:this.#l=i,this.#e.gain.setTargetAtTime(this.#l,s,.05);break;case 11:this.#u=i,this.#c.gain.setTargetAtTime(this.#u,s,.03);break;case 64:this.#m=t>=64,this.#m||(this.#p.forEach(a=>a()),this.#p.clear());break}}async#y(e){if(e.buffer)return e;if(e.delay=0,e.file){let t=atob(e.file),s=Uint8Array.from(t,i=>i.charCodeAt(0));try{e.buffer=await this.#s.decodeAudioData(s.buffer),this.#T(e)}catch(i){return console.error("Audio decoding error:",i),console.warn(this.#t),!1}}else this.#T(e);return e}#T(e){e.loopStart=this.#k(e.loopStart,0),e.loopEnd=this.#k(e.loopEnd,0),e.coarseTune=this.#k(e.coarseTune,0),e.fineTune=this.#k(e.fineTune,0),e.originalPitch=this.#k(e.originalPitch,6e3)}#g(e,t,s,i,a,o){e.gain.setValueAtTime(this.#h,this.#s.currentTime);let n=Math.min(o,a-this.#a),r=t.ahdsr&&t.ahdsr.length>0?t.ahdsr:[{duration:0,volume:1},{duration:n,volume:1}];e.gain.cancelScheduledValues(i);let c=(r[0]?.volume??1)*s;e.gain.linearRampToValueAtTime(this.#b(c),i+.002);let h=0,l=r[0]?.volume??1;for(let m of r){let{duration:u,volume:d}=m;if(u<=0)continue;let p=n-h;if(u>p){let y=p/u,k=l+y*(d-l);e.gain.exponentialRampToValueAtTime(this.#b(s*k),i+n);break}h+=u,l=d,e.gain.exponentialRampToValueAtTime(this.#b(s*l),i+h)}e.gain.exponentialRampToValueAtTime(this.#h,i+n+this.#a)}#i(e){let t=e||this.#e,s=this.#s.currentTime,i=this.#n.find(a=>a.target===t&&s>a.when+a.duration+.05);if(!i&&this.#n.length>=64){let a=this.#n.filter(o=>o.target===t);a.length>0&&(a.sort((o,n)=>o.when-n.when),i=a[0])}if(i){if(i.audioBufferSourceNode){try{i.audioBufferSourceNode.stop(0),i.audioBufferSourceNode.disconnect()}catch{}i.audioBufferSourceNode=null}i.gain.cancelScheduledValues(0),i.gain.setValueAtTime(this.#h,s)}else i=this.#s.createGain(),i.gain.value=0,i.target=t,i.connect(t),this.#n.push(i);return i.cancel=(a=!1)=>{let o=this.#s.currentTime;if(a&&i.audioBufferSourceNode){try{i.audioBufferSourceNode.stop(0),i.audioBufferSourceNode.disconnect()}catch{}i.audioBufferSourceNode=null}i.when+i.duration>o&&(i.gain.cancelScheduledValues(0),i.gain.setTargetAtTime(this.#h,o,a?.005:.02),i.when=o+1e-5,i.duration=0)},i}#x(e){return this.#t.zones.findLast(t=>e>=t.keyRangeLow&&e<=t.keyRangeHigh)}#E(e){let t=e?1*e:.5;return Math.min(t,.8)}#b(e){return e>this.#h?e:this.#h}#k(e,t){return typeof e=="number"?e:t}};typeof window<"u"&&(window.WebAudioFontPlayer=_);var q=_;var D=class f{#s=null;#r=null;#t=null;#n=null;#a=null;#h=null;#o=null;#e=0;#l=new Map;static#u=[32,64,128,256,512,1024,2048,4096,8192,16384];static#c=new Map([[32,.7],[64,.8],[128,.9],[256,1],[512,1.1],[1024,1.2],[2048,1.4],[4096,1.6],[8192,1.8],[16384,2]]);constructor(e,t,s){this.#t=e,this.#s=this.#t.createGain();let i=this.#s;[32,64,128,256,512,1024,2048,4096,8192,16384].forEach(o=>{i=this.#m(i,o);let n=o<1e3?o:o/1024+"k";this["band".concat(n)]=i}),this.#e=s,this.#h=this.#t.createConvolver(),this.#o=this.#t.createGain(),this.#o.gain.setValueAtTime(s,this.#t.currentTime),this.#d(1.5,2),this.#n=this.#t.createDynamicsCompressor(),this.#n.threshold.setValueAtTime(-10,this.#t.currentTime),this.#n.ratio.setValueAtTime(20,this.#t.currentTime),this.#n.attack.setValueAtTime(.001,this.#t.currentTime),this.#n.release.setValueAtTime(.1,this.#t.currentTime),this.#n.knee.setValueAtTime(0,this.#t.currentTime),this.#a=this.#t.createAnalyser(),this.#a.fftSize=256,this.#a.smoothingTimeConstant=.6,this.#r=this.#t.createGain(),this.#r.gain.setValueAtTime(t,this.#t.currentTime),i.connect(this.#r),this.#r.connect(this.#n),this.#r.connect(this.#h),this.#h.connect(this.#o),this.#n.connect(this.#a),this.#o.connect(this.#a),this.#a.connect(this.#t.destination)}get eqFrequencies(){return f.#u}get analyser(){return this.#a||null}get input(){return this.#s}get reverb(){return this.#e}set reverb(e){this.#e=Math.max(0,Math.min(1,e)),this.#o.gain.setTargetAtTime(this.#e,this.#t.currentTime,.1)}get masterVolume(){return this.#r.gain.value}set masterVolume(e){let t=Math.max(0,Math.min(1,e)),s=Math.pow(t,2);this.#r.gain.setTargetAtTime(s,this.#t.currentTime,.01)}killReverbTail(){let e=this.#t.currentTime;this.#o.gain.cancelScheduledValues(e),this.#o.gain.setValueAtTime(0,e)}restoreReverb(){this.reverb=this.#e}setEQ(e,t=.04){let s=this.#t.currentTime,i=12;for(let[a,o]of Object.entries(e)){let n=Number(a),r=this.#l.get(n);if(!r)continue;let c=Math.max(-i,Math.min(i,o));r.filter.gain.setTargetAtTime(c,s,t),r.gain=c}}getEQ(){let e={};for(let[t,s]of this.#l)e[t]=s.gain;return e}resetEQ(e=.04){let t={};for(let s of f.#u)t[s]=0;this.setEQ(t,e)}setEQPreset(e){let t={flat:{32:0,64:0,128:0,256:0,512:0,1024:0,2048:0,4096:0,8192:0,16384:0},bass:{32:7,64:6,128:4,256:2,512:0,1024:-1,2048:-1,4096:0,8192:0,16384:0},treble:{32:0,64:0,128:0,256:0,512:0,1024:1,2048:3,4096:5,8192:7,16384:8},vocal:{32:-3,64:-2,128:0,256:2,512:4,1024:5,2048:4,4096:2,8192:1,16384:0},loudness:{32:6,64:4,128:1,256:0,512:-1,1024:-1,2048:0,4096:2,8192:4,16384:5},classical:{32:4,64:3,128:2,256:0,512:0,1024:0,2048:0,4096:2,8192:3,16384:4},jazz:{32:4,64:3,128:1,256:0,512:-1,1024:-1,2048:0,4096:1,8192:3,16384:4},electronic:{32:6,64:5,128:2,256:-1,512:-2,1024:-1,2048:2,4096:4,8192:5,16384:6}},s=t[e];if(!s)throw new Error('Preset EQ unkown: "'.concat(e,'". Avaiables: ').concat(Object.keys(t).join(", ")));this.setEQ(s)}#m(e,t){let s=this.#t.createBiquadFilter();s.type="peaking",s.frequency.setValueAtTime(t,this.#t.currentTime),s.gain.setValueAtTime(0,this.#t.currentTime);let i=f.#c.get(t)??1;return s.Q.setValueAtTime(i,this.#t.currentTime),this.#l.set(t,{filter:s,gain:0}),e.connect(s),s}#d(e,t){let s=this.#t.sampleRate,i=s*e,a=this.#t.createBuffer(2,i,s),n=Math.floor(.015*s);for(let r=0;r<a.numberOfChannels;r++){let c=a.getChannelData(r),h=0,l=r===1?Math.floor(.002*s):0;for(let m=0;m<i;m++){if(m<n){c[m]=0;continue}let u=(m-n)/s,d=Math.exp(-u*(t/e)),p=Math.max(.01,.2*Math.exp(-u*2.5));h=(Math.random()*2-1)*p+h*(1-p);let k=h*d;u<.04&&(m%123===0||m%234===0)&&(k+=(Math.random()*2-1)*.2*(.04-u)/.04),m+l<i?c[m+l]=k:c[m]=k}}this.#h.buffer=a}};var J="MidiAudioPlayer",A="KeyValues";var M=null,F=1;async function O(f=F){return M&&f!==F&&(M.close(),M=null,F=f),M||new Promise((e,t)=>{let s=indexedDB.open(J,f);s.onupgradeneeded=i=>{let a=i.target.result;a.objectStoreNames.contains(A)||a.createObjectStore(A)},s.onsuccess=i=>{M=i.target.result,e(M)},s.onerror=i=>t(i.target.error)})}var X={async setVersion(f){await O(f)},async setItem(f,e,t=!1){let s=await O(),i=e,a=!1;if(t){let n=JSON.stringify(e),c=new Blob([n]).stream().pipeThrough(new CompressionStream("gzip"));i=await new Response(c).arrayBuffer(),a=!0}let o={data:i,isCompressed:a};return new Promise((n,r)=>{let l=s.transaction(A,"readwrite").objectStore(A).put(o,f);l.onsuccess=()=>n(),l.onerror=()=>r(l.error)})},async getItem(f){let e=await O(),t=await new Promise((s,i)=>{let n=e.transaction(A,"readonly").objectStore(A).get(f);n.onsuccess=()=>s(n.result),n.onerror=()=>i(n.error)});if(!t)return null;if(t.isCompressed){let i=new Blob([t.data]).stream().pipeThrough(new DecompressionStream("gzip")),o=await new Response(i).text();return JSON.parse(o)}return t.data},async removeItem(f){let e=await O();return new Promise((t,s)=>{let o=e.transaction(A,"readwrite").objectStore(A).delete(f);o.onsuccess=()=>t(),o.onerror=()=>s(o.error)})},async clear(){let f=await O();return new Promise((e,t)=>{let a=f.transaction(A,"readwrite").objectStore(A).clear();a.onsuccess=()=>e(),a.onerror=()=>t(a.error)})}},I=X;var z=(f,e,t)=>Math.min(Math.max(f,e),t),L=class f extends U.Player{static ENDPOINT="https://webaudiofonts.com/presets/";static DEFAULT_PRESET=-1;static REFERENCE_GAIN=.15;static KARAOKE_CHANNEL=0;#s=null;#r=null;#t=null;#n=null;#a={};#h={};#o={};#e={};#l={};#u={};#c={};#m=null;#d=null;#p=null;#y=null;#T=!1;#g="";#i={endpoint:f.ENDPOINT,volume:.6,reverb:.3,onEndFile:null,localCache:!0,presetRandom:!1,karaoke:!1,karaokeDelay:0,muteExpression:!1,maxCharPerLine:48,eqPreset:"flat",preferred:[],presets:[]};constructor(e={}){super(),this.#i={...this.#i,...e},this.#p=this.#x(),this.#r=new(window.AudioContext||window.webkitAudioContext),this.#t=new D(this.#r,this.#i.volume,this.#i.reverb),this.#t.setEQPreset(this.#i.eqPreset),this.#i.karaoke&&this.#C("intro")}get catalog(){return this.getCatalog()}get channels(){return this.#e}get channelStates(){return this.#h}get volume(){return this.#i.volume}set volume(e){this.#i.volume=z(e,0,1),this.#t.masterVolume=this.#i.volume}get volumes(){return this.#u}get reverb(){return this.#t.reverb}set reverb(e){this.#t.reverb=e}get muteExpression(){return this.#i.muteExpression}set muteExpression(e){this.#i.muteExpression=!!e}get eqFrequencies(){return this.#t.eqFrequencies}get eq(){return this.#t.getEQ()}getEQ(){return this.#t.getEQ()}setEQ(e){this.#t.setEQ(e)}setEQPreset(e){this.#t.setEQPreset(e)}setChannelVolume(e,t){this.#u[e]=t,this.#E()}async#x(){await Promise.all(this.#i.presets.map(async e=>{let t=await this.findPreset(e);t&&(this.#c[t.program]=t)}))}async findPreset(e){let t=null;return(await this.getCategories()).some(i=>{if(i.instruments.some(a=>{if(t=a.presets.find(o=>o.id==e),t)return t.category=i.name,t.instrument=a.name,t.program=a.program,!0}),t)return!0}),t}async close(){Object.keys(this.#e).forEach(e=>this.#e[e].close()),await this.#r.close()}async getCatalog(){if(this.#s)return this.#s;let e=this.#i.localCache?await sessionStorage.getItem("waf_catalog"):null;if(e)this.#s=JSON.parse(e);else{this.#f("Downloading catalog...");let i=await fetch("".concat(this.#i.endpoint,"catalog.json"));if(!i.ok)throw new Error("Impossible to download catalog: ".concat(i.status));this.#s=await i.json(),this.#i.localCache&&await sessionStorage.setItem("waf_catalog",JSON.stringify(this.#s))}let t=new Date(this.#s.updatedAt).getTime();return(await I.getItem("waf_catalog_version")||1)<t&&(await I.clear(),I.setItem("waf_catalog_version",t)),this.#s}async getCategories(){return(await this.getCatalog()).categories}async getProgramInstruments(e){let t=await this.getCategories(),s=[];return await Promise.all(t.map(async i=>i.instruments.filter(a=>a.program==e).forEach(a=>{a.presets.forEach(o=>{o.instrument=i.name+" / "+a.name,s.push(o)})}))),s}async getPreset(e){try{if(typeof e=="object")return e;let t="waf_preset_".concat(e),s=this.#i.localCache?await I.getItem(t):null;if(s)return JSON.parse(s);this.#f("Downloading preset ".concat(e,"..."));let a=await(await fetch("".concat(f.ENDPOINT).concat(e,".json"))).json();if(a.zones===void 0)throw console.error("Invalid preset: ".concat($id)),new Error("Invalid preset: ".concat($id));return this.#i.localCache&&await I.setItem(t,JSON.stringify(a),!0),a}catch{throw console.error("Invalid preset: ".concat(e)),new Error("Invalid preset: ".concat(e))}}async loadPreset(e,t){let s=await this.findPreset(e);if(!s)throw new Error("Invalid preset: ".concat(e));this.#c[s.program]=s;let i=await this.getPreset(e);await this.#e[t].setPreset(i),this.#E()}async load(e,t){typeof e=="string"&&(this.#f("Downloading song..."),e=await(await fetch(e)).arrayBuffer()),typeof t=="string"&&(this.#f("Downloading setup..."),t=await(await fetch(t)).json()),this.#m=await this.hashBuffer(e),await this.#p,this.isPlaying()&&this.stop(),this.#v(),await Promise.all(Object.values(this.#e).map(async n=>n.close())),this.#e={},this.#o={},this.#a={},this.#g="",this.#f("Loading buffer...");try{await this.loadArrayBuffer(e)}catch{await this.loadArrayBuffer(await this.#R(e))}this.#f("Loading instruments..."),this.#l=await this.#N(),this.#h=Object.keys(this.#l).reduce((n,r)=>({...n,[r]:!1}),{}),this.#u=Object.keys(this.#l).reduce((n,r)=>({...n,[r]:1}),{}),t?.volumes!==void 0&&await Promise.all(Object.keys(t.volumes).map(async n=>{this.#u[n]!==void 0&&(this.#u[n]=t.volumes[n])}));let s=new Set,i={};t?.presets!==void 0&&await Promise.all(Object.keys(t.presets).map(async n=>{let r=await this.findPreset(t.presets[n]);r&&(i[n]=await this.getPreset(r.id),s.add(r.program))}));let a=await this.#M();Object.values(this.#l).length||this.#f("Error: no instrument found");let o=Promise.all([...a].map(async n=>{if(s.has(n))return;let r=null;this.#c[n]!==void 0?r=await this.getPreset(this.#c[n].id):this.#i.presetRandom?r=await this.#I(n):r=await this.#L(n),this.#o[n]=r}));this.#i.karaoke&&(this.#f("Generating karaoke frames..."),this.#y=null,await this.#V(),this.#g&&this.#C("title",this.#g)),this.#f("Trim midi events..."),this.#B(),queueMicrotask(()=>this.triggerPlayerEvent("computed")),await o,await Promise.all(Object.keys(this.#l).map(async n=>{this.#e[n]&&this.#e[n].close(),i[n]!==void 0?this.#e[n]=await this.#P(i[n]):this.#e[n]=await this.#P(this.#o[this.#l[n]])})),this.#f("Initializing instrument states..."),await this.#k(),await this.triggerPlayerEvent("presetsLoaded",this.#o),await this.#E(),this.#f("Player ready")}async getSongSetup(){let e={hash:this.#m,presets:{},volumes:{}};return Object.keys(this.#e).map(async t=>e.presets[t]=this.#e[t].preset.id),e.volumes=this.#u,e}async getTrainingPresets(){return await Promise.all(Object.values(this.#c).map(async e=>e.id))}async play(e=null){if(this.#r.state==="suspended")try{await this.#r.resume()}catch{return!1}return e&&await this.load(e),await Promise.all(Object.keys(this.#e).map(async t=>await this.#e[t]?.cancelQueue())),this.#t.restoreReverb(),this.isPlaying()||(this.startTime||(this.startTime=new Date().getTime()),this.scheduledTime=Date.now(),this.schedulePlayLoop(this.sampleRate)),!0}async pause(){await super.pause(),this.#t.killReverbTail(),await this.#v(),await Promise.all(Object.keys(this.#e).map(async e=>await this.#e[e]?.cancelQueue()))}async stop(e=!1){return await super.stop(),this.setTimeoutId=!1,e||(this.#t.killReverbTail(),await Promise.all(Object.keys(this.#e).map(async t=>await this.#e[t]?.cancelQueue()))),await this.#v(),this.#i.karaoke&&this.#C("intro"),this}getRealTimeVolume(){let e=this.#t.analyser,t=new Uint8Array(e.frequencyBinCount);e.getByteFrequencyData(t);let s=0;for(let i=0;i<t.length;i++)s+=t[i];return s/(t.length*100)}getSongTimeRemaining(){return this.ticksToSeconds(this.getCurrentTick(),this.totalTicks)}async skipToSeconds(e){let t=this.getSongTime();if(e<0||e>t)throw e+" seconds not within song time of "+t;return await this.skipToTick(this.secondsToTicks(e)),this}async generateWaveformSVG(e=1e3){if(!this.totalTicks||!this.events)return"";let t=new Array(e).fill(0),s=this.totalTicks/e,i=this.events.flatMap((u,d)=>u.map(p=>({...p,computedChannel:p.channel!==void 0?p.channel:d}))).filter(u=>u.name==="Controller Change"||u.name==="Program Change"||u.name==="Note on"&&u.velocity>0).sort((u,d)=>u.tick-d.tick),a=new Map,o=new Map;i.forEach(u=>{let d=Math.floor(u.tick/s);if(d>=e)return;let p=u.computedChannel;if(a.has(p)||a.set(p,100),o.has(p)||o.set(p,127),u.name==="Controller Change")u.number===7?a.set(p,u.value):u.number===11&&o.set(p,u.value);else if(u.name==="Note on"){let y=a.get(p)/127,k=o.get(p)/127,g=u.velocity*y*k;t[d]+=g}});let n=t.reduce((u,d)=>isNaN(d)?u:d>u?d:u,0),r=n>0?t.map(u=>isNaN(u)?0:u/n):t.fill(0),c=e,h=c/5,l=r.map((u,d)=>{let p=d,y=Math.max(0,Math.min(h,h-u*h));return"".concat(p,",").concat(y.toFixed(2))}),m="M 0,".concat(h," L ").concat(l.join(" L ")," L ").concat(c,",").concat(h);return'<svg class="midiaudioplayer-waveform" viewBox="0 0 '.concat(c," ").concat(h,'" preserveAspectRatio="none"><path d="').concat(m,'" fill="none" stroke-linecap="round" stroke-linejoin="round" /></svg>')}async hashBuffer(e,t="SHA-256"){let s=await crypto.subtle.digest(t,e);return Array.from(new Uint8Array(s)).map(a=>a.toString(16).padStart(2,"0")).join("")}async#E(){this.#d&&clearTimeout(this.#d),this.#d=setTimeout(async()=>{let e=await this.getSongSetup();queueMicrotask(()=>this.triggerPlayerEvent("setupChange",e))},1e3)}async triggerPlayerEvent(e,t){e!="fileLoaded"&&(e=="computed"?(this.#n=await this.#F(),super.triggerPlayerEvent(e,{title:this.#g,karaoke:this.#T,vocalChannel:this.#n,tempo:this.tempo,division:this.division,duration:this.getSongTime(),sampleRate:this.sampleRate,totalTicks:this.totalTicks,totalEvents:this.totalEvents,channels:await this.#l})):e=="endOfFile"&&this.#i.karaoke?queueMicrotask(()=>super.triggerPlayerEvent(e,t)):super.triggerPlayerEvent(e,t))}async playLoop(e){if(this.inLoop)return;if(!e&&this.endOfFile()&&this.tick>0){await this.stop(!0),this.tick=0,this.triggerPlayerEvent("endOfFile");return}this.inLoop=!0,this.tick=this.getCurrentTick();let t=this.tracks.length;for(let s=0;s<t;s++){let i=this.tracks[s].handleEvent(this.tick,e);if(!i)continue;let a=i.constructor===Array,o=a?i.length:1;for(let n=0;n<o;n++){let r=a?i[n]:i,{name:c,data:h,value:l}=r;c==="Set Tempo"&&this.setTempo(h),e?c==="Program Change"&&!this.instruments.includes(l)&&this.instruments.push(l):this.emitEvent(r)}}!e&&this.isPlaying()&&this.triggerPlayerEvent("playing",{tick:this.tick}),this.inLoop=!1}schedulePlayLoop(e){this.setTimeoutId=setTimeout(()=>{if(this.setTimeoutId===!1)return;this.playLoop();let t=this.#r.currentTime;this._lastAudioTime||(this._lastAudioTime=t);let s=t-this._lastAudioTime;this._lastAudioTime=t;let i=this.sampleRate/1e3,a=s-i,o=Math.max(0,this.sampleRate-a*1e3);this.schedulePlayLoop(o)},e)}emitEvent(e){this.#D(e)}ticksToSeconds(e,t){if(t===void 0&&(t=e,e=0),e>=t)return 0;let s=0,i=this.tempoMap.length,a=60/this.division,o=0,n=i-1,r=0;for(;o<=n;){let h=o+n>>1;this.tempoMap[h].tick<=e?(r=h,o=h+1):n=h-1}let c=e;for(let h=r;h<i;h++){let l=this.tempoMap[h],m=h+1<i?this.tempoMap[h+1].tick:t;if(m<=e)continue;let u=Math.max(l.tick,e),d=Math.min(m,t);if(u>=t)break;s+=(d-u)/l.tempo*a,c=d}if(c<t){let h=this.tempoMap[i-1];s+=(t-c)/h.tempo*a}return s}secondsToTicks(e){let t=e,s=this.tempoMap.length,i=60/this.division;for(let a=0;a<s;a++){let o=this.tempoMap[a],c=((a+1<s?this.tempoMap[a+1].tick:1/0)-o.tick)/o.tempo*i;if(t<=c)return o.tick+Math.round(t*o.tempo/i);t-=c}return this.totalTicks}getTickBeforeSeconds(e,t){if(e<=0)return 0;let s=this.ticksToSeconds(0,e),i=Math.max(0,s-t);return this.secondsToTicks(i)}async skipToTick(e){let t=Math.max(0,Math.min(e,this.totalTicks||0)),s=this.isPlaying();if(this.#v(),Object.keys(this.channels).forEach(i=>this.channels[i]?.cancelQueue?.()),s&&super.pause(),this.startTick=t,this.tick=t,this.tempoMap&&this.tempoMap.length>0){for(let i=this.tempoMap.length-1;i>=0;i--)if(this.tempoMap[i].tick<=t){this.setTempo(this.tempoMap[i].tempo);break}}try{let i=[],a=[],o=[],n=[];this.#b(t).forEach(r=>{let c=r.channel;if(!((c===void 0||!this.channels[c])&&r.name!=="Karaoke Event"))switch(r.name){case"Controller Change":i[r.channel]=r;break;case"Program Change":a[r.channel]=r;break;case"Pitch Bend":o[r.channel]=r;break;case"Karaoke Event":n[r.channel]=r;break}}),i.forEach(r=>this.emitEvent(r)),a.forEach(r=>this.emitEvent(r)),o.forEach(r=>this.emitEvent(r)),n.forEach(r=>this.triggerPlayerEvent("karaoke",{type:r.type,tick:r.tick,html:r.text}))}catch(i){console.warn("Chase MIDI Error:",i),this.#f("Chase MIDI Error:",i)}return this.tracks&&this.tracks.length>0&&this.tracks.forEach((i,a)=>{let o=this.events[a];if(o&&o.length>0){let n=0,r=o.length-1,c=o.length;for(;n<=r;){let h=n+r>>1;o[h].tick>=t?(c=h,r=h-1):n=h+1}i.eventIndex=c}else typeof i.setEventIndexByTick=="function"&&i.setEventIndexByTick(t)}),s?this.play():this.triggerPlayerEvent("playing",{tick:t}),this}#b(e){let t={};if(!this.events)return[];for(let s=0;s<this.events.length;s++){let i=this.events[s];if(!i||i.length===0)continue;let a=0,o=i.length-1,n=i.length;for(;a<=o;){let r=a+o>>1;i[r].tick>=e?(n=r,o=r-1):a=r+1}for(let r=0;r<n;r++){let c=i[r],h;c.name==="Program Change"?h="pc:"+c.channel:c.name==="Controller Change"?h="cc:"+c.channel+":"+c.number:c.name==="Pitch Bend"?h="pb:"+c.channel:c.name==="Karaoke Event"&&(h="ke:"+c.channel),h&&(t[h]=c)}}return Object.keys(t).map(s=>t[s])}async#k(){this.events&&this.#b(1).forEach(e=>{let t=e.channel;if(this.#e[t])switch(e.name){case"Controller Change":this.#e[t].setController(e.number,e.value);break;case"Pitch Bend":this.#e[t].setPitchBend?.(e.value);break;case"Program Change":e.value>=0&&e.value<=127&&this.#o[e.value+1]!==void 0&&e.channel!=10&&this.#e[t].preset?.program!==e.value+1&&this.#e[t].setPreset(this.#o[e.value+1]);break}})}async#N(){let e={},t=new Set;return this.events.forEach(s=>{s.forEach(i=>{if(i.name==="Program Change"&&i.value>=0&&i.value<=127){if(e[i.channel])return;i.channel==10?e[i.channel]=-1:e[i.channel]=i.value+1}else i.name==="Note on"&&i.channel==10?(e[i.channel]=-1,t.add(10)):i.name==="Note on"&&t.add(i.channel)})}),Object.keys(e).forEach(s=>{t.has(Number(s))||delete e[s]}),e}async#M(){let e=new Set;return this.events.forEach(t=>{t.forEach(s=>{s.name==="Program Change"&&s.value>=0&&s.value<=127?e.add(s.channel==10?-1:s.value+1):s.name==="Note on"&&s.channel==10&&e.add(-1)})}),e}async#I(e){let t=await this.getProgramInstruments(e);if(!t.length)return null;let s=null;return this.#i.preferred.some(i=>{let a=new RegExp("_".concat(i,"$"),"i"),o=t.filter(n=>a.test(n.id));if(o.length)return s=o[Math.floor(Math.random()*o.length)],!0}),s||(s=t[Math.floor(Math.random()*t.length)]),this.#c[e]=s,await this.getPreset(s.id)}async#L(e){let t=await this.getProgramInstruments(e);if(!t.length)return null;let s=null;return this.#i.preferred.some(i=>{let a=new RegExp("_".concat(i,"$"),"i");if(s=t.find(o=>a.test(o.id)),s)return!0}),s||(s=t[0]),this.#c[e]=s,await this.getPreset(s.id)}async#P(e){return new q(e,this.#r,this.#t)}async#D(e){if(this.isPlaying())switch(e.name){case"Note on":if(e.tick<this.tick-100||e.noteNumber===void 0||e.channel==this.#n&&this.#i.muteExpression)return;if(e.velocity>0&&e.velocity<=127){if(this.#S(e.channel,e.noteNumber),this.#u[e.channel]==0)return;let t=e.velocity/127,s=f.REFERENCE_GAIN*Math.pow(t,2)*this.#u[e.channel],i=this.#e[e.channel]?.queueWaveTable(0,e.noteNumber,2,s);i&&this.#O(e.channel,e.noteNumber,i)}else this.#S(e.channel,e.noteNumber);break;case"Note off":if(e.noteNumber===void 0)return;this.#S(e.channel,e.noteNumber);break;case"Controller Change":this.#e[e.channel]?.setController(e.number,e.value);break;case"Pitch Bend":this.#e[e.channel]?.setPitchBend?.(e.value);break;case"Program Change":return;case"Karaoke Event":if(e.tick<this.tick-this.secondsToTicks(10))return;this.triggerPlayerEvent("karaoke",{type:e.type,tick:e.tick,html:e.text});break}}#O(e,t,s){this.#a[e]||(this.#a[e]=new Map),this.#a[e].set(t,s),this.#w();let i=(s.duration||0)*1e3;s.cleanupTimer=setTimeout(()=>{this.#a[e]?.get(t)===s&&(this.#a[e].delete(t),this.#w())},i+50)}#S(e,t){let s=this.#e[e],i=this.#a[e]?.get(t);if(i){i.cleanupTimer&&clearTimeout(i.cleanupTimer);let a=()=>{this.#a[e]?.delete(t),this.#w()};s&&s.isSustainActive()?s.registerSustainNote(()=>i.cancel(!1)):i.cancel(!1),a()}}#v(){Object.keys(this.#a).forEach(e=>{this.#a[e].forEach((t,s)=>{t&&(t.cleanupTimer&&clearTimeout(t.cleanupTimer),t.cancel&&t.cancel(!0)),this.#a[e]?.delete(s)})}),this.#w()}async#w(){let e=!1,t={};Object.keys(this.#e).forEach(s=>{let i=!!(this.#a[s]?.size&&this.#a[s].size>0);t[s]=i,this.#h[s]!==i&&(e=!0)}),e&&(this.#h=t,this.triggerPlayerEvent("channelState",this.#h))}async#R(e){let t=new Uint8Array(e),s=new DataView(e);if(String.fromCharCode(...t.slice(0,4))!=="MThd")throw new Error("Invalid MIDI file (MThd missing)");let a=s.getUint32(4),o=s.getUint16(8),n=s.getUint16(10),r=s.getUint16(12),c=[255,47,0],h=[],l=8+a;for(;l<t.length&&!(l+8>t.length);){let g=String.fromCharCode(...t.slice(l,l+4)),T=s.getUint32(l+4),S=l+8,x=S+T;if(g!=="MTrk"){let C=Math.min(x,t.length);h.push({tag:g,data:t.slice(l,C),repaired:!1}),l=x;continue}let E=h.filter(C=>C.tag==="MTrk").length+1,b=Math.min(T,t.length-S),v=t.slice(S,S+b),N=v.slice(-3);if(N[0]===255&&N[1]===47&&N[2]===0&&b===T)h.push({tag:g,data:v,repaired:!1});else{let C;if(b<T){let tt=T-b,K=v.slice(-2);K[0]===255&&K[1]===47?(C=new Uint8Array(v.length+1),C.set(v),C[v.length]=0):(C=new Uint8Array(v.length+3),C.set(v),C.set(c,v.length))}else C=new Uint8Array(v.length+3),C.set(v),C.set(c,v.length);h.push({tag:g,data:C,repaired:!0})}l=x}let m=h.filter(g=>g.tag==="MTrk").length,u=14+h.reduce((g,T)=>g+8+T.data.length,0),d=new Uint8Array(u),p=new DataView(d.buffer);d.set([77,84,104,100],0),p.setUint32(4,6),p.setUint16(8,o),p.setUint16(10,m),p.setUint16(12,r);let y=14;for(let g of h){let T=g.tag.split("").map(S=>S.charCodeAt(0));d.set(T,y),p.setUint32(y+4,g.data.length),d.set(g.data,y+8),y+=8+g.data.length}let k=h.filter(g=>g.repaired).length;return d.buffer}#B(){if(!this.events||this.events.length===0)return;let e=1/0,t=0;if(this.events.forEach(n=>{n.forEach(r=>{(r.name==="Note on"||r.name==="Note off")&&(r.tick<e&&(e=r.tick),r.tick>t&&(t=r.tick))})}),e===1/0)return;let s=[];this.events.forEach((n,r)=>{n.forEach(c=>{let h=c.name==="Program Change"||c.name==="Controller Change"||c.name==="Pitch Bend"||c.name==="Set Tempo";c.tick<e&&h&&s.push({event:c,trackIdx:r})})});let i=Object.fromEntries(this.events.map((n,r)=>[r,[]])),a=new Set;for(let n=s.length-1;n>=0;n--){let{event:r,trackIdx:c}=s[n],h=r.channel!==void 0?r.channel:"track-".concat(c),l=null;if(r.name==="Program Change"?l="pc:".concat(h):r.name==="Controller Change"?l="cc:".concat(h,":").concat(r.number):r.name==="Pitch Bend"?l="pb:".concat(h):r.name==="Set Tempo"&&(l="tempo"),l&&!a.has(l)){a.add(l);let m={...r,tick:0};i[c].push(m)}}let o=this.events.map((n,r)=>{let c=[];return n.forEach(l=>{let m=l.name==="Program Change"||l.name==="Controller Change"||l.name==="Pitch Bend"||l.name==="Set Tempo",u=l.name==="Text Event"||l.name==="Lyric Event"||l.name==="Track Name"||l.name==="Karaoke Event";if(l.tick<e)!m&&(u||r===0)&&(l.tick=0,c.push(l));else{l.tick=l.tick-e;let d=t-e;l.tick>d&&(l.tick=d),c.push(l)}}),[...i[r]||[],...c].sort((l,m)=>l.tick-m.tick)});this.events=o,this.totalTicks=t-e,typeof this.computeTempoMap=="function"&&this.computeTempoMap()}async#A(){if(this.#y)return this.#y;let e={language:"",title:"",paragraphs:[]},t=null,s=0;if(this.events.forEach(c=>{let h=c.filter(m=>m.name==="Text Event"||m.name==="Lyric Event"||m.name==="Cue Point"||m.name==="Marker"||m.name==="Track Name"),l=h.filter(m=>{let u=m.string||m.text||"";return u&&!u.startsWith("@")}).length;l>s&&(s=l,t=h)}),!t||t.length===0)return e;let i=t.sort((c,h)=>c.tick-h.tick),a=[],o=[],n=[],r=0;for(i.forEach(c=>{let h=this.#K(c.string||"");if(!h||/^Track-/i.test(h.trim())||/^Piste/i.test(h.trim())||h.trim()===""||c.tick===0&&h.length>20)return;if(h.startsWith("@L")){e.language=h.substring(2).trim();return}if(h.startsWith("@T")){e.title+=(e.title?" / ":"")+h.substring(2).trim();return}if(h.startsWith("@")||h.startsWith("(")||h.startsWith("PART")||/^\d+\s+\d+/.test(h.trim()))return;if(/^(Verse|Chorus|Bridge|Break|Intro|End\.)/i.test(h.trim())){let k=h.startsWith("\\")||h.startsWith("/"),g=n.length===0||c.tick-r>500;if((k||g)&&(n.length>0&&(o.push({tick:n[0].tick,blocks:n}),n=[]),o.length>0)){for(;o.length>4;){let T=o.splice(0,4);a.push({tick:T[0].tick,lines:T})}o.length>0&&(a.push({tick:o[0].tick,lines:o}),o=[])}return}let l=!1;if(n.length>0){let g=n[n.length-1].text,T=h.trimLeft();if(T.length>0){let S=/^[A-Z]/.test(T)||/^'[A-Z]/.test(T),x=/^[A-Z]/.test(g.trim())||/^'[A-Z]/.test(g.trim());S&&!g.endsWith(" ")&&g.trim()!="o"&&!x&&c.tick>r&&(l=!0),h.startsWith('"')&&g.endsWith('"')&&(l=!0),h.startsWith('"')&&(g.endsWith(")")||g.endsWith(')"'))&&(l=!0),T.startsWith('"')&&g.trimRight().endsWith(")")&&(l=!0)}}let m=h.startsWith("\\"),u=h.startsWith("/")||l;(m||u)&&(h.startsWith("\\")||h.startsWith("/"))&&(h=h.substring(1)),h=h.replace(/[\r\n]/g,"");let d=!1;r>0&&c.tick>r&&this.ticksToSeconds(r,c.tick)>2.5&&(d=!0);let y=n.reduce((k,g)=>k+g.text.length,0)+h.length>this.#i.maxCharPerLine;if((u||m||d||y)&&(n.length>0&&(o.push({tick:n[0].tick,blocks:n}),n=[]),o.length>0)){if(m||d){for(;o.length>4;){let k=o.splice(0,4);a.push({tick:k[0].tick,lines:k})}o.length>0&&(a.push({tick:o[0].tick,lines:o}),o=[])}else if(o.length>=6){let k=o.splice(0,4);a.push({tick:k[0].tick,lines:k})}}h.length>0&&(n.push({text:h,tick:c.tick}),r=c.tick)}),n.length>0&&o.push({tick:n[0].tick,blocks:n});o.length>4;){let c=o.splice(0,4);a.push({tick:c[0].tick,lines:c})}return o.length>0&&a.push({tick:o[0].tick,lines:o}),a=a.filter(c=>!(c.lines.length==1&&c.lines[0].blocks.length==1&&["intro","outro","sfx","solo","chorus","verse","bridge","break","end"].includes(c.lines[0].blocks[0].text.toLowerCase().trim()))),a.length<=2&&(a=[]),e.paragraphs=a,this.#y=e,e}async#V(){let e=await this.#A();if(!e.paragraphs.length){this.#T=!1,this.events[f.KARAOKE_CHANNEL].push({text:'<span class="karaoke-intro"></span>',name:"Karaoke Event",type:"intro",tick:0,channel:f.KARAOKE_CHANNEL}),this.events[f.KARAOKE_CHANNEL]=this.events[f.KARAOKE_CHANNEL].sort((l,m)=>l.tick-m.tick);return}this.#T=!0,this.#g=e.title;let t=0,s=this.secondsToTicks(this.#i.karaokeDelay),i=this.secondsToTicks(3),a=this.secondsToTicks(5),o=this.secondsToTicks(7),n=this.secondsToTicks(10),r=[];e.paragraphs.forEach((l,m)=>{l.lines.forEach((u,d)=>{u.blocks.forEach(p=>{r.push({block:p,lineIdx:d,paraIdx:m,paragraph:l,fastLinesText:l.lines.map(y=>y.blocks.map(k=>k.text).join(""))})})})});let c=[];e.paragraphs.forEach((l,m)=>{let u=this.getTickBeforeSeconds(l.tick,5);u<t&&(u=t+(l.tick-t)/2),m===0&&u<20&&(u=20),c[m]=u;let p=l.lines.map(y=>y.blocks.map(k=>k.text).join("")).map(y=>'<span class="karaoke-coming">'.concat(y,"</span>")).join("<br/>");if(this.events[f.KARAOKE_CHANNEL].push({text:p,name:"Karaoke Event",type:"lyric",tick:u,channel:f.KARAOKE_CHANNEL}),l.lines.length>0){let y=l.lines[l.lines.length-1];y.blocks.length>0&&(t=y.blocks[y.blocks.length-1].tick)}}),(c[0]||0)>25&&this.events[f.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:5,channel:f.KARAOKE_CHANNEL}),r.forEach((l,m)=>{let u=l.block,d=l.lineIdx,p=l.paraIdx,y=l.paragraph,k=l.fastLinesText,g=(S=!1)=>y.lines.map((x,E)=>{if(E<d)return'<span class="karaoke-played">'.concat(k[E],"</span>");if(E>d)return'<span class="karaoke-coming">'.concat(k[E],"</span>");let b="";return x.blocks.forEach(v=>{let N="coming";S||v.tick<u.tick?N="played":v.tick===u.tick&&(N="playing"),b+='<span class="karaoke-'.concat(N,'">').concat(v.text,"</span>")}),b}).join("<br>");this.events[f.KARAOKE_CHANNEL].push({text:g(!1),name:"Karaoke Event",type:"lyric",tick:u.tick-s,channel:f.KARAOKE_CHANNEL});let T=r[m+1];if(T){let S=T.block.tick-u.tick;if(S>i){let x=u.tick+i,E=u.tick+o,b=S>n&&p>0;if(T.paraIdx!==p){let v=c[T.paraIdx];x>=v&&(x=v-1),b&&(E>=v||v-E<i)&&(b=!1)}x>u.tick&&this.events[f.KARAOKE_CHANNEL].push({text:g(!0),name:"Karaoke Event",type:"lyric",tick:x-s,channel:f.KARAOKE_CHANNEL}),b&&E>x&&this.events[f.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:E-s,channel:f.KARAOKE_CHANNEL})}}t=u.tick}),this.totalTicks-t>this.secondsToTicks(5)?this.events[f.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:t+this.secondsToTicks(5),channel:f.KARAOKE_CHANNEL}):this.events[f.KARAOKE_CHANNEL].push({text:'<span class="karaoke-clear"></span>',name:"Karaoke Event",type:"clear",tick:this.totalTicks-1,channel:f.KARAOKE_CHANNEL}),this.events[f.KARAOKE_CHANNEL]=this.events[f.KARAOKE_CHANNEL].sort((l,m)=>l.tick-m.tick)}async#F(){let e=await this.#A();if(!e?.paragraphs?.length)return null;let t=e.paragraphs.flatMap(c=>c.lines.flatMap(h=>h.blocks.map(l=>l.tick)));if(t.length===0)return null;let s=this.division?this.division/2:48,i=48,a=84,o=Object.keys(this.#l).map(Number).filter(c=>c!==10),n=null,r=-1/0;for(let c of o){let h=this.events.flatMap(E=>E.filter(b=>b.name==="Note on"&&b.velocity>0&&b.channel===c));if(h.length===0)continue;let m=t.filter(E=>h.some(b=>Math.abs(b.tick-E)<=s)).length/t.length,d=h.filter(E=>E.noteNumber>=i&&E.noteNumber<=a).length/h.length;if(d<.3)continue;let p=[...h].sort((E,b)=>E.tick-b.tick),y=this.division/8||6,g=1-p.filter((E,b)=>b>0&&Math.abs(E.tick-p[b-1].tick)<y).length/Math.max(h.length-1,1),T=h.length/Math.max(t.length,1),S=T<.3?T/.3:T>5?Math.max(0,1-(T-5)/10):1,x=m*.45+d*.35+g*.15+S*.05;x>r&&(r=x,n=c)}return r>=.4?n:null}#K(e){if(!e)return"";let t=new Uint8Array(e.length);for(let a=0;a<e.length;a++)t[a]=e.charCodeAt(a)&255;let i=new TextDecoder("windows-1252").decode(t);return i=i.replace(/ÿ/g,""),i=i.replace(/’/g,"'"),i=i.replace(/`/g,"'"),i}#C(e="clear",t=""){let s='<span class="karaoke-'.concat(e,'">').concat(t.replace(/\s\/\s/g,"<br>"),"</span>");this.#i.karaoke&&(e=="title"?queueMicrotask(()=>this.triggerPlayerEvent("karaoke",{type:e,title:t,html:s})):queueMicrotask(()=>this.triggerPlayerEvent("karaoke",{type:e,html:s})))}#f(e,t=!1){queueMicrotask(()=>this.triggerPlayerEvent("logs",e))}};typeof window<"u"&&(window.MidiAudioPlayer=L);var ut=L;})();
|
package/package.json
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "midi-audio-player",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Real MIDI playback in the browser — powered by Web Audio API and WebAudioFont.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"midi",
|
|
7
7
|
"midi-player",
|
|
8
8
|
"webaudio",
|
|
9
9
|
"webaudiofont",
|
|
10
|
+
"webaudiofonts",
|
|
10
11
|
"audio",
|
|
11
12
|
"music",
|
|
12
13
|
"browser-audio",
|
|
13
14
|
"midi-js",
|
|
14
15
|
"web-audio-api",
|
|
15
|
-
"midi-playback"
|
|
16
|
+
"midi-playback",
|
|
17
|
+
"midi-audio-player",
|
|
18
|
+
"html5",
|
|
19
|
+
"midi-player-js"
|
|
16
20
|
],
|
|
17
|
-
"homepage": "https://webaudiofonts.com/",
|
|
21
|
+
"homepage": "https://webaudiofonts.com/midiaudioplayer/",
|
|
18
22
|
"bugs": {
|
|
19
23
|
"url": "https://github.com/WebAudioFonts/midi-audio-player/issues"
|
|
20
24
|
},
|
|
@@ -35,7 +39,6 @@
|
|
|
35
39
|
"index.js",
|
|
36
40
|
"index.d.ts",
|
|
37
41
|
"README.md",
|
|
38
|
-
"CHANGELOG.md",
|
|
39
42
|
"LICENSE"
|
|
40
43
|
],
|
|
41
44
|
"exports": {
|
|
@@ -52,12 +55,16 @@
|
|
|
52
55
|
},
|
|
53
56
|
"dependencies": {
|
|
54
57
|
"midi-player-js": "^2.0.17",
|
|
55
|
-
"webaudiofontplayer": "^1.0.
|
|
58
|
+
"webaudiofontplayer": "^1.0.2"
|
|
56
59
|
},
|
|
57
60
|
"devDependencies": {
|
|
58
61
|
"esbuild": "^0.28.0"
|
|
59
62
|
},
|
|
60
63
|
"engines": {
|
|
61
64
|
"node": ">=18"
|
|
65
|
+
},
|
|
66
|
+
"allowScripts": {
|
|
67
|
+
"esbuild@0.28.0": true,
|
|
68
|
+
"midi-player-js@2.0.17": true
|
|
62
69
|
}
|
|
63
70
|
}
|
package/src/midiaudioplayer.js
CHANGED
|
@@ -175,7 +175,7 @@ export default class MidiAudioPlayer extends MidiPlayer.Player {
|
|
|
175
175
|
if(!presetInfo) throw new Error(`Invalid preset: ${presetId}`);
|
|
176
176
|
this.#presetMap[presetInfo.program] = presetInfo;
|
|
177
177
|
const preset = await this.getPreset(presetId);
|
|
178
|
-
this.#players[channel].preset
|
|
178
|
+
await this.#players[channel].setPreset(preset);
|
|
179
179
|
this.#setupChange();
|
|
180
180
|
}
|
|
181
181
|
|
|
@@ -676,12 +676,11 @@ export default class MidiAudioPlayer extends MidiPlayer.Player {
|
|
|
676
676
|
break;
|
|
677
677
|
case 'Program Change':
|
|
678
678
|
if (
|
|
679
|
-
// (this.#opts.presetAuto || this.#opts.presetRandom) &&
|
|
680
679
|
event.value >= 0 && event.value <= 127 &&
|
|
681
680
|
this.#instruments[event.value + 1] !== undefined &&
|
|
682
681
|
event.channel != 10) {
|
|
683
682
|
if (this.#players[channel].preset?.program !== (event.value + 1)) {
|
|
684
|
-
this.#players[channel].
|
|
683
|
+
this.#players[channel].setPreset(this.#instruments[event.value + 1]);
|
|
685
684
|
}
|
|
686
685
|
}
|
|
687
686
|
break;
|
|
@@ -799,7 +798,7 @@ export default class MidiAudioPlayer extends MidiPlayer.Player {
|
|
|
799
798
|
if(!this.#players[event.channel]) return;
|
|
800
799
|
if(event.channel == 10 || event.value > 127 || event.value < 0) break;
|
|
801
800
|
if(this.#players[event.channel] !== undefined && this.#players[event.channel].preset.program != (event.value + 1))
|
|
802
|
-
this.#players[event.channel].
|
|
801
|
+
this.#players[event.channel].setPreset(this.#instruments[event.value + 1]);
|
|
803
802
|
break;
|
|
804
803
|
case 'Karaoke Event':
|
|
805
804
|
if (event.tick < (this.tick - this.secondsToTicks(10))) return;
|
package/CHANGELOG.md
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
## Changelog
|
|
2
|
-
|
|
3
|
-
### [2.0.0] - Unreleased
|
|
4
|
-
|
|
5
|
-
#### Changed
|
|
6
|
-
* Complete refactor
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
### [1.1.2] - 2026-05-10 00:19:40
|
|
10
|
-
|
|
11
|
-
#### Changed
|
|
12
|
-
* load() method now public
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
### [1.1.1] - 2026-05-09 19:45:23
|
|
16
|
-
|
|
17
|
-
#### Added
|
|
18
|
-
* index.js in the root
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
### [1.1.0] - 2026-05-09 17:16:45
|
|
22
|
-
|
|
23
|
-
#### Added
|
|
24
|
-
* Complete refactor
|
|
25
|
-
* Optimized WebAudioFont handling
|
|
26
|
-
* Change instrument option to preset
|