midi-audio-player 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -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.1
11
+ Build: 2026-05-31 02:44:14
12
12
  Author: Maxime Larrivée-Roy <mlarriveeroy@gmail.com>
13
13
  Github: https://github.com/webaudiofonts/midi-audio-player/
14
- Website: https://webaudiofonts.github.io/midi-audio-player/
14
+ Website: https://webaudiofonts.com/midiaudioplayer/
15
15
 
16
16
  */
17
17
 
@@ -1213,7 +1213,7 @@ var index = {
1213
1213
  };
1214
1214
 
1215
1215
  // node_modules/webaudiofontplayer/dist/index.js
1216
- var WebAudioFontPlayer = class {
1216
+ var WebAudioFontPlayer = class _WebAudioFontPlayer {
1217
1217
  #audioCtx = null;
1218
1218
  #compressor = null;
1219
1219
  #preset = null;
@@ -1228,24 +1228,33 @@ var WebAudioFontPlayer = class {
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) {
1247
1256
  this.#preset = preset;
1248
- this.#preset.zones.map((zone) => this.#adjustZone(zone));
1257
+ await Promise.all(this.#preset.zones.map((zone) => this.#adjustZone(zone)));
1249
1258
  }
1250
1259
  close() {
1251
1260
  const now = this.#audioCtx.currentTime;
@@ -1380,30 +1389,24 @@ var WebAudioFontPlayer = class {
1380
1389
  break;
1381
1390
  }
1382
1391
  }
1383
- #adjustZone(zone) {
1384
- if (zone.buffer) return Promise.resolve(zone);
1392
+ async #adjustZone(zone) {
1393
+ if (zone.buffer) return zone;
1385
1394
  zone.delay = 0;
1386
1395
  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
- );
1396
+ const binary = atob(zone.file);
1397
+ const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
1398
+ try {
1399
+ zone.buffer = await this.#audioCtx.decodeAudioData(bytes.buffer);
1400
+ this.#applyZoneParameters(zone);
1401
+ } catch (error) {
1402
+ console.error("Audio decoding error:", error);
1403
+ console.warn(this.#preset);
1404
+ return false;
1405
+ }
1403
1406
  } else {
1404
1407
  this.#applyZoneParameters(zone);
1405
- return zone;
1406
1408
  }
1409
+ return zone;
1407
1410
  }
1408
1411
  #applyZoneParameters(zone) {
1409
1412
  zone.loopStart = this.#numValue(zone.loopStart, 0);
@@ -1495,9 +1498,7 @@ var WebAudioFontPlayer = class {
1495
1498
  return envelope;
1496
1499
  }
1497
1500
  #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;
1501
+ return this.#preset.zones.findLast((z) => pitch >= z.keyRangeLow && pitch <= z.keyRangeHigh);
1501
1502
  }
1502
1503
  #limitVolume(v) {
1503
1504
  const requestedVolume = v ? 1 * v : 0.5;
@@ -1963,7 +1964,7 @@ var MidiAudioPlayer = class _MidiAudioPlayer extends index.Player {
1963
1964
  if (!presetInfo) throw new Error("Invalid preset: ".concat(presetId));
1964
1965
  this.#presetMap[presetInfo.program] = presetInfo;
1965
1966
  const preset = await this.getPreset(presetId);
1966
- this.#players[channel].preset = preset;
1967
+ await this.#players[channel].setPreset(preset);
1967
1968
  this.#setupChange();
1968
1969
  }
1969
1970
  async load(content, setup) {
@@ -2414,12 +2415,9 @@ var MidiAudioPlayer = class _MidiAudioPlayer extends index.Player {
2414
2415
  this.#players[channel].setPitchBend?.(event.value);
2415
2416
  break;
2416
2417
  case "Program Change":
2417
- if (
2418
- // (this.#opts.presetAuto || this.#opts.presetRandom) &&
2419
- event.value >= 0 && event.value <= 127 && this.#instruments[event.value + 1] !== void 0 && event.channel != 10
2420
- ) {
2418
+ if (event.value >= 0 && event.value <= 127 && this.#instruments[event.value + 1] !== void 0 && event.channel != 10) {
2421
2419
  if (this.#players[channel].preset?.program !== event.value + 1) {
2422
- this.#players[channel].preset = this.#instruments[event.value + 1];
2420
+ this.#players[channel].setPreset(this.#instruments[event.value + 1]);
2423
2421
  }
2424
2422
  }
2425
2423
  break;
@@ -2525,7 +2523,7 @@ var MidiAudioPlayer = class _MidiAudioPlayer extends index.Player {
2525
2523
  if (!this.#players[event.channel]) return;
2526
2524
  if (event.channel == 10 || event.value > 127 || event.value < 0) break;
2527
2525
  if (this.#players[event.channel] !== void 0 && this.#players[event.channel].preset.program != event.value + 1)
2528
- this.#players[event.channel].preset = this.#instruments[event.value + 1];
2526
+ this.#players[event.channel].setPreset(this.#instruments[event.value + 1]);
2529
2527
  break;
2530
2528
  case "Karaoke Event":
2531
2529
  if (event.tick < this.tick - this.secondsToTicks(10)) return;