@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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grame/faustwasm",
3
- "version": "0.6.4",
3
+ "version": "0.7.3",
4
4
  "description": "WebAssembly version of Faust Compiler",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -72,26 +72,36 @@ export class FaustAudioWorkletNode<Poly extends boolean = false> extends (global
72
72
 
73
73
  // Public API
74
74
 
75
+ // Accelerometer and gyroscope handlers
76
+ private handleDeviceMotion = ({ accelerationIncludingGravity }: DeviceMotionEvent) => {
77
+ const isAndroid: boolean = /Android/i.test(navigator.userAgent);
78
+ if (!accelerationIncludingGravity) return;
79
+ const { x, y, z } = accelerationIncludingGravity;
80
+ this.propagateAcc({ x, y, z }, isAndroid);
81
+ };
82
+
83
+ private handleDeviceOrientation = ({ alpha, beta, gamma }: DeviceOrientationEvent) => {
84
+ this.propagateGyr({ alpha, beta, gamma });
85
+ };
86
+
75
87
  /** Setup accelerometer and gyroscope handlers */
76
- async listenSensors() {
88
+ async startSensors() {
77
89
  if (this.hasAccInput) {
78
- const isAndroid: boolean = /Android/i.test(navigator.userAgent);
79
- const handleDeviceMotion = ({ accelerationIncludingGravity }: DeviceMotionEvent) => {
80
- if (!accelerationIncludingGravity) return;
81
- const { x, y, z } = accelerationIncludingGravity;
82
- this.propagateAcc({ x, y, z }, isAndroid);
83
- };
84
90
  if (window.DeviceMotionEvent) {
85
91
  if (typeof (window.DeviceMotionEvent as any).requestPermission === "function") { // for iOS 13+
86
92
  try {
87
93
  const response = await (window.DeviceMotionEvent as any).requestPermission();
88
- if (response !== "granted") throw new Error("Unable to access the accelerometer.");
89
- window.addEventListener("devicemotion", handleDeviceMotion, true);
94
+ if (response === "granted") {
95
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
96
+ } else if (response === "denied") {
97
+ alert('You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.');
98
+ throw new Error("Unable to access the accelerometer.");
99
+ }
90
100
  } catch (error) {
91
101
  console.error(error);
92
102
  }
93
103
  } else {
94
- window.addEventListener("devicemotion", handleDeviceMotion, true);
104
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
95
105
  }
96
106
  } else {
97
107
  // Browser doesn't support DeviceMotionEvent
@@ -99,20 +109,21 @@ export class FaustAudioWorkletNode<Poly extends boolean = false> extends (global
99
109
  }
100
110
  }
101
111
  if (this.hasGyrInput) {
102
- const handleDeviceOrientation = ({ alpha, beta, gamma }: DeviceOrientationEvent) => {
103
- this.propagateGyr({ alpha, beta, gamma });
104
- };
105
112
  if (window.DeviceMotionEvent) {
106
113
  if (typeof (window.DeviceOrientationEvent as any).requestPermission === "function") { // for iOS 13+
107
114
  try {
108
115
  const response = await (window.DeviceOrientationEvent as any).requestPermission();
109
- if (response !== "granted") throw new Error("Unable to access the gyroscope.");
110
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
116
+ if (response === "granted") {
117
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
118
+ } else if (response === "denied") {
119
+ alert('You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.');
120
+ throw new Error("Unable to access the gyroscope.");
121
+ }
111
122
  } catch (error) {
112
123
  console.error(error);
113
124
  }
114
125
  } else {
115
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
126
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
116
127
  }
117
128
  } else {
118
129
  // Browser doesn't support DeviceMotionEvent
@@ -121,6 +132,14 @@ export class FaustAudioWorkletNode<Poly extends boolean = false> extends (global
121
132
  }
122
133
  }
123
134
 
135
+ stopSensors() {
136
+ if (this.hasAccInput) {
137
+ window.removeEventListener("devicemotion", this.handleDeviceMotion, true);
138
+ }
139
+ if (this.hasGyrInput) {
140
+ window.removeEventListener("deviceorientation", this.handleDeviceOrientation, true);
141
+ }
142
+ }
124
143
  setOutputParamHandler(handler: OutputParamHandler | null) {
125
144
  this.fOutputHandler = handler;
126
145
  }
@@ -9,6 +9,8 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
9
9
  // Needed for ScriptProcessorNode
10
10
  protected fInputs!: Float32Array[];
11
11
  protected fOutputs!: Float32Array[];
12
+ protected handleDeviceMotion = undefined as any;
13
+ protected handleDeviceOrientation = undefined as any;
12
14
 
13
15
  init(instance: Poly extends true ? FaustPolyWebAudioDsp : FaustMonoWebAudioDsp) {
14
16
  this.fDSPCode = instance;
@@ -16,6 +18,18 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
16
18
  this.fInputs = new Array(this.fDSPCode.getNumInputs());
17
19
  this.fOutputs = new Array(this.fDSPCode.getNumOutputs());
18
20
 
21
+ // Accelerometer and gyroscope handlers
22
+ this.handleDeviceMotion = ({ accelerationIncludingGravity }: DeviceMotionEvent) => {
23
+ const isAndroid: boolean = /Android/i.test(navigator.userAgent);
24
+ if (!accelerationIncludingGravity) return;
25
+ const { x, y, z } = accelerationIncludingGravity;
26
+ this.propagateAcc({ x, y, z }, isAndroid);
27
+ };
28
+
29
+ this.handleDeviceOrientation = ({ alpha, beta, gamma }: DeviceOrientationEvent) => {
30
+ this.propagateGyr({ alpha, beta, gamma });
31
+ };
32
+
19
33
  this.onaudioprocess = (e) => {
20
34
 
21
35
  // Read inputs
@@ -37,25 +51,23 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
37
51
  // Public API
38
52
 
39
53
  /** Setup accelerometer and gyroscope handlers */
40
- async listenSensors() {
54
+ async startSensors() {
41
55
  if (this.hasAccInput) {
42
- const isAndroid: boolean = /Android/i.test(navigator.userAgent);
43
- const handleDeviceMotion = ({ accelerationIncludingGravity }: DeviceMotionEvent) => {
44
- if (!accelerationIncludingGravity) return;
45
- const { x, y, z } = accelerationIncludingGravity;
46
- this.propagateAcc({ x, y, z }, isAndroid);
47
- };
48
56
  if (window.DeviceMotionEvent) {
49
57
  if (typeof (window.DeviceMotionEvent as any).requestPermission === "function") { // for iOS 13+
50
58
  try {
51
59
  const response = await (window.DeviceMotionEvent as any).requestPermission();
52
- if (response !== "granted") throw new Error("Unable to access the accelerometer.");
53
- window.addEventListener("devicemotion", handleDeviceMotion, true);
60
+ if (response === "granted") {
61
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
62
+ } else if (response === "denied") {
63
+ alert('You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.');
64
+ throw new Error("Unable to access the accelerometer.");
65
+ }
54
66
  } catch (error) {
55
67
  console.error(error);
56
68
  }
57
69
  } else {
58
- window.addEventListener("devicemotion", handleDeviceMotion, true);
70
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
59
71
  }
60
72
  } else {
61
73
  // Browser doesn't support DeviceMotionEvent
@@ -63,20 +75,21 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
63
75
  }
64
76
  }
65
77
  if (this.hasGyrInput) {
66
- const handleDeviceOrientation = ({ alpha, beta, gamma }: DeviceOrientationEvent) => {
67
- this.propagateGyr({ alpha, beta, gamma });
68
- };
69
78
  if (window.DeviceMotionEvent) {
70
79
  if (typeof (window.DeviceOrientationEvent as any).requestPermission === "function") { // for iOS 13+
71
80
  try {
72
81
  const response = await (window.DeviceOrientationEvent as any).requestPermission();
73
- if (response !== "granted") throw new Error("Unable to access the gyroscope.");
74
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
82
+ if (response === "granted") {
83
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
84
+ } else if (response === "denied") {
85
+ alert('You have denied access to motion and orientation data. To enable it, go to Settings > Safari > Motion & Orientation Access.');
86
+ throw new Error("Unable to access the gyroscope.");
87
+ }
75
88
  } catch (error) {
76
89
  console.error(error);
77
90
  }
78
91
  } else {
79
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
92
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
80
93
  }
81
94
  } else {
82
95
  // Browser doesn't support DeviceMotionEvent
@@ -85,6 +98,15 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
85
98
  }
86
99
  }
87
100
 
101
+ stopSensors() {
102
+ if (this.hasAccInput) {
103
+ window.removeEventListener("devicemotion", this.handleDeviceMotion, true);
104
+ }
105
+ if (this.hasGyrInput) {
106
+ window.removeEventListener("deviceorientation", this.handleDeviceOrientation, true);
107
+ }
108
+ }
109
+
88
110
  compute(input: Float32Array[], output: Float32Array[]) { return this.fDSPCode.compute(input, output); }
89
111
 
90
112
  setOutputParamHandler(handler: OutputParamHandler) { this.fDSPCode.setOutputParamHandler(handler); }
@@ -497,12 +497,12 @@ export interface IFaustBaseWebAudioDsp {
497
497
  getJSON(): string;
498
498
 
499
499
  /**
500
- * Start the DSP.
500
+ * Start the DSP audio processing.
501
501
  */
502
502
  start(): void;
503
503
 
504
504
  /**
505
- * Stop the DSP.
505
+ * Stop the DSP audio processing.
506
506
  */
507
507
  stop(): void;
508
508
 
@@ -764,7 +764,6 @@ export class FaustBaseWebAudioDsp implements IFaustBaseWebAudioDsp {
764
764
  }
765
765
  }
766
766
 
767
-
768
767
  static extractUrlsFromMeta(dspMeta: FaustDspMeta): string[] {
769
768
  // Find the entry with the "soundfiles" key
770
769
  const soundfilesEntry = dspMeta.meta.find(entry => entry.soundfiles !== undefined);
@@ -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
  }