@grame/faustwasm 0.4.5 → 0.4.8

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.
Files changed (52) hide show
  1. package/COPYING.txt +520 -0
  2. package/README.md +4 -4
  3. package/assets/standalone/create-node.js +78 -0
  4. package/assets/standalone/faust-ui/index.css +8 -0
  5. package/assets/standalone/faust-ui/index.css.map +1 -1
  6. package/assets/standalone/faust-ui/index.js +56 -30
  7. package/assets/standalone/faust-ui/index.js.map +1 -1
  8. package/assets/standalone/faustwasm/index.d.ts +18 -10
  9. package/assets/standalone/faustwasm/index.js +81 -56
  10. package/assets/standalone/faustwasm/index.js.map +2 -2
  11. package/assets/standalone/index-template.html +17 -0
  12. package/assets/standalone/index-template.js +48 -0
  13. package/assets/standalone/index.html +4 -3
  14. package/assets/standalone/index.js +14 -71
  15. package/assets/standalone/manifest.json +5 -6
  16. package/assets/standalone/service-worker.js +51 -27
  17. package/dist/cjs/index.d.ts +18 -10
  18. package/dist/cjs/index.js +81 -56
  19. package/dist/cjs/index.js.map +2 -2
  20. package/dist/cjs-bundle/index.d.ts +18 -10
  21. package/dist/cjs-bundle/index.js +81 -56
  22. package/dist/cjs-bundle/index.js.map +2 -2
  23. package/dist/esm/index.d.ts +18 -10
  24. package/dist/esm/index.js +81 -56
  25. package/dist/esm/index.js.map +2 -2
  26. package/dist/esm-bundle/index.d.ts +18 -10
  27. package/dist/esm-bundle/index.js +81 -56
  28. package/dist/esm-bundle/index.js.map +2 -2
  29. package/fileutils.js +19 -21
  30. package/package.json +4 -2
  31. package/scripts/faust2wasm.js +10 -5
  32. package/src/FaustAudioWorkletNode.ts +18 -38
  33. package/src/FaustAudioWorkletProcessor.ts +28 -1
  34. package/src/FaustDspGenerator.ts +18 -9
  35. package/src/FaustFFTAudioWorkletProcessor.ts +27 -0
  36. package/src/FaustScriptProcessorNode.ts +5 -14
  37. package/src/copyWebStandaloneAssets.d.ts +2 -2
  38. package/src/copyWebStandaloneAssets.js +30 -19
  39. package/src/faust2wasmFiles.js +5 -5
  40. package/test/faustlive-wasm/faust-ui/index.css +8 -0
  41. package/test/faustlive-wasm/faust-ui/index.css.map +1 -1
  42. package/test/faustlive-wasm/faust-ui/index.js +56 -30
  43. package/test/faustlive-wasm/faust-ui/index.js.map +1 -1
  44. package/test/faustlive-wasm/faustwasm/index.d.ts +18 -10
  45. package/test/faustlive-wasm/faustwasm/index.js +81 -56
  46. package/test/faustlive-wasm/faustwasm/index.js.map +2 -2
  47. package/assets/standalone/index-poly.js +0 -234
  48. package/assets/standalone/service-worker-poly.js +0 -98
  49. package/assets/standalone/template-poly.html +0 -60
  50. package/assets/standalone/template.html +0 -50
  51. package/assets/standalone/template.js +0 -63
  52. package/assets/standalone/types.d.ts +0 -9
package/dist/cjs/index.js CHANGED
@@ -141,6 +141,10 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
141
141
  parameterDescriptors.forEach((pd) => {
142
142
  this.paramValuesCache[pd.name] = pd.defaultValue || 0;
143
143
  });
144
+ const { moduleId, instanceId } = options.processorOptions;
145
+ if (!moduleId || !instanceId)
146
+ return;
147
+ this.wamInfo = { moduleId, instanceId };
144
148
  }
145
149
  static get parameterDescriptors() {
146
150
  const params = [];
@@ -154,6 +158,23 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
154
158
  FaustBaseWebAudioDsp2.parseUI(effectMeta.ui, callback);
155
159
  return params;
156
160
  }
161
+ setupWamEventHandler() {
162
+ var _a;
163
+ if (!this.wamInfo)
164
+ return;
165
+ const { moduleId, instanceId } = this.wamInfo;
166
+ const { webAudioModules } = globalThis;
167
+ const ModuleScope = webAudioModules.getModuleScope(moduleId);
168
+ const paramMgrProcessor = (_a = ModuleScope == null ? void 0 : ModuleScope.paramMgrProcessors) == null ? void 0 : _a[instanceId];
169
+ if (!paramMgrProcessor)
170
+ return;
171
+ if (paramMgrProcessor.handleEvent)
172
+ return;
173
+ paramMgrProcessor.handleEvent = (event) => {
174
+ if (event.type === "wam-midi")
175
+ this.midiMessage(event.data.bytes);
176
+ };
177
+ }
157
178
  process(inputs, outputs, parameters) {
158
179
  for (const path in parameters) {
159
180
  const [paramValue] = parameters[path];
@@ -199,6 +220,10 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
199
220
  }
200
221
  break;
201
222
  }
223
+ case "setupWamEventHandler": {
224
+ this.setupWamEventHandler();
225
+ break;
226
+ }
202
227
  case "start": {
203
228
  this.fDSPCode.start();
204
229
  break;
@@ -408,6 +433,10 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
408
433
  this.sampleSize = sampleSize;
409
434
  this.soundfiles = factory.soundfiles;
410
435
  this.initFFT();
436
+ const { moduleId, instanceId } = options.processorOptions;
437
+ if (!moduleId || !instanceId)
438
+ return;
439
+ this.wamInfo = { moduleId, instanceId };
411
440
  }
412
441
  get fftProcessorBufferSize() {
413
442
  return this.fftSize / 2 + 1;
@@ -453,6 +482,23 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
453
482
  }
454
483
  ];
455
484
  }
485
+ setupWamEventHandler() {
486
+ var _a;
487
+ if (!this.wamInfo)
488
+ return;
489
+ const { moduleId, instanceId } = this.wamInfo;
490
+ const { webAudioModules } = globalThis;
491
+ const ModuleScope = webAudioModules.getModuleScope(moduleId);
492
+ const paramMgrProcessor = (_a = ModuleScope == null ? void 0 : ModuleScope.paramMgrProcessors) == null ? void 0 : _a[instanceId];
493
+ if (!paramMgrProcessor)
494
+ return;
495
+ if (paramMgrProcessor.handleEvent)
496
+ return;
497
+ paramMgrProcessor.handleEvent = (event) => {
498
+ if (event.type === "wam-midi")
499
+ this.midiMessage(event.data.bytes);
500
+ };
501
+ }
456
502
  processFFT() {
457
503
  let samplesForFFT = mod(this.$inputWrite - this.$inputRead, this.fftBufferSize) || this.fftBufferSize;
458
504
  while (samplesForFFT >= this.fftSize) {
@@ -590,6 +636,10 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
590
636
  (_a = this.fDSPCode) == null ? void 0 : _a.setPlotHandler(this.fPlotHandler);
591
637
  break;
592
638
  }
639
+ case "setupWamEventHandler": {
640
+ this.setupWamEventHandler();
641
+ break;
642
+ }
593
643
  case "start": {
594
644
  (_b = this.fDSPCode) == null ? void 0 : _b.start();
595
645
  break;
@@ -3649,7 +3699,7 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3649
3699
  // src/FaustAudioWorkletNode.ts
3650
3700
  var _hasAccInput, _hasGyrInput;
3651
3701
  var FaustAudioWorkletNode = class extends (globalThis.AudioWorkletNode || null) {
3652
- constructor(context, name, factory, options, nodeOptions = {}) {
3702
+ constructor(context, name, factory, options = {}) {
3653
3703
  const JSONObj = JSON.parse(factory.json);
3654
3704
  super(context, name, {
3655
3705
  numberOfInputs: JSONObj.inputs > 0 ? 1 : 0,
@@ -3658,8 +3708,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3658
3708
  outputChannelCount: [JSONObj.outputs],
3659
3709
  channelCountMode: "explicit",
3660
3710
  channelInterpretation: "speakers",
3661
- processorOptions: options,
3662
- ...nodeOptions
3711
+ processorOptions: options.processorOptions,
3712
+ ...options
3663
3713
  });
3664
3714
  __privateAdd(this, _hasAccInput, false);
3665
3715
  __privateAdd(this, _hasGyrInput, false);
@@ -3699,22 +3749,12 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3699
3749
  async listenSensors() {
3700
3750
  if (this.hasAccInput) {
3701
3751
  const isAndroid = /Android/i.test(navigator.userAgent);
3702
- let handleDeviceMotion = null;
3703
- if (isAndroid) {
3704
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3705
- if (!accelerationIncludingGravity)
3706
- return;
3707
- const { x, y, z } = accelerationIncludingGravity;
3708
- this.propagateAcc({ x, y, z }, true);
3709
- };
3710
- } else {
3711
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3712
- if (!accelerationIncludingGravity)
3713
- return;
3714
- const { x, y, z } = accelerationIncludingGravity;
3715
- this.propagateAcc({ x, y, z });
3716
- };
3717
- }
3752
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3753
+ if (!accelerationIncludingGravity)
3754
+ return;
3755
+ const { x, y, z } = accelerationIncludingGravity;
3756
+ this.propagateAcc({ x, y, z }, isAndroid);
3757
+ };
3718
3758
  if (window.DeviceMotionEvent) {
3719
3759
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3720
3760
  try {
@@ -3777,6 +3817,9 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3777
3817
  getPlotHandler() {
3778
3818
  return this.fPlotHandler;
3779
3819
  }
3820
+ setupWamEventHandler() {
3821
+ this.port.postMessage({ type: "setupWamEventHandler" });
3822
+ }
3780
3823
  getNumInputs() {
3781
3824
  return this.fJSONDsp.inputs;
3782
3825
  }
@@ -3870,33 +3913,25 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3870
3913
  _hasAccInput = new WeakMap();
3871
3914
  _hasGyrInput = new WeakMap();
3872
3915
  var FaustMonoAudioWorkletNode = class extends FaustAudioWorkletNode {
3873
- constructor(context, name, factory, sampleSize, nodeOptions = {}) {
3874
- super(context, name, factory, { name, factory, sampleSize }, nodeOptions);
3916
+ constructor(context, options) {
3917
+ super(context, options.processorOptions.name, options.processorOptions.factory, options);
3875
3918
  this.onprocessorerror = (e) => {
3876
3919
  throw e;
3877
3920
  };
3878
3921
  }
3879
3922
  };
3880
3923
  var FaustPolyAudioWorkletNode = class extends FaustAudioWorkletNode {
3881
- constructor(context, name, voiceFactory, mixerModule, voices, sampleSize, effectFactory, nodeOptions = {}) {
3924
+ constructor(context, options) {
3882
3925
  super(
3883
3926
  context,
3884
- name,
3885
- voiceFactory,
3886
- {
3887
- name,
3888
- voiceFactory,
3889
- mixerModule,
3890
- voices,
3891
- sampleSize,
3892
- effectFactory
3893
- },
3894
- nodeOptions
3927
+ options.processorOptions.name,
3928
+ options.processorOptions.voiceFactory,
3929
+ options
3895
3930
  );
3896
3931
  this.onprocessorerror = (e) => {
3897
3932
  throw e;
3898
3933
  };
3899
- this.fJSONEffect = effectFactory ? JSON.parse(effectFactory.json) : null;
3934
+ this.fJSONEffect = options.processorOptions.effectFactory ? JSON.parse(options.processorOptions.effectFactory.json) : null;
3900
3935
  if (this.fJSONEffect) {
3901
3936
  FaustBaseWebAudioDsp.parseUI(this.fJSONEffect.ui, this.fUICallback);
3902
3937
  }
@@ -3968,22 +4003,12 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3968
4003
  async listenSensors() {
3969
4004
  if (this.hasAccInput) {
3970
4005
  const isAndroid = /Android/i.test(navigator.userAgent);
3971
- let handleDeviceMotion = null;
3972
- if (isAndroid) {
3973
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3974
- if (!accelerationIncludingGravity)
3975
- return;
3976
- const { x, y, z } = accelerationIncludingGravity;
3977
- this.propagateAcc({ x, y, z }, true);
3978
- };
3979
- } else {
3980
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3981
- if (!accelerationIncludingGravity)
3982
- return;
3983
- const { x, y, z } = accelerationIncludingGravity;
3984
- this.propagateAcc({ x, y, z });
3985
- };
3986
- }
4006
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
4007
+ if (!accelerationIncludingGravity)
4008
+ return;
4009
+ const { x, y, z } = accelerationIncludingGravity;
4010
+ this.propagateAcc({ x, y, z }, isAndroid);
4011
+ };
3987
4012
  if (window.DeviceMotionEvent) {
3988
4013
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3989
4014
  try {
@@ -4146,7 +4171,7 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
4146
4171
  const map = SoundfileReader_default.findSoundfilesFromMeta(meta);
4147
4172
  return Object.keys(map);
4148
4173
  }
4149
- async createNode(context, name = this.name, factory = this.factory, sp = false, bufferSize = 1024, processorName = (factory == null ? void 0 : factory.shaKey) || name) {
4174
+ async createNode(context, name = this.name, factory = this.factory, sp = false, bufferSize = 1024, processorName = (factory == null ? void 0 : factory.shaKey) || name, processorOptions = {}) {
4150
4175
  var _a, _b;
4151
4176
  if (!factory)
4152
4177
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
@@ -4204,11 +4229,11 @@ const dependencies = {
4204
4229
  throw e;
4205
4230
  }
4206
4231
  }
4207
- const node = new FaustMonoAudioWorkletNode(context, processorName, factory, sampleSize);
4232
+ const node = new FaustMonoAudioWorkletNode(context, { processorOptions: { name: processorName, factory, sampleSize, ...processorOptions } });
4208
4233
  return node;
4209
4234
  }
4210
4235
  }
4211
- async createFFTNode(context, fftUtils, name = this.name, factory = this.factory, fftOptions = {}, processorName = (factory == null ? void 0 : factory.shaKey) ? `${factory.shaKey}_fft` : name) {
4236
+ async createFFTNode(context, fftUtils, name = this.name, factory = this.factory, fftOptions = {}, processorName = (factory == null ? void 0 : factory.shaKey) ? `${factory.shaKey}_fft` : name, processorOptions = {}) {
4212
4237
  var _a, _b;
4213
4238
  if (!factory)
4214
4239
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
@@ -4260,7 +4285,7 @@ const dependencies = {
4260
4285
  throw e;
4261
4286
  }
4262
4287
  }
4263
- const node = new FaustMonoAudioWorkletNode(context, processorName, factory, sampleSize, { channelCount: Math.max(1, Math.ceil(meta.inputs / 3)), outputChannelCount: [Math.ceil(meta.outputs / 2)] });
4288
+ const node = new FaustMonoAudioWorkletNode(context, { channelCount: Math.max(1, Math.ceil(meta.inputs / 3)), outputChannelCount: [Math.ceil(meta.outputs / 2)], processorOptions: { name: processorName, factory, sampleSize, ...processorOptions } });
4264
4289
  if (fftOptions.fftSize) {
4265
4290
  const param = node.parameters.get("fftSize");
4266
4291
  if (param)
@@ -4422,7 +4447,7 @@ process = adaptorIns(dsp_code.process) : dsp_code.effect : adaptorOuts;
4422
4447
  const effectMap = SoundfileReader_default.findSoundfilesFromMeta(effectMeta);
4423
4448
  return Object.keys({ ...effectMap, ...map });
4424
4449
  }
4425
- 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`) {
4450
+ 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`, processorOptions = {}) {
4426
4451
  var _a, _b;
4427
4452
  if (!voiceFactory)
4428
4453
  throw new Error("Code is not compiled, please define the factory or call `await this.compile()` first.");
@@ -4487,7 +4512,7 @@ const dependencies = {
4487
4512
  throw e;
4488
4513
  }
4489
4514
  }
4490
- const node = new FaustPolyAudioWorkletNode(context, processorName, voiceFactory, mixerModule, voices, sampleSize, effectFactory || void 0);
4515
+ const node = new FaustPolyAudioWorkletNode(context, { processorOptions: { name: processorName, voiceFactory, mixerModule, voices, sampleSize, effectFactory: effectFactory || void 0, ...processorOptions } });
4491
4516
  return node;
4492
4517
  }
4493
4518
  }