@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.
- package/COPYING.txt +520 -0
- package/README.md +4 -4
- package/assets/standalone/create-node.js +78 -0
- package/assets/standalone/faust-ui/index.css +8 -0
- package/assets/standalone/faust-ui/index.css.map +1 -1
- package/assets/standalone/faust-ui/index.js +56 -30
- package/assets/standalone/faust-ui/index.js.map +1 -1
- package/assets/standalone/faustwasm/index.d.ts +18 -10
- package/assets/standalone/faustwasm/index.js +81 -56
- package/assets/standalone/faustwasm/index.js.map +2 -2
- package/assets/standalone/index-template.html +17 -0
- package/assets/standalone/index-template.js +48 -0
- package/assets/standalone/index.html +4 -3
- package/assets/standalone/index.js +14 -71
- package/assets/standalone/manifest.json +5 -6
- package/assets/standalone/service-worker.js +51 -27
- package/dist/cjs/index.d.ts +18 -10
- package/dist/cjs/index.js +81 -56
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs-bundle/index.d.ts +18 -10
- package/dist/cjs-bundle/index.js +81 -56
- package/dist/cjs-bundle/index.js.map +2 -2
- package/dist/esm/index.d.ts +18 -10
- package/dist/esm/index.js +81 -56
- package/dist/esm/index.js.map +2 -2
- package/dist/esm-bundle/index.d.ts +18 -10
- package/dist/esm-bundle/index.js +81 -56
- package/dist/esm-bundle/index.js.map +2 -2
- package/fileutils.js +19 -21
- package/package.json +4 -2
- package/scripts/faust2wasm.js +10 -5
- package/src/FaustAudioWorkletNode.ts +18 -38
- package/src/FaustAudioWorkletProcessor.ts +28 -1
- package/src/FaustDspGenerator.ts +18 -9
- package/src/FaustFFTAudioWorkletProcessor.ts +27 -0
- package/src/FaustScriptProcessorNode.ts +5 -14
- package/src/copyWebStandaloneAssets.d.ts +2 -2
- package/src/copyWebStandaloneAssets.js +30 -19
- package/src/faust2wasmFiles.js +5 -5
- package/test/faustlive-wasm/faust-ui/index.css +8 -0
- package/test/faustlive-wasm/faust-ui/index.css.map +1 -1
- package/test/faustlive-wasm/faust-ui/index.js +56 -30
- package/test/faustlive-wasm/faust-ui/index.js.map +1 -1
- package/test/faustlive-wasm/faustwasm/index.d.ts +18 -10
- package/test/faustlive-wasm/faustwasm/index.js +81 -56
- package/test/faustlive-wasm/faustwasm/index.js.map +2 -2
- package/assets/standalone/index-poly.js +0 -234
- package/assets/standalone/service-worker-poly.js +0 -98
- package/assets/standalone/template-poly.html +0 -60
- package/assets/standalone/template.html +0 -50
- package/assets/standalone/template.js +0 -63
- 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
|
-
}
|