@grame/faustwasm 0.1.2 → 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.
@@ -395,6 +395,12 @@ declare class WasmAllocator {
395
395
  * @returns The view of the memory buffer as Int32Array.
396
396
  */
397
397
  getInt32Array(): Int32Array;
398
+ /**
399
+ * Returns the Int64 view of the underlying buffer object.
400
+ *
401
+ * @returns The view of the memory buffer as BigInt64Array.
402
+ */
403
+ getInt64Array(): BigInt64Array;
398
404
  /**
399
405
  * Returns the Float32 view of the underlying buffer object.
400
406
  *
@@ -416,8 +422,9 @@ declare class Soundfile {
416
422
  private readonly fOffset;
417
423
  private readonly fSampleSize;
418
424
  private readonly fPtrSize;
425
+ private readonly fIntSize;
419
426
  private readonly fAllocator;
420
- constructor(allocator: WasmAllocator, ptrSize: number, sampleSize: number, curChan: number, length: number, maxChan: number, totalParts: number);
427
+ constructor(allocator: WasmAllocator, sampleSize: number, curChan: number, length: number, maxChan: number, totalParts: number);
421
428
  private allocBuffers;
422
429
  shareBuffers(curChan: number, maxChan: number): void;
423
430
  copyToOut(part: number, maxChannels: number, offset: number, buffer: AudioBuffer): void;
@@ -432,11 +439,17 @@ declare class Soundfile {
432
439
  }
433
440
  declare class SoundfileReader {
434
441
  private readonly fAllocator;
435
- private readonly fPtrSize;
436
442
  private readonly fSampleSize;
437
443
  private readonly fContext;
438
444
  private readonly fAudioBuffers;
439
- constructor(allocator: WasmAllocator, context: BaseAudioContext, ptrSize: number, sampleSize: number);
445
+ constructor(allocator: WasmAllocator, context: BaseAudioContext, sampleSize: number);
446
+ /**
447
+ * Check if the file exists.
448
+ *
449
+ * @param url : the url of the file to check
450
+ * @returns : true if the file exists, otherwise false
451
+ */
452
+ private checkFileExists;
440
453
  /**
441
454
  * Check if the file exists in the given directories.
442
455
  *
@@ -476,9 +489,8 @@ declare class SoundfileReader {
476
489
  * Crate a soundfile, load all parts and copy audio data to the wasm soundfile buffer.
477
490
  * @param pathNameList : list of soundfile paths
478
491
  * @param maxChan : maximum number of channels
479
- * @param isDouble : whether the soundfile will be copied as double
480
492
  */
481
- createSoundfile(pathNameList: string[], maxChan: number, isDouble: boolean): Promise<Soundfile | null>;
493
+ createSoundfile(pathNameList: string[], maxChan: number): Promise<Soundfile | null>;
482
494
  getHEAP32(): Int32Array;
483
495
  }
484
496
  /**
@@ -1882,11 +1882,9 @@ var FaustWasmInstantiator = class {
1882
1882
  }
1883
1883
  }
1884
1884
  static async createAsyncMonoDSPInstance(factory) {
1885
- const parsedJson = JSON.parse(factory.json);
1886
- if (Array.isArray(parsedJson.ui) && parsedJson.ui.some((group) => {
1887
- var _a;
1888
- return (_a = group.items) == null ? void 0 : _a.some((item) => item.type === "soundfile");
1889
- })) {
1885
+ const pattern = /"type":\s*"soundfile"/;
1886
+ const isDetected = pattern.test(factory.json);
1887
+ if (isDetected) {
1890
1888
  const memory = this.createMemoryMono(factory);
1891
1889
  const instance = await WebAssembly.instantiate(factory.module, this.createWasmImport(memory));
1892
1890
  return this.createMonoDSPInstanceAux(instance, factory.json, memory);
@@ -1896,11 +1894,9 @@ var FaustWasmInstantiator = class {
1896
1894
  }
1897
1895
  }
1898
1896
  static createSyncMonoDSPInstance(factory) {
1899
- const parsedJson = JSON.parse(factory.json);
1900
- if (Array.isArray(parsedJson.ui) && parsedJson.ui.some((group) => {
1901
- var _a;
1902
- return (_a = group.items) == null ? void 0 : _a.some((item) => item.type === "soundfile");
1903
- })) {
1897
+ const pattern = /"type":\s*"soundfile"/;
1898
+ const isDetected = pattern.test(factory.json);
1899
+ if (isDetected) {
1904
1900
  const memory = this.createMemoryMono(factory);
1905
1901
  const instance = new WebAssembly.Instance(factory.module, this.createWasmImport(memory));
1906
1902
  return this.createMonoDSPInstanceAux(instance, factory.json, memory);
@@ -1994,6 +1990,9 @@ var WasmAllocator = class {
1994
1990
  getInt32Array() {
1995
1991
  return new Int32Array(this.memory.buffer);
1996
1992
  }
1993
+ getInt64Array() {
1994
+ return new BigInt64Array(this.memory.buffer);
1995
+ }
1997
1996
  getFloat32Array() {
1998
1997
  return new Float32Array(this.memory.buffer);
1999
1998
  }
@@ -2005,22 +2004,23 @@ var MAX_SOUNDFILE_PARTS = 256;
2005
2004
  var MAX_CHAN = 64;
2006
2005
  var BUFFER_SIZE = 1024;
2007
2006
  var SAMPLE_RATE = 44100;
2008
- var intSize = 4;
2009
2007
  var Soundfile = class {
2010
- constructor(allocator, ptrSize, sampleSize, curChan, length2, maxChan, totalParts) {
2008
+ constructor(allocator, sampleSize, curChan, length2, maxChan, totalParts) {
2011
2009
  this.fSampleSize = sampleSize;
2012
- this.fPtrSize = ptrSize;
2010
+ this.fIntSize = this.fSampleSize;
2011
+ this.fPtrSize = 4;
2013
2012
  this.fAllocator = allocator;
2014
- this.fPtr = allocator.alloc(4 * ptrSize);
2015
- this.fLength = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
2016
- this.fSR = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
2017
- this.fOffset = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
2013
+ console.log(`Soundfile constructor: curChan: ${curChan}, length: ${length2}, maxChan: ${maxChan}, totalParts: ${totalParts}`);
2014
+ this.fPtr = allocator.alloc(4 * this.fPtrSize);
2015
+ this.fLength = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
2016
+ this.fSR = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
2017
+ this.fOffset = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
2018
2018
  this.fBuffers = this.allocBuffers(curChan, length2, maxChan);
2019
2019
  const HEAP32 = this.fAllocator.getInt32Array();
2020
2020
  HEAP32[this.fPtr >> 2] = this.fBuffers;
2021
- HEAP32[this.fPtr + ptrSize >> 2] = this.fLength;
2022
- HEAP32[this.fPtr + 2 * ptrSize >> 2] = this.fSR;
2023
- HEAP32[this.fPtr + 3 * ptrSize >> 2] = this.fOffset;
2021
+ HEAP32[this.fPtr + this.fPtrSize >> 2] = this.fLength;
2022
+ HEAP32[this.fPtr + 2 * this.fPtrSize >> 2] = this.fSR;
2023
+ HEAP32[this.fPtr + 3 * this.fPtrSize >> 2] = this.fOffset;
2024
2024
  for (let chan = 0; chan < curChan; chan++) {
2025
2025
  const buffer = HEAP32[(this.fBuffers >> 2) + chan];
2026
2026
  console.log(`allocBuffers AFTER: ${chan} - ${buffer}`);
@@ -2043,10 +2043,18 @@ var Soundfile = class {
2043
2043
  }
2044
2044
  }
2045
2045
  copyToOut(part, maxChannels, offset, buffer) {
2046
- const HEAP32 = this.fAllocator.getInt32Array();
2047
- HEAP32[(this.fLength >> 2) + part] = buffer.length;
2048
- HEAP32[(this.fSR >> 2) + part] = buffer.sampleRate;
2049
- HEAP32[(this.fOffset >> 2) + part] = offset;
2046
+ if (this.fIntSize === 4) {
2047
+ const HEAP32 = this.fAllocator.getInt32Array();
2048
+ HEAP32[(this.fLength >> Math.log2(this.fIntSize)) + part] = buffer.length;
2049
+ HEAP32[(this.fSR >> Math.log2(this.fIntSize)) + part] = buffer.sampleRate;
2050
+ HEAP32[(this.fOffset >> Math.log2(this.fIntSize)) + part] = offset;
2051
+ } else {
2052
+ const HEAP64 = this.fAllocator.getInt64Array();
2053
+ HEAP64[(this.fLength >> Math.log2(this.fIntSize)) + part] = BigInt(buffer.length);
2054
+ HEAP64[(this.fSR >> Math.log2(this.fIntSize)) + part] = BigInt(buffer.sampleRate);
2055
+ HEAP64[(this.fOffset >> Math.log2(this.fIntSize)) + part] = BigInt(offset);
2056
+ }
2057
+ console.log(`copyToOut: part: ${part}, maxChannels: ${maxChannels}, offset: ${offset}, buffer: ${buffer}`);
2050
2058
  if (this.fSampleSize === 8) {
2051
2059
  this.copyToOutReal64(maxChannels, offset, buffer);
2052
2060
  } else {
@@ -2059,6 +2067,9 @@ var Soundfile = class {
2059
2067
  for (let chan = 0; chan < buffer.numberOfChannels; chan++) {
2060
2068
  const input = buffer.getChannelData(chan);
2061
2069
  const output = HEAP32[(this.fBuffers >> 2) + chan];
2070
+ const begin = output + offset * this.fSampleSize >> Math.log2(this.fSampleSize);
2071
+ const end = output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize);
2072
+ console.log(`copyToOutReal32 begin: ${begin}, end: ${end}, delta: ${end - begin}`);
2062
2073
  const outputReal = HEAPF.subarray(
2063
2074
  output + offset * this.fSampleSize >> Math.log2(this.fSampleSize),
2064
2075
  output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize)
@@ -2074,6 +2085,9 @@ var Soundfile = class {
2074
2085
  for (let chan = 0; chan < buffer.numberOfChannels; chan++) {
2075
2086
  const input = buffer.getChannelData(chan);
2076
2087
  const output = HEAP32[(this.fBuffers >> 2) + chan];
2088
+ const begin = output + offset * this.fSampleSize >> Math.log2(this.fSampleSize);
2089
+ const end = output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize);
2090
+ console.log(`copyToOutReal64 begin: ${begin}, end: ${end}, delta: ${end - begin}`);
2077
2091
  const outputReal = HEAPF.subarray(
2078
2092
  output + offset * this.fSampleSize >> Math.log2(this.fSampleSize),
2079
2093
  output + (offset + input.length) * this.fSampleSize >> Math.log2(this.fSampleSize)
@@ -2084,10 +2098,17 @@ var Soundfile = class {
2084
2098
  }
2085
2099
  }
2086
2100
  emptyFile(part, offset) {
2087
- const HEAP32 = this.fAllocator.getInt32Array();
2088
- HEAP32[(this.fLength >> 2) + part] = BUFFER_SIZE;
2089
- HEAP32[(this.fSR >> 2) + part] = SAMPLE_RATE;
2090
- HEAP32[(this.fOffset >> 2) + part] = offset;
2101
+ if (this.fIntSize === 4) {
2102
+ const HEAP32 = this.fAllocator.getInt32Array();
2103
+ HEAP32[(this.fLength >> Math.log2(this.fIntSize)) + part] = BUFFER_SIZE;
2104
+ HEAP32[(this.fSR >> Math.log2(this.fIntSize)) + part] = SAMPLE_RATE;
2105
+ HEAP32[(this.fOffset >> Math.log2(this.fIntSize)) + part] = offset;
2106
+ } else {
2107
+ const HEAP64 = this.fAllocator.getInt64Array();
2108
+ HEAP64[(this.fLength >> Math.log2(this.fIntSize)) + part] = BigInt(BUFFER_SIZE);
2109
+ HEAP64[(this.fSR >> Math.log2(this.fIntSize)) + part] = BigInt(SAMPLE_RATE);
2110
+ HEAP64[(this.fOffset >> Math.log2(this.fIntSize)) + part] = BigInt(offset);
2111
+ }
2091
2112
  return offset + BUFFER_SIZE;
2092
2113
  }
2093
2114
  displayMemory(where = "", mem = false) {
@@ -2119,30 +2140,29 @@ var Soundfile = class {
2119
2140
  }
2120
2141
  };
2121
2142
  var SoundfileReader = class {
2122
- constructor(allocator, context, ptrSize, sampleSize) {
2143
+ constructor(allocator, context, sampleSize) {
2123
2144
  this.fAllocator = allocator;
2124
- this.fPtrSize = ptrSize;
2125
2145
  this.fSampleSize = sampleSize;
2126
2146
  this.fContext = context;
2127
2147
  this.fAudioBuffers = [];
2128
2148
  }
2129
- async checkFile(directories, fileName) {
2130
- async function checkFileExists(url) {
2131
- try {
2132
- console.log(`"checkFileExists" url: ${url}`);
2133
- const response = await fetch(url, { method: "HEAD" });
2134
- return response.ok;
2135
- } catch (error) {
2136
- console.error("Fetch error:", error);
2137
- return false;
2138
- }
2149
+ async checkFileExists(url) {
2150
+ try {
2151
+ console.log(`"checkFileExists" url: ${url}`);
2152
+ const response = await fetch(url, { method: "HEAD" });
2153
+ return response.ok;
2154
+ } catch (error) {
2155
+ console.error("Fetch error:", error);
2156
+ return false;
2139
2157
  }
2140
- if (await checkFileExists(fileName)) {
2158
+ }
2159
+ async checkFile(directories, fileName) {
2160
+ if (await this.checkFileExists(fileName)) {
2141
2161
  return fileName;
2142
2162
  } else {
2143
2163
  for (let i = 0; i < directories.length; i++) {
2144
2164
  const pathName = directories[i] + "/" + fileName;
2145
- if (await checkFileExists(pathName)) {
2165
+ if (await this.checkFileExists(pathName)) {
2146
2166
  return pathName;
2147
2167
  }
2148
2168
  }
@@ -2188,7 +2208,7 @@ var SoundfileReader = class {
2188
2208
  return offset + BUFFER_SIZE;
2189
2209
  }
2190
2210
  }
2191
- async createSoundfile(pathNameList, maxChan, isDouble) {
2211
+ async createSoundfile(pathNameList, maxChan) {
2192
2212
  try {
2193
2213
  let curChan = 1;
2194
2214
  let totalLength = 0;
@@ -2206,7 +2226,7 @@ var SoundfileReader = class {
2206
2226
  totalLength += len;
2207
2227
  }
2208
2228
  totalLength += (MAX_SOUNDFILE_PARTS - pathNameList.length) * BUFFER_SIZE;
2209
- let soundfile = new Soundfile(this.fAllocator, this.fPtrSize, this.fSampleSize, curChan, totalLength, maxChan, pathNameList.length);
2229
+ let soundfile = new Soundfile(this.fAllocator, this.fSampleSize, curChan, totalLength, maxChan, pathNameList.length);
2210
2230
  let offset = 0;
2211
2231
  for (let part = 0; part < pathNameList.length; part++) {
2212
2232
  if (pathNameList[part] === "__empty_sound__") {
@@ -2330,7 +2350,7 @@ var FaustBaseWebAudioDsp = class {
2330
2350
  console.log(`Soundfile CACHE ${url}} : ${name} loaded at ${item.basePtr} in wasm memory with index ${item.index}`);
2331
2351
  HEAP32[baseDSP + item.index >> 2] = item.basePtr;
2332
2352
  } else {
2333
- const soundfile = await sfReader.createSoundfile(sfPathNames, MAX_CHAN, this.fSampleSize === 8);
2353
+ const soundfile = await sfReader.createSoundfile(sfPathNames, MAX_CHAN);
2334
2354
  if (soundfile) {
2335
2355
  const HEAP32 = soundfile.getHEAP32();
2336
2356
  item.basePtr = soundfile.getPtr();
@@ -2464,6 +2484,7 @@ var FaustMonoWebAudioDsp = class extends FaustBaseWebAudioDsp {
2464
2484
  constructor(instance, sampleRate, sampleSize, bufferSize) {
2465
2485
  super(sampleSize, bufferSize);
2466
2486
  this.fInstance = instance;
2487
+ console.log("sampleSize: " + sampleSize + " bufferSize: " + bufferSize);
2467
2488
  this.fJSONDsp = JSON.parse(this.fInstance.json);
2468
2489
  FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);
2469
2490
  this.fEndMemory = this.initMemory();
@@ -2472,7 +2493,7 @@ var FaustMonoWebAudioDsp = class extends FaustBaseWebAudioDsp {
2472
2493
  async init(context) {
2473
2494
  if (this.fSoundfiles.length > 0 && context) {
2474
2495
  const allocator = new WasmAllocator(this.fInstance.memory, this.fEndMemory);
2475
- const sfReader = new SoundfileReader(allocator, context, this.fPtrSize, this.fSampleSize);
2496
+ const sfReader = new SoundfileReader(allocator, context, this.fSampleSize);
2476
2497
  await this.initSoundfileMemory(allocator, sfReader, this.fDSP);
2477
2498
  }
2478
2499
  }
@@ -2676,6 +2697,7 @@ var FaustPolyWebAudioDsp = class extends FaustBaseWebAudioDsp {
2676
2697
  constructor(instance, sampleRate, sampleSize, bufferSize) {
2677
2698
  super(sampleSize, bufferSize);
2678
2699
  this.fInstance = instance;
2700
+ console.log("sampleSize: " + sampleSize + " bufferSize: " + bufferSize);
2679
2701
  this.fJSONDsp = JSON.parse(this.fInstance.voiceJSON);
2680
2702
  this.fJSONEffect = this.fInstance.effectAPI && this.fInstance.effectJSON ? JSON.parse(this.fInstance.effectJSON) : null;
2681
2703
  FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);
@@ -2698,7 +2720,7 @@ var FaustPolyWebAudioDsp = class extends FaustBaseWebAudioDsp {
2698
2720
  async init(context) {
2699
2721
  if (this.fSoundfiles.length > 0 && context) {
2700
2722
  const allocator = new WasmAllocator(this.fInstance.memory, this.fEndMemory);
2701
- const sfReader = new SoundfileReader(allocator, context, this.fPtrSize, this.fSampleSize);
2723
+ const sfReader = new SoundfileReader(allocator, context, this.fSampleSize);
2702
2724
  for (let voice = 0; voice < this.fInstance.voices; voice++) {
2703
2725
  await this.initSoundfileMemory(allocator, sfReader, this.fJSONDsp.size * voice);
2704
2726
  }