Debian Codegraph @

wasm/nodejs/src/singlethread.js

raw
1 import libcsoundFactory from "@csound/browser/dist/factory.esm.js";
2 import loadWasm from "./module.js";
3 import Speaker from "speaker";
4 import { Readable } from "stream";
5 import { csoundApiRename } from "./utils.js";
6
7 export default class SingleThread {
8 constructor() {
9 this.csoundInstance = undefined;
10 this.libcsound = undefined;
11 this.exportApi = undefined;
12
13 this.initialize = this.initialize.bind(this);
14 this.handleStart = this.handleStart.bind(this);
15 this.processRealtimeAudio = this.processRealtimeAudio.bind(this);
16 }
17
18 processKsmps(byteCount) {
19 const that = this.that;
20 const frameCount = byteCount / Float32Array.BYTES_PER_ELEMENT / this.nchnls;
21 const buffer = Buffer.alloc(byteCount);
22 if (this.startClickKillCount < 8) {
23 this.push(buffer);
24 this.startClickKillCount += 1;
25 return;
26 }
27 for (let frameIndex = 0; frameIndex < frameCount; frameIndex++) {
28 if (this.csoundOutputBuffer.length === 0) {
29 this.csoundOutputBuffer = new Float64Array(
30 that.wasm.exports.memory.buffer,
31 this.outputBufferPtr,
32 this.ksmps * this.nchnls,
33 );
34 }
35 if (this.csoundBufferPos === 0) {
36 this.lastReturn = that.libcsound.csoundPerformKsmps(that.csoundInstance);
37 }
38
39 if (this.lastReturn !== 0) {
40 return;
41 }
42
43 for (let channelIndex = 0; channelIndex < this.nchnls; channelIndex++) {
44 const csoundSampl = this.csoundOutputBuffer[
45 this.csoundBufferPos * this.nchnls + channelIndex
46 ];
47
48 buffer.writeFloatLE(
49 csoundSampl,
50 channelIndex * Float32Array.BYTES_PER_ELEMENT +
51 frameIndex * this.nchnls * Float32Array.BYTES_PER_ELEMENT,
52 );
53 }
54
55 this.csoundBufferPos = (this.csoundBufferPos + 1) % this.ksmps;
56 }
57
58 this.push(buffer);
59 }
60
61 processRealtimeAudio() {
62 const nchnls = this.libcsound.csoundGetNchnls(this.csoundInstance);
63 const ksmps = this.libcsound.csoundGetKsmps(this.csoundInstance);
64
65 const stream = new Readable();
66
67 const speaker = new Speaker({
68 float: true,
69 signed: true,
70 bitDepth: 32,
71 channels: nchnls,
72 sampleRate: this.libcsound.csoundGetSr(this.csoundInstance),
73 });
74
75 stream._read = this.processKsmps;
76 stream.that = this;
77 stream.ksmps = ksmps;
78 stream.nchnls = nchnls;
79 stream.csoundBufferPos = 0;
80 stream.zeroDecibelFullScale = this.libcsound.csoundGet0dBFS(this.csoundInstance);
81
82 const outputBufferPtr = this.libcsound.csoundGetSpout(this.csoundInstance);
83 stream.outputBufferPtr = outputBufferPtr;
84 stream.csoundOutputBuffer = new Float64Array(
85 this.wasm.exports.memory.buffer,
86 outputBufferPtr,
87 ksmps * nchnls,
88 );
89 stream.startClickKillCount = 0;
90 stream.pipe(speaker);
91 }
92
93 handleStart() {
94 const shouldDemonize = this.libcsound.csoundShouldDaemonize(this.csoundInstance) === 1;
95
96 if (shouldDemonize) {
97 this.libcsound.csoundSetOption(this.csoundInstance, "--daemon");
98 this.libcsound.csoundSetOption(this.csoundInstance, "-odac");
99 }
100
101 const startError = this.wasm.exports.csoundStartWasi(this.csoundInstance);
102 const outputName = this.libcsound.csoundGetOutputName(this.csoundInstance) || "test.wav";
103 const isExpectingRealtimeOutput = shouldDemonize || outputName.includes("dac");
104
105 if (startError !== 0) {
106 console.error(`error: start failed look out syntax or configuration errors`);
107 return;
108 }
109
110 if (isExpectingRealtimeOutput) {
111 this.processRealtimeAudio();
112 }
113 }
114
115 async initialize({ withPlugins = [] }) {
116 this.wasm = await loadWasm({ withPlugins });
117 this.libcsound = libcsoundFactory(this.wasm);
118 this.libcsound.csoundInitialize();
119 this.csoundInstance = this.libcsound.csoundCreate(0);
120 this.exportApi = Object.keys(this.libcsound).reduce((acc, key) => {
121 switch (key) {
122 case "csoundCreate": {
123 break;
124 }
125 case "csoundStart": {
126 const csoundStart = () => {
127 this.handleStart();
128 };
129
130 acc.start = csoundStart;
131 break;
132 }
133 default: {
134 acc[csoundApiRename(key)] = (...args) =>
135 this.libcsound[key].apply({}, [this.csoundInstance, ...args]);
136 }
137 }
138
139 return acc;
140 }, {});
141 return this.exportApi;
142 }
143 }
144 setTimeout(() => {}, 100000);