-
Notifications
You must be signed in to change notification settings - Fork 6
/
example-audio.js
212 lines (195 loc) · 4.95 KB
/
example-audio.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
if (isMainThread) {
// 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)
// reload this file in a new Worker thread:
const worker = new Worker(__filename, {
// 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("received", msg)
});
//worker.on('error', ...);
//worker.on('online', ...)
worker.on('exit', (code) => { console.log(`Worker stopped with exit code ${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);
});
} else {
// here's the worker!
// `workerData` can be passed here in the `new Worker` constructor above
console.log(workerData);
// handle messages from main thread:
parentPort.on("message", (msg) => {
console.log("got message from parent", msg)
})
parentPort.postMessage("yo from thread")
// parentPort.once('message', (msg) => {
// assert(msg.hereIsYourPort instanceof MessagePort);
// // send a message back:
// msg.hereIsYourPort.postMessage('the worker is sending this');
// //msg.hereIsYourPort.close();
// });
const audio = require('./audio.js');
/*
List available playback and capture devices & their features
For example:
{
playback: [
{
index: 0,
name: 'Built-in Output',
minChannels: 2,
maxChannels: 2,
minSampleRate: 44100,
maxSampleRate: 96000
},
{
index: 1,
name: 'iShowU Audio Capture',
minChannels: 2,
maxChannels: 2,
minSampleRate: 44100,
maxSampleRate: 192000
},
{
index: 2,
name: 'ZoomAudioDevice',
minChannels: 2,
maxChannels: 2,
minSampleRate: 48000,
maxSampleRate: 48000
}
],
capture: [
{
index: 0,
name: 'Built-in Microphone',
minChannels: 2,
maxChannels: 2,
minSampleRate: 44100,
maxSampleRate: 96000
},
{
index: 1,
name: 'NDI Audio',
minChannels: 2,
maxChannels: 2,
minSampleRate: 44100,
maxSampleRate: 48000
},
{
index: 2,
name: 'iShowU Audio Capture',
minChannels: 2,
maxChannels: 2,
minSampleRate: 44100,
maxSampleRate: 192000
},
{
index: 3,
name: 'ZoomAudioDevice',
minChannels: 2,
maxChannels: 2,
minSampleRate: 48000,
maxSampleRate: 48000
}
]
}
*/
console.log(audio.devices)
// start audio processing
// optional argument to specify settings
audio.start({
// these are the defaults:
samplerate: 48000,
indevice: 0,
inchannels: 2,
outdevice: 0,
outchannels: 2,
})
/*
e.g.:
{
outname: 'Built-in Output',
inname: 'Built-in Microphone',
outchannels: 2,
inchannels: 2,
outbuffer: Float32Array(2880),
inbuffer: Float32Array(2880),
samplerate: 48000,
frames: 1440,
latency: 0.029999999329447746,
pollms: 15
}
*/
console.log(audio)
// minimal sine oscillator:
nextSample = (function() {
let phase = 0;
let radiansPerFrame = Math.PI * 2 * 440/audio.samplerate;
return function () {
// compute next output:
phase += radiansPerFrame;
return 0.1 * Math.sin(phase);
}
})();
let frameIdx = 0;
let time = 0; // in seconds
let lasttime = 0
function update() {
let dt = time - lasttime // seconds since last update()
lasttime = time
// this is the time in the ringbuffer that has most recently been played (and is now zeroed)
// so we are safe to fill the buffer up to this point:
let at = audio.t
let outch = audio.outchannels
let inch = audio.inchannels
let secondsPerFrame = 1/audio.samplerate
//console.log(at, time)
// continue filling ringbuffer until we catch up to that point:
while (frameIdx != at) {
let inframe = audio.inbuffer.subarray(frameIdx*inch)
let outframe = audio.outbuffer.subarray(frameIdx*outch)
// compute next output:
let L = inframe[0]
let R = nextSample(time)
// write to output:
outframe[0] += L;
outframe[1] += R;
// time passes:
time += secondsPerFrame;
frameIdx = (frameIdx+1) % audio.frames;
}
// play for 10 seconds:
if (time > 10) {
audio.end()
console.log(audio)
process.exit()
} else {
//console.log(dt)
setTimeout(update, audio.pollms); // 300ms is enough. how??
}
}
update();
}