@grame/faustwasm 0.6.4 → 0.7.3

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.
@@ -19,7 +19,7 @@
19
19
  * @param {boolean} [sp] - Whether to create a ScriptProcessorNode instead of an AudioWorkletNode.
20
20
  * @returns {Promise<{ faustNode: FaustNode | null; dspMeta: FaustDspMeta }>} - An object containing the Faust audio node and the DSP metadata.
21
21
  */
22
- const createFaustNode = async (audioContext, dspName = "template", voices = 0, sp = false) => {
22
+ const createFaustNode = async (audioContext, dspName = "template", voices = 0, sp = false, bufferSize = 512) => {
23
23
  // Set to true if the DSP has an effect
24
24
  const FAUST_DSP_HAS_EFFECT = false;
25
25
 
@@ -60,7 +60,8 @@ const createFaustNode = async (audioContext, dspName = "template", voices = 0, s
60
60
  { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta), soundfiles: {} },
61
61
  faustDsp.mixerModule,
62
62
  faustDsp.effectModule ? { module: faustDsp.effectModule, json: JSON.stringify(faustDsp.effectMeta), soundfiles: {} } : undefined,
63
- sp
63
+ sp,
64
+ bufferSize
64
65
  );
65
66
  } else {
66
67
  // Create a standard Faust audio node
@@ -69,7 +70,8 @@ const createFaustNode = async (audioContext, dspName = "template", voices = 0, s
69
70
  audioContext,
70
71
  dspName,
71
72
  { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta), soundfiles: {} },
72
- sp
73
+ sp,
74
+ bufferSize
73
75
  );
74
76
  }
75
77
 
@@ -621,11 +621,11 @@ export interface IFaustBaseWebAudioDsp {
621
621
  */
622
622
  getJSON(): string;
623
623
  /**
624
- * Start the DSP.
624
+ * Start the DSP audio processing.
625
625
  */
626
626
  start(): void;
627
627
  /**
628
- * Stop the DSP.
628
+ * Stop the DSP audio processing.
629
629
  */
630
630
  stop(): void;
631
631
  /**
@@ -1289,8 +1289,11 @@ export declare class FaustAudioWorkletNode<Poly extends boolean = false> extends
1289
1289
  protected fUICallback: UIHandler;
1290
1290
  protected fDescriptor: FaustUIInputItem[];
1291
1291
  constructor(context: BaseAudioContext, name: string, factory: LooseFaustDspFactory, options?: Partial<FaustAudioWorkletNodeOptions<Poly>>);
1292
+ private handleDeviceMotion;
1293
+ private handleDeviceOrientation;
1292
1294
  /** Setup accelerometer and gyroscope handlers */
1293
- listenSensors(): Promise<void>;
1295
+ startSensors(): Promise<void>;
1296
+ stopSensors(): void;
1294
1297
  setOutputParamHandler(handler: OutputParamHandler | null): void;
1295
1298
  getOutputParamHandler(): OutputParamHandler | null;
1296
1299
  setComputeHandler(handler: ComputeHandler | null): void;
@@ -1352,9 +1355,12 @@ export declare class FaustScriptProcessorNode<Poly extends boolean = false> exte
1352
1355
  protected fDSPCode: Poly extends true ? FaustPolyWebAudioDsp : FaustMonoWebAudioDsp;
1353
1356
  protected fInputs: Float32Array[];
1354
1357
  protected fOutputs: Float32Array[];
1358
+ protected handleDeviceMotion: any;
1359
+ protected handleDeviceOrientation: any;
1355
1360
  init(instance: Poly extends true ? FaustPolyWebAudioDsp : FaustMonoWebAudioDsp): void;
1356
1361
  /** Setup accelerometer and gyroscope handlers */
1357
- listenSensors(): Promise<void>;
1362
+ startSensors(): Promise<void>;
1363
+ stopSensors(): void;
1358
1364
  compute(input: Float32Array[], output: Float32Array[]): boolean;
1359
1365
  setOutputParamHandler(handler: OutputParamHandler): void;
1360
1366
  getOutputParamHandler(): OutputParamHandler | null;
@@ -3696,6 +3696,18 @@ var FaustAudioWorkletNode = class extends (globalThis.AudioWorkletNode || null)
3696
3696
  });
3697
3697
  __privateAdd(this, _hasAccInput, false);
3698
3698
  __privateAdd(this, _hasGyrInput, false);
3699
+ // Public API
3700
+ // Accelerometer and gyroscope handlers
3701
+ this.handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3702
+ const isAndroid = /Android/i.test(navigator.userAgent);
3703
+ if (!accelerationIncludingGravity)
3704
+ return;
3705
+ const { x, y, z } = accelerationIncludingGravity;
3706
+ this.propagateAcc({ x, y, z }, isAndroid);
3707
+ };
3708
+ this.handleDeviceOrientation = ({ alpha, beta, gamma }) => {
3709
+ this.propagateGyr({ alpha, beta, gamma });
3710
+ };
3699
3711
  this.fJSONDsp = JSONObj;
3700
3712
  this.fJSON = factory.json;
3701
3713
  this.fOutputHandler = null;
@@ -3727,56 +3739,59 @@ var FaustAudioWorkletNode = class extends (globalThis.AudioWorkletNode || null)
3727
3739
  }
3728
3740
  };
3729
3741
  }
3730
- // Public API
3731
3742
  /** Setup accelerometer and gyroscope handlers */
3732
- async listenSensors() {
3743
+ async startSensors() {
3733
3744
  if (this.hasAccInput) {
3734
- const isAndroid = /Android/i.test(navigator.userAgent);
3735
- const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3736
- if (!accelerationIncludingGravity)
3737
- return;
3738
- const { x, y, z } = accelerationIncludingGravity;
3739
- this.propagateAcc({ x, y, z }, isAndroid);
3740
- };
3741
3745
  if (window.DeviceMotionEvent) {
3742
3746
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3743
3747
  try {
3744
3748
  const response = await window.DeviceMotionEvent.requestPermission();
3745
- if (response !== "granted")
3749
+ if (response === "granted") {
3750
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
3751
+ } else if (response === "denied") {
3752
+ alert("You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.");
3746
3753
  throw new Error("Unable to access the accelerometer.");
3747
- window.addEventListener("devicemotion", handleDeviceMotion, true);
3754
+ }
3748
3755
  } catch (error) {
3749
3756
  console.error(error);
3750
3757
  }
3751
3758
  } else {
3752
- window.addEventListener("devicemotion", handleDeviceMotion, true);
3759
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
3753
3760
  }
3754
3761
  } else {
3755
3762
  console.log("Cannot set the accelerometer handler.");
3756
3763
  }
3757
3764
  }
3758
3765
  if (this.hasGyrInput) {
3759
- const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
3760
- this.propagateGyr({ alpha, beta, gamma });
3761
- };
3762
3766
  if (window.DeviceMotionEvent) {
3763
3767
  if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
3764
3768
  try {
3765
3769
  const response = await window.DeviceOrientationEvent.requestPermission();
3766
- if (response !== "granted")
3770
+ if (response === "granted") {
3771
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
3772
+ } else if (response === "denied") {
3773
+ alert("You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.");
3767
3774
  throw new Error("Unable to access the gyroscope.");
3768
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
3775
+ }
3769
3776
  } catch (error) {
3770
3777
  console.error(error);
3771
3778
  }
3772
3779
  } else {
3773
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
3780
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
3774
3781
  }
3775
3782
  } else {
3776
3783
  console.log("Cannot set the gyroscope handler.");
3777
3784
  }
3778
3785
  }
3779
3786
  }
3787
+ stopSensors() {
3788
+ if (this.hasAccInput) {
3789
+ window.removeEventListener("devicemotion", this.handleDeviceMotion, true);
3790
+ }
3791
+ if (this.hasGyrInput) {
3792
+ window.removeEventListener("deviceorientation", this.handleDeviceOrientation, true);
3793
+ }
3794
+ }
3780
3795
  setOutputParamHandler(handler) {
3781
3796
  this.fOutputHandler = handler;
3782
3797
  }
@@ -3966,10 +3981,25 @@ var FaustPolyAudioWorkletNode = class extends FaustAudioWorkletNode {
3966
3981
 
3967
3982
  // src/FaustScriptProcessorNode.ts
3968
3983
  var FaustScriptProcessorNode = class extends (globalThis.ScriptProcessorNode || null) {
3984
+ constructor() {
3985
+ super(...arguments);
3986
+ this.handleDeviceMotion = void 0;
3987
+ this.handleDeviceOrientation = void 0;
3988
+ }
3969
3989
  init(instance) {
3970
3990
  this.fDSPCode = instance;
3971
3991
  this.fInputs = new Array(this.fDSPCode.getNumInputs());
3972
3992
  this.fOutputs = new Array(this.fDSPCode.getNumOutputs());
3993
+ this.handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3994
+ const isAndroid = /Android/i.test(navigator.userAgent);
3995
+ if (!accelerationIncludingGravity)
3996
+ return;
3997
+ const { x, y, z } = accelerationIncludingGravity;
3998
+ this.propagateAcc({ x, y, z }, isAndroid);
3999
+ };
4000
+ this.handleDeviceOrientation = ({ alpha, beta, gamma }) => {
4001
+ this.propagateGyr({ alpha, beta, gamma });
4002
+ };
3973
4003
  this.onaudioprocess = (e) => {
3974
4004
  for (let chan = 0; chan < this.fDSPCode.getNumInputs(); chan++) {
3975
4005
  this.fInputs[chan] = e.inputBuffer.getChannelData(chan);
@@ -3983,54 +4013,58 @@ var FaustScriptProcessorNode = class extends (globalThis.ScriptProcessorNode ||
3983
4013
  }
3984
4014
  // Public API
3985
4015
  /** Setup accelerometer and gyroscope handlers */
3986
- async listenSensors() {
4016
+ async startSensors() {
3987
4017
  if (this.hasAccInput) {
3988
- const isAndroid = /Android/i.test(navigator.userAgent);
3989
- const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3990
- if (!accelerationIncludingGravity)
3991
- return;
3992
- const { x, y, z } = accelerationIncludingGravity;
3993
- this.propagateAcc({ x, y, z }, isAndroid);
3994
- };
3995
4018
  if (window.DeviceMotionEvent) {
3996
4019
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3997
4020
  try {
3998
4021
  const response = await window.DeviceMotionEvent.requestPermission();
3999
- if (response !== "granted")
4022
+ if (response === "granted") {
4023
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
4024
+ } else if (response === "denied") {
4025
+ alert("You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.");
4000
4026
  throw new Error("Unable to access the accelerometer.");
4001
- window.addEventListener("devicemotion", handleDeviceMotion, true);
4027
+ }
4002
4028
  } catch (error) {
4003
4029
  console.error(error);
4004
4030
  }
4005
4031
  } else {
4006
- window.addEventListener("devicemotion", handleDeviceMotion, true);
4032
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
4007
4033
  }
4008
4034
  } else {
4009
4035
  console.log("Cannot set the accelerometer handler.");
4010
4036
  }
4011
4037
  }
4012
4038
  if (this.hasGyrInput) {
4013
- const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
4014
- this.propagateGyr({ alpha, beta, gamma });
4015
- };
4016
4039
  if (window.DeviceMotionEvent) {
4017
4040
  if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
4018
4041
  try {
4019
4042
  const response = await window.DeviceOrientationEvent.requestPermission();
4020
- if (response !== "granted")
4043
+ if (response === "granted") {
4044
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
4045
+ } else if (response === "denied") {
4046
+ alert("You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.");
4021
4047
  throw new Error("Unable to access the gyroscope.");
4022
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
4048
+ }
4023
4049
  } catch (error) {
4024
4050
  console.error(error);
4025
4051
  }
4026
4052
  } else {
4027
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
4053
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
4028
4054
  }
4029
4055
  } else {
4030
4056
  console.log("Cannot set the gyroscope handler.");
4031
4057
  }
4032
4058
  }
4033
4059
  }
4060
+ stopSensors() {
4061
+ if (this.hasAccInput) {
4062
+ window.removeEventListener("devicemotion", this.handleDeviceMotion, true);
4063
+ }
4064
+ if (this.hasGyrInput) {
4065
+ window.removeEventListener("deviceorientation", this.handleDeviceOrientation, true);
4066
+ }
4067
+ }
4034
4068
  compute(input, output) {
4035
4069
  return this.fDSPCode.compute(input, output);
4036
4070
  }