@grame/faustwasm 0.1.3 → 0.1.4

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.
@@ -402,6 +402,12 @@ declare class WasmAllocator {
402
402
  * @returns The view of the memory buffer as Int32Array.
403
403
  */
404
404
  getInt32Array(): Int32Array;
405
+ /**
406
+ * Returns the Int64 view of the underlying buffer object.
407
+ *
408
+ * @returns The view of the memory buffer as BigInt64Array.
409
+ */
410
+ getInt64Array(): BigInt64Array;
405
411
  /**
406
412
  * Returns the Float32 view of the underlying buffer object.
407
413
  *
@@ -423,8 +429,9 @@ declare class Soundfile {
423
429
  private readonly fOffset;
424
430
  private readonly fSampleSize;
425
431
  private readonly fPtrSize;
432
+ private readonly fIntSize;
426
433
  private readonly fAllocator;
427
- constructor(allocator: WasmAllocator, ptrSize: number, sampleSize: number, curChan: number, length: number, maxChan: number, totalParts: number);
434
+ constructor(allocator: WasmAllocator, sampleSize: number, curChan: number, length: number, maxChan: number, totalParts: number);
428
435
  private allocBuffers;
429
436
  shareBuffers(curChan: number, maxChan: number): void;
430
437
  copyToOut(part: number, maxChannels: number, offset: number, buffer: AudioBuffer): void;
@@ -439,11 +446,17 @@ declare class Soundfile {
439
446
  }
440
447
  declare class SoundfileReader {
441
448
  private readonly fAllocator;
442
- private readonly fPtrSize;
443
449
  private readonly fSampleSize;
444
450
  private readonly fContext;
445
451
  private readonly fAudioBuffers;
446
- constructor(allocator: WasmAllocator, context: BaseAudioContext, ptrSize: number, sampleSize: number);
452
+ constructor(allocator: WasmAllocator, context: BaseAudioContext, sampleSize: number);
453
+ /**
454
+ * Check if the file exists.
455
+ *
456
+ * @param url : the url of the file to check
457
+ * @returns : true if the file exists, otherwise false
458
+ */
459
+ private checkFileExists;
447
460
  /**
448
461
  * Check if the file exists in the given directories.
449
462
  *
@@ -483,9 +496,8 @@ declare class SoundfileReader {
483
496
  * Crate a soundfile, load all parts and copy audio data to the wasm soundfile buffer.
484
497
  * @param pathNameList : list of soundfile paths
485
498
  * @param maxChan : maximum number of channels
486
- * @param isDouble : whether the soundfile will be copied as double
487
499
  */
488
- createSoundfile(pathNameList: string[], maxChan: number, isDouble: boolean): Promise<Soundfile | null>;
500
+ createSoundfile(pathNameList: string[], maxChan: number): Promise<Soundfile | null>;
489
501
  getHEAP32(): Int32Array;
490
502
  }
491
503
  /**
@@ -7870,11 +7870,9 @@ var FaustWasmInstantiator = class {
7870
7870
  }
7871
7871
  }
7872
7872
  static async createAsyncMonoDSPInstance(factory) {
7873
- const parsedJson = JSON.parse(factory.json);
7874
- if (Array.isArray(parsedJson.ui) && parsedJson.ui.some((group) => {
7875
- var _a;
7876
- return (_a = group.items) == null ? void 0 : _a.some((item) => item.type === "soundfile");
7877
- })) {
7873
+ const pattern = /"type":\s*"soundfile"/;
7874
+ const isDetected = pattern.test(factory.json);
7875
+ if (isDetected) {
7878
7876
  const memory = this.createMemoryMono(factory);
7879
7877
  const instance = await WebAssembly.instantiate(factory.module, this.createWasmImport(memory));
7880
7878
  return this.createMonoDSPInstanceAux(instance, factory.json, memory);
@@ -7884,11 +7882,9 @@ var FaustWasmInstantiator = class {
7884
7882
  }
7885
7883
  }
7886
7884
  static createSyncMonoDSPInstance(factory) {
7887
- const parsedJson = JSON.parse(factory.json);
7888
- if (Array.isArray(parsedJson.ui) && parsedJson.ui.some((group) => {
7889
- var _a;
7890
- return (_a = group.items) == null ? void 0 : _a.some((item) => item.type === "soundfile");
7891
- })) {
7885
+ const pattern = /"type":\s*"soundfile"/;
7886
+ const isDetected = pattern.test(factory.json);
7887
+ if (isDetected) {
7892
7888
  const memory = this.createMemoryMono(factory);
7893
7889
  const instance = new WebAssembly.Instance(factory.module, this.createWasmImport(memory));
7894
7890
  return this.createMonoDSPInstanceAux(instance, factory.json, memory);
@@ -7982,6 +7978,9 @@ var WasmAllocator = class {
7982
7978
  getInt32Array() {
7983
7979
  return new Int32Array(this.memory.buffer);
7984
7980
  }
7981
+ getInt64Array() {
7982
+ return new BigInt64Array(this.memory.buffer);
7983
+ }
7985
7984
  getFloat32Array() {
7986
7985
  return new Float32Array(this.memory.buffer);
7987
7986
  }
@@ -7993,22 +7992,23 @@ var MAX_SOUNDFILE_PARTS = 256;
7993
7992
  var MAX_CHAN = 64;
7994
7993
  var BUFFER_SIZE = 1024;
7995
7994
  var SAMPLE_RATE = 44100;
7996
- var intSize = 4;
7997
7995
  var Soundfile = class {
7998
- constructor(allocator, ptrSize, sampleSize, curChan, length2, maxChan, totalParts) {
7996
+ constructor(allocator, sampleSize, curChan, length2, maxChan, totalParts) {
7999
7997
  this.fSampleSize = sampleSize;
8000
- this.fPtrSize = ptrSize;
7998
+ this.fIntSize = this.fSampleSize;
7999
+ this.fPtrSize = 4;
8001
8000
  this.fAllocator = allocator;
8002
- this.fPtr = allocator.alloc(4 * ptrSize);
8003
- this.fLength = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
8004
- this.fSR = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
8005
- this.fOffset = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
8001
+ console.log(`Soundfile constructor: curChan: ${curChan}, length: ${length2}, maxChan: ${maxChan}, totalParts: ${totalParts}`);
8002
+ this.fPtr = allocator.alloc(4 * this.fPtrSize);
8003
+ this.fLength = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
8004
+ this.fSR = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
8005
+ this.fOffset = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
8006
8006
  this.fBuffers = this.allocBuffers(curChan, length2, maxChan);
8007
8007
  const HEAP32 = this.fAllocator.getInt32Array();
8008
8008
  HEAP32[this.fPtr >> 2] = this.fBuffers;
8009
- HEAP32[this.fPtr + ptrSize >> 2] = this.fLength;
8010
- HEAP32[this.fPtr + 2 * ptrSize >> 2] = this.fSR;
8011
- HEAP32[this.fPtr + 3 * ptrSize >> 2] = this.fOffset;
8009
+ HEAP32[this.fPtr + this.fPtrSize >> 2] = this.fLength;
8010
+ HEAP32[this.fPtr + 2 * this.fPtrSize >> 2] = this.fSR;
8011
+ HEAP32[this.fPtr + 3 * this.fPtrSize >> 2] = this.fOffset;
8012
8012
  for (let chan = 0; chan < curChan; chan++) {
8013
8013
  const buffer = HEAP32[(this.fBuffers >> 2) + chan];
8014
8014
  console.log(`allocBuffers AFTER: ${chan} - ${buffer}`);
@@ -8031,10 +8031,18 @@ var Soundfile = class {
8031
8031
  }
8032
8032
  }
8033
8033
  copyToOut(part, maxChannels, offset, buffer) {
8034
- const HEAP32 = this.fAllocator.getInt32Array();
8035
- HEAP32[(this.fLength >> 2) + part] = buffer.length;
8036
- HEAP32[(this.fSR >> 2) + part] = buffer.sampleRate;
8037
- HEAP32[(this.fOffset >> 2) + part] = offset;
8034
+ if (this.fIntSize === 4) {
8035
+ const HEAP32 = this.fAllocator.getInt32Array();
8036
+ HEAP32[(this.fLength >> Math.log2(this.fIntSize)) + part] = buffer.length;
8037
+ HEAP32[(this.fSR >> Math.log2(this.fIntSize)) + part] = buffer.sampleRate;
8038
+ HEAP32[(this.fOffset >> Math.log2(this.fIntSize)) + part] = offset;
8039
+ } else {
8040
+ const HEAP64 = this.fAllocator.getInt64Array();
8041
+ HEAP64[(this.fLength >> Math.log2(this.fIntSize)) + part] = BigInt(buffer.length);
8042
+ HEAP64[(this.fSR >> Math.log2(this.fIntSize)) + part] = BigInt(buffer.sampleRate);
8043
+ HEAP64[(this.fOffset >> Math.log2(this.fIntSize)) + part] = BigInt(offset);
8044
+ }
8045
+ console.log(`copyToOut: part: ${part}, maxChannels: ${maxChannels}, offset: ${offset}, buffer: ${buffer}`);
8038
8046
  if (this.fSampleSize === 8) {
8039
8047
  this.copyToOutReal64(maxChannels, offset, buffer);
8040
8048
  } else {
@@ -8047,6 +8055,9 @@ var Soundfile = class {
8047
8055
  for (let chan = 0; chan < buffer.numberOfChannels; chan++) {
8048
8056
  const input = buffer.getChannelData(chan);
8049
8057
  const output = HEAP32[(this.fBuffers >> 2) + chan];
8058
+ const begin = output + offset * this.fSampleSize >> Math.log2(this.fSampleSize);
8059
+ const end = output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize);
8060
+ console.log(`copyToOutReal32 begin: ${begin}, end: ${end}, delta: ${end - begin}`);
8050
8061
  const outputReal = HEAPF.subarray(
8051
8062
  output + offset * this.fSampleSize >> Math.log2(this.fSampleSize),
8052
8063
  output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize)
@@ -8062,6 +8073,9 @@ var Soundfile = class {
8062
8073
  for (let chan = 0; chan < buffer.numberOfChannels; chan++) {
8063
8074
  const input = buffer.getChannelData(chan);
8064
8075
  const output = HEAP32[(this.fBuffers >> 2) + chan];
8076
+ const begin = output + offset * this.fSampleSize >> Math.log2(this.fSampleSize);
8077
+ const end = output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize);
8078
+ console.log(`copyToOutReal64 begin: ${begin}, end: ${end}, delta: ${end - begin}`);
8065
8079
  const outputReal = HEAPF.subarray(
8066
8080
  output + offset * this.fSampleSize >> Math.log2(this.fSampleSize),
8067
8081
  output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize)
@@ -8072,10 +8086,17 @@ var Soundfile = class {
8072
8086
  }
8073
8087
  }
8074
8088
  emptyFile(part, offset) {
8075
- const HEAP32 = this.fAllocator.getInt32Array();
8076
- HEAP32[(this.fLength >> 2) + part] = BUFFER_SIZE;
8077
- HEAP32[(this.fSR >> 2) + part] = SAMPLE_RATE;
8078
- HEAP32[(this.fOffset >> 2) + part] = offset;
8089
+ if (this.fIntSize === 4) {
8090
+ const HEAP32 = this.fAllocator.getInt32Array();
8091
+ HEAP32[(this.fLength >> Math.log2(this.fIntSize)) + part] = BUFFER_SIZE;
8092
+ HEAP32[(this.fSR >> Math.log2(this.fIntSize)) + part] = SAMPLE_RATE;
8093
+ HEAP32[(this.fOffset >> Math.log2(this.fIntSize)) + part] = offset;
8094
+ } else {
8095
+ const HEAP64 = this.fAllocator.getInt64Array();
8096
+ HEAP64[(this.fLength >> Math.log2(this.fIntSize)) + part] = BigInt(BUFFER_SIZE);
8097
+ HEAP64[(this.fSR >> Math.log2(this.fIntSize)) + part] = BigInt(SAMPLE_RATE);
8098
+ HEAP64[(this.fOffset >> Math.log2(this.fIntSize)) + part] = BigInt(offset);
8099
+ }
8079
8100
  return offset + BUFFER_SIZE;
8080
8101
  }
8081
8102
  displayMemory(where = "", mem = false) {
@@ -8107,30 +8128,29 @@ var Soundfile = class {
8107
8128
  }
8108
8129
  };
8109
8130
  var SoundfileReader = class {
8110
- constructor(allocator, context, ptrSize, sampleSize) {
8131
+ constructor(allocator, context, sampleSize) {
8111
8132
  this.fAllocator = allocator;
8112
- this.fPtrSize = ptrSize;
8113
8133
  this.fSampleSize = sampleSize;
8114
8134
  this.fContext = context;
8115
8135
  this.fAudioBuffers = [];
8116
8136
  }
8117
- async checkFile(directories, fileName) {
8118
- async function checkFileExists(url) {
8119
- try {
8120
- console.log(`"checkFileExists" url: ${url}`);
8121
- const response = await fetch(url, { method: "HEAD" });
8122
- return response.ok;
8123
- } catch (error) {
8124
- console.error("Fetch error:", error);
8125
- return false;
8126
- }
8137
+ async checkFileExists(url) {
8138
+ try {
8139
+ console.log(`"checkFileExists" url: ${url}`);
8140
+ const response = await fetch(url, { method: "HEAD" });
8141
+ return response.ok;
8142
+ } catch (error) {
8143
+ console.error("Fetch error:", error);
8144
+ return false;
8127
8145
  }
8128
- if (await checkFileExists(fileName)) {
8146
+ }
8147
+ async checkFile(directories, fileName) {
8148
+ if (await this.checkFileExists(fileName)) {
8129
8149
  return fileName;
8130
8150
  } else {
8131
8151
  for (let i = 0; i < directories.length; i++) {
8132
8152
  const pathName = directories[i] + "/" + fileName;
8133
- if (await checkFileExists(pathName)) {
8153
+ if (await this.checkFileExists(pathName)) {
8134
8154
  return pathName;
8135
8155
  }
8136
8156
  }
@@ -8176,7 +8196,7 @@ var SoundfileReader = class {
8176
8196
  return offset + BUFFER_SIZE;
8177
8197
  }
8178
8198
  }
8179
- async createSoundfile(pathNameList, maxChan, isDouble) {
8199
+ async createSoundfile(pathNameList, maxChan) {
8180
8200
  try {
8181
8201
  let curChan = 1;
8182
8202
  let totalLength = 0;
@@ -8194,7 +8214,7 @@ var SoundfileReader = class {
8194
8214
  totalLength += len;
8195
8215
  }
8196
8216
  totalLength += (MAX_SOUNDFILE_PARTS - pathNameList.length) * BUFFER_SIZE;
8197
- let soundfile = new Soundfile(this.fAllocator, this.fPtrSize, this.fSampleSize, curChan, totalLength, maxChan, pathNameList.length);
8217
+ let soundfile = new Soundfile(this.fAllocator, this.fSampleSize, curChan, totalLength, maxChan, pathNameList.length);
8198
8218
  let offset = 0;
8199
8219
  for (let part = 0; part < pathNameList.length; part++) {
8200
8220
  if (pathNameList[part] === "__empty_sound__") {
@@ -8318,7 +8338,7 @@ var FaustBaseWebAudioDsp = class {
8318
8338
  console.log(`Soundfile CACHE ${url}} : ${name} loaded at ${item.basePtr} in wasm memory with index ${item.index}`);
8319
8339
  HEAP32[baseDSP + item.index >> 2] = item.basePtr;
8320
8340
  } else {
8321
- const soundfile = await sfReader.createSoundfile(sfPathNames, MAX_CHAN, this.fSampleSize === 8);
8341
+ const soundfile = await sfReader.createSoundfile(sfPathNames, MAX_CHAN);
8322
8342
  if (soundfile) {
8323
8343
  const HEAP32 = soundfile.getHEAP32();
8324
8344
  item.basePtr = soundfile.getPtr();
@@ -8452,6 +8472,7 @@ var FaustMonoWebAudioDsp = class extends FaustBaseWebAudioDsp {
8452
8472
  constructor(instance, sampleRate, sampleSize, bufferSize) {
8453
8473
  super(sampleSize, bufferSize);
8454
8474
  this.fInstance = instance;
8475
+ console.log("sampleSize: " + sampleSize + " bufferSize: " + bufferSize);
8455
8476
  this.fJSONDsp = JSON.parse(this.fInstance.json);
8456
8477
  FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);
8457
8478
  this.fEndMemory = this.initMemory();
@@ -8460,7 +8481,7 @@ var FaustMonoWebAudioDsp = class extends FaustBaseWebAudioDsp {
8460
8481
  async init(context) {
8461
8482
  if (this.fSoundfiles.length > 0 && context) {
8462
8483
  const allocator = new WasmAllocator(this.fInstance.memory, this.fEndMemory);
8463
- const sfReader = new SoundfileReader(allocator, context, this.fPtrSize, this.fSampleSize);
8484
+ const sfReader = new SoundfileReader(allocator, context, this.fSampleSize);
8464
8485
  await this.initSoundfileMemory(allocator, sfReader, this.fDSP);
8465
8486
  }
8466
8487
  }
@@ -8664,6 +8685,7 @@ var FaustPolyWebAudioDsp = class extends FaustBaseWebAudioDsp {
8664
8685
  constructor(instance, sampleRate, sampleSize, bufferSize) {
8665
8686
  super(sampleSize, bufferSize);
8666
8687
  this.fInstance = instance;
8688
+ console.log("sampleSize: " + sampleSize + " bufferSize: " + bufferSize);
8667
8689
  this.fJSONDsp = JSON.parse(this.fInstance.voiceJSON);
8668
8690
  this.fJSONEffect = this.fInstance.effectAPI && this.fInstance.effectJSON ? JSON.parse(this.fInstance.effectJSON) : null;
8669
8691
  FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);
@@ -8686,7 +8708,7 @@ var FaustPolyWebAudioDsp = class extends FaustBaseWebAudioDsp {
8686
8708
  async init(context) {
8687
8709
  if (this.fSoundfiles.length > 0 && context) {
8688
8710
  const allocator = new WasmAllocator(this.fInstance.memory, this.fEndMemory);
8689
- const sfReader = new SoundfileReader(allocator, context, this.fPtrSize, this.fSampleSize);
8711
+ const sfReader = new SoundfileReader(allocator, context, this.fSampleSize);
8690
8712
  for (let voice = 0; voice < this.fInstance.voices; voice++) {
8691
8713
  await this.initSoundfileMemory(allocator, sfReader, this.fJSONDsp.size * voice);
8692
8714
  }