@grame/faustwasm 0.6.4 → 0.7.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grame/faustwasm",
3
- "version": "0.6.4",
3
+ "version": "0.7.0",
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
  }
@@ -36,26 +36,32 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
36
36
 
37
37
  // Public API
38
38
 
39
+ // Accelerometer and gyroscope handlers
40
+ private handleDeviceMotion = ({ accelerationIncludingGravity }: DeviceMotionEvent) => {
41
+ const isAndroid: boolean = /Android/i.test(navigator.userAgent);
42
+ if (!accelerationIncludingGravity) return;
43
+ const { x, y, z } = accelerationIncludingGravity;
44
+ this.propagateAcc({ x, y, z }, isAndroid);
45
+ };
46
+
47
+ private handleDeviceOrientation = ({ alpha, beta, gamma }: DeviceOrientationEvent) => {
48
+ this.propagateGyr({ alpha, beta, gamma });
49
+ };
50
+
39
51
  /** Setup accelerometer and gyroscope handlers */
40
- async listenSensors() {
52
+ async startSensors() {
41
53
  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
54
  if (window.DeviceMotionEvent) {
49
55
  if (typeof (window.DeviceMotionEvent as any).requestPermission === "function") { // for iOS 13+
50
56
  try {
51
57
  const response = await (window.DeviceMotionEvent as any).requestPermission();
52
58
  if (response !== "granted") throw new Error("Unable to access the accelerometer.");
53
- window.addEventListener("devicemotion", handleDeviceMotion, true);
59
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
54
60
  } catch (error) {
55
61
  console.error(error);
56
62
  }
57
63
  } else {
58
- window.addEventListener("devicemotion", handleDeviceMotion, true);
64
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
59
65
  }
60
66
  } else {
61
67
  // Browser doesn't support DeviceMotionEvent
@@ -63,20 +69,17 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
63
69
  }
64
70
  }
65
71
  if (this.hasGyrInput) {
66
- const handleDeviceOrientation = ({ alpha, beta, gamma }: DeviceOrientationEvent) => {
67
- this.propagateGyr({ alpha, beta, gamma });
68
- };
69
72
  if (window.DeviceMotionEvent) {
70
73
  if (typeof (window.DeviceOrientationEvent as any).requestPermission === "function") { // for iOS 13+
71
74
  try {
72
75
  const response = await (window.DeviceOrientationEvent as any).requestPermission();
73
76
  if (response !== "granted") throw new Error("Unable to access the gyroscope.");
74
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
77
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
75
78
  } catch (error) {
76
79
  console.error(error);
77
80
  }
78
81
  } else {
79
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
82
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
80
83
  }
81
84
  } else {
82
85
  // Browser doesn't support DeviceMotionEvent
@@ -85,6 +88,15 @@ export class FaustScriptProcessorNode<Poly extends boolean = false> extends (glo
85
88
  }
86
89
  }
87
90
 
91
+ stopSensors() {
92
+ if (this.hasAccInput) {
93
+ window.removeEventListener("devicemotion", this.handleDeviceMotion, true);
94
+ }
95
+ if (this.hasGyrInput) {
96
+ window.removeEventListener("deviceorientation", this.handleDeviceOrientation, true);
97
+ }
98
+ }
99
+
88
100
  compute(input: Float32Array[], output: Float32Array[]) { return this.fDSPCode.compute(input, output); }
89
101
 
90
102
  setOutputParamHandler(handler: OutputParamHandler) { this.fDSPCode.setOutputParamHandler(handler); }
@@ -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);
@@ -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;
@@ -1353,8 +1356,11 @@ export declare class FaustScriptProcessorNode<Poly extends boolean = false> exte
1353
1356
  protected fInputs: Float32Array[];
1354
1357
  protected fOutputs: Float32Array[];
1355
1358
  init(instance: Poly extends true ? FaustPolyWebAudioDsp : FaustMonoWebAudioDsp): void;
1359
+ private handleDeviceMotion;
1360
+ private handleDeviceOrientation;
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,6 +3981,21 @@ 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
+ // Public API
3987
+ // Accelerometer and gyroscope handlers
3988
+ this.handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3989
+ const isAndroid = /Android/i.test(navigator.userAgent);
3990
+ if (!accelerationIncludingGravity)
3991
+ return;
3992
+ const { x, y, z } = accelerationIncludingGravity;
3993
+ this.propagateAcc({ x, y, z }, isAndroid);
3994
+ };
3995
+ this.handleDeviceOrientation = ({ alpha, beta, gamma }) => {
3996
+ this.propagateGyr({ alpha, beta, gamma });
3997
+ };
3998
+ }
3969
3999
  init(instance) {
3970
4000
  this.fDSPCode = instance;
3971
4001
  this.fInputs = new Array(this.fDSPCode.getNumInputs());
@@ -3981,56 +4011,53 @@ var FaustScriptProcessorNode = class extends (globalThis.ScriptProcessorNode ||
3981
4011
  };
3982
4012
  this.start();
3983
4013
  }
3984
- // Public API
3985
4014
  /** Setup accelerometer and gyroscope handlers */
3986
- async listenSensors() {
4015
+ async startSensors() {
3987
4016
  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
4017
  if (window.DeviceMotionEvent) {
3996
4018
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3997
4019
  try {
3998
4020
  const response = await window.DeviceMotionEvent.requestPermission();
3999
4021
  if (response !== "granted")
4000
4022
  throw new Error("Unable to access the accelerometer.");
4001
- window.addEventListener("devicemotion", handleDeviceMotion, true);
4023
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
4002
4024
  } catch (error) {
4003
4025
  console.error(error);
4004
4026
  }
4005
4027
  } else {
4006
- window.addEventListener("devicemotion", handleDeviceMotion, true);
4028
+ window.addEventListener("devicemotion", this.handleDeviceMotion, true);
4007
4029
  }
4008
4030
  } else {
4009
4031
  console.log("Cannot set the accelerometer handler.");
4010
4032
  }
4011
4033
  }
4012
4034
  if (this.hasGyrInput) {
4013
- const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
4014
- this.propagateGyr({ alpha, beta, gamma });
4015
- };
4016
4035
  if (window.DeviceMotionEvent) {
4017
4036
  if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
4018
4037
  try {
4019
4038
  const response = await window.DeviceOrientationEvent.requestPermission();
4020
4039
  if (response !== "granted")
4021
4040
  throw new Error("Unable to access the gyroscope.");
4022
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
4041
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
4023
4042
  } catch (error) {
4024
4043
  console.error(error);
4025
4044
  }
4026
4045
  } else {
4027
- window.addEventListener("deviceorientation", handleDeviceOrientation, true);
4046
+ window.addEventListener("deviceorientation", this.handleDeviceOrientation, true);
4028
4047
  }
4029
4048
  } else {
4030
4049
  console.log("Cannot set the gyroscope handler.");
4031
4050
  }
4032
4051
  }
4033
4052
  }
4053
+ stopSensors() {
4054
+ if (this.hasAccInput) {
4055
+ window.removeEventListener("devicemotion", this.handleDeviceMotion, true);
4056
+ }
4057
+ if (this.hasGyrInput) {
4058
+ window.removeEventListener("deviceorientation", this.handleDeviceOrientation, true);
4059
+ }
4060
+ }
4034
4061
  compute(input, output) {
4035
4062
  return this.fDSPCode.compute(input, output);
4036
4063
  }