Debian Codegraph @

wasm/browser/src/mains/spn.main.js

raw
1 /*
2 CsoundScriptProcessor.js
3
4 Copyright (C) 2018 Steven Yi, Victor Lazzarini
5
6 This file is part of Csound.
7
8 The Csound Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 Csound is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with Csound; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 02110-1301 USA
22 */
23
24 import libcsoundFactory from "../libcsound";
25 import loadWasm from "../module";
26 import MessagePortState from "../utils/message-port-state";
27 import { isEmpty } from "rambda/dist/rambda.esm.js";
28 import { csoundApiRename, fetchPlugins, makeSingleThreadCallback } from "../utils";
29 import { messageEventHandler } from "./messages.main";
30 import { PublicEventAPI } from "../events";
31 import { EventPromises } from "../utils/event-promises";
32 import { requestMidi } from "../utils/request-midi";
33
34 class ScriptProcessorNodeSingleThread {
35 constructor({ audioContext, inputChannelCount = 1, outputChannelCount = 2 }) {
36 this.publicEvents = new PublicEventAPI(this);
37 this.eventPromises = new EventPromises();
38 this.audioContext = audioContext;
39 this.onaudioprocess = this.onaudioprocess.bind(this);
40 this.currentPlayState = undefined;
41 this.onPlayStateChange = this.onPlayStateChange.bind(this);
42 this.start = this.start.bind(this);
43 this.stop = this.stop.bind(this);
44 this.pause = this.pause.bind(this);
45 this.resume = this.resume.bind(this);
46 this.wasm = undefined;
47 this.csoundInstance = undefined;
48 this.csoundApi = undefined;
49 this.exportApi = {};
50 this.spn = audioContext.createScriptProcessor(0, inputChannelCount, outputChannelCount);
51 this.spn.audioContext = audioContext;
52 this.spn.inputChannelCount = inputChannelCount;
53 this.spn.outputChannelCount = outputChannelCount;
54 this.spn.onaudioprocess = this.onaudioprocess;
55 this.node = this.spn;
56 this.exportApi.getNode = async () => this.spn;
57 this.sampleRate = audioContext.sampleRate;
58 // this is the only actual single-thread usecase
59 // so we get away with just forwarding it as if it's form
60 // a message port
61 this.messagePort = new MessagePortState();
62 this.messagePort.post = (log) => messageEventHandler(this)({ data: { log } });
63 this.messagePort.ready = true;
64
65 // imports from original csound-wasm
66 this.running = false;
67 this.started = false;
68 }
69
70 async terminateInstance() {
71 if (this.spn) {
72 this.spn.disconnect();
73 delete this.spn;
74 }
75 if (this.audioContext) {
76 if (this.audioContext.state !== "closed") {
77 await this.audioContext.close();
78 }
79 delete this.audioContext;
80 }
81 if (this.publicEvents) {
82 this.publicEvents.terminateInstance();
83 delete this.publicEvents;
84 }
85 Object.keys(this.exportApi).forEach((key) => delete this.exportApi[key]);
86 Object.keys(this).forEach((key) => delete this[key]);
87 }
88
89 async onPlayStateChange(newPlayState) {
90 if (this.currentPlayState === newPlayState) {
91 return;
92 }
93 this.currentPlayState = newPlayState;
94 switch (newPlayState) {
95 case "realtimePerformanceStarted": {
96 this.publicEvents.triggerRealtimePerformanceStarted(this);
97 break;
98 }
99
100 case "realtimePerformanceEnded": {
101 this.publicEvents.triggerRealtimePerformanceEnded(this);
102 break;
103 }
104 case "realtimePerformancePaused": {
105 this.publicEvents.triggerRealtimePerformancePaused(this);
106 break;
107 }
108 case "realtimePerformanceResumed": {
109 this.publicEvents.triggerRealtimePerformanceResumed(this);
110 break;
111 }
112 case "renderStarted": {
113 this.publicEvents.triggerRenderStarted(this);
114 break;
115 }
116 case "renderEnded": {
117 this.publicEvents.triggerRenderEnded(this);
118
119 break;
120 }
121
122 default: {
123 break;
124 }
125 }
126 }
127
128 async pause() {
129 if (this.started && this.running) {
130 this.running = false;
131 this.onPlayStateChange("realtimePerformancePaused");
132 }
133 }
134
135 async resume() {
136 if (this.started && !this.running) {
137 this.running = true;
138 this.onPlayStateChange("realtimePerformanceResumed");
139 }
140 }
141
142 async stop() {
143 if (this.started) {
144 this.eventPromises.createStopPromise();
145 const stopResult = this.csoundApi.csoundStop(this.csoundInstance);
146 await this.eventPromises.waitForStop();
147 if (this.watcherStdOut) {
148 this.watcherStdOut.close();
149 delete this.watcherStdOut;
150 }
151
152 if (this.watcherStdErr) {
153 this.watcherStdErr.close();
154 delete this.watcherStdErr;
155 }
156
157 delete this.csoundInputBuffer;
158 delete this.csoundOutputBuffer;
159 delete this.currentPlayState;
160 return stopResult;
161 }
162 }
163
164 async start() {
165 if (!this.csoundApi) {
166 console.error("starting csound failed because csound instance wasn't created");
167 return;
168 }
169
170 if (this.currentPlayState !== "realtimePerformanceStarted") {
171 this.result = 0;
172 this.csoundApi.csoundSetOption(this.csoundInstance, "-odac");
173 this.csoundApi.csoundSetOption(this.csoundInstance, "-iadc");
174 this.csoundApi.csoundSetOption(this.csoundInstance, "--sample-rate=" + this.sampleRate);
175 this.nchnls = -1;
176 this.nchnls_i = -1;
177
178 const ksmps = this.csoundApi.csoundGetKsmps(this.csoundInstance);
179 this.ksmps = ksmps;
180 this.cnt = ksmps;
181
182 this.nchnls = this.csoundApi.csoundGetNchnls(this.csoundInstance);
183 this.nchnls_i = this.csoundApi.csoundGetNchnlsInput(this.csoundInstance);
184
185 const outputPointer = this.csoundApi.csoundGetSpout(this.csoundInstance);
186 this.csoundOutputBuffer = new Float64Array(
187 this.wasm.wasi.memory.buffer,
188 outputPointer,
189 ksmps * this.nchnls,
190 );
191
192 const inputPointer = this.csoundApi.csoundGetSpin(this.csoundInstance);
193 this.csoundInputBuffer = new Float64Array(
194 this.wasm.wasi.memory.buffer,
195 inputPointer,
196 ksmps * this.nchnls_i,
197 );
198 this.zerodBFS = this.csoundApi.csoundGet0dBFS(this.csoundInstance);
199
200 this.publicEvents.triggerOnAudioNodeCreated(this.spn);
201 this.eventPromises.createStartPromise();
202
203 const startResult = this.csoundApi.csoundStart(this.csoundInstance);
204 if (this.csoundApi._isRequestingRtMidiInput(this.csoundInstance)) {
205 requestMidi({
206 onMidiMessage: ({ data: event }) =>
207 this.csoundApi.csoundPushMidiMessage(this.csoundInstance, event[0], event[1], event[2]),
208 });
209 }
210 this.running = true;
211 await this.eventPromises.waitForStart();
212 return startResult;
213 }
214 }
215
216 async initialize({ wasmDataURI, withPlugins, autoConnect }) {
217 if (!this.plugins && withPlugins && !isEmpty(withPlugins)) {
218 withPlugins = await fetchPlugins(withPlugins);
219 }
220
221 if (!this.wasm) {
222 const [wasm, wasi] = await loadWasm({
223 wasmDataURI: wasmDataURI(),
224 withPlugins,
225 messagePort: this.messagePort,
226 });
227 this.wasm = wasm;
228 this.wasi = wasi;
229 wasm.wasi = wasi;
230 }
231
232 // libcsound
233 const csoundApi = libcsoundFactory(this.wasm);
234 this.csoundApi = csoundApi;
235 const csoundInstance = await csoundApi.csoundCreate(0);
236 this.csoundInstance = csoundInstance;
237
238 if (autoConnect) {
239 this.spn.connect(this.audioContext.destination);
240 }
241
242 this.resetCsound(false);
243
244 // csoundObj
245 Object.keys(csoundApi).reduce((accumulator, apiName) => {
246 if (["mkdir", "readdir", "writeFile"].includes(apiName)) {
247 accumulator.fs = accumulator.fs || {};
248 const reference = csoundApi[apiName];
249 const callback = async (...arguments_) =>
250 makeSingleThreadCallback(this.wasm, csoundApi[apiName]).apply({}, arguments_);
251 callback.toString = reference.toString;
252 accumulator.fs[apiName] = callback;
253 } else {
254 const renamedApiName = csoundApiRename(apiName);
255 accumulator[renamedApiName] = (...arguments_) => {
256 return makeSingleThreadCallback(csoundInstance, csoundApi[apiName]).apply({}, arguments_);
257 };
258 accumulator[renamedApiName].toString = csoundApi[apiName].toString;
259 }
260
261 return accumulator;
262 }, this.exportApi);
263
264 this.exportApi.pause = this.pause.bind(this);
265 this.exportApi.resume = this.resume.bind(this);
266 this.exportApi.start = this.start.bind(this);
267 this.exportApi.stop = this.stop.bind(this);
268 this.exportApi.terminateInstance = this.terminateInstance.bind(this);
269 this.exportApi.getAudioContext = async () => this.audioContext;
270 this.exportApi.name = "Csound: ScriptProcessor Node, Single-threaded";
271 // this.exportApi.fs = persistentFilesystem;
272
273 this.exportApi = this.publicEvents.decorateAPI(this.exportApi);
274
275 this.exportApi.reset = () => this.resetCsound(true);
276 // the default message listener
277 this.exportApi.addListener("message", console.log);
278 return this.exportApi;
279 }
280
281 async resetCsound(callReset) {
282 if (
283 callReset &&
284 this.currentPlayState !== "realtimePerformanceEnded" &&
285 this.currentPlayState !== "realtimePerformanceStarted"
286 ) {
287 // reset can't be called until performance has started or ended!
288 return -1;
289 }
290 if (this.currentPlayState === "realtimePerformanceStarted") {
291 this.onPlayStateChange("realtimePerformanceEnded");
292 }
293
294 this.running = false;
295 this.started = false;
296 this.result = 0;
297
298 const cs = this.csoundInstance;
299 const libraryCsound = this.csoundApi;
300
301 if (callReset) {
302 libraryCsound.csoundReset(cs);
303 }
304
305 // FIXME:
306 // libraryCsound.csoundSetMidiCallbacks(cs);
307
308 libraryCsound.csoundSetOption(cs, "-odac");
309 libraryCsound.csoundSetOption(cs, "-iadc");
310 libraryCsound.csoundSetOption(cs, "--sample-rate=" + this.sampleRate);
311 this.nchnls = -1;
312 this.nchnls_i = -1;
313 delete this.csoundOutputBuffer;
314 delete this.csoundInputBuffer;
315 }
316
317 onaudioprocess(event) {
318 if (this.csoundOutputBuffer === null || this.running === false) {
319 const output = event.outputBuffer;
320 const channelData = output.getChannelData(0);
321
322 if (channelData) {
323 const bufferLength = channelData.length;
324
325 for (let index = 0; index < bufferLength; index++) {
326 for (let channel = 0; channel < output.numberOfChannels; channel++) {
327 const outputChannel = output.getChannelData(channel);
328 outputChannel[index] = 0;
329 }
330 }
331 }
332 }
333
334 if (this.running && !this.started) {
335 this.started = true;
336 this.onPlayStateChange("realtimePerformanceStarted");
337 this.eventPromises && this.eventPromises.releaseStartPromise();
338 }
339
340 const input = event.inputBuffer;
341 const output = event.outputBuffer;
342
343 const bufferLength = output.getChannelData(0).length;
344
345 let csOut = this.csoundOutputBuffer;
346 let csIn = this.csoundInputBuffer;
347
348 const ksmps = this.ksmps;
349 const zerodBFS = this.zerodBFS;
350
351 const nchnls = this.nchnls;
352 const nchnlsIn = this.nchnls_i;
353
354 let cnt = this.cnt || 0;
355 let result = this.result || 0;
356
357 for (let index = 0; index < bufferLength; index++, cnt++) {
358 if (cnt === ksmps && result === 0) {
359 // if we need more samples from Csound
360 result = this.csoundApi.csoundPerformKsmps(this.csoundInstance);
361 cnt = 0;
362 if (result !== 0) {
363 this.running = false;
364 this.started = false;
365 this.onPlayStateChange("realtimePerformanceEnded");
366 this.eventPromises && this.eventPromises.releaseStopPromise();
367 }
368 }
369
370 /* Check if MEMGROWTH occured from csoundPerformKsmps or otherwise. If so,
371 rest output ant input buffers to new pointer locations. */
372 if (!csOut || csOut.length === 0) {
373 csOut = this.csoundOutputBuffer = new Float64Array(
374 this.wasm.wasi.memory.buffer,
375 this.csoundApi.csoundGetSpout(this.csoundInstance),
376 ksmps * nchnls,
377 );
378 }
379
380 if (!csIn || csIn.length === 0) {
381 csIn = this.csoundInputBuffer = new Float64Array(
382 this.wasm.wasi.memory.buffer,
383 this.csoundApi.csoundGetSpin(this.csoundInstance),
384 ksmps * nchnlsIn,
385 );
386 }
387
388 // handle 1->1, 1->2, 2->1, 2->2 input channel count mixing and nchnls_i
389 const inputChanMax = Math.min(this.nchnls_i, input.numberOfChannels);
390 for (let channel = 0; channel < inputChanMax; channel++) {
391 const inputChannel = input.getChannelData(channel);
392 csIn[cnt * nchnlsIn + channel] = inputChannel[index] * zerodBFS;
393 }
394
395 // Output Channel mixing matches behavior of:
396 // https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#Up-mixing_and_down-mixing
397
398 // handle 1->1, 1->2, 2->1, 2->2 output channel count mixing and nchnls
399 if (this.nchnls === output.numberOfChannels) {
400 for (let channel = 0; channel < output.numberOfChannels; channel++) {
401 const outputChannel = output.getChannelData(channel);
402 if (result === 0) outputChannel[index] = csOut[cnt * nchnls + channel] / zerodBFS;
403 else outputChannel[index] = 0;
404 }
405 } else if (this.nchnls === 2 && output.numberOfChannels === 1) {
406 const outputChannel = output.getChannelData(0);
407 if (result === 0) {
408 const left = csOut[cnt * nchnls] / zerodBFS;
409 const right = csOut[cnt * nchnls + 1] / zerodBFS;
410 outputChannel[index] = 0.5 * (left + right);
411 } else {
412 outputChannel[index] = 0;
413 }
414 } else if (this.nchnls === 1 && output.numberOfChannels === 2) {
415 const outChan0 = output.getChannelData(0);
416 const outChan1 = output.getChannelData(1);
417
418 if (result === 0) {
419 const value = csOut[cnt * nchnls] / zerodBFS;
420 outChan0[index] = value;
421 outChan1[index] = value;
422 } else {
423 outChan0[index] = 0;
424 outChan1[index] = 0;
425 }
426 } else {
427 // FIXME: we do not support other cases at this time
428 }
429
430 // for (let channel = 0; channel < input.numberOfChannels; channel++) {
431 // const inputChannel = input.getChannelData(channel);
432 // csIn[cnt * nchnls_i + channel] = inputChannel[i] * zerodBFS;
433 // }
434 // for (let channel = 0; channel < output.numberOfChannels; channel++) {
435 // const outputChannel = output.getChannelData(channel);
436 // if (result == 0) outputChannel[i] = csOut[cnt * nchnls + channel] / zerodBFS;
437 // else outputChannel[i] = 0;
438 // }
439 }
440
441 this.cnt = cnt;
442 this.result = result;
443 }
444 }
445
446 export default ScriptProcessorNodeSingleThread;