@grame/faust-web-component 0.2.9 → 0.2.11

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@grame/faust-web-component",
3
3
  "description": "Web component embedding the Faust Compiler",
4
- "version": "0.2.9",
4
+ "version": "0.2.11",
5
5
  "module": "dist/faust-web-component.js",
6
6
  "files": [
7
7
  "src/",
@@ -9,7 +9,8 @@
9
9
  "index.html",
10
10
  "tsconfig.json",
11
11
  "vite.config.js",
12
- "dist/faust-web-component.js"
12
+ "dist/faust-web-component.js",
13
+ "dist/02-XYLO1.mp3"
13
14
  ],
14
15
  "type": "module",
15
16
  "scripts": {
Binary file
@@ -267,7 +267,7 @@ export default class FaustEditor extends HTMLElement {
267
267
  let spectrum: Scope | undefined
268
268
  let gmidi = false
269
269
  let gnvoices = -1
270
-
270
+ let sourceNode: AudioBufferSourceNode = undefined;
271
271
 
272
272
  runButton.onclick = async () => {
273
273
  if (audioCtx.state === "suspended") {
@@ -435,6 +435,7 @@ export default class FaustEditor extends HTMLElement {
435
435
  audioInputSelector.appendChild(new Option(device.label || device.deviceId, device.deviceId))
436
436
  }
437
437
  }
438
+ audioInputSelector.appendChild(new Option("Audio File", "Audio File"))
438
439
  }
439
440
  deviceUpdateCallbacks.push(updateInputDevices)
440
441
 
@@ -446,8 +447,30 @@ export default class FaustEditor extends HTMLElement {
446
447
  input = undefined
447
448
  }
448
449
  if (node && node.numberOfInputs > 0) {
449
- input = audioCtx.createMediaStreamSource(stream)
450
- input.connect(node!)
450
+ if (deviceId == "Audio File") {
451
+ try {
452
+ // Load the file
453
+ let file = await fetch('./02-XYLO1.mp3');
454
+ const arrayBuffer = await file.arrayBuffer();
455
+ let audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
456
+ // Create a source node from the buffer
457
+ sourceNode = audioCtx.createBufferSource();
458
+ sourceNode.buffer = audioBuffer;
459
+ sourceNode.connect(node!);
460
+ // Start playing the file
461
+ sourceNode.start();
462
+ } catch (error) {
463
+ console.error("Error loading file: ", error);
464
+ }
465
+ } else {
466
+ if (sourceNode !== undefined) {
467
+ sourceNode.stop();
468
+ sourceNode.disconnect();
469
+ sourceNode = undefined;
470
+ }
471
+ input = audioCtx.createMediaStreamSource(stream);
472
+ input.connect(node!);
473
+ }
451
474
  }
452
475
  }
453
476
 
@@ -117,6 +117,7 @@ export default class FaustWidget extends HTMLElement {
117
117
  let input: MediaStreamAudioSourceNode | undefined
118
118
  let faustUI: FaustUI
119
119
  let generator: FaustMonoDspGenerator | FaustPolyDspGenerator
120
+ let sourceNode: AudioBufferSourceNode = undefined;
120
121
 
121
122
  const setup = async () => {
122
123
  await faustPromise
@@ -203,6 +204,7 @@ export default class FaustWidget extends HTMLElement {
203
204
  audioInputSelector.appendChild(new Option(device.label || device.deviceId, device.deviceId))
204
205
  }
205
206
  }
207
+ audioInputSelector.appendChild(new Option("Audio File", "Audio File"))
206
208
  }
207
209
  deviceUpdateCallbacks.push(updateInputDevices)
208
210
 
@@ -214,8 +216,30 @@ export default class FaustWidget extends HTMLElement {
214
216
  input = undefined
215
217
  }
216
218
  if (node && node.numberOfInputs > 0) {
217
- input = audioCtx.createMediaStreamSource(stream)
218
- input.connect(node!)
219
+ if (deviceId == "Audio File") {
220
+ try {
221
+ // Load the file
222
+ let file = await fetch('./02-XYLO1.mp3');
223
+ const arrayBuffer = await file.arrayBuffer();
224
+ let audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
225
+ // Create a source node from the buffer
226
+ sourceNode = audioCtx.createBufferSource();
227
+ sourceNode.buffer = audioBuffer;
228
+ sourceNode.connect(node!);
229
+ // Start playing the file
230
+ sourceNode.start();
231
+ } catch (error) {
232
+ console.error("Error loading file: ", error);
233
+ }
234
+ } else {
235
+ if (sourceNode !== undefined) {
236
+ sourceNode.stop();
237
+ sourceNode.disconnect();
238
+ sourceNode = undefined;
239
+ }
240
+ input = audioCtx.createMediaStreamSource(stream);
241
+ input.connect(node!);
242
+ }
219
243
  }
220
244
  }
221
245