@grame/faustwasm 0.2.0 → 0.2.2

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.
@@ -1203,7 +1203,7 @@ export declare class SoundfileReader {
1203
1203
  * @param dspMeta : the metadata
1204
1204
  * @returns : the URLs
1205
1205
  */
1206
- private static findSoundfilesFromMeta;
1206
+ static findSoundfilesFromMeta(dspMeta: FaustDspMeta): LooseFaustDspFactory["soundfiles"];
1207
1207
  /**
1208
1208
  * Check if the file exists.
1209
1209
  *
@@ -1343,7 +1343,19 @@ export declare class FaustPolyScriptProcessorNode extends FaustScriptProcessorNo
1343
1343
  keyOff(channel: number, pitch: number, velocity: number): void;
1344
1344
  allNotesOff(hard: boolean): void;
1345
1345
  }
1346
- export interface IFaustMonoDspGenerator {
1346
+ export interface GeneratorSupportingSoundfiles {
1347
+ /**
1348
+ * Attach a map of id - audio data, call after `compile()` before `createNode()`
1349
+ *
1350
+ * @param soundfileMap a map of id - `AudioData` as an object where `AudioData` contains channel data as `audioBuffer: Float32Array[]` and `sampleRate: number`
1351
+ */
1352
+ addSoundfiles(soundfileMap: Record<string, AudioData>): void;
1353
+ /**
1354
+ * Get a list of soundfiles needed, call after `compile()`
1355
+ */
1356
+ getSoundfileList(): string[];
1357
+ }
1358
+ export interface IFaustMonoDspGenerator extends GeneratorSupportingSoundfiles {
1347
1359
  /**
1348
1360
  * Compile a monophonic DSP factory from given code.
1349
1361
  *
@@ -1388,9 +1400,10 @@ export interface IFaustMonoDspGenerator {
1388
1400
  * @param sampleRate - the sample rate in Hz
1389
1401
  * @param bufferSize - the buffer size in frames
1390
1402
  * @param factory - default is the compiled factory
1403
+ * @param context - if this exists, will be used to fetch soundfiles online
1391
1404
  * @returns the compiled processor or 'null' if failure
1392
1405
  */
1393
- createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory): Promise<IFaustMonoOfflineProcessor | null>;
1406
+ createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory, context?: BaseAudioContext): Promise<IFaustMonoOfflineProcessor | null>;
1394
1407
  /**
1395
1408
  * Get DSP JSON description with its UI and metadata as object.
1396
1409
  *
@@ -1410,7 +1423,7 @@ export interface IFaustMonoDspGenerator {
1410
1423
  */
1411
1424
  getUI(): FaustUIDescriptor;
1412
1425
  }
1413
- export interface IFaustPolyDspGenerator {
1426
+ export interface IFaustPolyDspGenerator extends GeneratorSupportingSoundfiles {
1414
1427
  /**
1415
1428
  * Compile a monophonic DSP factory from given code.
1416
1429
  *
@@ -1447,9 +1460,10 @@ export interface IFaustPolyDspGenerator {
1447
1460
  * @param voiceFactory - the Faust factory for voices, either obtained with a compiler (createDSPFactory) or loaded from files (loadDSPFactory)
1448
1461
  * @param mixerModule - the wasm Mixer module (loaded from 'mixer32.wasm' or 'mixer64.wasm' files)
1449
1462
  * @param effectFactory - the Faust factory for the effect, either obtained with a compiler (createDSPFactory) or loaded from files (loadDSPFactory)
1463
+ * @param context - if this exists, will be used to fetch soundfiles online
1450
1464
  * @returns the compiled processor or 'null' if failure
1451
1465
  */
1452
- createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, processorName?: string): Promise<IFaustPolyOfflineProcessor | null>;
1466
+ createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, context?: BaseAudioContext): Promise<IFaustPolyOfflineProcessor | null>;
1453
1467
  /**
1454
1468
  * Get DSP JSON description with its UI and metadata as object.
1455
1469
  *
@@ -1475,6 +1489,8 @@ export declare class FaustMonoDspGenerator implements IFaustMonoDspGenerator {
1475
1489
  factory: FaustDspFactory | null;
1476
1490
  constructor();
1477
1491
  compile(compiler: IFaustCompiler, name: string, code: string, args: string): Promise<this | null>;
1492
+ addSoundfiles(soundfileMap: Record<string, AudioData>): void;
1493
+ getSoundfileList(): string[];
1478
1494
  createNode<SP extends boolean = false>(context: BaseAudioContext, name?: string, factory?: LooseFaustDspFactory, sp?: SP, bufferSize?: number, processorName?: string): Promise<SP extends true ? FaustMonoScriptProcessorNode | null : FaustMonoAudioWorkletNode | null>;
1479
1495
  createFFTNode(context: BaseAudioContext, fftUtils: typeof FFTUtils, name?: string, factory?: LooseFaustDspFactory, fftOptions?: Partial<FaustFFTOptionsData>, processorName?: string): Promise<FaustMonoAudioWorkletNode | null>;
1480
1496
  createAudioWorkletProcessor(name?: string, factory?: LooseFaustDspFactory, processorName?: string): Promise<{
@@ -1482,7 +1498,7 @@ export declare class FaustMonoDspGenerator implements IFaustMonoDspGenerator {
1482
1498
  prototype: AudioWorkletProcessor;
1483
1499
  parameterDescriptors: AudioParamDescriptor[];
1484
1500
  }>;
1485
- createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory): Promise<FaustMonoOfflineProcessor>;
1501
+ createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory, context?: BaseAudioContext): Promise<FaustMonoOfflineProcessor>;
1486
1502
  getMeta(): any;
1487
1503
  getJSON(): string;
1488
1504
  getUI(): any;
@@ -1496,13 +1512,15 @@ export declare class FaustPolyDspGenerator implements IFaustPolyDspGenerator {
1496
1512
  mixerModule: WebAssembly.Module;
1497
1513
  constructor();
1498
1514
  compile(compiler: IFaustCompiler, name: string, dspCodeAux: string, args: string, effectCodeAux?: string): Promise<this | null>;
1515
+ addSoundfiles(soundfileMap: Record<string, AudioData>): void;
1516
+ getSoundfileList(): string[];
1499
1517
  createNode<SP extends boolean = false>(context: BaseAudioContext, voices: number, name?: string, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, sp?: SP, bufferSize?: number, processorName?: string): Promise<SP extends true ? FaustPolyScriptProcessorNode | null : FaustPolyAudioWorkletNode | null>;
1500
1518
  createAudioWorkletProcessor(name?: string, voiceFactory?: LooseFaustDspFactory, effectFactory?: LooseFaustDspFactory | null, processorName?: string): Promise<{
1501
1519
  new (options: AudioWorkletNodeOptions): AudioWorkletProcessor;
1502
1520
  prototype: AudioWorkletProcessor;
1503
1521
  parameterDescriptors: AudioParamDescriptor[];
1504
1522
  }>;
1505
- createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null): Promise<FaustPolyOfflineProcessor>;
1523
+ createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, context?: BaseAudioContext): Promise<FaustPolyOfflineProcessor>;
1506
1524
  getMeta(): FaustDspMeta;
1507
1525
  getJSON(): string;
1508
1526
  getUI(): FaustUIDescriptor;
@@ -9114,7 +9114,7 @@ var WavDecoder_default = WavDecoder;
9114
9114
  // src/SoundfileReader.ts
9115
9115
  var SoundfileReader = class {
9116
9116
  static get fallbackPaths() {
9117
- return [location.href, location.origin, "http://127.0.0.1:8000"];
9117
+ return [location.href, location.origin];
9118
9118
  }
9119
9119
  /**
9120
9120
  * Convert an audio buffer to audio data.
@@ -9547,6 +9547,20 @@ var _FaustMonoDspGenerator = class _FaustMonoDspGenerator {
9547
9547
  return null;
9548
9548
  }
9549
9549
  }
9550
+ addSoundfiles(soundfileMap) {
9551
+ if (!this.factory)
9552
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
9553
+ for (const id in soundfileMap) {
9554
+ this.factory.soundfiles[id] = soundfileMap[id];
9555
+ }
9556
+ }
9557
+ getSoundfileList() {
9558
+ if (!this.factory)
9559
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
9560
+ const meta = JSON.parse(this.factory.json);
9561
+ const map = SoundfileReader_default.findSoundfilesFromMeta(meta);
9562
+ return Object.keys(map);
9563
+ }
9550
9564
  async createNode(context, name = this.name, factory = this.factory, sp = false, bufferSize = 1024, processorName = (factory == null ? void 0 : factory.shaKey) || name) {
9551
9565
  var _a, _b;
9552
9566
  if (!factory)
@@ -9704,12 +9718,14 @@ const dependencies = {
9704
9718
  throw e;
9705
9719
  }
9706
9720
  }
9707
- async createOfflineProcessor(sampleRate, bufferSize, factory = this.factory) {
9721
+ async createOfflineProcessor(sampleRate, bufferSize, factory = this.factory, context) {
9708
9722
  if (!factory)
9709
9723
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
9710
9724
  const meta = JSON.parse(factory.json);
9711
9725
  const instance = await FaustWasmInstantiator_default.createAsyncMonoDSPInstance(factory);
9712
9726
  const sampleSize = meta.compile_options.match("-double") ? 8 : 4;
9727
+ if (context)
9728
+ factory.soundfiles = await SoundfileReader_default.loadSoundfiles(meta, factory.soundfiles || {}, context);
9713
9729
  const monoDsp = new FaustMonoWebAudioDsp(instance, sampleRate, sampleSize, bufferSize, factory.soundfiles);
9714
9730
  return new FaustMonoOfflineProcessor(monoDsp, bufferSize);
9715
9731
  }
@@ -9799,6 +9815,24 @@ process = adaptorIns(dsp_code.process) : dsp_code.effect : adaptorOuts;
9799
9815
  return null;
9800
9816
  }
9801
9817
  }
9818
+ addSoundfiles(soundfileMap) {
9819
+ if (!this.voiceFactory)
9820
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
9821
+ for (const id in soundfileMap) {
9822
+ this.voiceFactory.soundfiles[id] = soundfileMap[id];
9823
+ }
9824
+ }
9825
+ getSoundfileList() {
9826
+ if (!this.voiceFactory)
9827
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
9828
+ const meta = JSON.parse(this.voiceFactory.json);
9829
+ const map = SoundfileReader_default.findSoundfilesFromMeta(meta);
9830
+ if (!this.effectFactory)
9831
+ return Object.keys(map);
9832
+ const effectMeta = JSON.parse(this.effectFactory.json);
9833
+ const effectMap = SoundfileReader_default.findSoundfilesFromMeta(effectMeta);
9834
+ return Object.keys({ ...effectMap, ...map });
9835
+ }
9802
9836
  async createNode(context, voices, name = this.name, voiceFactory = this.voiceFactory, mixerModule = this.mixerModule, effectFactory = this.effectFactory, sp = false, bufferSize = 1024, processorName = ((voiceFactory == null ? void 0 : voiceFactory.shaKey) || "") + ((effectFactory == null ? void 0 : effectFactory.shaKey) || "") || `${name}_poly`) {
9803
9837
  var _a, _b;
9804
9838
  if (!voiceFactory)
@@ -9893,13 +9927,18 @@ const dependencies = {
9893
9927
  throw e;
9894
9928
  }
9895
9929
  }
9896
- async createOfflineProcessor(sampleRate, bufferSize, voices, voiceFactory = this.voiceFactory, mixerModule = this.mixerModule, effectFactory = this.effectFactory) {
9930
+ async createOfflineProcessor(sampleRate, bufferSize, voices, voiceFactory = this.voiceFactory, mixerModule = this.mixerModule, effectFactory = this.effectFactory, context) {
9897
9931
  if (!voiceFactory)
9898
9932
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
9899
9933
  const voiceMeta = JSON.parse(voiceFactory.json);
9900
9934
  const effectMeta = effectFactory ? JSON.parse(effectFactory.json) : void 0;
9901
9935
  const instance = await FaustWasmInstantiator_default.createAsyncPolyDSPInstance(voiceFactory, mixerModule, voices, effectFactory || void 0);
9902
9936
  const sampleSize = voiceMeta.compile_options.match("-double") ? 8 : 4;
9937
+ if (context) {
9938
+ voiceFactory.soundfiles = await SoundfileReader_default.loadSoundfiles(voiceMeta, voiceFactory.soundfiles || {}, context);
9939
+ if (effectFactory)
9940
+ effectFactory.soundfiles = await SoundfileReader_default.loadSoundfiles(effectMeta, effectFactory.soundfiles || {}, context);
9941
+ }
9903
9942
  const soundfiles = { ...effectFactory == null ? void 0 : effectFactory.soundfiles, ...voiceFactory.soundfiles };
9904
9943
  const polyDsp = new FaustPolyWebAudioDsp(instance, sampleRate, sampleSize, bufferSize, soundfiles);
9905
9944
  return new FaustPolyOfflineProcessor(polyDsp, bufferSize);