@grame/faustwasm 0.4.2 → 0.4.4

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/README.md CHANGED
@@ -62,20 +62,20 @@ will create a set of files: `poly.js`, `poly.wasm`, `poly.json`, `poly.html` (an
62
62
  You can create a standalone Progressive Web Application using the same command line:
63
63
 
64
64
  ```bash
65
- rm -rf test/out # make sure you are under the faustwasm directory.
66
- node scripts/faust2wasm.js test/rev.dsp test/out -standalone
65
+ node scripts/faust2wasm.js test/rev.dsp test/rev -standalone
67
66
  ```
68
- will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `rev.js`, `rev.wasm`, `rev.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `out` folder.
67
+ will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `rev.js`, `rev.wasm`, `rev.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `rev` folder.
69
68
 
70
- The folder contains the necessary ressources to deploy the Faust application as a PWA on a server, to be installed and used offline. Note that audio files used by the `soundfile` primitives in the DSP code will have to be mannually added in the folder.
69
+ The folder contains the necessary ressources to deploy the Faust application as a PWA on a server, to be installed and used offline. Note that audio files used by the `soundfile` primitives in the DSP code will have to be mannually added in the folder. To test, be sure to launch a local web server at the `test` folder level, then go inside `test\rev` folder.
71
70
 
72
71
  A standalone polyphonic and MIDI standalone Progressive Web Application can be created with:
73
72
 
74
73
  ```bash
75
- rm -rf test/out # make sure you are under the faustwasm directory.
76
- node scripts/faust2wasm.js test/organ1.dsp test/out -standalone
74
+ node scripts/faust2wasm.js test/organ1.dsp test/organ1 -poly -standalone
77
75
  ```
78
- will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `organ1.js`, `organ1.wasm`, `organ1.json`, `organ1_effect.wasm`, `organ1_effect.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `out` folder.
76
+ will create a set of files: `icon.png`, `service-worker.js`, `manifest.json`, `organ1.js`, `organ1.wasm`, `organ1.json`, `organ1_effect.wasm`, `organ1_effect.json`, `index.html`, and the `faustwasm`, `faust-ui` folders in the `organ1` folder.
77
+
78
+ To test, be sure to launch a local web server at the `test` folder level, then go inside `test\organ1` folder.
79
79
 
80
80
  #### Generate SVG Diagrams of a Faust DSP
81
81
 
@@ -235,6 +235,8 @@ process = ba.pulsen(1, 10000) : pm.djembe(60, 0.3, 0.4, 1);
235
235
 
236
236
  ### Projects examples
237
237
 
238
+ Several examples can be tested by launching a local web server at the faustwasm root level, and going in `test/faustlive-wasm` and `libfaust-in-worklet` folders.
239
+
238
240
  The package is used in the following projects:
239
241
 
240
242
  - [faust-web-component](https://github.com/grame-cncm/faust-web-component), a package providing two web components for embedding interactive Faust snippets in web pages.
@@ -1,27 +1,76 @@
1
1
 
2
2
  const CACHE_NAME = 'FAUST_DSP-static'; // Cache name without versioning
3
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
+ */
4
32
  self.addEventListener('install', event => {
5
33
  event.waitUntil(
6
34
  caches.open(CACHE_NAME).then(cache => {
7
35
  console.log("Service worker installed");
8
- return cache.addAll([
9
- '/FAUST_DSP/',
10
- '/FAUST_DSP/faust-ui/index.js',
11
- '/FAUST_DSP/faust-ui/index.css',
12
- '/FAUST_DSP/faustwasm/index.js',
13
- '/FAUST_DSP/FAUST_DSP.js',
14
- '/FAUST_DSP/FAUST_DSP.wasm',
15
- '/FAUST_DSP/FAUST_DSP.json',
16
- '/FAUST_DSP/mixerModule.wasm',
17
- ]).catch(error => {
18
- // Catch and log any errors during the caching process
19
- console.error('Failed to cache resources during install:', error);
20
- });
36
+ const urlToCheck = '/FAUST_DSP/FAUST_DSP_effect.json';
37
+ return fetchResourceAndCache(cache, urlToCheck);
21
38
  })
22
39
  );
23
40
  });
24
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
+
25
74
  self.addEventListener("activate", event => {
26
75
  console.log("Service worker activated");
27
76
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grame/faustwasm",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "WebAssembly version of Faust Compiler",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -386,21 +386,21 @@ const uploadOn = (e) => {
386
386
  alert("WebAssembly is not supported in this browser !");
387
387
  reject("WebAssembly is not supported in this browser !");
388
388
  }
389
-
389
+
390
390
  // CASE 1 : THE DROPPED OBJECT IS A URL TO SOME FAUST CODE
391
391
  if (e.dataTransfer.getData('URL') && e.dataTransfer.getData('URL').split(':').shift() != "file") {
392
392
  const url = e.dataTransfer.getData('URL');
393
393
  let filename = url.toString().split('/').pop();
394
394
  filename = filename.toString().split('.').shift();
395
395
  const xmlhttp = new XMLHttpRequest();
396
-
396
+
397
397
  xmlhttp.onreadystatechange = () => {
398
398
  if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
399
399
  dsp_code = xmlhttp.responseText;
400
400
  callback(dsp_code);
401
401
  }
402
402
  }
403
-
403
+
404
404
  try {
405
405
  xmlhttp.open("GET", url, false);
406
406
  // Avoid error "mal formé" on firefox
@@ -410,10 +410,10 @@ const uploadOn = (e) => {
410
410
  alert(err);
411
411
  reject(err);
412
412
  }
413
-
413
+
414
414
  } else if (e.dataTransfer.getData('URL').split(':').shift() != "file") {
415
415
  dsp_code = e.dataTransfer.getData('text');
416
-
416
+
417
417
  // CASE 2 : THE DROPPED OBJECT IS SOME FAUST CODE
418
418
  if (dsp_code) {
419
419
  callback(dsp_code);
@@ -423,17 +423,17 @@ const uploadOn = (e) => {
423
423
  /** @type {FileList} */
424
424
  const files = e.target.files || e.dataTransfer.files;
425
425
  const file = files[0];
426
-
426
+
427
427
  if (location.host.indexOf("sitepointstatic") >= 0) return;
428
-
428
+
429
429
  const request = new XMLHttpRequest();
430
430
  if (request.upload) {
431
-
431
+
432
432
  const reader = new FileReader();
433
433
  const ext = file.name.toString().split('.').pop();
434
434
  const filename = file.name.toString().split('.').shift();
435
435
  let type;
436
-
436
+
437
437
  if (ext === "dsp") {
438
438
  type = "dsp";
439
439
  reader.readAsText(file);
@@ -441,7 +441,7 @@ const uploadOn = (e) => {
441
441
  type = "json";
442
442
  reader.readAsText(file);
443
443
  }
444
-
444
+
445
445
  reader.onloadend = (e) => {
446
446
  dsp_code = reader.result;
447
447
  callback(dsp_code);
@@ -492,7 +492,7 @@ const activateMonoDSP = (dsp) => {
492
492
  }
493
493
 
494
494
  // Setup UI
495
- DSP.listenMotion();
495
+ DSP.listenSensors();
496
496
  faustUI = new FaustUI({ ui: DSP.getUI(), root: faustUIRoot });
497
497
  faustUI.paramChangeByUI = (path, value) => DSP.setParamValue(path, value);
498
498
  DSP.setOutputParamHandler(output_handler);
@@ -520,7 +520,7 @@ const activatePolyDSP = (dsp) => {
520
520
  }
521
521
 
522
522
  // Setup UI
523
- DSP.listenMotion();
523
+ DSP.listenSensors();
524
524
  faustUI = new FaustUI({ ui: DSP.getUI(), root: faustUIRoot });
525
525
  faustUI.paramChangeByUI = (path, value) => DSP.setParamValue(path, value);
526
526
  DSP.setOutputParamHandler(output_handler);
@@ -1,13 +1,13 @@
1
1
  //@ts-check
2
2
  const div = document.getElementById("log");
3
3
  const log = (/** @type {string} */str) => div.innerHTML += str.replace("\n", "<br />") + "<br />";
4
- const options = "-ftz 2";
5
4
 
6
5
  (async () => {
7
6
  const ctx = new AudioContext();
8
7
  globalThis.ctx = ctx;
9
8
  globalThis.faustModule = await faustwasm.instantiateFaustModule();
10
9
  const post = async () => {
10
+ const options = "-ftz 2";
11
11
  const {
12
12
  instantiateFaustModule,
13
13
  LibFaust,
@@ -28,7 +28,7 @@ const options = "-ftz 2";
28
28
  const libFaust = new LibFaust(faustModule);
29
29
  const compiler = new FaustCompiler(libFaust);
30
30
  console.log(compiler.version());
31
- await gen.compile(compiler, 'dsp', code, "-ftz 0");
31
+ await gen.compile(compiler, 'dsp', code, options);
32
32
 
33
33
  console.log(await gen.createAudioWorkletProcessor());
34
34
  const processor = await gen.createOfflineProcessor(48000, 128);