@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grame/faustwasm",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "WebAssembly version of Faust Compiler",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -139,9 +139,13 @@ class FaustWasmInstantiator {
139
139
  }
140
140
 
141
141
  static async createAsyncMonoDSPInstance(factory: LooseFaustDspFactory) {
142
- const parsedJson = JSON.parse(factory.json);
143
- // If the JSON contains a soundfile UI element, we need to create a memory object
144
- if (Array.isArray(parsedJson.ui) && parsedJson.ui.some((group: { items: any[]; }) => group.items?.some(item => item.type === "soundfile"))) {
142
+
143
+ // Regular expression to match the 'type: soundfile' pattern
144
+ const pattern = /"type":\s*"soundfile"/;
145
+ // Check if the pattern exists in the JSON string
146
+ const isDetected = pattern.test(factory.json);
147
+
148
+ if (isDetected) {
145
149
  const memory = this.createMemoryMono(factory);
146
150
  const instance = await WebAssembly.instantiate(factory.module, this.createWasmImport(memory));
147
151
  return this.createMonoDSPInstanceAux(instance, factory.json, memory);
@@ -153,9 +157,14 @@ class FaustWasmInstantiator {
153
157
  }
154
158
 
155
159
  static createSyncMonoDSPInstance(factory: LooseFaustDspFactory) {
156
- const parsedJson = JSON.parse(factory.json);
160
+
161
+ // Regular expression to match the 'type: soundfile' pattern
162
+ const pattern = /"type":\s*"soundfile"/;
163
+ // Check if the pattern exists in the JSON string
164
+ const isDetected = pattern.test(factory.json);
165
+
157
166
  // If the JSON contains a soundfile UI element, we need to create a memory object
158
- if (Array.isArray(parsedJson.ui) && parsedJson.ui.some((group: { items: any[]; }) => group.items?.some(item => item.type === "soundfile"))) {
167
+ if (isDetected) {
159
168
  const memory = this.createMemoryMono(factory);
160
169
  const instance = new WebAssembly.Instance(factory.module, this.createWasmImport(memory));
161
170
  return this.createMonoDSPInstanceAux(instance, factory.json, memory);
@@ -82,6 +82,15 @@ class WasmAllocator {
82
82
  return new Int32Array(this.memory.buffer);
83
83
  }
84
84
 
85
+ /**
86
+ * Returns the Int64 view of the underlying buffer object.
87
+ *
88
+ * @returns The view of the memory buffer as BigInt64Array.
89
+ */
90
+ getInt64Array(): BigInt64Array {
91
+ return new BigInt64Array(this.memory.buffer);
92
+ }
93
+
85
94
  /**
86
95
  * Returns the Float32 view of the underlying buffer object.
87
96
  *
@@ -113,9 +122,6 @@ const BUFFER_SIZE = 1024;
113
122
  // Default sample rate.
114
123
  const SAMPLE_RATE = 44100;
115
124
 
116
- // Size of an integer in bytes.
117
- const intSize = 4;
118
-
119
125
  /**
120
126
  * Soundfile class to handle soundfile data in wasm memory.
121
127
  */
@@ -127,20 +133,32 @@ class Soundfile {
127
133
  private readonly fOffset: number;
128
134
  private readonly fSampleSize: number;
129
135
  private readonly fPtrSize: number;
136
+ private readonly fIntSize: number;
130
137
  private readonly fAllocator: WasmAllocator;
131
138
 
132
- constructor(allocator: WasmAllocator, ptrSize: number, sampleSize: number, curChan: number, length: number, maxChan: number, totalParts: number) {
133
- // Keep the soundfile structure parameters
139
+ constructor(allocator: WasmAllocator, sampleSize: number, curChan: number, length: number, maxChan: number, totalParts: number) {
134
140
 
135
141
  this.fSampleSize = sampleSize;
136
- this.fPtrSize = ptrSize;
142
+
143
+ // To be coherent with the code generated by the wast/wasm backends:
144
+ // - that uses 4 bytes for int when float is used
145
+ // - that uses 8 bytes for int when double is used (to simplify the code generation)
146
+ this.fIntSize = this.fSampleSize;
147
+
148
+ this.fPtrSize = 4; // Not related to float/double choice, so always 4
149
+
137
150
  this.fAllocator = allocator;
138
151
 
152
+ console.log(`Soundfile constructor: curChan: ${curChan}, length: ${length}, maxChan: ${maxChan}, totalParts: ${totalParts}`);
153
+
139
154
  // Allocate wasm memory for the soundfile structure
140
- this.fPtr = allocator.alloc(4 * ptrSize); // 4 ptrSize: fBuffers, fLength, fSR, fOffset
141
- this.fLength = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
142
- this.fSR = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
143
- this.fOffset = allocator.alloc(MAX_SOUNDFILE_PARTS * intSize);
155
+ this.fPtr = allocator.alloc(4 * this.fPtrSize); // 4 fPtrSize: fBuffers, fLength, fSR, fOffset
156
+
157
+ // Use the 4 or 8 bytes size for int. The access are then adapted in copyToOut and emptyFile methods
158
+ this.fLength = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
159
+ this.fSR = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
160
+ this.fOffset = allocator.alloc(MAX_SOUNDFILE_PARTS * this.fIntSize);
161
+
144
162
  this.fBuffers = this.allocBuffers(curChan, length, maxChan);
145
163
 
146
164
  //this.displayMemory("Allocated soundfile structure 1");
@@ -148,9 +166,9 @@ class Soundfile {
148
166
  // Set the soundfile structure in wasm memory
149
167
  const HEAP32 = this.fAllocator.getInt32Array();
150
168
  HEAP32[this.fPtr >> 2] = this.fBuffers;
151
- HEAP32[(this.fPtr + ptrSize) >> 2] = this.fLength;
152
- HEAP32[(this.fPtr + 2 * ptrSize) >> 2] = this.fSR;
153
- HEAP32[(this.fPtr + 3 * ptrSize) >> 2] = this.fOffset;
169
+ HEAP32[(this.fPtr + this.fPtrSize) >> 2] = this.fLength;
170
+ HEAP32[(this.fPtr + (2 * this.fPtrSize)) >> 2] = this.fSR;
171
+ HEAP32[(this.fPtr + (3 * this.fPtrSize)) >> 2] = this.fOffset;
154
172
 
155
173
  for (let chan = 0; chan < curChan; chan++) {
156
174
  const buffer: number = HEAP32[(this.fBuffers >> 2) + chan];
@@ -185,10 +203,21 @@ class Soundfile {
185
203
  }
186
204
 
187
205
  copyToOut(part: number, maxChannels: number, offset: number, buffer: AudioBuffer) {
188
- const HEAP32 = this.fAllocator.getInt32Array();
189
- HEAP32[(this.fLength >> 2) + part] = buffer.length;
190
- HEAP32[(this.fSR >> 2) + part] = buffer.sampleRate;
191
- HEAP32[(this.fOffset >> 2) + part] = offset;
206
+
207
+ // Set the soundfile fields in wasm memory
208
+ if (this.fIntSize === 4) {
209
+ const HEAP32 = this.fAllocator.getInt32Array();
210
+ HEAP32[(this.fLength >> Math.log2(this.fIntSize)) + part] = buffer.length;
211
+ HEAP32[(this.fSR >> Math.log2(this.fIntSize)) + part] = buffer.sampleRate;
212
+ HEAP32[(this.fOffset >> Math.log2(this.fIntSize)) + part] = offset;
213
+ } else {
214
+ const HEAP64 = this.fAllocator.getInt64Array();
215
+ HEAP64[(this.fLength >> Math.log2(this.fIntSize)) + part] = BigInt(buffer.length);
216
+ HEAP64[(this.fSR >> Math.log2(this.fIntSize)) + part] = BigInt(buffer.sampleRate);
217
+ HEAP64[(this.fOffset >> Math.log2(this.fIntSize)) + part] = BigInt(offset);
218
+ }
219
+
220
+ console.log(`copyToOut: part: ${part}, maxChannels: ${maxChannels}, offset: ${offset}, buffer: ${buffer}`);
192
221
 
193
222
  //this.displayMemory("IN copyToOut, BEFORE copyToOutReal", true);
194
223
  // Copy the soundfile data to the buffer
@@ -206,7 +235,10 @@ class Soundfile {
206
235
  for (let chan = 0; chan < buffer.numberOfChannels; chan++) {
207
236
  const input: Float32Array = buffer.getChannelData(chan);
208
237
  const output: number = HEAP32[(this.fBuffers >> 2) + chan];
209
- const outputReal: Float32Array = HEAPF.subarray((output + offset * this.fSampleSize) >> Math.log2(this.fSampleSize),
238
+ const begin: number = (output + (offset * this.fSampleSize)) >> Math.log2(this.fSampleSize);
239
+ const end: number = (output + (offset + input.length) * this.fSampleSize) >> Math.log2(this.fSampleSize);
240
+ console.log(`copyToOutReal32 begin: ${begin}, end: ${end}, delta: ${end - begin}`);
241
+ const outputReal: Float32Array = HEAPF.subarray((output + (offset * this.fSampleSize)) >> Math.log2(this.fSampleSize),
210
242
  (output + (offset + input.length) * this.fSampleSize) >> Math.log2(this.fSampleSize));
211
243
  for (let sample = 0; sample < input.length; sample++) {
212
244
  outputReal[sample] = input[sample];
@@ -220,7 +252,10 @@ class Soundfile {
220
252
  for (let chan = 0; chan < buffer.numberOfChannels; chan++) {
221
253
  const input: Float32Array = buffer.getChannelData(chan);
222
254
  const output: number = HEAP32[(this.fBuffers >> 2) + chan];
223
- const outputReal: Float64Array = HEAPF.subarray((output + offset * this.fSampleSize) >> Math.log2(this.fSampleSize),
255
+ const begin: number = (output + (offset * this.fSampleSize)) >> Math.log2(this.fSampleSize);
256
+ const end: number = (output + (offset + input.length) * this.fSampleSize) >> Math.log2(this.fSampleSize);
257
+ console.log(`copyToOutReal64 begin: ${begin}, end: ${end}, delta: ${end - begin}`);
258
+ const outputReal: Float64Array = HEAPF.subarray((output + (offset * this.fSampleSize)) >> Math.log2(this.fSampleSize),
224
259
  (output + (offset + input.length) * this.fSampleSize) >> Math.log2(this.fSampleSize));
225
260
  for (let sample = 0; sample < input.length; sample++) {
226
261
  outputReal[sample] = input[sample];
@@ -229,11 +264,19 @@ class Soundfile {
229
264
  }
230
265
 
231
266
  emptyFile(part: number, offset: number): number {
232
- // Set the soundfile buffer in wasm memory
233
- const HEAP32 = this.fAllocator.getInt32Array();
234
- HEAP32[(this.fLength >> 2) + part] = BUFFER_SIZE;
235
- HEAP32[(this.fSR >> 2) + part] = SAMPLE_RATE;
236
- HEAP32[(this.fOffset >> 2) + part] = offset;
267
+
268
+ // Set the soundfile fields in wasm memory
269
+ if (this.fIntSize === 4) {
270
+ const HEAP32 = this.fAllocator.getInt32Array();
271
+ HEAP32[(this.fLength >> Math.log2(this.fIntSize)) + part] = BUFFER_SIZE;
272
+ HEAP32[(this.fSR >> Math.log2(this.fIntSize)) + part] = SAMPLE_RATE;
273
+ HEAP32[(this.fOffset >> Math.log2(this.fIntSize)) + part] = offset;
274
+ } else {
275
+ const HEAP64 = this.fAllocator.getInt64Array();
276
+ HEAP64[(this.fLength >> Math.log2(this.fIntSize)) + part] = BigInt(BUFFER_SIZE);
277
+ HEAP64[(this.fSR >> Math.log2(this.fIntSize)) + part] = BigInt(SAMPLE_RATE);
278
+ HEAP64[(this.fOffset >> Math.log2(this.fIntSize)) + part] = BigInt(offset);
279
+ }
237
280
 
238
281
  // Update and return the new offset
239
282
  return offset + BUFFER_SIZE;
@@ -284,19 +327,34 @@ type AudioBufferItem = {
284
327
  class SoundfileReader {
285
328
 
286
329
  private readonly fAllocator: WasmAllocator;
287
- private readonly fPtrSize: number;
288
330
  private readonly fSampleSize: number;
289
331
  private readonly fContext;
290
332
  private readonly fAudioBuffers: AudioBufferItem[];
291
333
 
292
- constructor(allocator: WasmAllocator, context: BaseAudioContext, ptrSize: number, sampleSize: number) {
334
+ constructor(allocator: WasmAllocator, context: BaseAudioContext, sampleSize: number) {
293
335
  this.fAllocator = allocator;
294
- this.fPtrSize = ptrSize;
295
336
  this.fSampleSize = sampleSize;
296
337
  this.fContext = context;
297
338
  this.fAudioBuffers = [];
298
339
  }
299
340
 
341
+ /**
342
+ * Check if the file exists.
343
+ *
344
+ * @param url : the url of the file to check
345
+ * @returns : true if the file exists, otherwise false
346
+ */
347
+ private async checkFileExists(url: string): Promise<boolean> {
348
+ try {
349
+ console.log(`"checkFileExists" url: ${url}`);
350
+ const response = await fetch(url, { method: 'HEAD' });
351
+ return response.ok; // Will be true if the status code is 200-299
352
+ } catch (error) {
353
+ console.error('Fetch error:', error);
354
+ return false;
355
+ }
356
+ }
357
+
300
358
  /**
301
359
  * Check if the file exists in the given directories.
302
360
  *
@@ -306,23 +364,12 @@ class SoundfileReader {
306
364
  */
307
365
  private async checkFile(directories: string[], fileName: string): Promise<string> {
308
366
 
309
- async function checkFileExists(url: string): Promise<boolean> {
310
- try {
311
- console.log(`"checkFileExists" url: ${url}`);
312
- const response = await fetch(url, { method: 'HEAD' });
313
- return response.ok; // Will be true if the status code is 200-299
314
- } catch (error) {
315
- console.error('Fetch error:', error);
316
- return false;
317
- }
318
- }
319
-
320
- if (await checkFileExists(fileName)) {
367
+ if (await this.checkFileExists(fileName)) {
321
368
  return fileName;
322
369
  } else {
323
370
  for (let i = 0; i < directories.length; i++) {
324
371
  const pathName = directories[i] + "/" + fileName;
325
- if (await checkFileExists(pathName)) {
372
+ if (await this.checkFileExists(pathName)) {
326
373
  return pathName;
327
374
  }
328
375
  }
@@ -412,9 +459,8 @@ class SoundfileReader {
412
459
  * Crate a soundfile, load all parts and copy audio data to the wasm soundfile buffer.
413
460
  * @param pathNameList : list of soundfile paths
414
461
  * @param maxChan : maximum number of channels
415
- * @param isDouble : whether the soundfile will be copied as double
416
462
  */
417
- async createSoundfile(pathNameList: string[], maxChan: number, isDouble: boolean): Promise<Soundfile | null> {
463
+ async createSoundfile(pathNameList: string[], maxChan: number): Promise<Soundfile | null> {
418
464
  try {
419
465
  let curChan = 1; // At least one channel
420
466
  let totalLength = 0;
@@ -438,7 +484,7 @@ class SoundfileReader {
438
484
  totalLength += (MAX_SOUNDFILE_PARTS - pathNameList.length) * BUFFER_SIZE;
439
485
 
440
486
  // Create the soundfile
441
- let soundfile = new Soundfile(this.fAllocator, this.fPtrSize, this.fSampleSize, curChan, totalLength, maxChan, pathNameList.length);
487
+ let soundfile = new Soundfile(this.fAllocator, this.fSampleSize, curChan, totalLength, maxChan, pathNameList.length);
442
488
 
443
489
  //soundfile.displayMemory("After soundfile creation");
444
490
  // Init offset
@@ -887,7 +933,7 @@ export class FaustBaseWebAudioDsp implements IFaustBaseWebAudioDsp {
887
933
  } else {
888
934
 
889
935
  // Create the soundfiles
890
- const soundfile = await sfReader.createSoundfile(sfPathNames, MAX_CHAN, this.fSampleSize === 8);
936
+ const soundfile = await sfReader.createSoundfile(sfPathNames, MAX_CHAN);
891
937
  if (soundfile) {
892
938
 
893
939
  // Update HEAP32 after soundfile creation
@@ -1037,6 +1083,8 @@ export class FaustMonoWebAudioDsp extends FaustBaseWebAudioDsp implements IFaust
1037
1083
  super(sampleSize, bufferSize);
1038
1084
  this.fInstance = instance;
1039
1085
 
1086
+ console.log("sampleSize: " + sampleSize + " bufferSize: " + bufferSize);
1087
+
1040
1088
  // Create JSON object
1041
1089
  this.fJSONDsp = JSON.parse(this.fInstance.json);
1042
1090
 
@@ -1059,7 +1107,7 @@ export class FaustMonoWebAudioDsp extends FaustBaseWebAudioDsp implements IFaust
1059
1107
  const allocator = new WasmAllocator(this.fInstance.memory, this.fEndMemory);
1060
1108
 
1061
1109
  // Create soundfile reader
1062
- const sfReader = new SoundfileReader(allocator, context, this.fPtrSize, this.fSampleSize);
1110
+ const sfReader = new SoundfileReader(allocator, context, this.fSampleSize);
1063
1111
 
1064
1112
  // Init soundfiles memory
1065
1113
  await this.initSoundfileMemory(allocator, sfReader, this.fDSP);
@@ -1179,6 +1227,7 @@ export class FaustMonoWebAudioDsp extends FaustBaseWebAudioDsp implements IFaust
1179
1227
  for (let chan = 0; chan < Math.min(this.getNumOutputs(), output.length); chan++) {
1180
1228
  const dspOutput = this.fOutChannels[chan];
1181
1229
  output[chan].set(dspOutput);
1230
+ // console.log("chan: " + chan + " output: " + dspOutput[0]);
1182
1231
  }
1183
1232
  forPlot = output;
1184
1233
  }
@@ -1350,6 +1399,8 @@ export class FaustPolyWebAudioDsp extends FaustBaseWebAudioDsp implements IFaust
1350
1399
  super(sampleSize, bufferSize);
1351
1400
  this.fInstance = instance;
1352
1401
 
1402
+ console.log("sampleSize: " + sampleSize + " bufferSize: " + bufferSize);
1403
+
1353
1404
  // Create JSON for voice
1354
1405
  this.fJSONDsp = JSON.parse(this.fInstance.voiceJSON);
1355
1406
 
@@ -1388,7 +1439,7 @@ export class FaustPolyWebAudioDsp extends FaustBaseWebAudioDsp implements IFaust
1388
1439
  const allocator = new WasmAllocator(this.fInstance.memory, this.fEndMemory);
1389
1440
 
1390
1441
  // Create soundfile reader
1391
- const sfReader = new SoundfileReader(allocator, context, this.fPtrSize, this.fSampleSize);
1442
+ const sfReader = new SoundfileReader(allocator, context, this.fSampleSize);
1392
1443
 
1393
1444
  // Init soundfiles memory for all voices
1394
1445
  for (let voice = 0; voice < this.fInstance.voices; voice++) {
@@ -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
  }