@grame/faustwasm 0.9.6 → 0.10.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/assets/standalone/create-node.js +76 -1
- package/assets/standalone/index copie.js +171 -0
- package/assets/standalone/index-pwa.js +18 -0
- package/assets/standalone/index.js +38 -5
- package/package.json +1 -1
- package/test/organ/create-node.js +252 -0
- package/test/organ/dsp-meta.json +131 -0
- package/test/organ/dsp-module.wasm +0 -0
- package/test/organ/faust-ui/index.css +442 -0
- package/test/organ/faust-ui/index.css.map +1 -0
- package/test/organ/faust-ui/index.d.ts +1 -0
- package/test/organ/faust-ui/index.js +3729 -0
- package/test/organ/faust-ui/index.js.map +1 -0
- package/test/organ/faustwasm/index.d.ts +1695 -0
- package/test/organ/faustwasm/index.js +4888 -0
- package/test/organ/faustwasm/index.js.map +7 -0
- package/test/organ/icon.png +0 -0
- package/test/organ/index.html +76 -0
- package/test/organ/index.js +201 -0
- package/test/organ/manifest.json +17 -0
- package/test/organ/mixer-module.wasm +0 -0
- package/test/organ/service-worker.js +165 -0
|
@@ -172,6 +172,81 @@ async function requestPermissions() {
|
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Key2Midi: maps keyboard input to MIDI messages.
|
|
177
|
+
*/
|
|
178
|
+
class Key2Midi {
|
|
179
|
+
static KEY_MAP = {
|
|
180
|
+
a: 0, w: 1, s: 2, e: 3, d: 4, f: 5, t: 6, g: 7,
|
|
181
|
+
y: 8, h: 9, u: 10, j: 11, k: 12, o: 13, l: 14, p: 15, ";": 16,
|
|
182
|
+
z: "PREV", x: "NEXT", c: "VELDOWN", v: "VELUP"
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
constructor({ keyMap = Key2Midi.KEY_MAP, offset = 60, velocity = 100, handler = console.log } = {}) {
|
|
186
|
+
this.keyMap = keyMap;
|
|
187
|
+
this.offset = offset;
|
|
188
|
+
this.velocity = velocity;
|
|
189
|
+
this.velMap = [20, 40, 60, 80, 100, 127];
|
|
190
|
+
this.handler = handler;
|
|
191
|
+
this.pressed = {};
|
|
192
|
+
|
|
193
|
+
this.onKeyDown = this.onKeyDown.bind(this);
|
|
194
|
+
this.onKeyUp = this.onKeyUp.bind(this);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
start() {
|
|
198
|
+
window.addEventListener("keydown", this.onKeyDown);
|
|
199
|
+
window.addEventListener("keyup", this.onKeyUp);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
stop() {
|
|
203
|
+
window.removeEventListener("keydown", this.onKeyDown);
|
|
204
|
+
window.removeEventListener("keyup", this.onKeyUp);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
onKeyDown(e) {
|
|
208
|
+
const key = e.key.toLowerCase();
|
|
209
|
+
if (this.pressed[key]) return;
|
|
210
|
+
this.pressed[key] = true;
|
|
211
|
+
|
|
212
|
+
const val = this.keyMap[key];
|
|
213
|
+
if (typeof val === "number") {
|
|
214
|
+
const note = val + this.offset;
|
|
215
|
+
this.handler([0x90, note, this.velocity]);
|
|
216
|
+
} else if (val === "PREV") {
|
|
217
|
+
this.offset -= 1;
|
|
218
|
+
} else if (val === "NEXT") {
|
|
219
|
+
this.offset += 1;
|
|
220
|
+
} else if (val === "VELDOWN") {
|
|
221
|
+
const idx = Math.max(0, this.velMap.indexOf(this.velocity) - 1);
|
|
222
|
+
this.velocity = this.velMap[idx];
|
|
223
|
+
} else if (val === "VELUP") {
|
|
224
|
+
const idx = Math.min(this.velMap.length - 1, this.velMap.indexOf(this.velocity) + 1);
|
|
225
|
+
this.velocity = this.velMap[idx];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
onKeyUp(e) {
|
|
230
|
+
const key = e.key.toLowerCase();
|
|
231
|
+
const val = this.keyMap[key];
|
|
232
|
+
if (typeof val === "number") {
|
|
233
|
+
const note = val + this.offset;
|
|
234
|
+
this.handler([0x80, note, this.velocity]);
|
|
235
|
+
}
|
|
236
|
+
delete this.pressed[key];
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Creates a Key2Midi instance.
|
|
242
|
+
*
|
|
243
|
+
* @param {function} handler - The function to handle MIDI messages.
|
|
244
|
+
* @returns {Key2Midi} - The Key2Midi instance.
|
|
245
|
+
*/
|
|
246
|
+
function createKey2MIDI(handler) {
|
|
247
|
+
return new Key2Midi({ handler: handler });
|
|
248
|
+
}
|
|
249
|
+
|
|
175
250
|
// Export the functions
|
|
176
|
-
export { createFaustNode, createFaustUI, connectToAudioInput, requestPermissions };
|
|
251
|
+
export { createFaustNode, createFaustUI, createKey2MIDI, connectToAudioInput, requestPermissions };
|
|
177
252
|
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Set to > 0 if the DSP is polyphonic
|
|
2
|
+
const FAUST_DSP_VOICES = 0;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import("./faustwasm").FaustAudioWorkletNode} FaustAudioWorkletNode
|
|
6
|
+
* @typedef {import("./faustwasm").FaustDspMeta} FaustDspMeta
|
|
7
|
+
* @typedef {import("./faustwasm").FaustUIDescriptor} FaustUIDescriptor
|
|
8
|
+
* @typedef {import("./faustwasm").FaustUIGroup} FaustUIGroup
|
|
9
|
+
* @typedef {import("./faustwasm").FaustUIItem} FaustUIItem
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Registers the service worker.
|
|
14
|
+
*/
|
|
15
|
+
if ("serviceWorker" in navigator) {
|
|
16
|
+
window.addEventListener("load", () => {
|
|
17
|
+
navigator.serviceWorker.register("./service-worker.js")
|
|
18
|
+
.then(reg => console.log("Service Worker registered", reg))
|
|
19
|
+
.catch(err => console.log("Service Worker registration failed", err));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @type {HTMLSpanElement} */
|
|
24
|
+
const $spanAudioInput = document.getElementById("audio-input");
|
|
25
|
+
/** @type {HTMLSpanElement} */
|
|
26
|
+
const $spanMidiInput = document.getElementById("midi-input");
|
|
27
|
+
/** @type {HTMLSelectElement} */
|
|
28
|
+
const $selectAudioInput = document.getElementById("select-audio-input");
|
|
29
|
+
/** @type {HTMLSelectElement} */
|
|
30
|
+
const $selectMidiInput = document.getElementById("select-midi-input");
|
|
31
|
+
/** @type {HTMLSelectElement} */
|
|
32
|
+
const $buttonDsp = document.getElementById("button-dsp");
|
|
33
|
+
/** @type {HTMLDivElement} */
|
|
34
|
+
const $divFaustUI = document.getElementById("div-faust-ui");
|
|
35
|
+
|
|
36
|
+
/** @type {typeof AudioContext} */
|
|
37
|
+
const AudioCtx = window.AudioContext || window.webkitAudioContext;
|
|
38
|
+
const audioContext = new AudioCtx({ latencyHint: 0.00001 });
|
|
39
|
+
audioContext.destination.channelInterpretation = "discrete";
|
|
40
|
+
audioContext.suspend();
|
|
41
|
+
|
|
42
|
+
$buttonDsp.disabled = true;
|
|
43
|
+
|
|
44
|
+
// Declare faustNode as a global variable
|
|
45
|
+
let faustNode;
|
|
46
|
+
|
|
47
|
+
// Called at load time
|
|
48
|
+
(async () => {
|
|
49
|
+
|
|
50
|
+
const { createFaustNode, createKey2MIDI, connectToAudioInput, createFaustUI } = await import("./create-node.js");
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {FaustAudioWorkletNode} faustNode
|
|
54
|
+
*/
|
|
55
|
+
const buildAudioDeviceMenu = async (faustNode) => {
|
|
56
|
+
|
|
57
|
+
let inputStreamNode = null;
|
|
58
|
+
const handleDeviceChange = async () => {
|
|
59
|
+
const devicesInfo = await navigator.mediaDevices.enumerateDevices();
|
|
60
|
+
$selectAudioInput.innerHTML = "";
|
|
61
|
+
devicesInfo.forEach((deviceInfo, i) => {
|
|
62
|
+
const { kind, deviceId, label } = deviceInfo;
|
|
63
|
+
if (kind === "audioinput") {
|
|
64
|
+
const option = new Option(label || `microphone ${i + 1}`, deviceId);
|
|
65
|
+
$selectAudioInput.add(option);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
await handleDeviceChange();
|
|
70
|
+
navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange)
|
|
71
|
+
$selectAudioInput.onchange = async () => {
|
|
72
|
+
const id = $selectAudioInput.value;
|
|
73
|
+
if (faustNode.getNumInputs() > 0) {
|
|
74
|
+
inputStreamNode = await connectToAudioInput(audioContext, id, faustNode, inputStreamNode);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Connect to the default audio input device
|
|
79
|
+
if (faustNode.getNumInputs() > 0) {
|
|
80
|
+
inputStreamNode = await connectToAudioInput(audioContext, null, faustNode, inputStreamNode);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {FaustAudioWorkletNode} faustNode
|
|
86
|
+
*/
|
|
87
|
+
const buildMidiDeviceMenu = async (faustNode) => {
|
|
88
|
+
|
|
89
|
+
// Keyboard to MIDI handing
|
|
90
|
+
let keyboard2MIDI = createKey2MIDI((event) => faustNode.midiMessage(event));
|
|
91
|
+
keyboard2MIDI.start();
|
|
92
|
+
|
|
93
|
+
const midiAccess = await navigator.requestMIDIAccess();
|
|
94
|
+
/** @type {WebMidi.MIDIInput} */
|
|
95
|
+
let currentInput;
|
|
96
|
+
/**
|
|
97
|
+
* @param {WebMidi.MIDIMessageEvent} e
|
|
98
|
+
*/
|
|
99
|
+
const handleMidiMessage = e => faustNode.midiMessage(e.data);
|
|
100
|
+
const handleStateChange = () => {
|
|
101
|
+
const { inputs } = midiAccess;
|
|
102
|
+
if ($selectMidiInput.options.length === inputs.size + 1) return;
|
|
103
|
+
if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
|
|
104
|
+
$selectMidiInput.innerHTML = '<option value="-1" disabled selected>Select...</option>';
|
|
105
|
+
inputs.forEach((midiInput) => {
|
|
106
|
+
const { name, id } = midiInput;
|
|
107
|
+
const option = new Option(name, id);
|
|
108
|
+
$selectMidiInput.add(option);
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
handleStateChange();
|
|
112
|
+
midiAccess.addEventListener("statechange", handleStateChange);
|
|
113
|
+
$selectMidiInput.onchange = () => {
|
|
114
|
+
if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
|
|
115
|
+
const id = $selectMidiInput.value;
|
|
116
|
+
currentInput = midiAccess.inputs.get(id);
|
|
117
|
+
currentInput.addEventListener("midimessage", handleMidiMessage);
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// To test the ScriptProcessorNode mode
|
|
122
|
+
// const { faustNode, dspMeta: { name } } = await createFaustNode(audioContext, "FAUST_DSP_NAME", FAUST_DSP_VOICES, true);
|
|
123
|
+
const result = await createFaustNode(audioContext, "FAUST_DSP_NAME", FAUST_DSP_VOICES);
|
|
124
|
+
faustNode = result.faustNode; // Assign to the global variable
|
|
125
|
+
if (!faustNode) throw new Error("Faust DSP not compiled");
|
|
126
|
+
|
|
127
|
+
// Create the Faust UI
|
|
128
|
+
await createFaustUI($divFaustUI, faustNode);
|
|
129
|
+
|
|
130
|
+
// Connect the Faust node to the audio output
|
|
131
|
+
faustNode.connect(audioContext.destination);
|
|
132
|
+
|
|
133
|
+
// Build the audio device menu
|
|
134
|
+
if (faustNode.numberOfInputs > 0) await buildAudioDeviceMenu(faustNode);
|
|
135
|
+
else $spanAudioInput.hidden = true;
|
|
136
|
+
|
|
137
|
+
// Build the MIDI device menu
|
|
138
|
+
if (navigator.requestMIDIAccess) await buildMidiDeviceMenu(faustNode);
|
|
139
|
+
else $spanMidiInput.hidden = true;
|
|
140
|
+
|
|
141
|
+
})();
|
|
142
|
+
|
|
143
|
+
// Set the title and enable the DSP button
|
|
144
|
+
$buttonDsp.disabled = false;
|
|
145
|
+
document.title = name;
|
|
146
|
+
let sensorHandlersBound = false;
|
|
147
|
+
|
|
148
|
+
// Activate AudioContext and Sensors on user interaction
|
|
149
|
+
$buttonDsp.onclick = async () => {
|
|
150
|
+
|
|
151
|
+
// Import the requestPermissions function
|
|
152
|
+
const { requestPermissions } = await import("./create-node.js");
|
|
153
|
+
|
|
154
|
+
// Request permission for sensors
|
|
155
|
+
await requestPermissions();
|
|
156
|
+
|
|
157
|
+
// Activate sensor listeners
|
|
158
|
+
if (!sensorHandlersBound) {
|
|
159
|
+
await faustNode.startSensors();
|
|
160
|
+
sensorHandlersBound = true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Activate or suspend the AudioContext
|
|
164
|
+
if (audioContext.state === "running") {
|
|
165
|
+
$buttonDsp.textContent = "Suspended";
|
|
166
|
+
await audioContext.suspend();
|
|
167
|
+
} else if (audioContext.state === "suspended") {
|
|
168
|
+
$buttonDsp.textContent = "Running";
|
|
169
|
+
await audioContext.resume();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -101,6 +101,22 @@ function stopMIDI() {
|
|
|
101
101
|
let sensorHandlersBound = false;
|
|
102
102
|
let midiHandlersBound = false;
|
|
103
103
|
|
|
104
|
+
// Keyboard to MIDI handing
|
|
105
|
+
let keyboard2MIDI = null;
|
|
106
|
+
|
|
107
|
+
async function startKeyboard2MIDI() {
|
|
108
|
+
// Import the create-node module
|
|
109
|
+
const { createKey2MIDI } = await import("./create-node.js");
|
|
110
|
+
|
|
111
|
+
keyboard2MIDI = createKey2MIDI((event) => faustNode.midiMessage(event));
|
|
112
|
+
keyboard2MIDI.start();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function stopKeyboard2MIDI() {
|
|
116
|
+
keyboard2MIDI.stop();
|
|
117
|
+
keyboard2MIDI = null;
|
|
118
|
+
}
|
|
119
|
+
|
|
104
120
|
// Function to activate MIDI and Sensors on user interaction
|
|
105
121
|
async function activateMIDISensors() {
|
|
106
122
|
|
|
@@ -119,6 +135,7 @@ async function activateMIDISensors() {
|
|
|
119
135
|
// Initialize the MIDI setup
|
|
120
136
|
if (!midiHandlersBound) {
|
|
121
137
|
startMIDI();
|
|
138
|
+
await startKeyboard2MIDI();
|
|
122
139
|
midiHandlersBound = true;
|
|
123
140
|
}
|
|
124
141
|
|
|
@@ -153,6 +170,7 @@ async function deactivateAudioMIDISensors() {
|
|
|
153
170
|
// Deactivate the MIDI setup
|
|
154
171
|
if (midiHandlersBound && FAUST_DSP_VOICES > 0) {
|
|
155
172
|
stopMIDI();
|
|
173
|
+
stopKeyboard2MIDI();
|
|
156
174
|
midiHandlersBound = false;
|
|
157
175
|
}
|
|
158
176
|
}
|
|
@@ -47,7 +47,7 @@ let faustNode;
|
|
|
47
47
|
// Called at load time
|
|
48
48
|
(async () => {
|
|
49
49
|
|
|
50
|
-
const { createFaustNode, connectToAudioInput, createFaustUI } = await import("./create-node.js");
|
|
50
|
+
const { createFaustNode, createKey2MIDI, connectToAudioInput, createFaustUI } = await import("./create-node.js");
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* @param {FaustAudioWorkletNode} faustNode
|
|
@@ -85,6 +85,11 @@ let faustNode;
|
|
|
85
85
|
* @param {FaustAudioWorkletNode} faustNode
|
|
86
86
|
*/
|
|
87
87
|
const buildMidiDeviceMenu = async (faustNode) => {
|
|
88
|
+
|
|
89
|
+
// Keyboard to MIDI handling - create but don't start yet
|
|
90
|
+
let keyboard2MIDI = createKey2MIDI((event) => faustNode.midiMessage(event));
|
|
91
|
+
let isKeyboardActive = false;
|
|
92
|
+
|
|
88
93
|
const midiAccess = await navigator.requestMIDIAccess();
|
|
89
94
|
/** @type {WebMidi.MIDIInput} */
|
|
90
95
|
let currentInput;
|
|
@@ -94,9 +99,18 @@ let faustNode;
|
|
|
94
99
|
const handleMidiMessage = e => faustNode.midiMessage(e.data);
|
|
95
100
|
const handleStateChange = () => {
|
|
96
101
|
const { inputs } = midiAccess;
|
|
97
|
-
if
|
|
102
|
+
// Check if we need to rebuild the menu
|
|
103
|
+
const expectedOptions = inputs.size + 2; // +1 for "Select..." and +1 for "Keyboard"
|
|
104
|
+
if ($selectMidiInput.options.length === expectedOptions) return;
|
|
105
|
+
|
|
98
106
|
if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
|
|
99
107
|
$selectMidiInput.innerHTML = '<option value="-1" disabled selected>Select...</option>';
|
|
108
|
+
|
|
109
|
+
// Add computer keyboard option
|
|
110
|
+
const keyboardOption = new Option("Computer Keyboard", "Computer Keyboard");
|
|
111
|
+
$selectMidiInput.add(keyboardOption);
|
|
112
|
+
|
|
113
|
+
// Add MIDI device options
|
|
100
114
|
inputs.forEach((midiInput) => {
|
|
101
115
|
const { name, id } = midiInput;
|
|
102
116
|
const option = new Option(name, id);
|
|
@@ -106,10 +120,29 @@ let faustNode;
|
|
|
106
120
|
handleStateChange();
|
|
107
121
|
midiAccess.addEventListener("statechange", handleStateChange);
|
|
108
122
|
$selectMidiInput.onchange = () => {
|
|
123
|
+
// Disconnect previous MIDI input
|
|
109
124
|
if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
125
|
+
currentInput = null;
|
|
126
|
+
|
|
127
|
+
// Stop Computer Keyboard if it was active
|
|
128
|
+
if (isKeyboardActive) {
|
|
129
|
+
keyboard2MIDI.stop();
|
|
130
|
+
isKeyboardActive = false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const selectedValue = $selectMidiInput.value;
|
|
134
|
+
|
|
135
|
+
if (selectedValue === "Computer Keyboard") {
|
|
136
|
+
// Activate keyboard MIDI
|
|
137
|
+
keyboard2MIDI.start();
|
|
138
|
+
isKeyboardActive = true;
|
|
139
|
+
} else {
|
|
140
|
+
// Activate selected MIDI device
|
|
141
|
+
currentInput = midiAccess.inputs.get(selectedValue);
|
|
142
|
+
if (currentInput) {
|
|
143
|
+
currentInput.addEventListener("midimessage", handleMidiMessage);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
113
146
|
};
|
|
114
147
|
};
|
|
115
148
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,252 @@
|
|
|
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, bufferSize = 512) => {
|
|
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
|
+
// Create a polyphonic Faust audio node
|
|
55
|
+
const generator = new FaustPolyDspGenerator();
|
|
56
|
+
faustNode = await generator.createNode(
|
|
57
|
+
audioContext,
|
|
58
|
+
voices,
|
|
59
|
+
dspName,
|
|
60
|
+
{ module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta), soundfiles: {} },
|
|
61
|
+
faustDsp.mixerModule,
|
|
62
|
+
faustDsp.effectModule ? { module: faustDsp.effectModule, json: JSON.stringify(faustDsp.effectMeta), soundfiles: {} } : undefined,
|
|
63
|
+
sp,
|
|
64
|
+
bufferSize
|
|
65
|
+
);
|
|
66
|
+
} else {
|
|
67
|
+
// Create a standard Faust audio node
|
|
68
|
+
const generator = new FaustMonoDspGenerator();
|
|
69
|
+
faustNode = await generator.createNode(
|
|
70
|
+
audioContext,
|
|
71
|
+
dspName,
|
|
72
|
+
{ module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta), soundfiles: {} },
|
|
73
|
+
sp,
|
|
74
|
+
bufferSize
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Return an object with the Faust audio node and the DSP metadata
|
|
79
|
+
return { faustNode, dspMeta };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Connects an audio input stream to a Faust WebAudio node.
|
|
84
|
+
*
|
|
85
|
+
* @param {AudioContext} audioContext - The Web Audio API AudioContext to which the Faust audio node is connected.
|
|
86
|
+
* @param {string} id - The ID of the audio input device to connect.
|
|
87
|
+
* @param {FaustNode} faustNode - The Faust audio node to which the audio input stream will be connected.
|
|
88
|
+
* @param {MediaStreamAudioSourceNode} oldInputStreamNode - The old audio input stream node to be disconnected from the Faust audio node.
|
|
89
|
+
* @returns {Promise<MediaStreamAudioSourceNode>} - The new audio input stream node connected to the Faust audio node.
|
|
90
|
+
*/
|
|
91
|
+
async function connectToAudioInput(audioContext, id, faustNode, oldInputStreamNode) {
|
|
92
|
+
// Create an audio input stream node
|
|
93
|
+
const constraints = {
|
|
94
|
+
audio: {
|
|
95
|
+
echoCancellation: false,
|
|
96
|
+
noiseSuppression: false,
|
|
97
|
+
autoGainControl: false,
|
|
98
|
+
deviceId: id ? { exact: id } : undefined,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
// Get the audio input stream
|
|
102
|
+
const stream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
103
|
+
if (stream) {
|
|
104
|
+
if (oldInputStreamNode) oldInputStreamNode.disconnect();
|
|
105
|
+
const newInputStreamNode = audioContext.createMediaStreamSource(stream);
|
|
106
|
+
newInputStreamNode.connect(faustNode);
|
|
107
|
+
return newInputStreamNode;
|
|
108
|
+
} else {
|
|
109
|
+
return oldInputStreamNode;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Creates a Faust UI for a Faust audio node.
|
|
115
|
+
*
|
|
116
|
+
* @param {FaustAudioWorkletNode} faustNode
|
|
117
|
+
*/
|
|
118
|
+
async function createFaustUI(divFaustUI, faustNode) {
|
|
119
|
+
const { FaustUI } = await import("./faust-ui/index.js");
|
|
120
|
+
const $container = document.createElement("div");
|
|
121
|
+
$container.style.margin = "0";
|
|
122
|
+
$container.style.position = "absolute";
|
|
123
|
+
$container.style.overflow = "auto";
|
|
124
|
+
$container.style.display = "flex";
|
|
125
|
+
$container.style.flexDirection = "column";
|
|
126
|
+
$container.style.width = "100%";
|
|
127
|
+
$container.style.height = "100%";
|
|
128
|
+
divFaustUI.appendChild($container);
|
|
129
|
+
const faustUI = new FaustUI({
|
|
130
|
+
ui: faustNode.getUI(),
|
|
131
|
+
root: $container,
|
|
132
|
+
listenWindowMessage: false,
|
|
133
|
+
listenWindowResize: true,
|
|
134
|
+
});
|
|
135
|
+
faustUI.paramChangeByUI = (path, value) => faustNode.setParamValue(path, value);
|
|
136
|
+
faustNode.setOutputParamHandler((path, value) => faustUI.paramChangeByDSP(path, value));
|
|
137
|
+
$container.style.minWidth = `${faustUI.minWidth}px`;
|
|
138
|
+
$container.style.minHeight = `${faustUI.minHeight}px`;
|
|
139
|
+
faustUI.resize();
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Request permission to use motion and orientation sensors.
|
|
144
|
+
*/
|
|
145
|
+
async function requestPermissions() {
|
|
146
|
+
|
|
147
|
+
// Explicitly request permission on iOS before calling startSensors()
|
|
148
|
+
if (typeof window.DeviceMotionEvent !== "undefined" && typeof window.DeviceMotionEvent.requestPermission === "function") {
|
|
149
|
+
try {
|
|
150
|
+
const permissionState = await window.DeviceMotionEvent.requestPermission();
|
|
151
|
+
if (permissionState !== "granted") {
|
|
152
|
+
console.warn("Motion sensor permission denied.");
|
|
153
|
+
} else {
|
|
154
|
+
console.log("Motion sensor permission granted.");
|
|
155
|
+
}
|
|
156
|
+
} catch (error) {
|
|
157
|
+
console.error("Error requesting motion sensor permission:", error);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (typeof window.DeviceOrientationEvent !== "undefined" && typeof window.DeviceOrientationEvent.requestPermission === "function") {
|
|
162
|
+
try {
|
|
163
|
+
const permissionState = await window.DeviceOrientationEvent.requestPermission();
|
|
164
|
+
if (permissionState !== "granted") {
|
|
165
|
+
console.warn("Orientation sensor permission denied.");
|
|
166
|
+
} else {
|
|
167
|
+
console.log("Orientation sensor permission granted.");
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.error("Error requesting orientation sensor permission:", error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Key2Midi: maps keyboard input to MIDI messages.
|
|
177
|
+
*/
|
|
178
|
+
class Key2Midi {
|
|
179
|
+
static KEY_MAP = {
|
|
180
|
+
a: 0, w: 1, s: 2, e: 3, d: 4, f: 5, t: 6, g: 7,
|
|
181
|
+
y: 8, h: 9, u: 10, j: 11, k: 12, o: 13, l: 14, p: 15, ";": 16,
|
|
182
|
+
z: "PREV", x: "NEXT", c: "VELDOWN", v: "VELUP"
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
constructor({ keyMap = Key2Midi.KEY_MAP, offset = 60, velocity = 100, handler = console.log } = {}) {
|
|
186
|
+
this.keyMap = keyMap;
|
|
187
|
+
this.offset = offset;
|
|
188
|
+
this.velocity = velocity;
|
|
189
|
+
this.velMap = [20, 40, 60, 80, 100, 127];
|
|
190
|
+
this.handler = handler;
|
|
191
|
+
this.pressed = {};
|
|
192
|
+
|
|
193
|
+
this.onKeyDown = this.onKeyDown.bind(this);
|
|
194
|
+
this.onKeyUp = this.onKeyUp.bind(this);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
start() {
|
|
198
|
+
window.addEventListener("keydown", this.onKeyDown);
|
|
199
|
+
window.addEventListener("keyup", this.onKeyUp);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
stop() {
|
|
203
|
+
window.removeEventListener("keydown", this.onKeyDown);
|
|
204
|
+
window.removeEventListener("keyup", this.onKeyUp);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
onKeyDown(e) {
|
|
208
|
+
const key = e.key.toLowerCase();
|
|
209
|
+
if (this.pressed[key]) return;
|
|
210
|
+
this.pressed[key] = true;
|
|
211
|
+
|
|
212
|
+
const val = this.keyMap[key];
|
|
213
|
+
if (typeof val === "number") {
|
|
214
|
+
const note = val + this.offset;
|
|
215
|
+
this.handler([0x90, note, this.velocity]);
|
|
216
|
+
} else if (val === "PREV") {
|
|
217
|
+
this.offset -= 1;
|
|
218
|
+
} else if (val === "NEXT") {
|
|
219
|
+
this.offset += 1;
|
|
220
|
+
} else if (val === "VELDOWN") {
|
|
221
|
+
const idx = Math.max(0, this.velMap.indexOf(this.velocity) - 1);
|
|
222
|
+
this.velocity = this.velMap[idx];
|
|
223
|
+
} else if (val === "VELUP") {
|
|
224
|
+
const idx = Math.min(this.velMap.length - 1, this.velMap.indexOf(this.velocity) + 1);
|
|
225
|
+
this.velocity = this.velMap[idx];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
onKeyUp(e) {
|
|
230
|
+
const key = e.key.toLowerCase();
|
|
231
|
+
const val = this.keyMap[key];
|
|
232
|
+
if (typeof val === "number") {
|
|
233
|
+
const note = val + this.offset;
|
|
234
|
+
this.handler([0x80, note, this.velocity]);
|
|
235
|
+
}
|
|
236
|
+
delete this.pressed[key];
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Creates a Key2Midi instance.
|
|
242
|
+
*
|
|
243
|
+
* @param {function} handler - The function to handle MIDI messages.
|
|
244
|
+
* @returns {Key2Midi} - The Key2Midi instance.
|
|
245
|
+
*/
|
|
246
|
+
function createKey2MIDI(handler) {
|
|
247
|
+
return new Key2Midi({ handler: handler });
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Export the functions
|
|
251
|
+
export { createFaustNode, createFaustUI, createKey2MIDI, connectToAudioInput, requestPermissions };
|
|
252
|
+
|