@grame/faustwasm 0.4.5 → 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 (52) hide show
  1. package/COPYING.txt +520 -0
  2. package/README.md +4 -4
  3. package/assets/standalone/create-node.js +78 -0
  4. package/assets/standalone/faust-ui/index.css +8 -0
  5. package/assets/standalone/faust-ui/index.css.map +1 -1
  6. package/assets/standalone/faust-ui/index.js +56 -30
  7. package/assets/standalone/faust-ui/index.js.map +1 -1
  8. package/assets/standalone/faustwasm/index.d.ts +18 -10
  9. package/assets/standalone/faustwasm/index.js +81 -56
  10. package/assets/standalone/faustwasm/index.js.map +2 -2
  11. package/assets/standalone/index-template.html +17 -0
  12. package/assets/standalone/index-template.js +48 -0
  13. package/assets/standalone/index.html +4 -3
  14. package/assets/standalone/index.js +14 -71
  15. package/assets/standalone/manifest.json +5 -6
  16. package/assets/standalone/service-worker.js +51 -27
  17. package/dist/cjs/index.d.ts +18 -10
  18. package/dist/cjs/index.js +81 -56
  19. package/dist/cjs/index.js.map +2 -2
  20. package/dist/cjs-bundle/index.d.ts +18 -10
  21. package/dist/cjs-bundle/index.js +81 -56
  22. package/dist/cjs-bundle/index.js.map +2 -2
  23. package/dist/esm/index.d.ts +18 -10
  24. package/dist/esm/index.js +81 -56
  25. package/dist/esm/index.js.map +2 -2
  26. package/dist/esm-bundle/index.d.ts +18 -10
  27. package/dist/esm-bundle/index.js +81 -56
  28. package/dist/esm-bundle/index.js.map +2 -2
  29. package/fileutils.js +19 -21
  30. package/package.json +4 -2
  31. package/scripts/faust2wasm.js +10 -5
  32. package/src/FaustAudioWorkletNode.ts +18 -38
  33. package/src/FaustAudioWorkletProcessor.ts +28 -1
  34. package/src/FaustDspGenerator.ts +18 -9
  35. package/src/FaustFFTAudioWorkletProcessor.ts +27 -0
  36. package/src/FaustScriptProcessorNode.ts +5 -14
  37. package/src/copyWebStandaloneAssets.d.ts +2 -2
  38. package/src/copyWebStandaloneAssets.js +30 -19
  39. package/src/faust2wasmFiles.js +5 -5
  40. package/test/faustlive-wasm/faust-ui/index.css +8 -0
  41. package/test/faustlive-wasm/faust-ui/index.css.map +1 -1
  42. package/test/faustlive-wasm/faust-ui/index.js +56 -30
  43. package/test/faustlive-wasm/faust-ui/index.js.map +1 -1
  44. package/test/faustlive-wasm/faustwasm/index.d.ts +18 -10
  45. package/test/faustlive-wasm/faustwasm/index.js +81 -56
  46. package/test/faustlive-wasm/faustwasm/index.js.map +2 -2
  47. package/assets/standalone/index-poly.js +0 -234
  48. package/assets/standalone/service-worker-poly.js +0 -98
  49. package/assets/standalone/template-poly.html +0 -60
  50. package/assets/standalone/template.html +0 -50
  51. package/assets/standalone/template.js +0 -63
  52. package/assets/standalone/types.d.ts +0 -9
@@ -1,234 +0,0 @@
1
- /**
2
- * @typedef {import("./types").FaustDspDistribution} FaustDspDistribution
3
- * @typedef {import("./faustwasm").FaustAudioWorkletNode} FaustAudioWorkletNode
4
- * @typedef {import("./faustwasm").FaustDspMeta} FaustDspMeta
5
- * @typedef {import("./faustwasm").FaustUIDescriptor} FaustUIDescriptor
6
- * @typedef {import("./faustwasm").FaustUIGroup} FaustUIGroup
7
- * @typedef {import("./faustwasm").FaustUIItem} FaustUIItem
8
- */
9
-
10
- /**
11
- * Registers the service worker.
12
- */
13
- if ('serviceWorker' in navigator) {
14
- window.addEventListener('load', () => {
15
- navigator.serviceWorker.register('./service-worker.js')
16
- .then(reg => console.log('Service Worker registered', reg))
17
- .catch(err => console.log('Service Worker registration failed', err));
18
- });
19
- }
20
-
21
- /** @type {HTMLSpanElement} */
22
- const $spanAudioInput = document.getElementById("audio-input");
23
- /** @type {HTMLSpanElement} */
24
- const $spanMidiInput = document.getElementById("midi-input");
25
- /** @type {HTMLSelectElement} */
26
- const $selectAudioInput = document.getElementById("select-audio-input");
27
- /** @type {HTMLSelectElement} */
28
- const $selectMidiInput = document.getElementById("select-midi-input");
29
- /** @type {HTMLSelectElement} */
30
- const $buttonDsp = document.getElementById("button-dsp");
31
- /** @type {HTMLDivElement} */
32
- const $divFaustUI = document.getElementById("div-faust-ui");
33
-
34
- /** @type {typeof AudioContext} */
35
- const AudioCtx = window.AudioContext || window.webkitAudioContext;
36
- const audioContext = new AudioCtx({ latencyHint: 0.00001, echoCancellation: false, autoGainControl: false, noiseSuppression: false });
37
- audioContext.destination.channelInterpretation = "discrete";
38
- audioContext.suspend();
39
- $buttonDsp.disabled = true;
40
-
41
- /**
42
- * @param {FaustAudioWorkletNode} faustNode
43
- */
44
- const buildAudioDeviceMenu = async (faustNode) => {
45
- /** @type {MediaStreamAudioSourceNode} */
46
- let inputStreamNode;
47
- const handleDeviceChange = async () => {
48
- const devicesInfo = await navigator.mediaDevices.enumerateDevices();
49
- $selectAudioInput.innerHTML = '';
50
- devicesInfo.forEach((deviceInfo, i) => {
51
- const { kind, deviceId, label } = deviceInfo;
52
- if (kind === "audioinput") {
53
- const option = new Option(label || `microphone ${i + 1}`, deviceId);
54
- $selectAudioInput.add(option);
55
- }
56
- });
57
- }
58
- await handleDeviceChange();
59
- navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange)
60
- $selectAudioInput.onchange = async () => {
61
- const id = $selectAudioInput.value;
62
- const constraints = {
63
- audio: {
64
- echoCancellation: false,
65
- noiseSuppression: false,
66
- autoGainControl: false,
67
- deviceId: id ? { exact: id } : undefined,
68
- },
69
- };
70
- const stream = await navigator.mediaDevices.getUserMedia(constraints);
71
- if (inputStreamNode) inputStreamNode.disconnect();
72
- inputStreamNode = audioContext.createMediaStreamSource(stream);
73
- inputStreamNode.connect(faustNode);
74
- };
75
-
76
- const defaultConstraints = {
77
- audio: {
78
- echoCancellation: false,
79
- mozNoiseSuppression: false,
80
- mozAutoGainControl: false
81
- }
82
- };
83
- const defaultStream = await navigator.mediaDevices.getUserMedia(defaultConstraints);
84
- if (defaultStream) {
85
- inputStreamNode = audioContext.createMediaStreamSource(defaultStream);
86
- inputStreamNode.connect(faustNode);
87
- }
88
- };
89
-
90
- /**
91
- * @param {FaustAudioWorkletNode} faustNode
92
- */
93
- const buildMidiDeviceMenu = async (faustNode) => {
94
- const midiAccess = await navigator.requestMIDIAccess();
95
- /** @type {WebMidi.MIDIInput} */
96
- let currentInput;
97
- /**
98
- * @param {WebMidi.MIDIMessageEvent} e
99
- */
100
- const handleMidiMessage = e => faustNode.midiMessage(e.data);
101
- const handleStateChange = () => {
102
- const { inputs } = midiAccess;
103
- if ($selectMidiInput.options.length === inputs.size + 1) return;
104
- if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
105
- $selectMidiInput.innerHTML = '<option value="-1" disabled selected>Select...</option>';
106
- inputs.forEach((midiInput) => {
107
- const { name, id } = midiInput;
108
- const option = new Option(name, id);
109
- $selectMidiInput.add(option);
110
- });
111
- };
112
- handleStateChange();
113
- midiAccess.addEventListener("statechange", handleStateChange);
114
- $selectMidiInput.onchange = () => {
115
- if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
116
- const id = $selectMidiInput.value;
117
- currentInput = midiAccess.inputs.get(id);
118
- currentInput.addEventListener("midimessage", handleMidiMessage);
119
- };
120
- };
121
-
122
- /**
123
- * Creates a Faust audio node for use in the Web Audio API.
124
- *
125
- * @param {AudioContext} audioContext - The Web Audio API AudioContext to which the Faust audio node will be connected.
126
- * @param {string} dspName - The name of the DSP to be loaded.
127
- * @param {number} voices - The number of voices to be used for polyphonic DSPs.
128
- * @param {boolean} sp - Whether to create a ScriptProcessorNode instead of an AudioWorkletNode.
129
- * @returns {Object} - An object containing the Faust audio node and the DSP metadata.
130
- */
131
- const createFaustNode = async (audioContext, dspName = "template", voices = 0, sp = false) => {
132
- // Import necessary Faust modules and data
133
- const { FaustMonoDspGenerator, FaustPolyDspGenerator } = await import("./faustwasm/index.js");
134
-
135
- // Load DSP metadata from JSON
136
- /** @type {FaustDspMeta} */
137
- const dspMeta = await (await fetch(`./${dspName}.json`)).json();
138
-
139
- // Compile the DSP module from WebAssembly binary data
140
- const dspModule = await WebAssembly.compileStreaming(await fetch(`./${dspName}.wasm`));
141
-
142
- // Create an object representing Faust DSP with metadata and module
143
- /** @type {FaustDspDistribution} */
144
- const faustDsp = { dspMeta, dspModule };
145
-
146
- /** @type {FaustAudioWorkletNode} */
147
- let faustNode;
148
-
149
- // Create either a polyphonic or monophonic Faust audio node based on the number of voices
150
- if (voices > 0) {
151
-
152
- // Try to load optional mixer and effect modules
153
- try {
154
- faustDsp.mixerModule = await WebAssembly.compileStreaming(await fetch("./mixerModule.wasm"));
155
- faustDsp.effectMeta = await (await fetch(`./${dspName}_effect.json`)).json();
156
- faustDsp.effectModule = await WebAssembly.compileStreaming(await fetch(`./${dspName}_effect.wasm`));
157
- } catch (e) { }
158
-
159
- const generator = new FaustPolyDspGenerator();
160
- faustNode = await generator.createNode(
161
- audioContext,
162
- voices,
163
- "FaustPolyDSP",
164
- { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta) },
165
- faustDsp.mixerModule,
166
- faustDsp.effectModule ? { module: faustDsp.effectModule, json: JSON.stringify(faustDsp.effectMeta) } : undefined,
167
- sp
168
- );
169
- } else {
170
- const generator = new FaustMonoDspGenerator();
171
- faustNode = await generator.createNode(
172
- audioContext,
173
- "FaustMonoDSP",
174
- { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta) },
175
- sp
176
- );
177
- }
178
-
179
- // Return an object with the Faust audio node and the DSP metadata
180
- return { faustNode, dspMeta };
181
- }
182
-
183
- /**
184
- * @param {FaustAudioWorkletNode} faustNode
185
- */
186
- const createFaustUI = async (faustNode) => {
187
- const { FaustUI } = await import("./faust-ui/index.js");
188
- const $container = document.createElement("div");
189
- $container.style.margin = "0";
190
- $container.style.position = "absolute";
191
- $container.style.overflow = "auto";
192
- $container.style.display = "flex";
193
- $container.style.flexDirection = "column";
194
- $container.style.width = "100%";
195
- $container.style.height = "100%";
196
- $divFaustUI.appendChild($container);
197
- const faustUI = new FaustUI({
198
- ui: faustNode.getUI(),
199
- root: $container,
200
- listenWindowMessage: false,
201
- listenWindowResize: true,
202
- });
203
- faustUI.paramChangeByUI = (path, value) => faustNode.setParamValue(path, value);
204
- faustNode.setOutputParamHandler((path, value) => faustUI.paramChangeByDSP(path, value));
205
- $container.style.minWidth = `${faustUI.minWidth}px`;
206
- $container.style.minHeight = `${faustUI.minHeight}px`;
207
- faustUI.resize();
208
- };
209
-
210
- (async () => {
211
- const { faustNode, dspMeta: { name } } = await createFaustNode(audioContext, "FAUST_DSP", 16);
212
- await createFaustUI(faustNode);
213
- faustNode.connect(audioContext.destination);
214
- if (faustNode.numberOfInputs) await buildAudioDeviceMenu(faustNode);
215
- else $spanAudioInput.hidden = true;
216
- if (navigator.requestMIDIAccess) await buildMidiDeviceMenu(faustNode);
217
- else $spanMidiInput.hidden = true;
218
- $buttonDsp.disabled = false;
219
- document.title = name;
220
- let motionHandlersBound = false;
221
- $buttonDsp.onclick = async () => {
222
- if (!motionHandlersBound) {
223
- await faustNode.listenSensors();
224
- motionHandlersBound = true;
225
- }
226
- if (audioContext.state === "running") {
227
- $buttonDsp.textContent = "Suspended";
228
- audioContext.suspend();
229
- } else if (audioContext.state === "suspended") {
230
- $buttonDsp.textContent = "Running";
231
- audioContext.resume();
232
- }
233
- }
234
- })();
@@ -1,98 +0,0 @@
1
-
2
- const CACHE_NAME = 'FAUST_DSP-static'; // Cache name without versioning
3
-
4
- const POLY_EFFECT_RESOURCES = [
5
- '/FAUST_DSP/',
6
- '/FAUST_DSP/faust-ui/index.js',
7
- '/FAUST_DSP/faust-ui/index.css',
8
- '/FAUST_DSP/faustwasm/index.js',
9
- '/FAUST_DSP/FAUST_DSP.js',
10
- '/FAUST_DSP/FAUST_DSP.wasm',
11
- '/FAUST_DSP/FAUST_DSP.json',
12
- '/FAUST_DSP/mixerModule.wasm',
13
- '/FAUST_DSP/FAUST_DSP_effect.wasm',
14
- '/FAUST_DSP/FAUST_DSP_effect.json',
15
- ];
16
-
17
- const POLY_RESOURCES = [
18
- '/FAUST_DSP/',
19
- '/FAUST_DSP/faust-ui/index.js',
20
- '/FAUST_DSP/faust-ui/index.css',
21
- '/FAUST_DSP/faustwasm/index.js',
22
- '/FAUST_DSP/FAUST_DSP.js',
23
- '/FAUST_DSP/FAUST_DSP.wasm',
24
- '/FAUST_DSP/FAUST_DSP.json',
25
- '/FAUST_DSP/mixerModule.wasm',
26
- ];
27
-
28
- /**
29
- * Install the service worker and cache the resources
30
-
31
- */
32
- self.addEventListener('install', event => {
33
- event.waitUntil(
34
- caches.open(CACHE_NAME).then(cache => {
35
- console.log("Service worker installed");
36
- const urlToCheck = '/FAUST_DSP/FAUST_DSP_effect.json';
37
- return fetchResourceAndCache(cache, urlToCheck);
38
- })
39
- );
40
- });
41
-
42
- /**
43
- * Fetch the resources and cache them
44
- *
45
- * @param {Cache} cache The cache to store the resources
46
- * @param {string} urlToCheck The URL of the resource to fetch
47
- *
48
- * @returns {Promise<void>} A promise that resolves when the resource is fetched and cached
49
- */
50
-
51
- function fetchResourceAndCache(cache, urlToCheck) {
52
- return fetch(urlToCheck).then(response => {
53
- if (response.ok) {
54
- return cacheResources(cache, POLY_EFFECT_RESOURCES);
55
- } else {
56
- return cacheResources(cache, POLY_RESOURCES);
57
- }
58
- }).catch(fetchError => {
59
- console.error(`Failed to fetch ${urlToCheck}:`, fetchError);
60
- });
61
- }
62
- /**
63
- *
64
- * @param {*} cache The cache to store the resources
65
- * @param {*} resources The resources to cache
66
- * @returns
67
- */
68
- function cacheResources(cache, resources) {
69
- return cache.addAll(resources).catch(error => {
70
- console.error('Failed to cache resources:', error);
71
- });
72
- }
73
-
74
- self.addEventListener("activate", event => {
75
- console.log("Service worker activated");
76
- });
77
-
78
- self.addEventListener('fetch', event => {
79
- event.respondWith((async () => {
80
- const cache = await caches.open(CACHE_NAME);
81
- const cachedResponse = await cache.match(event.request);
82
- if (cachedResponse) {
83
- return cachedResponse;
84
- } else {
85
- try {
86
- const fetchResponse = await fetch(event.request);
87
- // Ensure the response is valid before caching it
88
- if (event.request.method === 'GET' && fetchResponse && fetchResponse.status === 200 && fetchResponse.type === 'basic') {
89
- cache.put(event.request, fetchResponse.clone());
90
- }
91
- return fetchResponse;
92
- } catch (e) {
93
- // Network access failure
94
- console.log('Network access error', e);
95
- }
96
- }
97
- })());
98
- });
@@ -1,60 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
-
4
- <head>
5
- <title>Faust DSP</title>
6
- </head>
7
-
8
- <body>
9
- <main>
10
- <span id="dsp-switch">
11
- <center><button id="button-dsp">Activate DSP</button></center>
12
- </span>
13
- </main>
14
- <script src="./FAUST_DSP.js"></script>
15
- <script>
16
- (async () => {
17
-
18
- // Create audio context
19
- const AudioCtx = window.AudioContext || window.webkitAudioContext;
20
- const audioContext = new AudioCtx();
21
- audioContext.suspend();
22
-
23
- // Create audio context activation button
24
- const $buttonDsp = document.getElementById("button-dsp");
25
-
26
- // Play some notes in loop
27
- function play(node) {
28
- node.keyOn(0, 60, 100);
29
- setTimeout(() => node.keyOn(0, 64, 100), 1000);
30
- setTimeout(() => node.keyOn(0, 67, 100), 2000);
31
- setTimeout(() => node.allNotesOff(), 5000);
32
- setTimeout(() => play(node), 7000);
33
- }
34
-
35
- // Activate audio context
36
- $buttonDsp.disabled = true;
37
- $buttonDsp.onclick = () => {
38
- if (audioContext.state === "running") {
39
- $buttonDsp.textContent = "Suspended";
40
- audioContext.suspend();
41
- } else if (audioContext.state === "suspended") {
42
- $buttonDsp.textContent = "Running";
43
- audioContext.resume();
44
- play(faustNode);
45
- }
46
- }
47
-
48
- // Create Faust node
49
- const { faustNode, dspMeta: { name } } = await createFaustNode(audioContext, "FAUST_DSP", 16);
50
-
51
- // Connect Faust node to audio context
52
- faustNode.connect(audioContext.destination);
53
-
54
- // Create Faust node activation button
55
- $buttonDsp.disabled = false;
56
- })();
57
- </script>
58
- </body>
59
-
60
- </html>
@@ -1,50 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
-
4
- <head>
5
- <title>Faust DSP</title>
6
- </head>
7
-
8
- <body>
9
- <main>
10
- <span id="dsp-switch">
11
- <center><button id="button-dsp">Activate DSP</button></center>
12
- </span>
13
- </main>
14
- <script src="./FAUST_DSP.js"></script>
15
- <script>
16
- (async () => {
17
-
18
- // Create audio context
19
- const AudioCtx = window.AudioContext || window.webkitAudioContext;
20
- const audioContext = new AudioCtx();
21
- audioContext.suspend();
22
-
23
- // Create audio context activation button
24
- const $buttonDsp = document.getElementById("button-dsp");
25
-
26
- // Function to activate audio context
27
- $buttonDsp.disabled = true;
28
- $buttonDsp.onclick = () => {
29
- if (audioContext.state === "running") {
30
- $buttonDsp.textContent = "Suspended";
31
- audioContext.suspend();
32
- } else if (audioContext.state === "suspended") {
33
- $buttonDsp.textContent = "Running";
34
- audioContext.resume();
35
- }
36
- }
37
-
38
- // Create Faust node
39
- const { faustNode, dspMeta: { name } } = await createFaustNode(audioContext, "FAUST_DSP");
40
-
41
- // Connect Faust node to audio context
42
- faustNode.connect(audioContext.destination);
43
-
44
- // Create Faust node activation button
45
- $buttonDsp.disabled = false;
46
- })();
47
- </script>
48
- </body>
49
-
50
- </html>
@@ -1,63 +0,0 @@
1
- /**
2
- * @typedef {import("./types").FaustDspDistribution} FaustDspDistribution
3
- * @typedef {import("./faustwasm").FaustAudioWorkletNode} FaustAudioWorkletNode
4
- * @typedef {import("./faustwasm").FaustDspMeta} FaustDspMeta
5
- */
6
-
7
- /**
8
- * Creates a Faust audio node for use in the Web Audio API.
9
- *
10
- * @param {AudioContext} audioContext - The Web Audio API AudioContext to which the Faust audio node will be connected.
11
- * @param {string} dspName - The name of the DSP to be loaded.
12
- * @param {number} voices - The number of voices to be used for polyphonic DSPs.
13
- * @returns {Object} - An object containing the Faust audio node and the DSP metadata.
14
- */
15
- const createFaustNode = async (audioContext, dspName = "template", voices = 0) => {
16
- // Import necessary Faust modules and data
17
- const { FaustMonoDspGenerator, FaustPolyDspGenerator } = await import("./faustwasm/index.js");
18
-
19
- // Load DSP metadata from JSON
20
- /** @type {FaustDspMeta} */
21
- const dspMeta = await (await fetch(`./${dspName}.json`)).json();
22
-
23
- // Compile the DSP module from WebAssembly binary data
24
- const dspModule = await WebAssembly.compileStreaming(await fetch(`./${dspName}.wasm`));
25
-
26
- // Create an object representing Faust DSP with metadata and module
27
- /** @type {FaustDspDistribution} */
28
- const faustDsp = { dspMeta, dspModule };
29
-
30
- /** @type {FaustAudioWorkletNode} */
31
- let faustNode;
32
-
33
- // Create either a polyphonic or monophonic Faust audio node based on the number of voices
34
- if (voices > 0) {
35
-
36
- // Try to load optional mixer and effect modules
37
- try {
38
- faustDsp.mixerModule = await WebAssembly.compileStreaming(await fetch("./mixerModule.wasm"));
39
- faustDsp.effectMeta = await (await fetch(`./${dspName}_effect.json`)).json();
40
- faustDsp.effectModule = await WebAssembly.compileStreaming(await fetch(`./${dspName}_effect.wasm`));
41
- } catch (e) { }
42
-
43
- const generator = new FaustPolyDspGenerator();
44
- faustNode = await generator.createNode(
45
- audioContext,
46
- voices,
47
- "FaustPolyDSP",
48
- { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta) },
49
- faustDsp.mixerModule,
50
- faustDsp.effectModule ? { module: faustDsp.effectModule, json: JSON.stringify(faustDsp.effectMeta) } : undefined
51
- );
52
- } else {
53
- const generator = new FaustMonoDspGenerator();
54
- faustNode = await generator.createNode(
55
- audioContext,
56
- "FaustMonoDSP",
57
- { module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta) }
58
- );
59
- }
60
-
61
- // Return an object with the Faust audio node and the DSP metadata
62
- return { faustNode, dspMeta };
63
- }
@@ -1,9 +0,0 @@
1
- import { FaustDspMeta } from "./faustwasm";
2
-
3
- export interface FaustDspDistribution {
4
- dspModule: WebAssembly.Module;
5
- dspMeta: FaustDspMeta;
6
- effectModule?: WebAssembly.Module;
7
- effectMeta?: FaustDspMeta;
8
- mixerModule?: WebAssembly.Module;
9
- }