@grame/faustwasm 0.2.2 → 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.
package/dist/cjs/index.js CHANGED
@@ -28,6 +28,24 @@
28
28
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
29
  mod
30
30
  ));
31
+ var __accessCheck = (obj, member, msg) => {
32
+ if (!member.has(obj))
33
+ throw TypeError("Cannot " + msg);
34
+ };
35
+ var __privateGet = (obj, member, getter) => {
36
+ __accessCheck(obj, member, "read from private field");
37
+ return getter ? getter.call(obj) : member.get(obj);
38
+ };
39
+ var __privateAdd = (obj, member, value) => {
40
+ if (member.has(obj))
41
+ throw TypeError("Cannot add the same private member more than once");
42
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
43
+ };
44
+ var __privateSet = (obj, member, value, setter) => {
45
+ __accessCheck(obj, member, "write to private field");
46
+ setter ? setter.call(obj, value) : member.set(obj, value);
47
+ return value;
48
+ };
31
49
 
32
50
  // src/instantiateFaustModuleFromFile.ts
33
51
  var instantiateFaustModuleFromFile = async (jsFile, dataFile = jsFile.replace(/c?js$/, "data"), wasmFile = jsFile.replace(/c?js$/, "wasm")) => {
@@ -149,18 +167,30 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
149
167
  handleMessageAux(e) {
150
168
  const msg = e.data;
151
169
  switch (msg.type) {
152
- case "midi":
170
+ case "acc": {
171
+ this.propagateAcc(msg.data);
172
+ break;
173
+ }
174
+ case "gyr": {
175
+ this.propagateGyr(msg.data);
176
+ break;
177
+ }
178
+ case "midi": {
153
179
  this.midiMessage(msg.data);
154
180
  break;
155
- case "ctrlChange":
181
+ }
182
+ case "ctrlChange": {
156
183
  this.ctrlChange(msg.data[0], msg.data[1], msg.data[2]);
157
184
  break;
158
- case "pitchWheel":
185
+ }
186
+ case "pitchWheel": {
159
187
  this.pitchWheel(msg.data[0], msg.data[1]);
160
188
  break;
161
- case "param":
189
+ }
190
+ case "param": {
162
191
  this.setParamValue(msg.data.path, msg.data.value);
163
192
  break;
193
+ }
164
194
  case "setPlotHandler": {
165
195
  if (msg.data) {
166
196
  this.fDSPCode.setPlotHandler((output, index, events) => this.port.postMessage({ type: "plot", value: output, index, events }));
@@ -199,6 +229,12 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
199
229
  pitchWheel(channel, wheel) {
200
230
  this.fDSPCode.pitchWheel(channel, wheel);
201
231
  }
232
+ propagateAcc(accelerationIncludingGravity) {
233
+ this.fDSPCode.propagateAcc(accelerationIncludingGravity);
234
+ }
235
+ propagateGyr(event) {
236
+ this.fDSPCode.propagateGyr(event);
237
+ }
202
238
  }
203
239
  class FaustMonoAudioWorkletProcessor extends FaustAudioWorkletProcessor {
204
240
  constructor(options) {
@@ -1514,6 +1550,270 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
1514
1550
  };
1515
1551
  var FaustWasmInstantiator_default = FaustWasmInstantiator;
1516
1552
 
1553
+ // src/FaustSensors.ts
1554
+ var FaustSensors = class _FaustSensors {
1555
+ /**
1556
+ * Function to convert a number to an axis type
1557
+ *
1558
+ * @param value number
1559
+ * @returns axis type
1560
+ */
1561
+ static convertToAxis(value) {
1562
+ switch (value) {
1563
+ case 0:
1564
+ return 0 /* x */;
1565
+ case 1:
1566
+ return 1 /* y */;
1567
+ case 2:
1568
+ return 2 /* z */;
1569
+ default:
1570
+ console.error("Error: Axis not found value: " + value);
1571
+ return 0 /* x */;
1572
+ }
1573
+ }
1574
+ /**
1575
+ * Function to convert a number to a curve type
1576
+ *
1577
+ * @param value number
1578
+ * @returns curve type
1579
+ */
1580
+ static convertToCurve(value) {
1581
+ switch (value) {
1582
+ case 0:
1583
+ return 0 /* Up */;
1584
+ case 1:
1585
+ return 1 /* Down */;
1586
+ case 2:
1587
+ return 2 /* UpDown */;
1588
+ case 3:
1589
+ return 3 /* DownUp */;
1590
+ default:
1591
+ console.error("Error: Curve not found value: " + value);
1592
+ return 0 /* Up */;
1593
+ }
1594
+ }
1595
+ static get Range() {
1596
+ if (!this._Range) {
1597
+ this._Range = class {
1598
+ constructor(x, y) {
1599
+ this.fLo = Math.min(x, y);
1600
+ this.fHi = Math.max(x, y);
1601
+ }
1602
+ clip(x) {
1603
+ if (x < this.fLo)
1604
+ return this.fLo;
1605
+ if (x > this.fHi)
1606
+ return this.fHi;
1607
+ return x;
1608
+ }
1609
+ };
1610
+ }
1611
+ return this._Range;
1612
+ }
1613
+ /**
1614
+ * Interpolator class
1615
+ */
1616
+ static get Interpolator() {
1617
+ if (!this._Interpolator) {
1618
+ this._Interpolator = class {
1619
+ constructor(lo, hi, v1, v2) {
1620
+ this.fRange = new _FaustSensors.Range(lo, hi);
1621
+ if (hi !== lo) {
1622
+ this.fCoef = (v2 - v1) / (hi - lo);
1623
+ this.fOffset = v1 - lo * this.fCoef;
1624
+ } else {
1625
+ this.fCoef = 0;
1626
+ this.fOffset = (v1 + v2) / 2;
1627
+ }
1628
+ }
1629
+ returnMappedValue(v) {
1630
+ var x = this.fRange.clip(v);
1631
+ return this.fOffset + x * this.fCoef;
1632
+ }
1633
+ getLowHigh(amin, amax) {
1634
+ return { amin: this.fRange.fLo, amax: this.fRange.fHi };
1635
+ }
1636
+ };
1637
+ }
1638
+ return this._Interpolator;
1639
+ }
1640
+ /**
1641
+ * Interpolator3pt class, combine two interpolators
1642
+ */
1643
+ static get Interpolator3pt() {
1644
+ if (!this._Interpolator3pt) {
1645
+ this._Interpolator3pt = class {
1646
+ constructor(lo, mid, hi, v1, vMid, v2) {
1647
+ this.fSegment1 = new _FaustSensors.Interpolator(lo, mid, v1, vMid);
1648
+ this.fSegment2 = new _FaustSensors.Interpolator(mid, hi, vMid, v2);
1649
+ this.fMid = mid;
1650
+ }
1651
+ returnMappedValue(x) {
1652
+ return x < this.fMid ? this.fSegment1.returnMappedValue(x) : this.fSegment2.returnMappedValue(x);
1653
+ }
1654
+ getMappingValues(amin, amid, amax) {
1655
+ var lowHighSegment1 = this.fSegment1.getLowHigh(amin, amid);
1656
+ var lowHighSegment2 = this.fSegment2.getLowHigh(amid, amax);
1657
+ return { amin: lowHighSegment1.amin, amid: lowHighSegment2.amin, amax: lowHighSegment2.amax };
1658
+ }
1659
+ };
1660
+ }
1661
+ return this._Interpolator3pt;
1662
+ }
1663
+ /**
1664
+ * UpConverter class, convert accelerometer value to Faust value
1665
+ */
1666
+ static get UpConverter() {
1667
+ if (!this._UpConverter) {
1668
+ this._UpConverter = class {
1669
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
1670
+ this.fActive = true;
1671
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmin, fmid, fmax);
1672
+ this.fF2A = new _FaustSensors.Interpolator3pt(fmin, fmid, fmax, amin, amid, amax);
1673
+ }
1674
+ uiToFaust(x) {
1675
+ return this.fA2F.returnMappedValue(x);
1676
+ }
1677
+ faustToUi(x) {
1678
+ return this.fF2A.returnMappedValue(x);
1679
+ }
1680
+ setMappingValues(amin, amid, amax, min, init, max) {
1681
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, min, init, max);
1682
+ this.fF2A = new _FaustSensors.Interpolator3pt(min, init, max, amin, amid, amax);
1683
+ }
1684
+ getMappingValues(amin, amid, amax) {
1685
+ return this.fA2F.getMappingValues(amin, amid, amax);
1686
+ }
1687
+ setActive(onOff) {
1688
+ this.fActive = onOff;
1689
+ }
1690
+ getActive() {
1691
+ return this.fActive;
1692
+ }
1693
+ };
1694
+ }
1695
+ return this._UpConverter;
1696
+ }
1697
+ /**
1698
+ * DownConverter class, convert accelerometer value to Faust value
1699
+ */
1700
+ static get DownConverter() {
1701
+ if (!this._DownConverter) {
1702
+ this._DownConverter = class {
1703
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
1704
+ this.fActive = true;
1705
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmax, fmid, fmin);
1706
+ this.fF2A = new _FaustSensors.Interpolator3pt(fmin, fmid, fmax, amax, amid, amin);
1707
+ }
1708
+ uiToFaust(x) {
1709
+ return this.fA2F.returnMappedValue(x);
1710
+ }
1711
+ faustToUi(x) {
1712
+ return this.fF2A.returnMappedValue(x);
1713
+ }
1714
+ setMappingValues(amin, amid, amax, min, init, max) {
1715
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, max, init, min);
1716
+ this.fF2A = new _FaustSensors.Interpolator3pt(min, init, max, amax, amid, amin);
1717
+ }
1718
+ getMappingValues(amin, amid, amax) {
1719
+ return this.fA2F.getMappingValues(amin, amid, amax);
1720
+ }
1721
+ setActive(onOff) {
1722
+ this.fActive = onOff;
1723
+ }
1724
+ getActive() {
1725
+ return this.fActive;
1726
+ }
1727
+ };
1728
+ }
1729
+ return this._DownConverter;
1730
+ }
1731
+ /**
1732
+ * UpDownConverter class, convert accelerometer value to Faust value
1733
+ */
1734
+ static get UpDownConverter() {
1735
+ if (!this._UpDownConverter) {
1736
+ this._UpDownConverter = class {
1737
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
1738
+ this.fActive = true;
1739
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmin, fmax, fmin);
1740
+ this.fF2A = new _FaustSensors.Interpolator(fmin, fmax, amin, amax);
1741
+ }
1742
+ uiToFaust(x) {
1743
+ return this.fA2F.returnMappedValue(x);
1744
+ }
1745
+ faustToUi(x) {
1746
+ return this.fF2A.returnMappedValue(x);
1747
+ }
1748
+ setMappingValues(amin, amid, amax, min, init, max) {
1749
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, min, max, min);
1750
+ this.fF2A = new _FaustSensors.Interpolator(min, max, amin, amax);
1751
+ }
1752
+ getMappingValues(amin, amid, amax) {
1753
+ return this.fA2F.getMappingValues(amin, amid, amax);
1754
+ }
1755
+ setActive(onOff) {
1756
+ this.fActive = onOff;
1757
+ }
1758
+ getActive() {
1759
+ return this.fActive;
1760
+ }
1761
+ };
1762
+ }
1763
+ return this._UpDownConverter;
1764
+ }
1765
+ static get DownUpConverter() {
1766
+ if (!this._DownUpConverter) {
1767
+ this._DownUpConverter = class {
1768
+ constructor(amin, amid, amax, fmin, fmid, fmax) {
1769
+ this.fActive = true;
1770
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, fmax, fmin, fmax);
1771
+ this.fF2A = new _FaustSensors.Interpolator(fmin, fmax, amin, amax);
1772
+ }
1773
+ uiToFaust(x) {
1774
+ return this.fA2F.returnMappedValue(x);
1775
+ }
1776
+ faustToUi(x) {
1777
+ return this.fF2A.returnMappedValue(x);
1778
+ }
1779
+ setMappingValues(amin, amid, amax, min, init, max) {
1780
+ this.fA2F = new _FaustSensors.Interpolator3pt(amin, amid, amax, max, min, max);
1781
+ this.fF2A = new _FaustSensors.Interpolator(min, max, amin, amax);
1782
+ }
1783
+ getMappingValues(amin, amid, amax) {
1784
+ return this.fA2F.getMappingValues(amin, amid, amax);
1785
+ }
1786
+ setActive(onOff) {
1787
+ this.fActive = onOff;
1788
+ }
1789
+ getActive() {
1790
+ return this.fActive;
1791
+ }
1792
+ };
1793
+ }
1794
+ return this._DownUpConverter;
1795
+ }
1796
+ /**
1797
+ * Public function to build the accelerometer handler
1798
+ *
1799
+ * @returns `UpdatableValueConverter` built for the given curve
1800
+ */
1801
+ static buildHandler(curve, amin, amid, amax, min, init, max) {
1802
+ switch (curve) {
1803
+ case 0 /* Up */:
1804
+ return new _FaustSensors.UpConverter(amin, amid, amax, min, init, max);
1805
+ case 1 /* Down */:
1806
+ return new _FaustSensors.DownConverter(amin, amid, amax, min, init, max);
1807
+ case 2 /* UpDown */:
1808
+ return new _FaustSensors.UpDownConverter(amin, amid, amax, min, init, max);
1809
+ case 3 /* DownUp */:
1810
+ return new _FaustSensors.DownUpConverter(amin, amid, amax, min, init, max);
1811
+ default:
1812
+ return new _FaustSensors.UpConverter(amin, amid, amax, min, init, max);
1813
+ }
1814
+ }
1815
+ };
1816
+
1517
1817
  // src/FaustWebAudioDsp.ts
1518
1818
  var WasmAllocator = class {
1519
1819
  constructor(memory, offset) {
@@ -1766,17 +2066,25 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
1766
2066
  if (!item.meta)
1767
2067
  return;
1768
2068
  item.meta.forEach((meta) => {
1769
- const { midi } = meta;
1770
- if (!midi)
1771
- return;
1772
- const strMidi = midi.trim();
1773
- if (strMidi === "pitchwheel") {
1774
- this.fPitchwheelLabel.push({ path: item.address, min: item.min, max: item.max });
1775
- } else {
1776
- const matched = strMidi.match(/^ctrl\s(\d+)/);
1777
- if (!matched)
1778
- return;
1779
- this.fCtrlLabel[parseInt(matched[1])].push({ path: item.address, min: item.min, max: item.max });
2069
+ const { midi, acc, gyr } = meta;
2070
+ if (midi) {
2071
+ const strMidi = midi.trim();
2072
+ if (strMidi === "pitchwheel") {
2073
+ this.fPitchwheelLabel.push({ path: item.address, min: item.min, max: item.max });
2074
+ } else {
2075
+ const matched = strMidi.match(/^ctrl\s(\d+)/);
2076
+ if (matched) {
2077
+ this.fCtrlLabel[parseInt(matched[1])].push({ path: item.address, min: item.min, max: item.max });
2078
+ }
2079
+ }
2080
+ }
2081
+ if (acc) {
2082
+ const numAcc = acc.trim().split(" ").map(Number);
2083
+ this.setupAccHandler(item.address, FaustSensors.convertToAxis(numAcc[0]), FaustSensors.convertToCurve(numAcc[1]), numAcc[2], numAcc[3], numAcc[4], item.min, item.init, item.max);
2084
+ }
2085
+ if (gyr) {
2086
+ const numAcc = gyr.trim().split(" ").map(Number);
2087
+ this.setupGyrHandler(item.address, FaustSensors.convertToAxis(numAcc[0]), FaustSensors.convertToCurve(numAcc[1]), numAcc[2], numAcc[3], numAcc[4], item.min, item.init, item.max);
1780
2088
  }
1781
2089
  });
1782
2090
  } else if (item.type === "soundfile") {
@@ -1791,6 +2099,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
1791
2099
  this.fPtrSize = sampleSize;
1792
2100
  this.fSampleSize = sampleSize;
1793
2101
  this.fSoundfileBuffers = soundfiles;
2102
+ this.fAcc = { x: [], y: [], z: [] };
2103
+ this.fGyr = { x: [], y: [], z: [] };
1794
2104
  }
1795
2105
  // Tools
1796
2106
  static remap(v, mn0, mx0, mn1, mx1) {
@@ -1820,6 +2130,60 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
1820
2130
  let trimmed = input.replace(/^\{|\}$/g, "");
1821
2131
  return trimmed.split(";").map((str) => str.length <= 2 ? "" : str.substring(1, str.length - 1));
1822
2132
  }
2133
+ get hasAccInput() {
2134
+ return this.fAcc.x.length + this.fAcc.y.length + this.fAcc.z.length > 0;
2135
+ }
2136
+ propagateAcc(accelerationIncludingGravity) {
2137
+ const { x, y, z } = accelerationIncludingGravity;
2138
+ if (x !== null)
2139
+ this.fAcc.x.forEach((handler) => handler(x));
2140
+ if (y !== null)
2141
+ this.fAcc.y.forEach((handler) => handler(y));
2142
+ if (z !== null)
2143
+ this.fAcc.z.forEach((handler) => handler(z));
2144
+ }
2145
+ get hasGyrInput() {
2146
+ return this.fGyr.x.length + this.fGyr.y.length + this.fGyr.z.length > 0;
2147
+ }
2148
+ propagateGyr(event) {
2149
+ const { alpha, beta, gamma } = event;
2150
+ if (alpha !== null)
2151
+ this.fGyr.x.forEach((handler) => handler(alpha));
2152
+ if (beta !== null)
2153
+ this.fGyr.y.forEach((handler) => handler(beta));
2154
+ if (gamma !== null)
2155
+ this.fGyr.z.forEach((handler) => handler(gamma));
2156
+ }
2157
+ /** Build the accelerometer handler */
2158
+ setupAccHandler(path, axis, curve, amin, amid, amax, min, init, max) {
2159
+ const handler = FaustSensors.buildHandler(curve, amin, amid, amax, min, init, max);
2160
+ switch (axis) {
2161
+ case 0 /* x */:
2162
+ this.fAcc.x.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
2163
+ break;
2164
+ case 1 /* y */:
2165
+ this.fAcc.y.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
2166
+ break;
2167
+ case 2 /* z */:
2168
+ this.fAcc.z.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
2169
+ break;
2170
+ }
2171
+ }
2172
+ /** Build the gyroscope handler */
2173
+ setupGyrHandler(path, axis, curve, amin, amid, amax, min, init, max) {
2174
+ const handler = FaustSensors.buildHandler(curve, amin, amid, amax, min, init, max);
2175
+ switch (axis) {
2176
+ case 0 /* x */:
2177
+ this.fGyr.x.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
2178
+ break;
2179
+ case 1 /* y */:
2180
+ this.fGyr.y.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
2181
+ break;
2182
+ case 2 /* z */:
2183
+ this.fGyr.z.push((val) => this.setParamValue(path, handler.uiToFaust(val)));
2184
+ break;
2185
+ }
2186
+ }
1823
2187
  static extractUrlsFromMeta(dspMeta) {
1824
2188
  const soundfilesEntry = dspMeta.meta.find((entry) => entry.soundfiles !== void 0);
1825
2189
  if (soundfilesEntry) {
@@ -2633,6 +2997,18 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
2633
2997
  destroy() {
2634
2998
  this.fDSPCode.destroy();
2635
2999
  }
3000
+ get hasAccInput() {
3001
+ return this.fDSPCode.hasAccInput;
3002
+ }
3003
+ propagateAcc(accelerationIncludingGravity) {
3004
+ this.fDSPCode.propagateAcc(accelerationIncludingGravity);
3005
+ }
3006
+ get hasGyrInput() {
3007
+ return this.fDSPCode.hasGyrInput;
3008
+ }
3009
+ propagateGyr(event) {
3010
+ this.fDSPCode.propagateGyr(event);
3011
+ }
2636
3012
  /**
2637
3013
  * Render frames in an array.
2638
3014
  *
@@ -3252,6 +3628,7 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3252
3628
  var SoundfileReader_default = SoundfileReader;
3253
3629
 
3254
3630
  // src/FaustAudioWorkletNode.ts
3631
+ var _hasAccInput, _hasGyrInput;
3255
3632
  var FaustAudioWorkletNode = class extends (globalThis.AudioWorkletNode || null) {
3256
3633
  constructor(context, name, factory, options, nodeOptions = {}) {
3257
3634
  const JSONObj = JSON.parse(factory.json);
@@ -3265,6 +3642,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3265
3642
  processorOptions: options,
3266
3643
  ...nodeOptions
3267
3644
  });
3645
+ __privateAdd(this, _hasAccInput, false);
3646
+ __privateAdd(this, _hasGyrInput, false);
3268
3647
  this.fJSONDsp = JSONObj;
3269
3648
  this.fJSON = factory.json;
3270
3649
  this.fOutputHandler = null;
@@ -3276,6 +3655,15 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3276
3655
  if (item.type === "vslider" || item.type === "hslider" || item.type === "button" || item.type === "checkbox" || item.type === "nentry") {
3277
3656
  this.fInputsItems.push(item.address);
3278
3657
  this.fDescriptor.push(item);
3658
+ if (!item.meta)
3659
+ return;
3660
+ item.meta.forEach((meta) => {
3661
+ const { midi, acc, gyr } = meta;
3662
+ if (acc)
3663
+ __privateSet(this, _hasAccInput, true);
3664
+ if (gyr)
3665
+ __privateSet(this, _hasGyrInput, true);
3666
+ });
3279
3667
  }
3280
3668
  };
3281
3669
  FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);
@@ -3288,6 +3676,54 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3288
3676
  };
3289
3677
  }
3290
3678
  // Public API
3679
+ /** Setup accelerometer and gyroscope handlers */
3680
+ async listenMotion() {
3681
+ if (this.hasAccInput) {
3682
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3683
+ if (!accelerationIncludingGravity)
3684
+ return;
3685
+ const { x, y, z } = accelerationIncludingGravity;
3686
+ this.propagateAcc({ x, y, z });
3687
+ };
3688
+ if (window.DeviceMotionEvent) {
3689
+ if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3690
+ try {
3691
+ const response = await window.DeviceMotionEvent.requestPermission();
3692
+ if (response !== "granted")
3693
+ throw new Error("Unable to access the accelerometer.");
3694
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
3695
+ } catch (error) {
3696
+ console.error(error);
3697
+ }
3698
+ } else {
3699
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
3700
+ }
3701
+ } else {
3702
+ console.log("Cannot set the accelerometer handler.");
3703
+ }
3704
+ }
3705
+ if (this.hasGyrInput) {
3706
+ const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
3707
+ this.propagateGyr({ alpha, beta, gamma });
3708
+ };
3709
+ if (window.DeviceMotionEvent) {
3710
+ if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
3711
+ try {
3712
+ const response = await window.DeviceOrientationEvent.requestPermission();
3713
+ if (response !== "granted")
3714
+ throw new Error("Unable to access the gyroscope.");
3715
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
3716
+ } catch (error) {
3717
+ console.error(error);
3718
+ }
3719
+ } else {
3720
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
3721
+ }
3722
+ } else {
3723
+ console.log("Cannot set the gyroscope handler.");
3724
+ }
3725
+ }
3726
+ }
3291
3727
  setOutputParamHandler(handler) {
3292
3728
  this.fOutputHandler = handler;
3293
3729
  }
@@ -3346,6 +3782,24 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3346
3782
  const e = { type: "pitchWheel", data: [channel, wheel] };
3347
3783
  this.port.postMessage(e);
3348
3784
  }
3785
+ get hasAccInput() {
3786
+ return __privateGet(this, _hasAccInput);
3787
+ }
3788
+ propagateAcc(accelerationIncludingGravity) {
3789
+ if (!accelerationIncludingGravity)
3790
+ return;
3791
+ const e = { type: "acc", data: accelerationIncludingGravity };
3792
+ this.port.postMessage(e);
3793
+ }
3794
+ get hasGyrInput() {
3795
+ return __privateGet(this, _hasGyrInput);
3796
+ }
3797
+ propagateGyr(event) {
3798
+ if (!event)
3799
+ return;
3800
+ const e = { type: "gyr", data: event };
3801
+ this.port.postMessage(e);
3802
+ }
3349
3803
  setParamValue(path, value) {
3350
3804
  const e = { type: "param", data: { path, value } };
3351
3805
  this.port.postMessage(e);
@@ -3383,6 +3837,8 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3383
3837
  this.port.close();
3384
3838
  }
3385
3839
  };
3840
+ _hasAccInput = new WeakMap();
3841
+ _hasGyrInput = new WeakMap();
3386
3842
  var FaustMonoAudioWorkletNode = class extends FaustAudioWorkletNode {
3387
3843
  constructor(context, name, factory, sampleSize, nodeOptions = {}) {
3388
3844
  super(context, name, factory, { name, factory, sampleSize }, nodeOptions);
@@ -3478,6 +3934,54 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3478
3934
  this.start();
3479
3935
  }
3480
3936
  // Public API
3937
+ /** Setup accelerometer and gyroscope handlers */
3938
+ async listenMotion() {
3939
+ if (this.hasAccInput) {
3940
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3941
+ if (!accelerationIncludingGravity)
3942
+ return;
3943
+ const { x, y, z } = accelerationIncludingGravity;
3944
+ this.propagateAcc({ x, y, z });
3945
+ };
3946
+ if (window.DeviceMotionEvent) {
3947
+ if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3948
+ try {
3949
+ const response = await window.DeviceMotionEvent.requestPermission();
3950
+ if (response !== "granted")
3951
+ throw new Error("Unable to access the accelerometer.");
3952
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
3953
+ } catch (error) {
3954
+ console.error(error);
3955
+ }
3956
+ } else {
3957
+ window.addEventListener("devicemotion", handleDeviceMotion, true);
3958
+ }
3959
+ } else {
3960
+ console.log("Cannot set the accelerometer handler.");
3961
+ }
3962
+ }
3963
+ if (this.hasGyrInput) {
3964
+ const handleDeviceOrientation = ({ alpha, beta, gamma }) => {
3965
+ this.propagateGyr({ alpha, beta, gamma });
3966
+ };
3967
+ if (window.DeviceMotionEvent) {
3968
+ if (typeof window.DeviceOrientationEvent.requestPermission === "function") {
3969
+ try {
3970
+ const response = await window.DeviceOrientationEvent.requestPermission();
3971
+ if (response !== "granted")
3972
+ throw new Error("Unable to access the gyroscope.");
3973
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
3974
+ } catch (error) {
3975
+ console.error(error);
3976
+ }
3977
+ } else {
3978
+ window.addEventListener("deviceorientation", handleDeviceOrientation, true);
3979
+ }
3980
+ } else {
3981
+ console.log("Cannot set the gyroscope handler.");
3982
+ }
3983
+ }
3984
+ }
3481
3985
  compute(input, output) {
3482
3986
  return this.fDSPCode.compute(input, output);
3483
3987
  }
@@ -3546,6 +4050,18 @@ export default ${(_b = jsCode.match(jsCodeHead)) == null ? void 0 : _b[1]};
3546
4050
  destroy() {
3547
4051
  this.fDSPCode.destroy();
3548
4052
  }
4053
+ get hasAccInput() {
4054
+ return this.fDSPCode.hasAccInput;
4055
+ }
4056
+ propagateAcc(accelerationIncludingGravity) {
4057
+ this.fDSPCode.propagateAcc(accelerationIncludingGravity);
4058
+ }
4059
+ get hasGyrInput() {
4060
+ return this.fDSPCode.hasGyrInput;
4061
+ }
4062
+ propagateGyr(event) {
4063
+ this.fDSPCode.propagateGyr(event);
4064
+ }
3549
4065
  };
3550
4066
  var FaustMonoScriptProcessorNode = class extends FaustScriptProcessorNode {
3551
4067
  };
@@ -3629,6 +4145,8 @@ var ${Soundfile.name} = ${Soundfile.toString()}
3629
4145
  var Soundfile = ${Soundfile.name};
3630
4146
  var ${WasmAllocator.name} = ${WasmAllocator.toString()}
3631
4147
  var WasmAllocator = ${WasmAllocator.name};
4148
+ var ${FaustSensors.name} = ${FaustSensors.toString()}
4149
+ var FaustSensors = ${FaustSensors.name};
3632
4150
  // Put them in dependencies
3633
4151
  const dependencies = {
3634
4152
  FaustBaseWebAudioDsp,
@@ -3681,6 +4199,8 @@ var ${Soundfile.name} = ${Soundfile.toString()}
3681
4199
  var Soundfile = ${Soundfile.name};
3682
4200
  var ${WasmAllocator.name} = ${WasmAllocator.toString()}
3683
4201
  var WasmAllocator = ${WasmAllocator.name};
4202
+ var ${FaustSensors.name} = ${FaustSensors.toString()}
4203
+ var FaustSensors = ${FaustSensors.name};
3684
4204
  var FFTUtils = ${fftUtils.toString()}
3685
4205
  // Put them in dependencies
3686
4206
  const dependencies = {
@@ -3908,6 +4428,8 @@ var ${Soundfile.name} = ${Soundfile.toString()}
3908
4428
  var Soundfile = ${Soundfile.name};
3909
4429
  var ${WasmAllocator.name} = ${WasmAllocator.toString()}
3910
4430
  var WasmAllocator = ${WasmAllocator.name};
4431
+ var ${FaustSensors.name} = ${FaustSensors.toString()}
4432
+ var FaustSensors = ${FaustSensors.name};
3911
4433
  // Put them in dependencies
3912
4434
  const dependencies = {
3913
4435
  FaustBaseWebAudioDsp,