@grame/faustwasm 0.4.6 → 0.4.8

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 (36) hide show
  1. package/README.md +4 -4
  2. package/assets/standalone/create-node.js +78 -0
  3. package/assets/standalone/faustwasm/index.js +12 -32
  4. package/assets/standalone/faustwasm/index.js.map +2 -2
  5. package/assets/standalone/index-template.html +17 -0
  6. package/assets/standalone/index-template.js +48 -0
  7. package/assets/standalone/index.html +4 -3
  8. package/assets/standalone/index.js +14 -71
  9. package/assets/standalone/manifest.json +5 -6
  10. package/assets/standalone/service-worker.js +51 -27
  11. package/dist/cjs/index.js +12 -32
  12. package/dist/cjs/index.js.map +2 -2
  13. package/dist/cjs-bundle/index.js +12 -32
  14. package/dist/cjs-bundle/index.js.map +2 -2
  15. package/dist/esm/index.js +12 -32
  16. package/dist/esm/index.js.map +2 -2
  17. package/dist/esm-bundle/index.js +12 -32
  18. package/dist/esm-bundle/index.js.map +2 -2
  19. package/fileutils.js +19 -21
  20. package/package.json +1 -1
  21. package/scripts/faust2wasm.js +10 -5
  22. package/src/FaustAudioWorkletNode.ts +5 -14
  23. package/src/FaustScriptProcessorNode.ts +5 -14
  24. package/src/copyWebStandaloneAssets.d.ts +2 -2
  25. package/src/copyWebStandaloneAssets.js +30 -19
  26. package/src/faust2wasmFiles.js +5 -5
  27. package/test/faustlive-wasm/faustwasm/index.js +12 -32
  28. package/test/faustlive-wasm/faustwasm/index.js.map +2 -2
  29. package/assets/standalone/index-poly.js +0 -234
  30. package/assets/standalone/service-worker-poly.js +0 -98
  31. package/assets/standalone/template-poly.html +0 -60
  32. package/assets/standalone/template.html +0 -50
  33. package/assets/standalone/template.js +0 -63
  34. package/assets/standalone/types.d.ts +0 -9
  35. package/node +0 -0
  36. package/src/instantiateFaustModule.js +0 -82
package/README.md CHANGED
@@ -49,7 +49,7 @@ For example:
49
49
  rm -rf test/out # make sure you are under the faustwasm directory.
50
50
  node scripts/faust2wasm.js test/mono.dsp test/out
51
51
  ```
52
- will create a set of files: `mono.js`, `mono.wasm`, `mono.json`, `mono.html` in the `out` folder.
52
+ will create a set of files: `index.js`, `dsp-module.wasm`, `dsp-meta.json`, `index.html` in the `out` folder. `-no-template` option will omit `index.js` and `index.html` files.
53
53
 
54
54
  Polyphonic instrument with:
55
55
 
@@ -57,7 +57,7 @@ Polyphonic instrument with:
57
57
  rm -rf test/out # make sure you are under the faustwasm directory.
58
58
  node scripts/faust2wasm.js test/poly.dsp test/out -poly
59
59
  ```
60
- will create a set of files: `poly.js`, `poly.wasm`, `poly.json`, `poly.html` (and possibly `poly_effect.wasm`, `poly_effect.json`) in the `out` folder.
60
+ will create a set of files: `index.js`, `dsp-module.wasm`, `dsp-meta.json`, `index.html` (and possibly `effect-module.wasm`, `effect-meta.json`) in the `out` folder.
61
61
 
62
62
 
63
63
  #### Creating a Progressive Web Application of a Faust DSP
@@ -67,7 +67,7 @@ You can create a standalone Progressive Web Application using the same command l
67
67
  ```bash
68
68
  node scripts/faust2wasm.js test/rev.dsp test/rev -standalone
69
69
  ```
70
- will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `rev.js`, `rev.wasm`, `rev.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `rev` folder.
70
+ will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `index.js`, `dsp-module.wasm`, `dsp-meta.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `rev` folder.
71
71
 
72
72
  The folder contains the necessary ressources to deploy the Faust application as a PWA on a server, to be installed and used offline. Note that audio files used by the `soundfile` primitives in the DSP code will have to be mannually added in the folder. To test, be sure to launch a local web server at the `test` folder level, then go inside `test\rev` folder.
73
73
 
@@ -76,7 +76,7 @@ A standalone polyphonic and MIDI standalone Progressive Web Application can be c
76
76
  ```bash
77
77
  node scripts/faust2wasm.js test/organ1.dsp test/organ1 -poly -standalone
78
78
  ```
79
- will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `organ1.js`, `organ1.wasm`, `organ1.json`, `organ1_effect.wasm`, `organ1_effect.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `organ1` folder.
79
+ will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `index.js`, `dsp-module.wasm`, `dsp-meta.json`, `effect-module.wasm`, `effect-meta.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `organ1` folder.
80
80
 
81
81
  To test, be sure to launch a local web server at the `test` folder level, then go inside `test\organ1` folder.
82
82
 
@@ -0,0 +1,78 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @typedef {{ dspModule: WebAssembly.Module; dspMeta: FaustDspMeta; effectModule?: WebAssembly.Module; effectMeta?: FaustDspMeta; mixerModule?: WebAssembly.Module }} FaustDspDistribution
5
+ * @typedef {import("./faustwasm").FaustDspMeta} FaustDspMeta
6
+ * @typedef {import("./faustwasm").FaustMonoAudioWorkletNode} FaustMonoAudioWorkletNode
7
+ * @typedef {import("./faustwasm").FaustPolyAudioWorkletNode} FaustPolyAudioWorkletNode
8
+ * @typedef {import("./faustwasm").FaustMonoScriptProcessorNode} FaustMonoScriptProcessorNode
9
+ * @typedef {import("./faustwasm").FaustPolyScriptProcessorNode} FaustPolyScriptProcessorNode
10
+ * @typedef {FaustMonoAudioWorkletNode | FaustPolyAudioWorkletNode | FaustMonoScriptProcessorNode | FaustPolyScriptProcessorNode} FaustNode
11
+ */
12
+
13
+ /**
14
+ * Creates a Faust audio node for use in the Web Audio API.
15
+ *
16
+ * @param {AudioContext} audioContext - The Web Audio API AudioContext to which the Faust audio node will be connected.
17
+ * @param {string} [dspName] - The name of the DSP to be loaded.
18
+ * @param {number} [voices] - The number of voices to be used for polyphonic DSPs.
19
+ * @param {boolean} [sp] - Whether to create a ScriptProcessorNode instead of an AudioWorkletNode.
20
+ * @returns {Promise<{ faustNode: FaustNode | null; dspMeta: FaustDspMeta }>} - An object containing the Faust audio node and the DSP metadata.
21
+ */
22
+ const createFaustNode = async (audioContext, dspName = "template", voices = 0, sp = false) => {
23
+ // Set to true if the DSP has an effect
24
+ const FAUST_DSP_HAS_EFFECT = false;
25
+
26
+ // Import necessary Faust modules and data
27
+ const { FaustMonoDspGenerator, FaustPolyDspGenerator } = await import("./faustwasm/index.js");
28
+
29
+ // Load DSP metadata from JSON
30
+ /** @type {FaustDspMeta} */
31
+ const dspMeta = await (await fetch("./dsp-meta.json")).json();
32
+
33
+ // Compile the DSP module from WebAssembly binary data
34
+ const dspModule = await WebAssembly.compileStreaming(await fetch("./dsp-module.wasm"));
35
+
36
+ // Create an object representing Faust DSP with metadata and module
37
+ /** @type {FaustDspDistribution} */
38
+ const faustDsp = { dspMeta, dspModule };
39
+
40
+ /** @type {FaustNode | null} */
41
+ let faustNode = null;
42
+
43
+ // Create either a polyphonic or monophonic Faust audio node based on the number of voices
44
+ if (voices > 0) {
45
+
46
+ // Try to load optional mixer and effect modules
47
+ faustDsp.mixerModule = await WebAssembly.compileStreaming(await fetch("./mixer-module.wasm"));
48
+
49
+ if (FAUST_DSP_HAS_EFFECT) {
50
+ faustDsp.effectMeta = await (await fetch("./effect-meta.json")).json();
51
+ faustDsp.effectModule = await WebAssembly.compileStreaming(await fetch("./effect-module.wasm"));
52
+ }
53
+
54
+ const generator = new FaustPolyDspGenerator();
55
+ faustNode = await generator.createNode(
56
+ audioContext,
57
+ voices,
58
+ dspName,
59
+ { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta), soundfiles: {} },
60
+ faustDsp.mixerModule,
61
+ faustDsp.effectModule ? { module: faustDsp.effectModule, json: JSON.stringify(faustDsp.effectMeta), soundfiles: {} } : undefined,
62
+ sp
63
+ );
64
+ } else {
65
+ const generator = new FaustMonoDspGenerator();
66
+ faustNode = await generator.createNode(
67
+ audioContext,
68
+ dspName,
69
+ { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta), soundfiles: {} },
70
+ sp
71
+ );
72
+ }
73
+
74
+ // Return an object with the Faust audio node and the DSP metadata
75
+ return { faustNode, dspMeta };
76
+ }
77
+
78
+ export default createFaustNode;
@@ -3719,22 +3719,12 @@ var FaustAudioWorkletNode = class extends (globalThis.AudioWorkletNode || null)
3719
3719
  async listenSensors() {
3720
3720
  if (this.hasAccInput) {
3721
3721
  const isAndroid = /Android/i.test(navigator.userAgent);
3722
- let handleDeviceMotion = null;
3723
- if (isAndroid) {
3724
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3725
- if (!accelerationIncludingGravity)
3726
- return;
3727
- const { x, y, z } = accelerationIncludingGravity;
3728
- this.propagateAcc({ x, y, z }, true);
3729
- };
3730
- } else {
3731
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3732
- if (!accelerationIncludingGravity)
3733
- return;
3734
- const { x, y, z } = accelerationIncludingGravity;
3735
- this.propagateAcc({ x, y, z });
3736
- };
3737
- }
3722
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3723
+ if (!accelerationIncludingGravity)
3724
+ return;
3725
+ const { x, y, z } = accelerationIncludingGravity;
3726
+ this.propagateAcc({ x, y, z }, isAndroid);
3727
+ };
3738
3728
  if (window.DeviceMotionEvent) {
3739
3729
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
3740
3730
  try {
@@ -3983,22 +3973,12 @@ var FaustScriptProcessorNode = class extends (globalThis.ScriptProcessorNode ||
3983
3973
  async listenSensors() {
3984
3974
  if (this.hasAccInput) {
3985
3975
  const isAndroid = /Android/i.test(navigator.userAgent);
3986
- let handleDeviceMotion = null;
3987
- if (isAndroid) {
3988
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3989
- if (!accelerationIncludingGravity)
3990
- return;
3991
- const { x, y, z } = accelerationIncludingGravity;
3992
- this.propagateAcc({ x, y, z }, true);
3993
- };
3994
- } else {
3995
- handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3996
- if (!accelerationIncludingGravity)
3997
- return;
3998
- const { x, y, z } = accelerationIncludingGravity;
3999
- this.propagateAcc({ x, y, z });
4000
- };
4001
- }
3976
+ const handleDeviceMotion = ({ accelerationIncludingGravity }) => {
3977
+ if (!accelerationIncludingGravity)
3978
+ return;
3979
+ const { x, y, z } = accelerationIncludingGravity;
3980
+ this.propagateAcc({ x, y, z }, isAndroid);
3981
+ };
4002
3982
  if (window.DeviceMotionEvent) {
4003
3983
  if (typeof window.DeviceMotionEvent.requestPermission === "function") {
4004
3984
  try {