spessasynth_core 4.4.8 → 4.4.9

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.d.ts CHANGED
@@ -181,9 +181,8 @@ export declare function audioToWav(audioData: Float32Array[], sampleRate: number
181
181
  * Reads a WAVE file into PCM audio data, the counterpart of {@link audioToWav}.
182
182
  *
183
183
  * It reads 8, 16, 24 and 32-bit integer PCM and 32 and 64-bit float files (including `WAVE_FORMAT_EXTENSIBLE`).
184
- * The loop is read from the first loop of the `smpl` chunk. Without one, the first two points of the `cue ` chunk
185
- * are taken as the loop start and end, which is how {@link audioToWav} writes it.
186
- * Other files may use cue points as plain markers, so such a loop is only reliable for files written by core.
184
+ * The loop is read from the first loop of the `smpl` chunk. Without one, a `cue ` chunk with exactly two points
185
+ * is taken as the loop start and end, which is how {@link audioToWav} writes it.
187
186
  *
188
187
  * > **Note**
189
188
  * >
package/dist/index.js CHANGED
@@ -634,9 +634,8 @@ function audioToWav(audioData, sampleRate, options = DEFAULT_WAV_WRITE_OPTIONS)
634
634
  * Reads a WAVE file into PCM audio data, the counterpart of {@link audioToWav}.
635
635
  *
636
636
  * It reads 8, 16, 24 and 32-bit integer PCM and 32 and 64-bit float files (including `WAVE_FORMAT_EXTENSIBLE`).
637
- * The loop is read from the first loop of the `smpl` chunk. Without one, the first two points of the `cue ` chunk
638
- * are taken as the loop start and end, which is how {@link audioToWav} writes it.
639
- * Other files may use cue points as plain markers, so such a loop is only reliable for files written by core.
637
+ * The loop is read from the first loop of the `smpl` chunk. Without one, a `cue ` chunk with exactly two points
638
+ * is taken as the loop start and end, which is how {@link audioToWav} writes it.
640
639
  *
641
640
  * > **Note**
642
641
  * >
@@ -649,9 +648,13 @@ function audioToWav(audioData, sampleRate, options = DEFAULT_WAV_WRITE_OPTIONS)
649
648
  * @group Utilities
650
649
  */
651
650
  function wavToAudio(buffer) {
652
- const view = new DataView(buffer);
653
651
  const bytes = new Uint8Array(buffer);
654
652
  const fourCC = (at) => String.fromCodePoint(bytes[at], bytes[at + 1], bytes[at + 2], bytes[at + 3]);
653
+ const isFourCC = (at) => at + 4 <= bytes.length && /^[\dA-Za-z]+ *$/.test(fourCC(at));
654
+ const nextChunk = (body, size) => {
655
+ const end = body + size;
656
+ return size % 2 === 1 && !(isFourCC(end) && !isFourCC(end + 1)) ? end + 1 : end;
657
+ };
655
658
  if (buffer.byteLength < 12 || fourCC(0) !== "RIFF" || fourCC(8) !== "WAVE") throw new Error("Not a WAVE file.");
656
659
  let format = 0;
657
660
  let channels = 0;
@@ -661,66 +664,104 @@ function wavToAudio(buffer) {
661
664
  let dataSize = 0;
662
665
  let smplLoop;
663
666
  const cuePoints = new Array();
667
+ const namedCues = /* @__PURE__ */ new Set();
664
668
  let pos = 12;
665
669
  while (pos + 8 <= buffer.byteLength) {
666
670
  const id = fourCC(pos);
667
- const size = view.getUint32(pos + 4, true);
671
+ const size = readLittleEndian(bytes, 4, pos + 4);
668
672
  const body = pos + 8;
669
673
  const available = Math.min(size, buffer.byteLength - body);
670
674
  switch (id) {
671
675
  case "fmt ":
672
- format = view.getUint16(body, true);
673
- channels = view.getUint16(body + 2, true);
674
- sampleRate = view.getUint32(body + 4, true);
675
- bitsPerSample = view.getUint16(body + 14, true);
676
- if (format === 65534 && size >= 26) format = view.getUint16(body + 24, true);
676
+ if (available < 16) throw new Error("The WAVE file's fmt chunk is too short.");
677
+ format = readLittleEndian(bytes, 2, body);
678
+ channels = readLittleEndian(bytes, 2, body + 2);
679
+ sampleRate = readLittleEndian(bytes, 4, body + 4);
680
+ bitsPerSample = readLittleEndian(bytes, 2, body + 14);
681
+ if (format === 65534 && available >= 26) format = readLittleEndian(bytes, 2, body + 24);
677
682
  break;
678
683
  case "data":
679
684
  dataStart = body;
680
685
  dataSize = available;
681
686
  break;
682
687
  case "smpl":
683
- if ((size >= 36 ? view.getUint32(body + 28, true) : 0) > 0 && size >= 60) smplLoop = {
684
- start: view.getUint32(body + 36 + 8, true),
685
- end: view.getUint32(body + 36 + 12, true) + 1
688
+ if ((available >= 36 ? readLittleEndian(bytes, 4, body + 28) : 0) > 0 && available >= 60) smplLoop = {
689
+ start: readLittleEndian(bytes, 4, body + 36 + 8),
690
+ end: readLittleEndian(bytes, 4, body + 36 + 12) + 1
686
691
  };
687
692
  break;
688
693
  case "cue ": {
689
- const count = view.getUint32(body, true);
690
- for (let i = 0; i < count && 4 + (i + 1) * 24 <= size; i++) cuePoints.push(view.getUint32(body + 4 + i * 24 + 20, true));
694
+ const count = available >= 4 ? readLittleEndian(bytes, 4, body) : 0;
695
+ for (let i = 0; i < count && 4 + (i + 1) * 24 <= available; i++) {
696
+ const point = body + 4 + i * 24;
697
+ cuePoints.push({
698
+ id: readLittleEndian(bytes, 4, point),
699
+ offset: readLittleEndian(bytes, 4, point + 20)
700
+ });
701
+ }
702
+ break;
703
+ }
704
+ case "LIST": {
705
+ if (available < 4 || fourCC(body) !== "adtl") break;
706
+ const end = body + available;
707
+ let at = body + 4;
708
+ while (at + 12 <= end) {
709
+ const subId = fourCC(at);
710
+ const subSize = readLittleEndian(bytes, 4, at + 4);
711
+ if (subSize >= 4 && (subId === "labl" || subId === "note" || subId === "ltxt")) namedCues.add(readLittleEndian(bytes, 4, at + 8));
712
+ at = nextChunk(at + 8, subSize);
713
+ }
691
714
  break;
692
715
  }
693
716
  }
694
- pos = body + size + size % 2;
717
+ pos = nextChunk(body, size);
695
718
  }
696
719
  if (channels === 0 || dataStart === -1) throw new Error("The WAVE file has no fmt or data chunk!");
697
720
  const bytesPerSample = bitsPerSample >> 3;
698
721
  const isFloat = format === 3;
699
722
  if (format !== 1 && format !== 3 || (isFloat ? ![4, 8].includes(bytesPerSample) : bytesPerSample < 1 || bytesPerSample > 4)) throw new Error(`Unsupported WAVE format ${format} with ${bitsPerSample} bits per sample.`);
700
723
  const frames = Math.floor(dataSize / (bytesPerSample * channels));
701
- const audioData = Array.from({ length: channels }, () => new Float32Array(frames));
702
- for (let i = 0; i < frames; i++) for (let c = 0; c < channels; c++) {
703
- const at = dataStart + (i * channels + c) * bytesPerSample;
704
- let value;
705
- if (isFloat) value = bytesPerSample === 4 ? view.getFloat32(at, true) : view.getFloat64(at, true);
706
- else switch (bytesPerSample) {
707
- case 1:
708
- value = (view.getUint8(at) - 128) / 128;
709
- break;
710
- case 2:
711
- value = view.getInt16(at, true) / 32768;
712
- break;
713
- case 3:
714
- value = ((bytes[at + 2] << 24 | bytes[at + 1] << 16 | bytes[at] << 8) >> 8) / 8388608;
715
- break;
716
- default: value = view.getInt32(at, true) / 2147483648;
724
+ const count = frames * channels;
725
+ const pcm = bytes.slice(dataStart, dataStart + count * bytesPerSample);
726
+ let samples;
727
+ let offset = 0;
728
+ let scale = 1;
729
+ if (isFloat) samples = bytesPerSample === 4 ? new Float32Array(pcm.buffer) : new Float64Array(pcm.buffer);
730
+ else switch (bytesPerSample) {
731
+ case 1:
732
+ samples = pcm;
733
+ offset = 128;
734
+ scale = 128;
735
+ break;
736
+ case 2:
737
+ samples = new Int16Array(pcm.buffer);
738
+ scale = 32768;
739
+ break;
740
+ case 3: {
741
+ const s24 = new Int32Array(count);
742
+ for (let i = 0; i < count; i++) {
743
+ const at = i * 3;
744
+ s24[i] = (pcm[at + 2] << 24 | pcm[at + 1] << 16 | pcm[at] << 8) >> 8;
745
+ }
746
+ samples = s24;
747
+ scale = 8388608;
748
+ break;
717
749
  }
718
- audioData[c][i] = value;
750
+ default:
751
+ samples = new Int32Array(pcm.buffer);
752
+ scale = 2147483648;
719
753
  }
754
+ const audioData = Array.from({ length: channels }, () => new Float32Array(frames));
755
+ for (let i = 0; i < frames; i++) for (let c = 0; c < channels; c++) audioData[c][i] = (samples[i * channels + c] - offset) / scale;
720
756
  let loop = smplLoop;
721
- if (!loop && cuePoints.length >= 2) loop = {
722
- start: cuePoints[0],
723
- end: cuePoints[1]
757
+ const [cueStart, cueEnd] = cuePoints;
758
+ if (!loop && cuePoints.length === 2 && !namedCues.has(cueStart.id) && !namedCues.has(cueEnd.id)) loop = {
759
+ start: cueStart.offset,
760
+ end: cueEnd.offset
761
+ };
762
+ if (loop) loop = loop.end <= loop.start || loop.start >= frames ? void 0 : {
763
+ start: loop.start,
764
+ end: Math.min(loop.end, frames)
724
765
  };
725
766
  return {
726
767
  audioData,
@@ -2739,6 +2780,13 @@ var MIDIPatchTools = class MIDIPatchTools {
2739
2780
  returnReplacement(patches[0]);
2740
2781
  return patches[0];
2741
2782
  }
2783
+ if (system === "gs") {
2784
+ p = matchingPrograms.find((p) => p.bankMSB === 0 && p.bankLSB === bankLSB);
2785
+ if (p) {
2786
+ returnReplacement(p);
2787
+ return p;
2788
+ }
2789
+ }
2742
2790
  p = isXG ? matchingPrograms.find((p) => p.bankLSB === bankLSB) : matchingPrograms.find((p) => p.bankMSB === bankMSB);
2743
2791
  if (p) {
2744
2792
  returnReplacement(p);
@@ -7649,13 +7697,9 @@ var BasicSample = class {
7649
7697
  return /* @__PURE__ */ new Float32Array(1);
7650
7698
  }
7651
7699
  const audioData = new Float32Array(length);
7652
- if (data.byteOffset % 2 === 0) {
7653
- const s16 = new Int16Array(data.buffer, data.byteOffset, length);
7654
- for (let i = 0; i < length; i++) audioData[i] = s16[i] / 32768;
7655
- } else {
7656
- const view = new DataView(data.buffer, data.byteOffset, length * 2);
7657
- for (let i = 0; i < length; i++) audioData[i] = view.getInt16(i * 2, true) / 32768;
7658
- }
7700
+ const aligned = data.byteOffset % 2 === 0 ? data : data.slice(0, length * 2);
7701
+ const s16 = new Int16Array(aligned.buffer, aligned.byteOffset, length);
7702
+ for (let i = 0; i < length; i++) audioData[i] = s16[i] / 32768;
7659
7703
  return audioData;
7660
7704
  }
7661
7705
  /**
@@ -61029,8 +61073,8 @@ var SpessaSynthProcessor = class {
61029
61073
  createMIDIChannelInternal(sendEvent) {
61030
61074
  const channel = new MIDIChannel(this, this.defaultPreset, this.drumPreset, this.midiChannels.length);
61031
61075
  this.midiChannels.push(channel);
61032
- channel.reset(false);
61033
61076
  if (sendEvent) this.callEvent("channelAdded", void 0);
61077
+ channel.reset(false);
61034
61078
  }
61035
61079
  processMessageInternal(message, channelOffset) {
61036
61080
  const byte = message[0];