-
Notifications
You must be signed in to change notification settings - Fork 6
/
example-genish.js
65 lines (57 loc) · 2.25 KB
/
example-genish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const assert = require('assert');
const { Worker, MessageChannel, MessagePort, isMainThread, parentPort, workerData, SHARE_ENV } = require('worker_threads');
// see https://nodejs.org/api/worker_threads.html#worker_threads_class_worker
/*
This script is adapted from the test-audio.js script
This script spawns a web-worker (think: a separate thread of Node.js) to do the actual audio processing.
That way, any heavy processing in audio does not interrupt main-thread graphics,
and any laggy processing in the main thread does not interrupt audio.
*/
// // wrap shared memory as a float array
// // this can now be accessible from both threads
// let sab = new Float32Array(new SharedArrayBuffer(1024));
// console.log("sab", sab)
// load a js file as a new Worker thread:
const worker = new Worker("./genish-worker.js", {
// argv: becomes process.argv in worker
// eval: true if 1st arg is a string of code rather than a filepath
//workerData: "hello" // pass initial data to worker
});
// here's a message channel to receive messages from it:
//const workerChannel = new MessageChannel();
// receicve a message:
worker.on('message', function (msg) {
console.log("main received message from audio:", msg)
});
//worker.on('error', ...);
//worker.on('online', ...)
worker.on('exit', (code) => {
console.log(`Worker stopped with exit code ${code}`)
process.exit(code)
})
// send it a message:
// note that postMessage will copy data.
// To transfer without copying, also list the item in an array as the 2nd argument of postMessage
// The exception is SharedArrayBuffer, which is not copied, and can be read/written on both sides
//worker.postMessage({ shared: sab })
// worker.postMessage({
// hereIsYourPort: workerChannel.port1
// }, [workerChannel.port1]);
// // handle replies:
// workerChannel.port2.on('message', (value) => {
// console.log('received:', value);
// });
// periodically send random graphs:
setInterval(() => {
let n = Math.ceil(Math.random() * 10 + 20) * Math.random() * 10
let graph
graph = `cycle( add(${n}, pow(${n * 2}, add(cycle(${n * 2}), cycle(${Math.random() * 10})))) )`
worker.postMessage({
cmd: "graph",
graph: graph
})
}, 1000)
// kill the audio thread after 10 seconds
setTimeout(() => {
worker.postMessage({ cmd: "end" })
}, 10 * 1000)