@grame/faustwasm 0.2.3 → 0.3.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.
@@ -35,6 +35,24 @@
35
35
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
36
36
  mod
37
37
  ));
38
+ var __accessCheck = (obj, member, msg) => {
39
+ if (!member.has(obj))
40
+ throw TypeError("Cannot " + msg);
41
+ };
42
+ var __privateGet = (obj, member, getter) => {
43
+ __accessCheck(obj, member, "read from private field");
44
+ return getter ? getter.call(obj) : member.get(obj);
45
+ };
46
+ var __privateAdd = (obj, member, value) => {
47
+ if (member.has(obj))
48
+ throw TypeError("Cannot add the same private member more than once");
49
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
50
+ };
51
+ var __privateSet = (obj, member, value, setter) => {
52
+ __accessCheck(obj, member, "write to private field");
53
+ setter ? setter.call(obj, value) : member.set(obj, value);
54
+ return value;
55
+ };
38
56
  var __toBinary = /* @__PURE__ */ (() => {
39
57
  var table = new Uint8Array(128);
40
58
  for (var i = 0; i < 64; i++)
@@ -6166,18 +6184,30 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
6166
6184
  handleMessageAux(e) {
6167
6185
  const msg = e.data;
6168
6186
  switch (msg.type) {
6169
- case "midi":
6187
+ case "acc": {
6188
+ this.propagateAcc(msg.data);
6189
+ break;
6190
+ }
6191
+ case "gyr": {
6192
+ this.propagateGyr(msg.data);
6193
+ break;
6194
+ }
6195
+ case "midi": {
6170
6196
  this.midiMessage(msg.data);
6171
6197
  break;
6172
- case "ctrlChange":
6198
+ }
6199
+ case "ctrlChange": {
6173
6200
  this.ctrlChange(msg.data[0], msg.data[1], msg.data[2]);
6174
6201
  break;
6175
- case "pitchWheel":
6202
+ }
6203
+ case "pitchWheel": {
6176
6204
  this.pitchWheel(msg.data[0], msg.data[1]);
6177
6205
  break;
6178
- case "param":
6206
+ }
6207
+ case "param": {
6179
6208
  this.setParamValue(msg.data.path, msg.data.value);
6180
6209
  break;
6210
+ }
6181
6211
  case "setPlotHandler": {
6182
6212
  if (msg.data) {
6183
6213
  this.fDSPCode.setPlotHandler((output, index, events) => this.port.postMessage({ type: "plot", value: output, index, events }));
@@ -6216,6 +6246,12 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
6216
6246
  pitchWheel(channel, wheel) {
6217
6247
  this.fDSPCode.pitchWheel(channel, wheel);
6218
6248
  }
6249
+ propagateAcc(accelerationIncludingGravity) {
6250
+ this.fDSPCode.propagateAcc(accelerationIncludingGravity);
6251
+ }
6252
+ propagateGyr(event) {
6253
+ this.fDSPCode.propagateGyr(event);
6254
+ }
6219
6255
  }
6220
6256
  class FaustMonoAudioWorkletProcessor extends FaustAudioWorkletProcessor {
6221
6257
  constructor(options) {
@@ -7531,6 +7567,270 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
7531
7567
  };
7532
7568
  var FaustWasmInstantiator_default = FaustWasmInstantiator;
7533
7569
 
7570
+ // src/FaustSensors.ts
7571
+ var FaustSensors = class _FaustSensors {
7572
+ /**
7573
+ * Function to convert a number to an axis type
7574
+ *
7575
+ * @param value number
7576
+ * @returns axis type
7577
+ */
7578
+ static convertToAxis(value) {
7579
+ switch (value) {
7580
+ case 0:
7581
+ return 0 /* x */;
7582
+ case 1:
7583
+ return 1 /* y */;
7584
+ case 2:
7585
+ return 2 /* z */;
7586
+ default:
7587
+ console.error("Error: Axis not found value: " + value);
7588
+ return 0 /* x */;
7589
+ }
7590
+ }
7591
+ /**
7592
+ * Function to convert a number to a curve type
7593
+ *
7594
+ * @param value number
7595
+ * @returns curve type
7596
+ */
7597
+ static convertToCurve(value) {
7598
+ switch (value) {
7599
+ case 0:
7600
+ return 0 /* Up */;
7601
+ case 1:
7602
+ return 1 /* Down */;
7603
+ case 2:
7604
+ return 2 /* UpDown */;
7605
+ case 3:
7606
+ return 3 /* DownUp */;
7607
+ default:
7608
+ console.error("Error: Curve not found value: " + value);
7609
+ return 0 /* Up */;
7610
+ }
7611
+ }
7612
+ static get Range() {
7613
+ if (!this._Range) {
7614
+ this._Range = class {
7615
+ constructor(x, y) {
7616
+ this.fLo = Math.min(x, y);
7617
+ this.fHi = Math.max(x, y);
7618
+ }
7619
+ clip(x) {
7620
+ if (x < this.fLo)
7621
+ return this.fLo;
7622
+ if (x > this.fHi)
7623
+ return this.fHi;
7624
+ return x;
7625
+ }
7626
+ };
7627
+ }
7628
+ return this._Range;
7629
+ }
7630
+ /**
7631
+ * Interpolator class
7632
+ */
7633
+ static get Interpolator() {
7634
+ if (!this._Interpolator) {
7635
+ this._Interpolator = class {
7636
+ constructor(lo, hi, v1, v2) {
7637
+ this.fRange = new _FaustSensors.Range(lo, hi);
7638
+ if (hi !== lo) {
7639
+ this.fCoef = (v2 - v1) / (hi - lo);
7640
+ this.fOffset = v1 - lo * this.fCoef;
7641
+ } else {
7642
+ this.fCoef = 0;
7643
+ this.fOffset = (v1 + v2) / 2;
7644
+ }
7645
+ }
7646
+ returnMappedValue(v) {
7647
+ var x = this.fRange.clip(v);
7648
+ return this.fOffset + x * this.fCoef;
7649
+ }
7650
+ getLowHigh(amin, amax) {
7651
+ return { amin: this.fRange.fLo, amax: this.fRange.fHi };
7652
+ }
7653
+ };
7654
+ }
7655
+ return this._Interpolator;
7656
+ }
7657
+ /**
7658
+ * Interpolator3pt class, combine two interpolators
7659
+ */
7660
+ static get Interpolator3pt() {
7661
+ if (!this._Interpolator3pt) {
7662
+ this._Interpolator3pt = class {
7663
+ constructor(lo, mid, hi, v1, vMid, v2) {
7664
+ this.fSegment1 = new _FaustSensors.Interpolator(lo, mid, v1, vMid);
7665
+ this.fSegment2 = new _FaustSensors.Interpolator(mid, hi, vMid, v2);
7666
+ this.fMid = mid;
7667
+ }
7668
+ returnMappedValue(x) {
7669
+ return x < this.fMid ? this.fSegment1.returnMappedValue(x) : this.fSegment2.returnMappedValue(x);
7670
+ }
7671
+ getMappingValues(amin, amid, amax) {
7672
+ var lowHighSegment1 = this.fSegment1.getLowHigh(amin, amid);
7673
+ var lowHighSegment2 = this.fSegment2.getLowHigh(amid, amax);
7674
+ return { amin: lowHighSegment1.amin, amid: lowHighSegment2.amin, amax: lowHighSegment2.amax };
7675
+ }
7676
+ };
7677
+ }
7678
+ return this._Interpolator3pt;
7679
+ }
7680
+ /**
7681
+ * UpConverter class, convert accelerometer value to Faust value
7682
+ */
7683
+ static get UpConverter() {
7684
+ if (!this._UpConverter) {
7685
+ this._UpConverter = class {
7686
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
7687
+ this.fActive = true;
7688
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmin, fmid, fmax);
7689
+ this.fF2A = new _FaustSensors.Interpolator3pt(fmin, fmid, fmax, amin, amid, amax);
7690
+ }
7691
+ uiToFaust(x) {
7692
+ return this.fA2F.returnMappedValue(x);
7693
+ }
7694
+ faustToUi(x) {
7695
+ return this.fF2A.returnMappedValue(x);
7696
+ }
7697
+ setMappingValues(amin, amid, amax, min, init, max) {
7698
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, min, init, max);
7699
+ this.fF2A = new _FaustSensors.Interpolator3pt(min, init, max, amin, amid, amax);
7700
+ }
7701
+ getMappingValues(amin, amid, amax) {
7702
+ return this.fA2F.getMappingValues(amin, amid, amax);
7703
+ }
7704
+ setActive(onOff) {
7705
+ this.fActive = onOff;
7706
+ }
7707
+ getActive() {
7708
+ return this.fActive;
7709
+ }
7710
+ };
7711
+ }
7712
+ return this._UpConverter;
7713
+ }
7714
+ /**
7715
+ * DownConverter class, convert accelerometer value to Faust value
7716
+ */
7717
+ static get DownConverter() {
7718
+ if (!this._DownConverter) {
7719
+ this._DownConverter = class {
7720
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
7721
+ this.fActive = true;
7722
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmax, fmid, fmin);
7723
+ this.fF2A = new _FaustSensors.Interpolator3pt(fmin, fmid, fmax, amax, amid, amin);
7724
+ }
7725
+ uiToFaust(x) {
7726
+ return this.fA2F.returnMappedValue(x);
7727
+ }
7728
+ faustToUi(x) {
7729
+ return this.fF2A.returnMappedValue(x);
7730
+ }
7731
+ setMappingValues(amin, amid, amax, min, init, max) {
7732
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, max, init, min);
7733
+ this.fF2A = new _FaustSensors.Interpolator3pt(min, init, max, amax, amid, amin);
7734
+ }
7735
+ getMappingValues(amin, amid, amax) {
7736
+ return this.fA2F.getMappingValues(amin, amid, amax);
7737
+ }
7738
+ setActive(onOff) {
7739
+ this.fActive = onOff;
7740
+ }
7741
+ getActive() {
7742
+ return this.fActive;
7743
+ }
7744
+ };
7745
+ }
7746
+ return this._DownConverter;
7747
+ }
7748
+ /**
7749
+ * UpDownConverter class, convert accelerometer value to Faust value
7750
+ */
7751
+ static get UpDownConverter() {
7752
+ if (!this._UpDownConverter) {
7753
+ this._UpDownConverter = class {
7754
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
7755
+ this.fActive = true;
7756
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmin, fmax, fmin);
7757
+ this.fF2A = new _FaustSensors.Interpolator(fmin, fmax, amin, amax);
7758
+ }
7759
+ uiToFaust(x) {
7760
+ return this.fA2F.returnMappedValue(x);
7761
+ }
7762
+ faustToUi(x) {
7763
+ return this.fF2A.returnMappedValue(x);
7764
+ }
7765
+ setMappingValues(amin, amid, amax, min, init, max) {
7766
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, min, max, min);
7767
+ this.fF2A = new _FaustSensors.Interpolator(min, max, amin, amax);
7768
+ }
7769
+ getMappingValues(amin, amid, amax) {
7770
+ return this.fA2F.getMappingValues(amin, amid, amax);
7771
+ }
7772
+ setActive(onOff) {
7773
+ this.fActive = onOff;
7774
+ }
7775
+ getActive() {
7776
+ return this.fActive;
7777
+ }
7778
+ };
7779
+ }
7780
+ return this._UpDownConverter;
7781
+ }
7782
+ static get DownUpConverter() {
7783
+ if (!this._DownUpConverter) {
7784
+ this._DownUpConverter = class {
7785
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
7786
+ this.fActive = true;
7787
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmax, fmin, fmax);
7788
+ this.fF2A = new _FaustSensors.Interpolator(fmin, fmax, amin, amax);
7789
+ }
7790
+ uiToFaust(x) {
7791
+ return this.fA2F.returnMappedValue(x);
7792
+ }
7793
+ faustToUi(x) {
7794
+ return this.fF2A.returnMappedValue(x);
7795
+ }
7796
+ setMappingValues(amin, amid, amax, min, init, max) {
7797
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, max, min, max);
7798
+ this.fF2A = new _FaustSensors.Interpolator(min, max, amin, amax);
7799
+ }
7800
+ getMappingValues(amin, amid, amax) {
7801
+ return this.fA2F.getMappingValues(amin, amid, amax);
7802
+ }
7803
+ setActive(onOff) {
7804
+ this.fActive = onOff;
7805
+ }
7806
+ getActive() {
7807
+ return this.fActive;
7808
+ }
7809
+ };
7810
+ }
7811
+ return this._DownUpConverter;
7812
+ }
7813
+ /**
7814
+ * Public function to build the accelerometer handler
7815
+ *
7816
+ * @returns `UpdatableValueConverter` built for the given curve
7817
+ */
7818
+ static buildHandler(curve, amin, amid, amax, min, init, max) {
7819
+ switch (curve) {
7820
+ case 0 /* Up */:
7821
+ return new _FaustSensors.UpConverter(amin, amid, amax, min, init, max);
7822
+ case 1 /* Down */:
7823
+ return new _FaustSensors.DownConverter(amin, amid, amax, min, init, max);
7824
+ case 2 /* UpDown */:
7825
+ return new _FaustSensors.UpDownConverter(amin, amid, amax, min, init, max);
7826
+ case 3 /* DownUp */:
7827
+ return new _FaustSensors.DownUpConverter(amin, amid, amax, min, init, max);
7828
+ default:
7829
+ return new _FaustSensors.UpConverter(amin, amid, amax, min, init, max);
7830
+ }
7831
+ }
7832
+ };
7833
+
7534
7834
  // src/FaustWebAudioDsp.ts
7535
7835
  var WasmAllocator = class {
7536
7836
  constructor(memory, offset) {
@@ -7783,17 +8083,25 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
7783
8083
  if (!item.meta)
7784
8084
  return;
7785
8085
  item.meta.forEach((meta) => {
7786
- const { midi } = meta;
7787
- if (!midi)
7788
- return;
7789
- const strMidi = midi.trim();
7790
- if (strMidi === "pitchwheel") {
7791
- this.fPitchwheelLabel.push({ path: item.address, min: item.min, max: item.max });
7792
- } else {
7793
- const matched = strMidi.match(/^ctrl\s(\d+)/);
7794
- if (!matched)
7795
- return;
7796
- this.fCtrlLabel[parseInt(matched[1])].push({ path: item.address, min: item.min, max: item.max });
8086
+ const { midi, acc, gyr } = meta;
8087
+ if (midi) {
8088
+ const strMidi = midi.trim();
8089
+ if (strMidi === "pitchwheel") {
8090
+ this.fPitchwheelLabel.push({ path: item.address, min: item.min, max: item.max });
8091
+ } else {
8092
+ const matched = strMidi.match(/^ctrl\s(\d+)/);
8093
+ if (matched) {
8094
+ this.fCtrlLabel[parseInt(matched[1])].push({ path: item.address, min: item.min, max: item.max });
8095
+ }
8096
+ }
8097
+ }
8098
+ if (acc) {
8099
+ const numAcc = acc.trim().split(" ").map(Number);
8100
+ this.setupAccHandler(item.address, FaustSensors.convertToAxis(numAcc[0]), FaustSensors.convertToCurve(numAcc[1]), numAcc[2], numAcc[3], numAcc[4], item.min, item.init, item.max);
8101
+ }
8102
+ if (gyr) {
8103
+ const numAcc = gyr.trim().split(" ").map(Number);
8104
+ this.setupGyrHandler(item.address, FaustSensors.convertToAxis(numAcc[0]), FaustSensors.convertToCurve(numAcc[1]), numAcc[2], numAcc[3], numAcc[4], item.min, item.init, item.max);
7797
8105
  }
7798
8106
  });
7799
8107
  } else if (item.type === "soundfile") {
@@ -7808,6 +8116,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
7808
8116
  this.fPtrSize = sampleSize;
7809
8117
  this.fSampleSize = sampleSize;
7810
8118
  this.fSoundfileBuffers = soundfiles;
8119
+ this.fAcc = { x: [], y: [], z: [] };
8120
+ this.fGyr = { x: [], y: [], z: [] };
7811
8121
  }
7812
8122
  // Tools
7813
8123
  static remap(v, mn0, mx0, mn1, mx1) {
@@ -7837,6 +8147,60 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
7837
8147
  let trimmed = input.replace(/^\{|\}$/g, "");
7838
8148
  return trimmed.split(";").map((str) => str.length <= 2 ? "" : str.substring(1, str.length - 1));
7839
8149
  }
8150
+ get hasAccInput() {
8151
+ return this.fAcc.x.length + this.fAcc.y.length + this.fAcc.z.length > 0;
8152
+ }
8153
+ propagateAcc(accelerationIncludingGravity) {
8154
+ const { x, y, z } = accelerationIncludingGravity;
8155
+ if (x !== null)
8156
+ this.fAcc.x.forEach((handler) => handler(x));
8157
+ if (y !== null)
8158
+ this.fAcc.y.forEach((handler) => handler(y));
8159
+ if (z !== null)
8160
+ this.fAcc.z.forEach((handler) => handler(z));
8161
+ }
8162
+ get hasGyrInput() {
8163
+ return this.fGyr.x.length + this.fGyr.y.length + this.fGyr.z.length > 0;
8164
+ }
8165
+ propagateGyr(event) {
8166
+ const { alpha, beta, gamma } = event;
8167
+ if (alpha !== null)
8168
+ this.fGyr.x.forEach((handler) => handler(alpha));
8169
+ if (beta !== null)
8170
+ this.fGyr.y.forEach((handler) => handler(beta));
8171
+ if (gamma !== null)
8172
+ this.fGyr.z.forEach((handler) => handler(gamma));
8173
+ }
8174
+ /** Build the accelerometer handler */
8175
+ setupAccHandler(path, axis, curve, amin, amid, amax, min, init, max) {
8176
+ const handler = FaustSensors.buildHandler(curve, amin, amid, amax, min, init, max);
8177
+ switch (axis) {
8178
+ case 0 /* x */:
8179
+ this.fAcc.x.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
8180
+ break;
8181
+ case 1 /* y */:
8182
+ this.fAcc.y.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
8183
+ break;
8184
+ case 2 /* z */:
8185
+ this.fAcc.z.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
8186
+ break;
8187
+ }
8188
+ }
8189
+ /** Build the gyroscope handler */
8190
+ setupGyrHandler(path, axis, curve, amin, amid, amax, min, init, max) {
8191
+ const handler = FaustSensors.buildHandler(curve, amin, amid, amax, min, init, max);
8192
+ switch (axis) {
8193
+ case 0 /* x */:
8194
+ this.fGyr.x.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
8195
+ break;
8196
+ case 1 /* y */:
8197
+ this.fGyr.y.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
8198
+ break;
8199
+ case 2 /* z */:
8200
+ this.fGyr.z.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
8201
+ break;
8202
+ }
8203
+ }
7840
8204
  static extractUrlsFromMeta(dspMeta) {
7841
8205
  const soundfilesEntry = dspMeta.meta.find((entry) => entry.soundfiles !== void 0);
7842
8206
  if (soundfilesEntry) {
@@ -8650,6 +9014,18 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
8650
9014
  destroy() {
8651
9015
  this.fDSPCode.destroy();
8652
9016
  }
9017
+ get hasAccInput() {
9018
+ return this.fDSPCode.hasAccInput;
9019
+ }
9020
+ propagateAcc(accelerationIncludingGravity) {
9021
+ this.fDSPCode.propagateAcc(accelerationIncludingGravity);
9022
+ }
9023
+ get hasGyrInput() {
9024
+ return this.fDSPCode.hasGyrInput;
9025
+ }
9026
+ propagateGyr(event) {
9027
+ this.fDSPCode.propagateGyr(event);
9028
+ }
8653
9029
  /**
8654
9030
  * Render frames in an array.
8655
9031
  *
@@ -9269,6 +9645,7 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9269
9645
  var SoundfileReader_default = SoundfileReader;
9270
9646
 
9271
9647
  // src/FaustAudioWorkletNode.ts
9648
+ var _hasAccInput, _hasGyrInput;
9272
9649
  var FaustAudioWorkletNode = class extends (globalThis.AudioWorkletNode || null) {
9273
9650
  constructor(context, name, factory, options, nodeOptions = {}) {
9274
9651
  const JSONObj = JSON.parse(factory.json);
@@ -9282,6 +9659,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9282
9659
  processorOptions: options,
9283
9660
  ...nodeOptions
9284
9661
  });
9662
+ __privateAdd(this, _hasAccInput, false);
9663
+ __privateAdd(this, _hasGyrInput, false);
9285
9664
  this.fJSONDsp = JSONObj;
9286
9665
  this.fJSON = factory.json;
9287
9666
  this.fOutputHandler = null;
@@ -9293,6 +9672,15 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9293
9672
  if (item.type === "vslider" || item.type === "hslider" || item.type === "button" || item.type === "checkbox" || item.type === "nentry") {
9294
9673
  this.fInputsItems.push(item.address);
9295
9674
  this.fDescriptor.push(item);
9675
+ if (!item.meta)
9676
+ return;
9677
+ item.meta.forEach((meta) => {
9678
+ const { midi, acc, gyr } = meta;
9679
+ if (acc)
9680
+ __privateSet(this, _hasAccInput, true);
9681
+ if (gyr)
9682
+ __privateSet(this, _hasGyrInput, true);
9683
+ });
9296
9684
  }
9297
9685
  };
9298
9686
  FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);
@@ -9305,6 +9693,54 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9305
9693
  };
9306
9694
  }
9307
9695
  // Public API
9696
+ /** Setup accelerometer and gyroscope handlers */
9697
+ async listenMotion() {
9698
+ if (this.hasAccInput) {
9699
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
9700
+ if (!accelerationIncludingGravity)
9701
+ return;
9702
+ const { x, y, z } = accelerationIncludingGravity;
9703
+ this.propagateAcc({ x, y, z });
9704
+ };
9705
+ if (window.DeviceMotionEvent) {
9706
+ if (typeof window.DeviceMotionEvent.requestPermission === "function") {
9707
+ try {
9708
+ const response = await window.DeviceMotionEvent.requestPermission();
9709
+ if (response !== "granted")
9710
+ throw new Error("Unable to access the accelerometer.");
9711
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
9712
+ } catch (error) {
9713
+ console.error(error);
9714
+ }
9715
+ } else {
9716
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
9717
+ }
9718
+ } else {
9719
+ console.log("Cannot set the accelerometer handler.");
9720
+ }
9721
+ }
9722
+ if (this.hasGyrInput) {
9723
+ const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
9724
+ this.propagateGyr({ alpha, beta, gamma });
9725
+ };
9726
+ if (window.DeviceMotionEvent) {
9727
+ if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
9728
+ try {
9729
+ const response = await window.DeviceOrientationEvent.requestPermission();
9730
+ if (response !== "granted")
9731
+ throw new Error("Unable to access the gyroscope.");
9732
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
9733
+ } catch (error) {
9734
+ console.error(error);
9735
+ }
9736
+ } else {
9737
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
9738
+ }
9739
+ } else {
9740
+ console.log("Cannot set the gyroscope handler.");
9741
+ }
9742
+ }
9743
+ }
9308
9744
  setOutputParamHandler(handler) {
9309
9745
  this.fOutputHandler = handler;
9310
9746
  }
@@ -9363,6 +9799,24 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9363
9799
  const e = { type: "pitchWheel", data: [channel, wheel] };
9364
9800
  this.port.postMessage(e);
9365
9801
  }
9802
+ get hasAccInput() {
9803
+ return __privateGet(this, _hasAccInput);
9804
+ }
9805
+ propagateAcc(accelerationIncludingGravity) {
9806
+ if (!accelerationIncludingGravity)
9807
+ return;
9808
+ const e = { type: "acc", data: accelerationIncludingGravity };
9809
+ this.port.postMessage(e);
9810
+ }
9811
+ get hasGyrInput() {
9812
+ return __privateGet(this, _hasGyrInput);
9813
+ }
9814
+ propagateGyr(event) {
9815
+ if (!event)
9816
+ return;
9817
+ const e = { type: "gyr", data: event };
9818
+ this.port.postMessage(e);
9819
+ }
9366
9820
  setParamValue(path, value) {
9367
9821
  const e = { type: "param", data: { path, value } };
9368
9822
  this.port.postMessage(e);
@@ -9400,6 +9854,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9400
9854
  this.port.close();
9401
9855
  }
9402
9856
  };
9857
+ _hasAccInput = new WeakMap();
9858
+ _hasGyrInput = new WeakMap();
9403
9859
  var FaustMonoAudioWorkletNode = class extends FaustAudioWorkletNode {
9404
9860
  constructor(context, name, factory, sampleSize, nodeOptions = {}) {
9405
9861
  super(context, name, factory, { name, factory, sampleSize }, nodeOptions);
@@ -9495,6 +9951,54 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9495
9951
  this.start();
9496
9952
  }
9497
9953
  // Public API
9954
+ /** Setup accelerometer and gyroscope handlers */
9955
+ async listenMotion() {
9956
+ if (this.hasAccInput) {
9957
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
9958
+ if (!accelerationIncludingGravity)
9959
+ return;
9960
+ const { x, y, z } = accelerationIncludingGravity;
9961
+ this.propagateAcc({ x, y, z });
9962
+ };
9963
+ if (window.DeviceMotionEvent) {
9964
+ if (typeof window.DeviceMotionEvent.requestPermission === "function") {
9965
+ try {
9966
+ const response = await window.DeviceMotionEvent.requestPermission();
9967
+ if (response !== "granted")
9968
+ throw new Error("Unable to access the accelerometer.");
9969
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
9970
+ } catch (error) {
9971
+ console.error(error);
9972
+ }
9973
+ } else {
9974
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
9975
+ }
9976
+ } else {
9977
+ console.log("Cannot set the accelerometer handler.");
9978
+ }
9979
+ }
9980
+ if (this.hasGyrInput) {
9981
+ const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
9982
+ this.propagateGyr({ alpha, beta, gamma });
9983
+ };
9984
+ if (window.DeviceMotionEvent) {
9985
+ if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
9986
+ try {
9987
+ const response = await window.DeviceOrientationEvent.requestPermission();
9988
+ if (response !== "granted")
9989
+ throw new Error("Unable to access the gyroscope.");
9990
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
9991
+ } catch (error) {
9992
+ console.error(error);
9993
+ }
9994
+ } else {
9995
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
9996
+ }
9997
+ } else {
9998
+ console.log("Cannot set the gyroscope handler.");
9999
+ }
10000
+ }
10001
+ }
9498
10002
  compute(input, output) {
9499
10003
  return this.fDSPCode.compute(input, output);
9500
10004
  }
@@ -9563,6 +10067,18 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
9563
10067
  destroy() {
9564
10068
  this.fDSPCode.destroy();
9565
10069
  }
10070
+ get hasAccInput() {
10071
+ return this.fDSPCode.hasAccInput;
10072
+ }
10073
+ propagateAcc(accelerationIncludingGravity) {
10074
+ this.fDSPCode.propagateAcc(accelerationIncludingGravity);
10075
+ }
10076
+ get hasGyrInput() {
10077
+ return this.fDSPCode.hasGyrInput;
10078
+ }
10079
+ propagateGyr(event) {
10080
+ this.fDSPCode.propagateGyr(event);
10081
+ }
9566
10082
  };
9567
10083
  var FaustMonoScriptProcessorNode = class extends FaustScriptProcessorNode {
9568
10084
  };
@@ -9646,6 +10162,8 @@ var ${Soundfile.name} = ${Soundfile.toString()}
9646
10162
  var Soundfile = ${Soundfile.name};
9647
10163
  var ${WasmAllocator.name} = ${WasmAllocator.toString()}
9648
10164
  var WasmAllocator = ${WasmAllocator.name};
10165
+ var ${FaustSensors.name} = ${FaustSensors.toString()}
10166
+ var FaustSensors = ${FaustSensors.name};
9649
10167
  // Put them in dependencies
9650
10168
  const dependencies = {
9651
10169
  FaustBaseWebAudioDsp,
@@ -9698,6 +10216,8 @@ var ${Soundfile.name} = ${Soundfile.toString()}
9698
10216
  var Soundfile = ${Soundfile.name};
9699
10217
  var ${WasmAllocator.name} = ${WasmAllocator.toString()}
9700
10218
  var WasmAllocator = ${WasmAllocator.name};
10219
+ var ${FaustSensors.name} = ${FaustSensors.toString()}
10220
+ var FaustSensors = ${FaustSensors.name};
9701
10221
  var FFTUtils = ${fftUtils.toString()}
9702
10222
  // Put them in dependencies
9703
10223
  const dependencies = {
@@ -9925,6 +10445,8 @@ var ${Soundfile.name} = ${Soundfile.toString()}
9925
10445
  var Soundfile = ${Soundfile.name};
9926
10446
  var ${WasmAllocator.name} = ${WasmAllocator.toString()}
9927
10447
  var WasmAllocator = ${WasmAllocator.name};
10448
+ var ${FaustSensors.name} = ${FaustSensors.toString()}
10449
+ var FaustSensors = ${FaustSensors.name};
9928
10450
  // Put them in dependencies
9929
10451
  const dependencies = {
9930
10452
  FaustBaseWebAudioDsp,