midi-audio-player 2.0.0 → 2.0.2

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.
@@ -7,11 +7,11 @@
7
7
  ██║ ╚═╝ ██║██║██████╔╝██║██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝██║ ███████╗██║ ██║ ██║ ███████╗██║ ██║
8
8
  ╚═╝ ╚═╝╚═╝╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
9
9
 
10
- Version: 2.0.0
11
- Build: 2026-05-29 17:16:44
10
+ Version: 2.0.2
11
+ Build: 2026-06-02 12:19:59
12
12
  Author: Maxime Larrivée-Roy <mlarriveeroy@gmail.com>
13
13
  Github: https://github.com/webaudiofonts/midi-audio-player/
14
- Website: https://webaudiofonts.github.io/midi-audio-player/
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,34 @@
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.#preset.zones.map((zone) => this.#adjustZone(zone));
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, nonblocking = false) {
1247
1256
  this.#preset = preset;
1248
- this.#preset.zones.map((zone) => this.#adjustZone(zone));
1257
+ if (nonblocking) this.#preset.zones.map((zone) => this.#adjustZone(zone));
1258
+ else await Promise.all(this.#preset.zones.map((zone) => this.#adjustZone(zone)));
1249
1259
  }
1250
1260
  close() {
1251
1261
  const now = this.#audioCtx.currentTime;
@@ -1380,30 +1390,24 @@
1380
1390
  break;
1381
1391
  }
1382
1392
  }
1383
- #adjustZone(zone) {
1384
- if (zone.buffer) return Promise.resolve(zone);
1393
+ async #adjustZone(zone) {
1394
+ if (zone.buffer) return zone;
1385
1395
  zone.delay = 0;
1386
1396
  if (zone.file) {
1387
- const decoded = atob(zone.file);
1388
- const uint8Array = new Uint8Array(decoded.length);
1389
- for (let i = 0; i < decoded.length; i++) uint8Array[i] = decoded.charCodeAt(i);
1390
- this.#audioCtx.decodeAudioData(
1391
- uint8Array.buffer,
1392
- (audioBuffer) => {
1393
- zone.buffer = audioBuffer;
1394
- this.#applyZoneParameters(zone);
1395
- return zone;
1396
- },
1397
- (error) => {
1398
- console.error("Audio decoding error:", error);
1399
- console.warn(this.#preset);
1400
- return false;
1401
- }
1402
- );
1397
+ const binary = atob(zone.file);
1398
+ const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
1399
+ try {
1400
+ zone.buffer = await this.#audioCtx.decodeAudioData(bytes.buffer);
1401
+ this.#applyZoneParameters(zone);
1402
+ } catch (error) {
1403
+ console.error("Audio decoding error:", error);
1404
+ console.warn(this.#preset);
1405
+ return false;
1406
+ }
1403
1407
  } else {
1404
1408
  this.#applyZoneParameters(zone);
1405
- return zone;
1406
1409
  }
1410
+ return zone;
1407
1411
  }
1408
1412
  #applyZoneParameters(zone) {
1409
1413
  zone.loopStart = this.#numValue(zone.loopStart, 0);
@@ -1495,9 +1499,7 @@
1495
1499
  return envelope;
1496
1500
  }
1497
1501
  #findZone(pitch) {
1498
- const zone = this.#preset.zones.findLast((z) => pitch >= z.keyRangeLow && pitch <= z.keyRangeHigh + 1);
1499
- if (zone) this.#adjustZone(zone);
1500
- return zone;
1502
+ return this.#preset.zones.findLast((z) => pitch >= z.keyRangeLow && pitch <= z.keyRangeHigh);
1501
1503
  }
1502
1504
  #limitVolume(v) {
1503
1505
  const requestedVolume = v ? 1 * v : 0.5;
@@ -1958,13 +1960,16 @@
1958
1960
  throw new Error("Invalid preset: ".concat(id));
1959
1961
  }
1960
1962
  }
1961
- async loadPreset(presetId, channel) {
1963
+ async loadPreset(presetId, channel, nonblocking = false) {
1962
1964
  const presetInfo = await this.findPreset(presetId);
1963
1965
  if (!presetInfo) throw new Error("Invalid preset: ".concat(presetId));
1964
1966
  this.#presetMap[presetInfo.program] = presetInfo;
1965
1967
  const preset = await this.getPreset(presetId);
1966
- this.#players[channel].preset = preset;
1967
- this.#setupChange();
1968
+ if (nonblocking) this.#players[channel].setPreset(preset, nonblocking).then(() => this.#setupChange());
1969
+ else {
1970
+ await this.#players[channel].setPreset(preset, nonblocking);
1971
+ this.#setupChange();
1972
+ }
1968
1973
  }
1969
1974
  async load(content, setup) {
1970
1975
  if (typeof content === "string") {
@@ -1992,6 +1997,15 @@
1992
1997
  } catch (e) {
1993
1998
  await this.loadArrayBuffer(await this.#repairMidi(content));
1994
1999
  }
2000
+ if (this.#opts.karaoke) {
2001
+ this.#log("Generating karaoke frames...");
2002
+ this.#lyrics = null;
2003
+ await this.#generateKaraokeFrames();
2004
+ if (this.#title) this.#sendKaraokeFrame("title", this.#title);
2005
+ }
2006
+ this.#log("Trim midi events...");
2007
+ this.#trimMidiEvents();
2008
+ queueMicrotask(() => this.triggerPlayerEvent("computed"));
1995
2009
  this.#log("Loading instruments...");
1996
2010
  this.#channels = await this.#getInstruments();
1997
2011
  this.#channelStates = Object.keys(this.#channels).reduce((acc, key) => ({ ...acc, [key]: false }), {});
@@ -2002,8 +2016,8 @@
2002
2016
  this.#channelVolumes[channel] = setup.volumes[channel];
2003
2017
  }));
2004
2018
  }
2005
- const setupPrograms = /* @__PURE__ */ new Set();
2006
2019
  const setupPresets = {};
2020
+ const setupPrograms = /* @__PURE__ */ new Set();
2007
2021
  if (setup?.presets !== void 0) {
2008
2022
  await Promise.all(Object.keys(setup.presets).map(async (channel) => {
2009
2023
  const presetInfo = await this.findPreset(setup.presets[channel]);
@@ -2014,7 +2028,7 @@
2014
2028
  }
2015
2029
  const uniqueInstruments = await this.#getUniqueInstruments();
2016
2030
  if (!Object.values(this.#channels).length) this.#log("Error: no instrument found");
2017
- const presets = Promise.all([...uniqueInstruments].map(async (program) => {
2031
+ await Promise.all([...uniqueInstruments].map(async (program) => {
2018
2032
  if (setupPrograms.has(program)) return;
2019
2033
  let preset = null;
2020
2034
  if (this.#presetMap[program] !== void 0) preset = await this.getPreset(this.#presetMap[program].id);
@@ -2022,16 +2036,6 @@
2022
2036
  else preset = await this.#getAutoPreset(program);
2023
2037
  this.#instruments[program] = preset;
2024
2038
  }));
2025
- if (this.#opts.karaoke) {
2026
- this.#log("Generating karaoke frames...");
2027
- this.#lyrics = null;
2028
- await this.#generateKaraokeFrames();
2029
- if (this.#title) this.#sendKaraokeFrame("title", this.#title);
2030
- }
2031
- this.#log("Trim midi events...");
2032
- this.#trimMidiEvents();
2033
- queueMicrotask(() => this.triggerPlayerEvent("computed"));
2034
- await presets;
2035
2039
  await Promise.all(Object.keys(this.#channels).map(async (channel) => {
2036
2040
  if (this.#players[channel]) this.#players[channel].close();
2037
2041
  if (setupPresets[channel] !== void 0) this.#players[channel] = await this.#createPlayer(setupPresets[channel]);
@@ -2414,12 +2418,9 @@
2414
2418
  this.#players[channel].setPitchBend?.(event.value);
2415
2419
  break;
2416
2420
  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
- ) {
2421
+ if (event.value >= 0 && event.value <= 127 && this.#instruments[event.value + 1] !== void 0 && event.channel != 10) {
2421
2422
  if (this.#players[channel].preset?.program !== event.value + 1) {
2422
- this.#players[channel].preset = this.#instruments[event.value + 1];
2423
+ this.#players[channel].setPreset(this.#instruments[event.value + 1]);
2423
2424
  }
2424
2425
  }
2425
2426
  break;
@@ -2489,8 +2490,8 @@
2489
2490
  this.#presetMap[program] = preset;
2490
2491
  return await this.getPreset(preset.id);
2491
2492
  }
2492
- async #createPlayer(preset) {
2493
- return new index_default(preset, this.#audioCtx, this.#compressor);
2493
+ #createPlayer(preset) {
2494
+ return index_default.load(preset, this.#audioCtx, this.#compressor);
2494
2495
  }
2495
2496
  async #handleMidiPipeline(event) {
2496
2497
  if (!this.isPlaying()) return;
@@ -2525,7 +2526,7 @@
2525
2526
  if (!this.#players[event.channel]) return;
2526
2527
  if (event.channel == 10 || event.value > 127 || event.value < 0) break;
2527
2528
  if (this.#players[event.channel] !== void 0 && this.#players[event.channel].preset.program != event.value + 1)
2528
- this.#players[event.channel].preset = this.#instruments[event.value + 1];
2529
+ this.#players[event.channel].setPreset(this.#instruments[event.value + 1]);
2529
2530
  break;
2530
2531
  case "Karaoke Event":
2531
2532
  if (event.tick < this.tick - this.secondsToTicks(10)) return;
@@ -7,11 +7,11 @@
7
7
  ██║ ╚═╝ ██║██║██████╔╝██║██║ ██║╚██████╔╝██████╔╝██║╚██████╔╝██║ ███████╗██║ ██║ ██║ ███████╗██║ ██║
8
8
  ╚═╝ ╚═╝╚═╝╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
9
9
 
10
- Version: 2.0.0
11
- Build: 2026-05-29 17:16:44
10
+ Version: 2.0.2
11
+ Build: 2026-06-02 12:19:59
12
12
  Author: Maxime Larrivée-Roy <mlarriveeroy@gmail.com>
13
13
  Github: https://github.com/webaudiofonts/midi-audio-player/
14
- Website: https://webaudiofonts.github.io/midi-audio-player/
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(),n=i>=a;if(this.pointer<this.data.length&&(s||n)){var r=this.parseEvent();if(this.enabled)return r}}else{for(var o=[];this.events[this.eventIndex]&&this.events[this.eventIndex].tick<=t;)this.enabled&&o.push(this.events[this.eventIndex]),this.eventIndex++;if(o.length>0)return o}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 n=w.getVarIntLength(this.data.subarray(t+2)),r=w.readVarInt(this.data.subarray(t+2,t+2+n));this.pointer+=i+2+n+r}else if(this.data[t]===240){s.name="Sysex";var o=w.getVarIntLength(this.data.subarray(t+1)),c=w.readVarInt(this.data.subarray(t+1,t+1+o));s.data=this.data.subarray(t+1+o,t+1+o+c),this.pointer+=i+1+o+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 n=Array.isArray(a)?a:[a];n.forEach(function(r){r.hasOwnProperty("name")&&r.name==="Set Tempo"&&this.setTempo(r.data),this.emitEvent(r)},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 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&&(s[n]=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,n=0;n<this.tempoMap.length;n++){var r=this.tempoMap[n],o=n+1<this.tempoMap.length?this.tempoMap[n+1].tick:s;if(!(o<=t)){var c=Math.max(r.tick,t),h=Math.min(o,s);if(c>=s)break;var l=h-c;i+=l/this.division/r.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 n=this.tempoMap[a],r=a+1<this.tempoMap.length?this.tempoMap[a+1].tick:1/0,o=r-n.tick,c=o/this.division/n.tempo*60;if(s<=c)return i=n.tick+Math.round(s/60*n.tempo*this.division),i;s-=c,i=r}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,t=!1){this.#t=e,t?this.#t.zones.map(s=>this.#g(s)):await Promise.all(this.#t.zones.map(s=>this.#g(s)))}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 n=this.#b(i),r=this.#x(Math.round(t));if(!r?.buffer)return null;let o=r.originalPitch-100*r.coarseTune-r.fineTune,c=t*100,h=(this.#d-8192)/8192*this.#o*100,l=c-o+h,m=Math.pow(2,l/1200),u=Math.max(e,this.#s.currentTime),d=s+this.#a,p=r.loopStart>=1&&r.loopStart<r.loopEnd;p||(d=Math.min(d,r.buffer.duration/m));let y=this.#i();this.#k(y,r,n,u,d,s);let k=this.#s.createBufferSource();if(k.buffer=r.buffer,k.playbackRate.setValueAtTime(m,0),a?.length>0&&(k.playbackRate.setValueAtTime(m,u),a.forEach(g=>{let S=(t+g.delta)*100-o+h,x=Math.pow(2,S/1200);k.playbackRate.linearRampToValueAtTime(x,u+g.when)})),k.loop=p,p){let g=r.delay??0;k.loopStart=r.loopStart/r.sampleRate+g,k.loopEnd=r.loopEnd/r.sampleRate+g}return k.connect(y),k.start(u,r.delay??0),k.stop(u+d),y.audioBufferSourceNode=k,y.when=u,y.duration=d,y.pitch=t,y.baseDetune=o,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 n=a.pitch*100,r=a.baseDetune,o=s*100,c=n-r+o,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#g(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.#y(e.loopStart,0),e.loopEnd=this.#y(e.loopEnd,0),e.coarseTune=this.#y(e.coarseTune,0),e.fineTune=this.#y(e.fineTune,0),e.originalPitch=this.#y(e.originalPitch,6e3)}#k(e,t,s,i,a,n){e.gain.setValueAtTime(this.#h,this.#s.currentTime);let r=Math.min(n,a-this.#a),o=t.ahdsr&&t.ahdsr.length>0?t.ahdsr:[{duration:0,volume:1},{duration:r,volume:1}];e.gain.cancelScheduledValues(i);let c=(o[0]?.volume??1)*s;e.gain.linearRampToValueAtTime(this.#E(c),i+.002);let h=0,l=o[0]?.volume??1;for(let m of o){let{duration:u,volume:d}=m;if(u<=0)continue;let p=r-h;if(u>p){let y=p/u,k=l+y*(d-l);e.gain.exponentialRampToValueAtTime(this.#E(s*k),i+r);break}h+=u,l=d,e.gain.exponentialRampToValueAtTime(this.#E(s*l),i+h)}e.gain.exponentialRampToValueAtTime(this.#h,i+r+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(n=>n.target===t);a.length>0&&(a.sort((n,r)=>n.when-r.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 n=this.#s.currentTime;if(a&&i.audioBufferSourceNode){try{i.audioBufferSourceNode.stop(0),i.audioBufferSourceNode.disconnect()}catch{}i.audioBufferSourceNode=null}i.when+i.duration>n&&(i.gain.cancelScheduledValues(0),i.gain.setTargetAtTime(this.#h,n,a?.005:.02),i.when=n+1e-5,i.duration=0)},i}#x(e){return this.#t.zones.findLast(t=>e>=t.keyRangeLow&&e<=t.keyRangeHigh)}#b(e){let t=e?1*e:.5;return Math.min(t,.8)}#E(e){return e>this.#h?e:this.#h}#y(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(n=>{i=this.#m(i,n);let r=n<1e3?n:n/1024+"k";this["band".concat(r)]=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,n]of Object.entries(e)){let r=Number(a),o=this.#l.get(r);if(!o)continue;let c=Math.max(-i,Math.min(i,n));o.filter.gain.setTargetAtTime(c,s,t),o.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),r=Math.floor(.015*s);for(let o=0;o<a.numberOfChannels;o++){let c=a.getChannelData(o),h=0,l=o===1?Math.floor(.002*s):0;for(let m=0;m<i;m++){if(m<r){c[m]=0;continue}let u=(m-r)/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 z={async setVersion(f){await O(f)},async setItem(f,e,t=!1){let s=await O(),i=e,a=!1;if(t){let r=JSON.stringify(e),c=new Blob([r]).stream().pipeThrough(new CompressionStream("gzip"));i=await new Response(c).arrayBuffer(),a=!0}let n={data:i,isCompressed:a};return new Promise((r,o)=>{let l=s.transaction(A,"readwrite").objectStore(A).put(n,f);l.onsuccess=()=>r(),l.onerror=()=>o(l.error)})},async getItem(f){let e=await O(),t=await new Promise((s,i)=>{let r=e.transaction(A,"readonly").objectStore(A).get(f);r.onsuccess=()=>s(r.result),r.onerror=()=>i(r.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(f){let e=await O();return new Promise((t,s)=>{let n=e.transaction(A,"readwrite").objectStore(A).delete(f);n.onsuccess=()=>t(),n.onerror=()=>s(n.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=z;var X=(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;#g=null;#T=!1;#k="";#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=X(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.#b()}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(n=>n.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(n=>{n.instrument=i.name+" / "+a.name,s.push(n)})}))),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,s=!1){let i=await this.findPreset(e);if(!i)throw new Error("Invalid preset: ".concat(e));this.#c[i.program]=i;let a=await this.getPreset(e);s?this.#e[t].setPreset(a,s).then(()=>this.#b()):(await this.#e[t].setPreset(a,s),this.#b())}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.#k="",this.#f("Loading buffer...");try{await this.loadArrayBuffer(e)}catch{await this.loadArrayBuffer(await this.#R(e))}this.#i.karaoke&&(this.#f("Generating karaoke frames..."),this.#g=null,await this.#V(),this.#k&&this.#C("title",this.#k)),this.#f("Trim midi events..."),this.#B(),queueMicrotask(()=>this.triggerPlayerEvent("computed")),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={},i=new Set;t?.presets!==void 0&&await Promise.all(Object.keys(t.presets).map(async n=>{let r=await this.findPreset(t.presets[n]);r&&(s[n]=await this.getPreset(r.id),i.add(r.program))}));let a=await this.#M();Object.values(this.#l).length||this.#f("Error: no instrument found"),await Promise.all([...a].map(async n=>{if(i.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})),await Promise.all(Object.keys(this.#l).map(async n=>{this.#e[n]&&this.#e[n].close(),s[n]!==void 0?this.#e[n]=await this.#P(s[n]):this.#e[n]=await this.#P(this.#o[this.#l[n]])})),this.#f("Initializing instrument states..."),await this.#y(),await this.triggerPlayerEvent("presetsLoaded",this.#o),await this.#b(),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,n=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),n.has(p)||n.set(p,127),u.name==="Controller Change")u.number===7?a.set(p,u.value):u.number===11&&n.set(p,u.value);else if(u.name==="Note on"){let y=a.get(p)/127,k=n.get(p)/127,g=u.velocity*y*k;t[d]+=g}});let r=t.reduce((u,d)=>isNaN(d)?u:d>u?d:u,0),o=r>0?t.map(u=>isNaN(u)?0:u/r):t.fill(0),c=e,h=c/5,l=o.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#b(){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.#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})):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,n=a?i.length:1;for(let r=0;r<n;r++){let o=a?i[r]:i,{name:c,data:h,value:l}=o;c==="Set Tempo"&&this.setTempo(h),e?c==="Program Change"&&!this.instruments.includes(l)&&this.instruments.push(l):this.emitEvent(o)}}!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,n=Math.max(0,this.sampleRate-a*1e3);this.schedulePlayLoop(n)},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,n=0,r=i-1,o=0;for(;n<=r;){let h=n+r>>1;this.tempoMap[h].tick<=e?(o=h,n=h+1):r=h-1}let c=e;for(let h=o;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 n=this.tempoMap[a],c=((a+1<s?this.tempoMap[a+1].tick:1/0)-n.tick)/n.tempo*i;if(t<=c)return n.tick+Math.round(t*n.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=[],n=[],r=[];this.#E(t).forEach(o=>{let c=o.channel;if(!((c===void 0||!this.channels[c])&&o.name!=="Karaoke Event"))switch(o.name){case"Controller Change":i[o.channel]=o;break;case"Program Change":a[o.channel]=o;break;case"Pitch Bend":n[o.channel]=o;break;case"Karaoke Event":r[o.channel]=o;break}}),i.forEach(o=>this.emitEvent(o)),a.forEach(o=>this.emitEvent(o)),n.forEach(o=>this.emitEvent(o)),r.forEach(o=>this.triggerPlayerEvent("karaoke",{type:o.type,tick:o.tick,html:o.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 r=0,o=n.length-1,c=n.length;for(;r<=o;){let h=r+o>>1;n[h].tick>=t?(c=h,o=h-1):r=h+1}i.eventIndex=c}else typeof i.setEventIndexByTick=="function"&&i.setEventIndexByTick(t)}),s?this.play():this.triggerPlayerEvent("playing",{tick:t}),this}#E(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,n=i.length-1,r=i.length;for(;a<=n;){let o=a+n>>1;i[o].tick>=e?(r=o,n=o-1):a=o+1}for(let o=0;o<r;o++){let c=i[o],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#y(){this.events&&this.#E(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"),n=t.filter(r=>a.test(r.id));if(n.length)return s=n[Math.floor(Math.random()*n.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(n=>a.test(n.id)),s)return!0}),s||(s=t[0]),this.#c[e]=s,await this.getPreset(s.id)}#P(e){return q.load(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),n=s.getUint16(8),r=s.getUint16(10),o=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,n),p.setUint16(10,m),p.setUint16(12,o);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(r=>{r.forEach(o=>{(o.name==="Note on"||o.name==="Note off")&&(o.tick<e&&(e=o.tick),o.tick>t&&(t=o.tick))})}),e===1/0)return;let s=[];this.events.forEach((r,o)=>{r.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:o})})});let i=Object.fromEntries(this.events.map((r,o)=>[o,[]])),a=new Set;for(let r=s.length-1;r>=0;r--){let{event:o,trackIdx:c}=s[r],h=o.channel!==void 0?o.channel:"track-".concat(c),l=null;if(o.name==="Program Change"?l="pc:".concat(h):o.name==="Controller Change"?l="cc:".concat(h,":").concat(o.number):o.name==="Pitch Bend"?l="pb:".concat(h):o.name==="Set Tempo"&&(l="tempo"),l&&!a.has(l)){a.add(l);let m={...o,tick:0};i[c].push(m)}}let n=this.events.map((r,o)=>{let c=[];return r.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||o===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[o]||[],...c].sort((l,m)=>l.tick-m.tick)});this.events=n,this.totalTicks=t-e,typeof this.computeTempoMap=="function"&&this.computeTempoMap()}async#A(){if(this.#g)return this.#g;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=[],n=[],r=[],o=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=r.length===0||c.tick-o>500;if((k||g)&&(r.length>0&&(n.push({tick:r[0].tick,blocks:r}),r=[]),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 l=!1;if(r.length>0){let g=r[r.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>o&&(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;o>0&&c.tick>o&&this.ticksToSeconds(o,c.tick)>2.5&&(d=!0);let y=r.reduce((k,g)=>k+g.text.length,0)+h.length>this.#i.maxCharPerLine;if((u||m||d||y)&&(r.length>0&&(n.push({tick:r[0].tick,blocks:r}),r=[]),n.length>0)){if(m||d){for(;n.length>4;){let k=n.splice(0,4);a.push({tick:k[0].tick,lines:k})}n.length>0&&(a.push({tick:n[0].tick,lines:n}),n=[])}else if(n.length>=6){let k=n.splice(0,4);a.push({tick:k[0].tick,lines:k})}}h.length>0&&(r.push({text:h,tick:c.tick}),o=c.tick)}),r.length>0&&n.push({tick:r[0].tick,blocks:r});n.length>4;){let c=n.splice(0,4);a.push({tick:c[0].tick,lines:c})}return n.length>0&&a.push({tick:n[0].tick,lines:n}),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.#g=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.#k=e.title;let t=0,s=this.secondsToTicks(this.#i.karaokeDelay),i=this.secondsToTicks(3),a=this.secondsToTicks(5),n=this.secondsToTicks(7),r=this.secondsToTicks(10),o=[];e.paragraphs.forEach((l,m)=>{l.lines.forEach((u,d)=>{u.blocks.forEach(p=>{o.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}),o.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=o[m+1];if(T){let S=T.block.tick-u.tick;if(S>i){let x=u.tick+i,E=u.tick+n,b=S>r&&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,n=Object.keys(this.#l).map(Number).filter(c=>c!==10),r=null,o=-1/0;for(let c of n){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>o&&(o=x,r=c)}return o>=.4?r: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.0",
3
+ "version": "2.0.2",
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.0"
58
+ "webaudiofontplayer": "^1.0.3"
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
  }