@grame/faustwasm 0.9.6 → 0.10.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 (33) hide show
  1. package/assets/standalone/create-node.js +76 -1
  2. package/assets/standalone/index copie.js +171 -0
  3. package/assets/standalone/index-pwa.js +18 -0
  4. package/assets/standalone/index.js +38 -5
  5. package/dist/cjs-bundle/index.js +10 -58
  6. package/dist/cjs-bundle/index.js.map +3 -3
  7. package/dist/esm-bundle/index.js +10 -58
  8. package/dist/esm-bundle/index.js.map +3 -3
  9. package/libfaust-wasm/dossier sans titre/libfaust-wasm.data +0 -0
  10. package/libfaust-wasm/dossier sans titre/libfaust-wasm.js +20 -0
  11. package/libfaust-wasm/dossier sans titre/libfaust-wasm.wasm +0 -0
  12. package/libfaust-wasm/libfaust-wasm.js +1 -1
  13. package/libfaust-wasm/libfaust-wasm.wasm +0 -0
  14. package/package.json +1 -1
  15. package/src/FaustDspGenerator.ts +0 -1
  16. package/src/FaustWebAudioDsp.ts +0 -2
  17. package/src.zip +0 -0
  18. package/test/organ1/create-node.js +252 -0
  19. package/test/organ1/dsp-meta.json +126 -0
  20. package/test/organ1/dsp-module.wasm +0 -0
  21. package/test/organ1/faust-ui/index.css +442 -0
  22. package/test/organ1/faust-ui/index.css.map +1 -0
  23. package/test/organ1/faust-ui/index.d.ts +1 -0
  24. package/test/organ1/faust-ui/index.js +3729 -0
  25. package/test/organ1/faust-ui/index.js.map +1 -0
  26. package/test/organ1/faustwasm/index.d.ts +1695 -0
  27. package/test/organ1/faustwasm/index.js +4888 -0
  28. package/test/organ1/faustwasm/index.js.map +7 -0
  29. package/test/organ1/icon.png +0 -0
  30. package/test/organ1/index.html +76 -0
  31. package/test/organ1/index.js +201 -0
  32. package/test/organ1/manifest.json +17 -0
  33. package/test/organ1/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 ($selectMidiInput.options.length === inputs.size + 1) return;
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
- const id = $selectMidiInput.value;
111
- currentInput = midiAccess.inputs.get(id);
112
- currentInput.addEventListener("midimessage", handleMidiMessage);
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