@grame/faustwasm 0.12.2 → 0.13.1

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.
Files changed (81) hide show
  1. package/.prettierrc +5 -0
  2. package/README.md +21 -3
  3. package/assets/standalone/faustwasm/index.d.ts +38 -16
  4. package/assets/standalone/faustwasm/index.js +1593 -404
  5. package/assets/standalone/faustwasm/index.js.map +2 -2
  6. package/dist/cjs/index.d.ts +38 -16
  7. package/dist/cjs/index.js +1592 -404
  8. package/dist/cjs/index.js.map +2 -2
  9. package/dist/cjs-bundle/index.d.ts +38 -16
  10. package/dist/cjs-bundle/index.js +1595 -406
  11. package/dist/cjs-bundle/index.js.map +2 -2
  12. package/dist/esm/index.d.ts +38 -16
  13. package/dist/esm/index.js +1593 -404
  14. package/dist/esm/index.js.map +2 -2
  15. package/dist/esm-bundle/index.d.ts +38 -16
  16. package/dist/esm-bundle/index.js +1595 -406
  17. package/dist/esm-bundle/index.js.map +2 -2
  18. package/eslint.config.js +11 -0
  19. package/package.json +57 -51
  20. package/sound.cpp +163 -0
  21. package/sound.dsp +4 -0
  22. package/sound.json +1 -0
  23. package/sound.wasm +0 -0
  24. package/src/FaustAudioWorkletCommunicator.ts +150 -143
  25. package/src/FaustAudioWorkletNode.ts +173 -75
  26. package/src/FaustAudioWorkletProcessor.ts +218 -90
  27. package/src/FaustCmajor.ts +9 -3
  28. package/src/FaustCompiler.ts +112 -35
  29. package/src/FaustDspGenerator.ts +589 -117
  30. package/src/FaustDspInstance.ts +58 -24
  31. package/src/FaustFFTAudioWorkletProcessor.ts +822 -612
  32. package/src/FaustOfflineProcessor.ts +187 -56
  33. package/src/FaustScriptProcessorNode.ts +156 -49
  34. package/src/FaustSensors.ts +426 -96
  35. package/src/FaustSvgDiagrams.ts +18 -5
  36. package/src/FaustWasmInstantiator.ts +224 -70
  37. package/src/FaustWebAudioDsp.ts +1060 -349
  38. package/src/LibFaust.ts +13 -4
  39. package/src/SoundfileReader.ts +175 -131
  40. package/src/WavDecoder.ts +42 -20
  41. package/src/WavEncoder.ts +35 -21
  42. package/src/copyWebStandaloneAssets.d.ts +20 -5
  43. package/src/copyWebStandaloneAssets.js +203 -75
  44. package/src/exports-bundle.ts +5 -5
  45. package/src/exports.ts +24 -24
  46. package/src/faust2CmajorFiles.d.ts +7 -3
  47. package/src/faust2cmajorFiles.js +10 -8
  48. package/src/faust2svgFiles.d.ts +7 -3
  49. package/src/faust2svgFiles.js +9 -7
  50. package/src/faust2wasmFiles.d.ts +10 -5
  51. package/src/faust2wasmFiles.js +34 -17
  52. package/src/faust2wavFiles.d.ts +12 -3
  53. package/src/faust2wavFiles.js +25 -12
  54. package/src/index-bundle-iife.ts +2 -2
  55. package/src/index-bundle.ts +1 -1
  56. package/src/index.ts +1 -1
  57. package/src/instantiateFaustModule.ts +43 -36
  58. package/src/instantiateFaustModuleFromFile.ts +30 -14
  59. package/src/types.ts +101 -25
  60. package/test/faustlive-wasm/faustwasm/index.d.ts +38 -16
  61. package/test/faustlive-wasm/faustwasm/index.js +1593 -404
  62. package/test/faustlive-wasm/faustwasm/index.js.map +2 -2
  63. package/test/web/create-faust-node.html +53 -0
  64. package/test/web/create-faust-node.js +181 -0
  65. package/test/organ/create-node.js +0 -252
  66. package/test/organ/dsp-meta.json +0 -131
  67. package/test/organ/dsp-module.wasm +0 -0
  68. package/test/organ/faust-pwa.js +0 -344
  69. package/test/organ/faust-ui/index.css +0 -442
  70. package/test/organ/faust-ui/index.css.map +0 -1
  71. package/test/organ/faust-ui/index.d.ts +0 -1
  72. package/test/organ/faust-ui/index.js +0 -3756
  73. package/test/organ/faust-ui/index.js.map +0 -1
  74. package/test/organ/faustwasm/index.d.ts +0 -1705
  75. package/test/organ/faustwasm/index.js +0 -4773
  76. package/test/organ/faustwasm/index.js.map +0 -7
  77. package/test/organ/icon.png +0 -0
  78. package/test/organ/index.html +0 -76
  79. package/test/organ/index.js +0 -69
  80. package/test/organ/manifest.json +0 -17
  81. package/test/organ/service-worker.js +0 -165
@@ -0,0 +1,11 @@
1
+ import eslint from "@eslint/js";
2
+ import { defineConfig } from "eslint/config";
3
+ import tseslint from "typescript-eslint";
4
+
5
+ export default defineConfig(
6
+ eslint.configs.recommended,
7
+ tseslint.configs.recommended,
8
+ {
9
+ ignores: ["**/dist", "**/test", "**/libfaust-wasm.js"],
10
+ },
11
+ );
package/package.json CHANGED
@@ -1,53 +1,59 @@
1
1
  {
2
- "name": "@grame/faustwasm",
3
- "version": "0.12.2",
4
- "description": "WebAssembly version of Faust Compiler",
5
- "main": "dist/cjs/index.js",
6
- "types": "dist/esm/index.d.ts",
7
- "module": "dist/esm/index.js",
8
- "type": "module",
9
- "scripts": {
10
- "build": "npm run build-cjs && npm run build-cjs-bundle && npm run build-esm && npm run build-esm-bundle && node postbuild-bundled.js & npm run build-types & npm run build-types-bundle",
11
- "build-cjs": "esbuild src/index.ts --target=es2019 --bundle --sourcemap --outdir=dist/cjs --format=iife --external:fs --external:url",
12
- "build-cjs-bundle": "node prebuild-bundled.js && esbuild src/index-bundle-iife.ts --target=es2019 --bundle --sourcemap --loader:.wasm=binary --loader:.data=binary --outfile=dist/cjs-bundle/index.js --format=iife --external:fs --external:url --external:path --external:ws && node postbuild-bundled.js",
13
- "build-esm": "esbuild src/index.ts --target=es2019 --bundle --sourcemap --outdir=dist/esm --format=esm --external:fs --external:url",
14
- "build-esm-bundle": "node prebuild-bundled.js && esbuild src/index-bundle.ts --target=es2019 --bundle --sourcemap --loader:.wasm=binary --loader:.data=binary --outfile=dist/esm-bundle/index.js --format=esm --external:fs --external:url --external:path --external:ws && node postbuild-bundled.js",
15
- "build-types": "dts-bundle-generator -o dist/cjs/index.d.ts src/index.ts --external-imports",
16
- "build-types-bundle": "dts-bundle-generator -o dist/cjs-bundle/index.d.ts src/index-bundle.ts --external-imports",
17
- "postbuild": "node postbuild.js"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/grame-cncm/faustwasm.git"
22
- },
23
- "keywords": [
24
- "faust",
25
- "webassembly",
26
- "audio",
27
- "signal processing"
28
- ],
29
- "bin": {
30
- "faust2sndfile-ts": "scripts/faust2sndfile.js",
31
- "faust2svg-ts": "scripts/faust2svg.js",
32
- "faust2wasm-ts": "scripts/faust2wasm.js"
33
- },
34
- "author": "Grame-CNCM",
35
- "license": "LGPL-3.0",
36
- "bugs": {
37
- "url": "https://github.com/grame-cncm/faustwasm/issues"
38
- },
39
- "homepage": "https://github.com/grame-cncm/faustwasm#readme",
40
- "devDependencies": {
41
- "@aws-crypto/sha256-js": "^5.2.0",
42
- "@shren/faust-ui": "^1.1.16",
43
- "@types/node": "^20.12.7",
44
- "@webaudiomodules/api": "^2.0.0-alpha.6",
45
- "@webaudiomodules/sdk-parammgr": "^0.0.13",
46
- "dts-bundle-generator": "^9.5.1",
47
- "esbuild": "^0.25.12",
48
- "typescript": "^5.4.5"
49
- },
50
- "dependencies": {
51
- "@types/emscripten": "^1.39.10"
52
- }
2
+ "name": "@grame/faustwasm",
3
+ "version": "0.13.1",
4
+ "description": "WebAssembly version of Faust Compiler",
5
+ "main": "dist/cjs/index.js",
6
+ "types": "dist/esm/index.d.ts",
7
+ "module": "dist/esm/index.js",
8
+ "type": "module",
9
+ "scripts": {
10
+ "build": "npm run build-cjs && npm run build-cjs-bundle && npm run build-esm && npm run build-esm-bundle && node postbuild-bundled.js & npm run build-types & npm run build-types-bundle",
11
+ "build-cjs": "esbuild src/index.ts --target=es2019 --bundle --sourcemap --outdir=dist/cjs --format=iife --external:fs --external:url",
12
+ "build-cjs-bundle": "node prebuild-bundled.js && esbuild src/index-bundle-iife.ts --target=es2019 --bundle --sourcemap --loader:.wasm=binary --loader:.data=binary --outfile=dist/cjs-bundle/index.js --format=iife --external:fs --external:url --external:path --external:ws && node postbuild-bundled.js",
13
+ "build-esm": "esbuild src/index.ts --target=es2019 --bundle --sourcemap --outdir=dist/esm --format=esm --external:fs --external:url",
14
+ "build-esm-bundle": "node prebuild-bundled.js && esbuild src/index-bundle.ts --target=es2019 --bundle --sourcemap --loader:.wasm=binary --loader:.data=binary --outfile=dist/esm-bundle/index.js --format=esm --external:fs --external:url --external:path --external:ws && node postbuild-bundled.js",
15
+ "build-types": "dts-bundle-generator -o dist/cjs/index.d.ts src/index.ts --external-imports",
16
+ "build-types-bundle": "dts-bundle-generator -o dist/cjs-bundle/index.d.ts src/index-bundle.ts --external-imports",
17
+ "format": "prettier --write src",
18
+ "lint": " prettier --check src && eslint src",
19
+ "postbuild": "node postbuild.js"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/grame-cncm/faustwasm.git"
24
+ },
25
+ "keywords": [
26
+ "faust",
27
+ "webassembly",
28
+ "audio",
29
+ "signal processing"
30
+ ],
31
+ "bin": {
32
+ "faust2sndfile-ts": "scripts/faust2sndfile.js",
33
+ "faust2svg-ts": "scripts/faust2svg.js",
34
+ "faust2wasm-ts": "scripts/faust2wasm.js"
35
+ },
36
+ "author": "Grame-CNCM",
37
+ "license": "LGPL-3.0",
38
+ "bugs": {
39
+ "url": "https://github.com/grame-cncm/faustwasm/issues"
40
+ },
41
+ "homepage": "https://github.com/grame-cncm/faustwasm#readme",
42
+ "devDependencies": {
43
+ "@aws-crypto/sha256-js": "^5.2.0",
44
+ "@eslint/js": "^9.39.1",
45
+ "@shren/faust-ui": "^1.1.16",
46
+ "@types/node": "^20.12.7",
47
+ "@webaudiomodules/api": "^2.0.0-alpha.6",
48
+ "@webaudiomodules/sdk-parammgr": "^0.0.13",
49
+ "dts-bundle-generator": "^9.5.1",
50
+ "esbuild": "^0.27.0",
51
+ "eslint": "^9.39.1",
52
+ "prettier": "^3.6.2",
53
+ "typescript": "^5.4.5",
54
+ "typescript-eslint": "^8.46.3"
55
+ },
56
+ "dependencies": {
57
+ "@types/emscripten": "^1.39.10"
58
+ }
53
59
  }
package/sound.cpp ADDED
@@ -0,0 +1,163 @@
1
+ /* ------------------------------------------------------------
2
+ name: "sound"
3
+ Code generated with Faust 2.82.1 (https://faust.grame.fr)
4
+ Compilation options: -lang cpp -ct 1 -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0
5
+ ------------------------------------------------------------ */
6
+
7
+ #ifndef __mydsp_H__
8
+ #define __mydsp_H__
9
+
10
+ #ifndef FAUSTFLOAT
11
+ #define FAUSTFLOAT float
12
+ #endif
13
+
14
+ #include <algorithm>
15
+ #include <cmath>
16
+ #include <cstdint>
17
+ #include <math.h>
18
+
19
+ #ifndef FAUSTCLASS
20
+ #define FAUSTCLASS mydsp
21
+ #endif
22
+
23
+ #ifdef __APPLE__
24
+ #define exp10f __exp10f
25
+ #define exp10 __exp10
26
+ #endif
27
+
28
+ #if defined(_WIN32)
29
+ #define RESTRICT __restrict
30
+ #else
31
+ #define RESTRICT __restrict__
32
+ #endif
33
+
34
+
35
+ class mydsp : public dsp {
36
+
37
+ private:
38
+
39
+ Soundfile* fSoundfile0;
40
+ int fSampleRate;
41
+ float fConst0;
42
+ int iVec0[2];
43
+ float fRec0[2];
44
+ float fRec1[2];
45
+
46
+ void allocate() {
47
+ fSoundfile0 = 0;
48
+ }
49
+
50
+ public:
51
+ mydsp() {
52
+ allocate();
53
+ }
54
+
55
+ void metadata(Meta* m) {
56
+ m->declare("compile_options", "-lang cpp -ct 1 -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0");
57
+ m->declare("filename", "sound.dsp");
58
+ m->declare("interpolators.lib/name", "Faust Interpolator Library");
59
+ m->declare("interpolators.lib/version", "1.4.0");
60
+ m->declare("maths.lib/author", "GRAME");
61
+ m->declare("maths.lib/copyright", "GRAME");
62
+ m->declare("maths.lib/license", "LGPL with exception");
63
+ m->declare("maths.lib/name", "Faust Math Library");
64
+ m->declare("maths.lib/version", "2.9.0");
65
+ m->declare("name", "sound");
66
+ m->declare("platform.lib/name", "Generic Platform Library");
67
+ m->declare("platform.lib/version", "1.3.0");
68
+ m->declare("signals.lib/name", "Faust Signal Routing Library");
69
+ m->declare("signals.lib/version", "1.6.0");
70
+ m->declare("soundfiles.lib/name", "Faust Soundfile Library");
71
+ m->declare("soundfiles.lib/version", "1.7.0");
72
+ }
73
+
74
+ virtual int getNumInputs() {
75
+ return 0;
76
+ }
77
+ virtual int getNumOutputs() {
78
+ return 1;
79
+ }
80
+
81
+ static void classInit(int sample_rate) {
82
+ }
83
+
84
+ virtual void instanceConstants(int sample_rate) {
85
+ fSampleRate = sample_rate;
86
+ fConst0 = 1.0f / std::min<float>(1.92e+05f, std::max<float>(1.0f, static_cast<float>(fSampleRate)));
87
+ }
88
+
89
+ virtual void instanceResetUserInterface() {
90
+ if (reinterpret_cast<uintptr_t>(fSoundfile0) == 0) {
91
+ fSoundfile0 = defaultsound;
92
+ }
93
+ }
94
+
95
+ virtual void instanceClear() {
96
+ for (int l0 = 0; l0 < 2; l0 = l0 + 1) {
97
+ iVec0[l0] = 0;
98
+ }
99
+ for (int l1 = 0; l1 < 2; l1 = l1 + 1) {
100
+ fRec0[l1] = 0.0f;
101
+ }
102
+ for (int l2 = 0; l2 < 2; l2 = l2 + 1) {
103
+ fRec1[l2] = 0.0f;
104
+ }
105
+ }
106
+
107
+ virtual void init(int sample_rate) {
108
+ classInit(sample_rate);
109
+ instanceInit(sample_rate);
110
+ }
111
+
112
+ virtual void instanceInit(int sample_rate) {
113
+ instanceConstants(sample_rate);
114
+ instanceResetUserInterface();
115
+ instanceClear();
116
+ }
117
+
118
+ virtual mydsp* clone() {
119
+ return new mydsp();
120
+ }
121
+
122
+ virtual int getSampleRate() {
123
+ return fSampleRate;
124
+ }
125
+
126
+ virtual void buildUserInterface(UI* ui_interface) {
127
+ ui_interface->openVerticalBox("sound");
128
+ ui_interface->addSoundfile("sound", "{'toto'}", &fSoundfile0);
129
+ ui_interface->closeBox();
130
+ }
131
+
132
+ virtual void compute(int count, FAUSTFLOAT** RESTRICT inputs, FAUSTFLOAT** RESTRICT outputs) {
133
+ FAUSTFLOAT* output0 = outputs[0];
134
+ Soundfile* fSoundfile0ca = fSoundfile0;
135
+ int* fSoundfile0ca_ra0 = fSoundfile0ca->fSR;
136
+ float fSlow0 = fConst0 * static_cast<float>(fSoundfile0ca_ra0[0]);
137
+ float fSlow1 = std::floor(fSlow0);
138
+ float fSlow2 = static_cast<float>(static_cast<int>(fSlow0));
139
+ int* fSoundfile0ca_le0 = fSoundfile0ca->fLength;
140
+ int iSlow3 = fSoundfile0ca_le0[0];
141
+ float fSlow4 = static_cast<float>(iSlow3);
142
+ float fSlow5 = static_cast<float>(iSlow3 + -1);
143
+ int* fSoundfile0ca_of0 = fSoundfile0ca->fOffset;
144
+ float** fSoundfile0ca_bu0 = static_cast<float**>(fSoundfile0ca->fBuffers);
145
+ float* fSoundfile0ca_bu_ch0 = fSoundfile0ca_bu0[0];
146
+ for (int i0 = 0; i0 < count; i0 = i0 + 1) {
147
+ iVec0[0] = 1;
148
+ float fTemp0 = fSlow0 + (fRec0[1] - fSlow1);
149
+ int iRec2 = 1;
150
+ float fTemp1 = static_cast<float>((1 <= iVec0[1]) * iRec2);
151
+ fRec0[0] = (fSlow0 + (fRec0[1] - std::floor(fTemp0) - fSlow1)) * fTemp1;
152
+ fRec1[0] = fTemp1 * std::fmod(fSlow2 + fRec1[1] + static_cast<float>(static_cast<int>(fTemp0)), fSlow4);
153
+ output0[i0] = static_cast<FAUSTFLOAT>(fSoundfile0ca_bu_ch0[fSoundfile0ca_of0[0] + static_cast<int>(std::max<float>(0.0f, std::min<float>(fRec1[0], fSlow5)))]);
154
+ iVec0[1] = iVec0[0];
155
+ fRec0[1] = fRec0[0];
156
+ fRec1[1] = fRec1[0];
157
+ }
158
+ fSoundfile0 = fSoundfile0ca;
159
+ }
160
+
161
+ };
162
+
163
+ #endif
package/sound.dsp ADDED
@@ -0,0 +1,4 @@
1
+ import("stdfaust.lib");
2
+ import("soundfiles.lib");
3
+ s = soundfile("sound [url:{'toto'}]", 1);
4
+ process = so.sound(s, 0).loop;
package/sound.json ADDED
@@ -0,0 +1 @@
1
+ {"name": "sound","filename": "sound.dsp","version": "2.82.1","compile_options": "-lang wasm -ct 1 -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0","library_list": ["/usr/local/share/faust/soundfiles.lib","/usr/local/share/faust/stdfaust.lib","/usr/local/share/faust/interpolators.lib","/usr/local/share/faust/signals.lib","/usr/local/share/faust/maths.lib","/usr/local/share/faust/platform.lib"],"include_pathnames": ["/usr/local/share/faust","/usr/local/share/faust","/usr/share/faust",".","/Users/letz/Developpements/faust-wasm/faustwasm"],"size": 36,"code": "aW1wb3J0KCJzdGRmYXVzdC5saWIiKTsKaW1wb3J0KCJzb3VuZGZpbGVzLmxpYiIpOwpzID0gc291bmRmaWxlKCJzb3VuZCBbdXJsOnsndG90byd9XSIsIDEpOwpwcm9jZXNzID0gc28uc291bmQocywgMCkubG9vcDsK","inputs": 0,"outputs": 1,"meta": [ { "compile_options": "-lang wasm -ct 1 -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0" },{ "filename": "sound.dsp" },{ "interpolators.lib/name": "Faust Interpolator Library" },{ "interpolators.lib/version": "1.4.0" },{ "maths.lib/author": "GRAME" },{ "maths.lib/copyright": "GRAME" },{ "maths.lib/license": "LGPL with exception" },{ "maths.lib/name": "Faust Math Library" },{ "maths.lib/version": "2.9.0" },{ "name": "sound" },{ "platform.lib/name": "Generic Platform Library" },{ "platform.lib/version": "1.3.0" },{ "signals.lib/name": "Faust Signal Routing Library" },{ "signals.lib/version": "1.6.0" },{ "soundfiles.lib/name": "Faust Soundfile Library" },{ "soundfiles.lib/version": "1.7.0" }],"ui": [ {"type": "vgroup","label": "sound","items": [ {"type": "soundfile","label": "sound","varname": "fSoundfile0","shortname": "sound","url": "{-toto-}","address": "/sound/sound","index": 0}]}]}
package/sound.wasm ADDED
Binary file
@@ -1,143 +1,150 @@
1
-
2
- /**
3
- * Layout:
4
- *
5
- *
6
- * invert-isAndroid (uint8)
7
- * new-acc-data-available (uint8)
8
- * new-gyr-data-available (uint8)
9
- * empty (uint8)
10
- *
11
- * acc.x, acc.y, acc.z (f32)
12
- *
13
- * gyr.alpha, gyr.beta, gyr.gamma (f32)
14
- */
15
- export class FaustAudioWorkletCommunicator {
16
- protected readonly port: MessagePort;
17
- protected readonly supportSharedArrayBuffer: boolean;
18
- protected readonly byteLength: number;
19
- protected uin8Invert: Uint8ClampedArray;
20
- protected uin8NewAccData: Uint8ClampedArray;
21
- protected uin8NewGyrData: Uint8ClampedArray;
22
- protected f32Acc: Float32Array;
23
- protected f32Gyr: Float32Array;
24
- constructor(port: MessagePort) {
25
- this.port = port;
26
- this.supportSharedArrayBuffer = !!globalThis.SharedArrayBuffer;
27
- this.byteLength
28
- = 4 * Uint8Array.BYTES_PER_ELEMENT
29
- + 3 * Float32Array.BYTES_PER_ELEMENT
30
- + 3 * Float32Array.BYTES_PER_ELEMENT;
31
- }
32
- initializeBuffer(ab: SharedArrayBuffer | ArrayBuffer) {
33
- let ptr = 0;
34
- this.uin8Invert = new Uint8ClampedArray(ab, ptr, 1);
35
- ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
36
- this.uin8NewAccData = new Uint8ClampedArray(ab, ptr, 1);
37
- ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
38
- this.uin8NewGyrData = new Uint8ClampedArray(ab, ptr, 1);
39
- ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
40
- ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;; // empty
41
- this.f32Acc = new Float32Array(ab, ptr, 3);
42
- ptr += 3 * Float32Array.BYTES_PER_ELEMENT;
43
- this.f32Gyr = new Float32Array(ab, ptr, 3);
44
- ptr += 3 * Float32Array.BYTES_PER_ELEMENT;
45
- }
46
- setNewAccDataAvailable(value: boolean) {
47
- if (!this.uin8NewAccData) return;
48
- this.uin8NewAccData[0] = +value;
49
- }
50
- getNewAccDataAvailable() {
51
- return !!this.uin8NewAccData?.[0];
52
- }
53
- setNewGyrDataAvailable(value: boolean) {
54
- if (!this.uin8NewGyrData) return;
55
- this.uin8NewGyrData[0] = +value;
56
- }
57
- getNewGyrDataAvailable() {
58
- return !!this.uin8NewGyrData?.[0];
59
- }
60
- setAcc({ x, y, z }: { x: number, y: number, z: number }, invert = false) {
61
- if (!this.supportSharedArrayBuffer) {
62
- const e = { type: "acc", data: { x, y, z }, invert };
63
- this.port.postMessage(e);
64
- }
65
- if (!this.uin8NewAccData) return;
66
- this.uin8Invert[0] = +invert;
67
- this.f32Acc[0] = x;
68
- this.f32Acc[1] = y;
69
- this.f32Acc[2] = z;
70
- this.uin8NewAccData[0] = 1;
71
- }
72
- getAcc() {
73
- if (!this.uin8NewAccData) return;
74
- const invert = !!this.uin8Invert[0];
75
- const [x, y, z] = this.f32Acc;
76
- return { x, y, z, invert };
77
- }
78
- setGyr({ alpha, beta, gamma }: { alpha: number, beta: number, gamma: number }) {
79
- if (!this.supportSharedArrayBuffer) {
80
- const e = { type: "gyr", data: { alpha, beta, gamma } };
81
- this.port.postMessage(e);
82
- }
83
- if (!this.uin8NewGyrData) return;
84
- this.f32Gyr[0] = alpha;
85
- this.f32Gyr[1] = beta;
86
- this.f32Gyr[2] = gamma;
87
- this.uin8NewGyrData[0] = 1;
88
- }
89
- getGyr() {
90
- if (!this.uin8NewGyrData) return;
91
- const [alpha, beta, gamma] = this.f32Gyr;
92
- return { alpha, beta, gamma };
93
- }
94
- }
95
-
96
- export class FaustAudioWorkletNodeCommunicator extends FaustAudioWorkletCommunicator {
97
- constructor(port: MessagePort) {
98
- super(port);
99
- if (this.supportSharedArrayBuffer) {
100
- const sab = new SharedArrayBuffer(this.byteLength);
101
- this.initializeBuffer(sab);
102
- this.port.postMessage({ type: "initSab", sab });
103
- } else {
104
- const ab = new ArrayBuffer(this.byteLength);
105
- this.initializeBuffer(ab);
106
- }
107
- }
108
- }
109
-
110
- export class FaustAudioWorkletProcessorCommunicator extends FaustAudioWorkletCommunicator {
111
- constructor(port: MessagePort) {
112
- super(port);
113
-
114
- if (this.supportSharedArrayBuffer) {
115
- this.port.addEventListener("message", (event) => {
116
- const { data } = event;
117
- if (data.type === "initSab") {
118
- this.initializeBuffer(data.sab);
119
- }
120
- });
121
- } else {
122
- const ab = new ArrayBuffer(this.byteLength);
123
- this.initializeBuffer(ab);
124
- this.port.addEventListener("message", (event) => {
125
- const msg = event.data;
126
-
127
- switch (msg.type) {
128
- // Sensors messages
129
- case "acc": {
130
- this.setAcc(msg.data, msg.invert);
131
- break;
132
- }
133
- case "gyr": {
134
- this.setGyr(msg.data);
135
- break;
136
- }
137
- default:
138
- break;
139
- }
140
- });
141
- }
142
- }
143
- }
1
+ /**
2
+ * Layout:
3
+ *
4
+ *
5
+ * invert-isAndroid (uint8)
6
+ * new-acc-data-available (uint8)
7
+ * new-gyr-data-available (uint8)
8
+ * empty (uint8)
9
+ *
10
+ * acc.x, acc.y, acc.z (f32)
11
+ *
12
+ * gyr.alpha, gyr.beta, gyr.gamma (f32)
13
+ */
14
+ export class FaustAudioWorkletCommunicator {
15
+ protected readonly port: MessagePort;
16
+ protected readonly supportSharedArrayBuffer: boolean;
17
+ protected readonly byteLength: number;
18
+ protected uin8Invert: Uint8ClampedArray;
19
+ protected uin8NewAccData: Uint8ClampedArray;
20
+ protected uin8NewGyrData: Uint8ClampedArray;
21
+ protected f32Acc: Float32Array;
22
+ protected f32Gyr: Float32Array;
23
+ constructor(port: MessagePort) {
24
+ this.port = port;
25
+ this.supportSharedArrayBuffer = !!globalThis.SharedArrayBuffer;
26
+ this.byteLength =
27
+ 4 * Uint8Array.BYTES_PER_ELEMENT +
28
+ 3 * Float32Array.BYTES_PER_ELEMENT +
29
+ 3 * Float32Array.BYTES_PER_ELEMENT;
30
+ }
31
+ initializeBuffer(ab: SharedArrayBuffer | ArrayBuffer) {
32
+ let ptr = 0;
33
+ this.uin8Invert = new Uint8ClampedArray(ab, ptr, 1);
34
+ ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
35
+ this.uin8NewAccData = new Uint8ClampedArray(ab, ptr, 1);
36
+ ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
37
+ this.uin8NewGyrData = new Uint8ClampedArray(ab, ptr, 1);
38
+ ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
39
+ ptr += Uint8ClampedArray.BYTES_PER_ELEMENT; // empty
40
+ this.f32Acc = new Float32Array(ab, ptr, 3);
41
+ ptr += 3 * Float32Array.BYTES_PER_ELEMENT;
42
+ this.f32Gyr = new Float32Array(ab, ptr, 3);
43
+ ptr += 3 * Float32Array.BYTES_PER_ELEMENT;
44
+ }
45
+ setNewAccDataAvailable(value: boolean) {
46
+ if (!this.uin8NewAccData) return;
47
+ this.uin8NewAccData[0] = +value;
48
+ }
49
+ getNewAccDataAvailable() {
50
+ return !!this.uin8NewAccData?.[0];
51
+ }
52
+ setNewGyrDataAvailable(value: boolean) {
53
+ if (!this.uin8NewGyrData) return;
54
+ this.uin8NewGyrData[0] = +value;
55
+ }
56
+ getNewGyrDataAvailable() {
57
+ return !!this.uin8NewGyrData?.[0];
58
+ }
59
+ setAcc({ x, y, z }: { x: number; y: number; z: number }, invert = false) {
60
+ if (!this.supportSharedArrayBuffer) {
61
+ const e = { type: 'acc', data: { x, y, z }, invert };
62
+ this.port.postMessage(e);
63
+ }
64
+ if (!this.uin8NewAccData) return;
65
+ this.uin8Invert[0] = +invert;
66
+ this.f32Acc[0] = x;
67
+ this.f32Acc[1] = y;
68
+ this.f32Acc[2] = z;
69
+ this.uin8NewAccData[0] = 1;
70
+ }
71
+ getAcc() {
72
+ if (!this.uin8NewAccData) return;
73
+ const invert = !!this.uin8Invert[0];
74
+ const [x, y, z] = this.f32Acc;
75
+ return { x, y, z, invert };
76
+ }
77
+ setGyr({
78
+ alpha,
79
+ beta,
80
+ gamma
81
+ }: {
82
+ alpha: number;
83
+ beta: number;
84
+ gamma: number;
85
+ }) {
86
+ if (!this.supportSharedArrayBuffer) {
87
+ const e = { type: 'gyr', data: { alpha, beta, gamma } };
88
+ this.port.postMessage(e);
89
+ }
90
+ if (!this.uin8NewGyrData) return;
91
+ this.f32Gyr[0] = alpha;
92
+ this.f32Gyr[1] = beta;
93
+ this.f32Gyr[2] = gamma;
94
+ this.uin8NewGyrData[0] = 1;
95
+ }
96
+ getGyr() {
97
+ if (!this.uin8NewGyrData) return;
98
+ const [alpha, beta, gamma] = this.f32Gyr;
99
+ return { alpha, beta, gamma };
100
+ }
101
+ }
102
+
103
+ export class FaustAudioWorkletNodeCommunicator extends FaustAudioWorkletCommunicator {
104
+ constructor(port: MessagePort) {
105
+ super(port);
106
+ if (this.supportSharedArrayBuffer) {
107
+ const sab = new SharedArrayBuffer(this.byteLength);
108
+ this.initializeBuffer(sab);
109
+ this.port.postMessage({ type: 'initSab', sab });
110
+ } else {
111
+ const ab = new ArrayBuffer(this.byteLength);
112
+ this.initializeBuffer(ab);
113
+ }
114
+ }
115
+ }
116
+
117
+ export class FaustAudioWorkletProcessorCommunicator extends FaustAudioWorkletCommunicator {
118
+ constructor(port: MessagePort) {
119
+ super(port);
120
+
121
+ if (this.supportSharedArrayBuffer) {
122
+ this.port.addEventListener('message', (event) => {
123
+ const { data } = event;
124
+ if (data.type === 'initSab') {
125
+ this.initializeBuffer(data.sab);
126
+ }
127
+ });
128
+ } else {
129
+ const ab = new ArrayBuffer(this.byteLength);
130
+ this.initializeBuffer(ab);
131
+ this.port.addEventListener('message', (event) => {
132
+ const msg = event.data;
133
+
134
+ switch (msg.type) {
135
+ // Sensors messages
136
+ case 'acc': {
137
+ this.setAcc(msg.data, msg.invert);
138
+ break;
139
+ }
140
+ case 'gyr': {
141
+ this.setGyr(msg.data);
142
+ break;
143
+ }
144
+ default:
145
+ break;
146
+ }
147
+ });
148
+ }
149
+ }
150
+ }