@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.
@@ -1196,7 +1196,7 @@ export declare class SoundfileReader {
1196
1196
  * @param dspMeta : the metadata
1197
1197
  * @returns : the URLs
1198
1198
  */
1199
- private static findSoundfilesFromMeta;
1199
+ static findSoundfilesFromMeta(dspMeta: FaustDspMeta): LooseFaustDspFactory["soundfiles"];
1200
1200
  /**
1201
1201
  * Check if the file exists.
1202
1202
  *
@@ -1336,7 +1336,19 @@ export declare class FaustPolyScriptProcessorNode extends FaustScriptProcessorNo
1336
1336
  keyOff(channel: number, pitch: number, velocity: number): void;
1337
1337
  allNotesOff(hard: boolean): void;
1338
1338
  }
1339
- export interface IFaustMonoDspGenerator {
1339
+ export interface GeneratorSupportingSoundfiles {
1340
+ /**
1341
+ * Attach a map of id - audio data, call after `compile()` before `createNode()`
1342
+ *
1343
+ * @param soundfileMap a map of id - `AudioData` as an object where `AudioData` contains channel data as `audioBuffer: Float32Array[]` and `sampleRate: number`
1344
+ */
1345
+ addSoundfiles(soundfileMap: Record<string, AudioData>): void;
1346
+ /**
1347
+ * Get a list of soundfiles needed, call after `compile()`
1348
+ */
1349
+ getSoundfileList(): string[];
1350
+ }
1351
+ export interface IFaustMonoDspGenerator extends GeneratorSupportingSoundfiles {
1340
1352
  /**
1341
1353
  * Compile a monophonic DSP factory from given code.
1342
1354
  *
@@ -1381,9 +1393,10 @@ export interface IFaustMonoDspGenerator {
1381
1393
  * @param sampleRate - the sample rate in Hz
1382
1394
  * @param bufferSize - the buffer size in frames
1383
1395
  * @param factory - default is the compiled factory
1396
+ * @param context - if this exists, will be used to fetch soundfiles online
1384
1397
  * @returns the compiled processor or 'null' if failure
1385
1398
  */
1386
- createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory): Promise<IFaustMonoOfflineProcessor | null>;
1399
+ createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory, context?: BaseAudioContext): Promise<IFaustMonoOfflineProcessor | null>;
1387
1400
  /**
1388
1401
  * Get DSP JSON description with its UI and metadata as object.
1389
1402
  *
@@ -1403,7 +1416,7 @@ export interface IFaustMonoDspGenerator {
1403
1416
  */
1404
1417
  getUI(): FaustUIDescriptor;
1405
1418
  }
1406
- export interface IFaustPolyDspGenerator {
1419
+ export interface IFaustPolyDspGenerator extends GeneratorSupportingSoundfiles {
1407
1420
  /**
1408
1421
  * Compile a monophonic DSP factory from given code.
1409
1422
  *
@@ -1440,9 +1453,10 @@ export interface IFaustPolyDspGenerator {
1440
1453
  * @param voiceFactory - the Faust factory for voices, either obtained with a compiler (createDSPFactory) or loaded from files (loadDSPFactory)
1441
1454
  * @param mixerModule - the wasm Mixer module (loaded from 'mixer32.wasm' or 'mixer64.wasm' files)
1442
1455
  * @param effectFactory - the Faust factory for the effect, either obtained with a compiler (createDSPFactory) or loaded from files (loadDSPFactory)
1456
+ * @param context - if this exists, will be used to fetch soundfiles online
1443
1457
  * @returns the compiled processor or 'null' if failure
1444
1458
  */
1445
- createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, processorName?: string): Promise<IFaustPolyOfflineProcessor | null>;
1459
+ createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, context?: BaseAudioContext): Promise<IFaustPolyOfflineProcessor | null>;
1446
1460
  /**
1447
1461
  * Get DSP JSON description with its UI and metadata as object.
1448
1462
  *
@@ -1468,6 +1482,8 @@ export declare class FaustMonoDspGenerator implements IFaustMonoDspGenerator {
1468
1482
  factory: FaustDspFactory | null;
1469
1483
  constructor();
1470
1484
  compile(compiler: IFaustCompiler, name: string, code: string, args: string): Promise<this | null>;
1485
+ addSoundfiles(soundfileMap: Record<string, AudioData>): void;
1486
+ getSoundfileList(): string[];
1471
1487
  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>;
1472
1488
  createFFTNode(context: BaseAudioContext, fftUtils: typeof FFTUtils, name?: string, factory?: LooseFaustDspFactory, fftOptions?: Partial<FaustFFTOptionsData>, processorName?: string): Promise<FaustMonoAudioWorkletNode | null>;
1473
1489
  createAudioWorkletProcessor(name?: string, factory?: LooseFaustDspFactory, processorName?: string): Promise<{
@@ -1475,7 +1491,7 @@ export declare class FaustMonoDspGenerator implements IFaustMonoDspGenerator {
1475
1491
  prototype: AudioWorkletProcessor;
1476
1492
  parameterDescriptors: AudioParamDescriptor[];
1477
1493
  }>;
1478
- createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory): Promise<FaustMonoOfflineProcessor>;
1494
+ createOfflineProcessor(sampleRate: number, bufferSize: number, factory?: LooseFaustDspFactory, context?: BaseAudioContext): Promise<FaustMonoOfflineProcessor>;
1479
1495
  getMeta(): any;
1480
1496
  getJSON(): string;
1481
1497
  getUI(): any;
@@ -1489,13 +1505,15 @@ export declare class FaustPolyDspGenerator implements IFaustPolyDspGenerator {
1489
1505
  mixerModule: WebAssembly.Module;
1490
1506
  constructor();
1491
1507
  compile(compiler: IFaustCompiler, name: string, dspCodeAux: string, args: string, effectCodeAux?: string): Promise<this | null>;
1508
+ addSoundfiles(soundfileMap: Record<string, AudioData>): void;
1509
+ getSoundfileList(): string[];
1492
1510
  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>;
1493
1511
  createAudioWorkletProcessor(name?: string, voiceFactory?: LooseFaustDspFactory, effectFactory?: LooseFaustDspFactory | null, processorName?: string): Promise<{
1494
1512
  new (options: AudioWorkletNodeOptions): AudioWorkletProcessor;
1495
1513
  prototype: AudioWorkletProcessor;
1496
1514
  parameterDescriptors: AudioParamDescriptor[];
1497
1515
  }>;
1498
- createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null): Promise<FaustPolyOfflineProcessor>;
1516
+ createOfflineProcessor(sampleRate: number, bufferSize: number, voices: number, voiceFactory?: LooseFaustDspFactory, mixerModule?: WebAssembly.Module, effectFactory?: LooseFaustDspFactory | null, context?: BaseAudioContext): Promise<FaustPolyOfflineProcessor>;
1499
1517
  getMeta(): FaustDspMeta;
1500
1518
  getJSON(): string;
1501
1519
  getUI(): FaustUIDescriptor;
package/dist/cjs/index.js CHANGED
@@ -3142,7 +3142,7 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3142
3142
  // src/SoundfileReader.ts
3143
3143
  var SoundfileReader = class {
3144
3144
  static get fallbackPaths() {
3145
- return [location.href, location.origin, "http://127.0.0.1:8000"];
3145
+ return [location.href, location.origin];
3146
3146
  }
3147
3147
  /**
3148
3148
  * Convert an audio buffer to audio data.
@@ -3575,6 +3575,20 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3575
3575
  return null;
3576
3576
  }
3577
3577
  }
3578
+ addSoundfiles(soundfileMap) {
3579
+ if (!this.factory)
3580
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
3581
+ for (const id in soundfileMap) {
3582
+ this.factory.soundfiles[id] = soundfileMap[id];
3583
+ }
3584
+ }
3585
+ getSoundfileList() {
3586
+ if (!this.factory)
3587
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
3588
+ const meta = JSON.parse(this.factory.json);
3589
+ const map = SoundfileReader_default.findSoundfilesFromMeta(meta);
3590
+ return Object.keys(map);
3591
+ }
3578
3592
  async createNode(context, name = this.name, factory = this.factory, sp = false, bufferSize = 1024, processorName = (factory == null ? void 0 : factory.shaKey) || name) {
3579
3593
  var _a, _b;
3580
3594
  if (!factory)
@@ -3732,12 +3746,14 @@ const dependencies = {
3732
3746
  throw e;
3733
3747
  }
3734
3748
  }
3735
- async createOfflineProcessor(sampleRate, bufferSize, factory = this.factory) {
3749
+ async createOfflineProcessor(sampleRate, bufferSize, factory = this.factory, context) {
3736
3750
  if (!factory)
3737
3751
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
3738
3752
  const meta = JSON.parse(factory.json);
3739
3753
  const instance = await FaustWasmInstantiator_default.createAsyncMonoDSPInstance(factory);
3740
3754
  const sampleSize = meta.compile_options.match("-double") ? 8 : 4;
3755
+ if (context)
3756
+ factory.soundfiles = await SoundfileReader_default.loadSoundfiles(meta, factory.soundfiles || {}, context);
3741
3757
  const monoDsp = new FaustMonoWebAudioDsp(instance, sampleRate, sampleSize, bufferSize, factory.soundfiles);
3742
3758
  return new FaustMonoOfflineProcessor(monoDsp, bufferSize);
3743
3759
  }
@@ -3827,6 +3843,24 @@ process = adaptorIns(dsp_code.process) : dsp_code.effect : adaptorOuts;
3827
3843
  return null;
3828
3844
  }
3829
3845
  }
3846
+ addSoundfiles(soundfileMap) {
3847
+ if (!this.voiceFactory)
3848
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
3849
+ for (const id in soundfileMap) {
3850
+ this.voiceFactory.soundfiles[id] = soundfileMap[id];
3851
+ }
3852
+ }
3853
+ getSoundfileList() {
3854
+ if (!this.voiceFactory)
3855
+ throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
3856
+ const meta = JSON.parse(this.voiceFactory.json);
3857
+ const map = SoundfileReader_default.findSoundfilesFromMeta(meta);
3858
+ if (!this.effectFactory)
3859
+ return Object.keys(map);
3860
+ const effectMeta = JSON.parse(this.effectFactory.json);
3861
+ const effectMap = SoundfileReader_default.findSoundfilesFromMeta(effectMeta);
3862
+ return Object.keys({ ...effectMap, ...map });
3863
+ }
3830
3864
  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`) {
3831
3865
  var _a, _b;
3832
3866
  if (!voiceFactory)
@@ -3921,13 +3955,18 @@ const dependencies = {
3921
3955
  throw e;
3922
3956
  }
3923
3957
  }
3924
- async createOfflineProcessor(sampleRate, bufferSize, voices, voiceFactory = this.voiceFactory, mixerModule = this.mixerModule, effectFactory = this.effectFactory) {
3958
+ async createOfflineProcessor(sampleRate, bufferSize, voices, voiceFactory = this.voiceFactory, mixerModule = this.mixerModule, effectFactory = this.effectFactory, context) {
3925
3959
  if (!voiceFactory)
3926
3960
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
3927
3961
  const voiceMeta = JSON.parse(voiceFactory.json);
3928
3962
  const effectMeta = effectFactory ? JSON.parse(effectFactory.json) : void 0;
3929
3963
  const instance = await FaustWasmInstantiator_default.createAsyncPolyDSPInstance(voiceFactory, mixerModule, voices, effectFactory || void 0);
3930
3964
  const sampleSize = voiceMeta.compile_options.match("-double") ? 8 : 4;
3965
+ if (context) {
3966
+ voiceFactory.soundfiles = await SoundfileReader_default.loadSoundfiles(voiceMeta, voiceFactory.soundfiles || {}, context);
3967
+ if (effectFactory)
3968
+ effectFactory.soundfiles = await SoundfileReader_default.loadSoundfiles(effectMeta, effectFactory.soundfiles || {}, context);
3969
+ }
3931
3970
  const soundfiles = { ...effectFactory == null ? void 0 : effectFactory.soundfiles, ...voiceFactory.soundfiles };
3932
3971
  const polyDsp = new FaustPolyWebAudioDsp(instance, sampleRate, sampleSize, bufferSize, soundfiles);
3933
3972
  return new FaustPolyOfflineProcessor(polyDsp, bufferSize);