Debian Codegraph @

wasm/src/csound_wasm.c

raw
1 #include "csound.h"
2 #include "csoundCore.h"
3 #include "emugens_common.h"
4 #include <errno.h>
5 #include <limits.h>
6 #include <stdarg.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 // returns the address of a string
14 // pointer which is writable from js
15 __attribute__((used))
16 char* allocStringMem (int length) {
17 char *ptr = NULL;
18 ptr = malloc(((length + 1) * sizeof(char)));
19 // NULL Terminate
20 ptr[length] = 0;
21 return ptr;
22 }
23
24 // free the allocated String Memory
25 // (this could be unneccecary, dont know)
26 void freeStringMem (char* ptr) {
27 free(ptr);
28 }
29
30 __attribute__((used))
31 CSOUND_PARAMS* allocCsoundParamsStruct() {
32 CSOUND_PARAMS* ptr = NULL;
33 ptr = malloc(sizeof(CSOUND_PARAMS));
34 return ptr;
35 }
36
37 void freeCsoundParams(CSOUND_PARAMS* ptr) {
38 free(ptr);
39 }
40
41 __attribute__((used))
42 double* allocFloatArray(int length) {
43 double *ptr = NULL;
44 ptr = malloc(length * sizeof(double));
45 return ptr;
46 }
47
48 void freeFloatArrayMem(double* ptr) {
49 free(ptr);
50 }
51
52
53 // START CS_MIDIDEVICE
54 int sizeOfMidiStruct() {
55 // TODO: write comparison test of js/c sizeof
56 return sizeof(CS_MIDIDEVICE);
57 }
58
59 CS_MIDIDEVICE* allocCsMidiDeviceStruct(int num) {
60 CS_MIDIDEVICE* ptr = NULL;
61 ptr = malloc(sizeof(CS_MIDIDEVICE) * num);
62 return ptr;
63 }
64
65
66 void freeCsMidiDeviceStruct(CSOUND_PARAMS* ptr) {
67 free(ptr);
68 }
69
70 // internal function to determine
71 // if csound has started in daemon mode
72 // or without orcCompile*/evalCode* called
73 __attribute__((used))
74 int csoundShouldDaemonize(CSOUND *csound) {
75 if (csound->oparms->daemon == 1 ||
76 (csound->instr0 == NULL && *csound->csdname == '\0')) {
77 return 1;
78 } else {
79 return 0;
80 }
81 }
82
83
84 // END CS_MIDIDEVICE
85 __attribute__((used))
86 int csoundStartWasi(CSOUND *csound) {
87 const char* outputDev = csoundGetOutputName(csound);
88
89 // detect realtime mode automatically
90 if ((strncmp("dac", outputDev, 3) == 0) ||
91 csoundShouldDaemonize(csound)) {
92 csoundSetHostImplementedAudioIO(csound, 1, 0);
93 }
94 return csoundStart(csound);
95 }
96
97 extern int sensevents(CSOUND *);
98
99 // The built-in performKsmps has mutex and setjmp
100 // which we don't have in wasi based wasm
101 int csoundPerformKsmpsWasi(CSOUND *csound)
102 {
103 int done;
104
105 if (UNLIKELY(!(csound->engineStatus & CS_STATE_COMP))) {
106 csound->Warning(csound,
107 Str("Csound not ready for performance: csoundStart() "
108 "has not been called\n"));
109 return CSOUND_ERROR;
110 }
111 done = sensevents(csound);
112 if (done || csound->performState == -1) {
113 csoundMessage(csound, Str("Score finished in csoundPerformKsmps() with %d.\n"), done);
114 return -1;
115 } else {
116 return csound->kperf(csound);
117 }
118 }
119 // for message callbacks, probably we don't want this for non-js
120 // wasm interpretors
121
122 void csoundWasiJsMessageCallback(
123 CSOUND *csound,
124 int attr,
125 int len,
126 const char *str
127 ) __attribute__((
128 used,
129 __import_module__("env"),
130 __import_name__("csoundWasiJsMessageCallback")
131 ));
132
133
134 void csoundWasiCMessageCallback(CSOUND *csound, int attr, const char *format, va_list args) {
135 char buffer[MAX_MESSAGE_STR];
136 int len = vsnprintf(buffer, MAX_MESSAGE_STR, format, args);
137 (* csoundWasiJsMessageCallback)(csound, attr, len, buffer);
138 }
139
140
141 void __wasi_js_csoundSetMessageStringCallback() {
142 return csoundSetDefaultMessageCallback(&csoundWasiCMessageCallback);
143 }
144
145 // copy/paste from upstream csound-emscripten
146 // https://github.com/csound/csound/blob/develop/Emscripten/src/CsoundObj.c
147
148 #define MIDI_QUEUE_SIZE 1024
149
150 struct MidiData {
151 unsigned char status;
152 unsigned char data1;
153 unsigned char data2;
154 unsigned char flag;
155 };
156
157 struct MidiCallbackData {
158 struct MidiData *midiData;
159 int p, q;
160 };
161
162 struct MidiData midiData[MIDI_QUEUE_SIZE];
163 struct MidiCallbackData midiCallbackData = { midiData, 0, 0 };
164
165 void pushMidiMessage(CSOUND *csound, unsigned char status, unsigned char data1, unsigned char data2){
166 midiCallbackData.midiData[midiCallbackData.p].status = status;
167 midiCallbackData.midiData[midiCallbackData.p].data1 = data1;
168 midiCallbackData.midiData[midiCallbackData.p].data2= data2;
169 midiCallbackData.midiData[midiCallbackData.p].flag = 1;
170 midiCallbackData.p++;
171 if (midiCallbackData.p == MIDI_QUEUE_SIZE) {
172 midiCallbackData.p = 0;
173 }
174 }
175
176 /* used to distinguish between 1 and 2-byte messages */
177 static const int datbyts[8] = { 2, 2, 2, 2, 1, 1, 2, 0 };
178
179 /* csound MIDI read callback, called every k-cycle */
180 static int midiDataRead(CSOUND *csound, void *userData, unsigned char *mbuf, int nbytes) {
181 struct MidiData *mdata = midiCallbackData.midiData;
182 int *q = &midiCallbackData.q, st, d1, d2, n = 0;
183 /* check if there is new data on circular queue */
184 while (mdata[*q].flag) {
185 st = (int) mdata[*q].status;
186 d1 = (int) mdata[*q].data1;
187 d2 = (int) mdata[*q].data2;
188 if (st < 0x80)
189 goto next;
190 if (st >= 0xF0 &&
191 !(st == 0xF8 || st == 0xFA || st == 0xFB ||
192 st == 0xFC || st == 0xFF))
193 goto next;
194 nbytes -= (datbyts[(st - 0x80) >> 4] + 1);
195 if (nbytes < 0) break;
196 /* write to csound midi buffer */
197 n += (datbyts[(st - 0x80) >> 4] + 1);
198 switch (datbyts[(st - 0x80) >> 4]) {
199 case 0:
200 *mbuf++ = (unsigned char) st;
201 break;
202 case 1:
203 *mbuf++ = (unsigned char) st;
204 *mbuf++ = (unsigned char) d1;
205 break;
206 case 2:
207 *mbuf++ = (unsigned char) st;
208 *mbuf++ = (unsigned char) d1;
209 *mbuf++ = (unsigned char) d2;
210 break;
211 }
212 next:
213 mdata[*q].flag = 0;
214 (*q)++;
215 if(*q== MIDI_QUEUE_SIZE) *q = 0;
216 }
217 /* return the number of bytes read */
218 return n;
219 }
220
221 static int midiInOpen(CSOUND *csound, void **userData, const char *dev) {
222 return OK;
223 }
224
225 static int midiInClose(CSOUND *csound, void *userData) {
226 return OK;
227 }
228
229 __attribute__((used))
230 void csoundSetMidiCallbacks(CSOUND *csound) {
231 csoundSetHostImplementedMIDIIO(csound, 1);
232 csoundSetExternalMidiInOpenCallback(csound, midiInOpen);
233 csoundSetExternalMidiReadCallback(csound, midiDataRead);
234 csoundSetExternalMidiInCloseCallback(csound, midiInClose);
235 }
236
237
238 // same as csoundCreate but also loads
239 // opcodes which need initialization to
240 // be callable (aka static_modules)
241 __attribute__((used))
242 CSOUND *csoundCreateWasi() {
243 CSOUND *csound = csoundCreate(NULL);
244 csoundSetMidiCallbacks(csound);
245 return csound;
246 }
247
248 // same as csoundReset but also loads
249 // opcodes which need re-initialization to
250 // be callable (aka static_modules)
251 int csoundResetWasi(CSOUND *csound) {
252 int cleanResult = csoundCleanup(csound);
253 csoundReset(csound);
254 csoundSetMidiCallbacks(csound);
255 return cleanResult;
256 }
257
258 int isRequestingRtMidiInput(CSOUND *csound) {
259 if (csound->oparms->Midiin || csound->oparms->FMidiin || csound->oparms->RMidiin) {
260 return 1;
261 } else {
262 return 0;
263 }
264 }
265
266 char* getRtMidiName(CSOUND *csound) {
267 return csound->QueryGlobalVariable(csound, "_RTMIDI");
268 }
269
270 char* getMidiOutFileName(CSOUND *csound) {
271 if (csound->oparms->FMidiname == NULL) {
272 return "\0";
273 } else {
274 /* char* str = (char*) malloc((100)*sizeof(char)); */
275 /* sprintf(str, "%s\n", csound->oparms->FMidiname); */
276 /* printf("STR %s \n", str); */
277 return csound->oparms->FMidiname;
278 }
279 }
280
281 double csoundGetControlChannelWasi(CSOUND* csound, char* channelName) {
282 int *error = NULL;
283 double returnValue = csoundGetControlChannel(csound, channelName, error);
284
285 // printf("csoundGetControlChannel: Channel Name %s\n", channelName);
286 if (error != NULL) {
287 printf("csoundGetControlChannel: Error %d\n", *error);
288 return 0;
289 } else {
290 return returnValue;
291 }
292 }
293
294 char* csoundGetStringChannelWasi(CSOUND* csound, const char *channelName) {
295 int len = csoundGetChannelDatasize(csound, channelName);
296 char *data = calloc(1, sizeof(char) * (len + 1));
297
298 csoundGetStringChannel(csound, channelName, data);
299 return data;
300 }
301
302 extern size_t __heap_base;
303
304 // DUMMY MAIN (never called, but is needed)
305 int main (int argc, char *argv[] ) {}
306
307
308 // Compilation fix for unsupported functions defined
309 // wasi-libc/expected/wasm32-wasi/undefined-symbols.txt
310
311 int vsiprintf(char *restrict s, const char *restrict fmt, va_list ap) {
312 return vsnprintf(s, INT_MAX, fmt, ap);
313 }
314
315 int __small_vsprintf(char *restrict s, const char *restrict fmt, va_list ap) {
316 return vsnprintf(s, INT_MAX, fmt, ap);
317 }
318
319 int siprintf(char *restrict s, const char *restrict fmt, ...) {
320 int ret;
321 va_list ap;
322 va_start(ap, fmt);
323 ret = vsiprintf(s, fmt, ap);
324 va_end(ap);
325 return ret;
326 }
327
328 int __small_sprintf(char *restrict s, const char *restrict fmt, ...) {
329 int ret;
330 va_list ap;
331 va_start(ap, fmt);
332 ret = __small_vsprintf(s, fmt, ap);
333 va_end(ap);
334 return ret;
335 }
336
337
338 int32_t fiprintf(int32_t x, int32_t y, int32_t z) {
339 printf("ERROR: call to unsupported function fiprintf");
340 return 0;
341 }
342
343 int32_t __small_fprintf(int32_t x, int32_t y, int32_t z) {
344 printf("ERROR: call to unsupported function __small_fprintf");
345 return 0;
346 }
347
348 int32_t __getf2(int64_t x, int64_t y, int64_t z, int64_t zz) {
349 if (x > y) {
350 return 1;
351 } else if (x == y) {
352 return 0;
353 } else {
354 return -1;
355 }
356 }
357
358 int32_t __netf2(int64_t b, int64_t c, int64_t d, int64_t e) {
359 return __getf2(b, c, d, e);
360 }
361
362 int32_t __gttf2(int64_t b, int64_t c, int64_t d, int64_t e) {
363 return __getf2(b, c, d, e);
364 }
365
366 void __extenddftf2(int32_t x, double y) {}
367
368 void __multi3(int32_t a, int64_t b, int64_t c, int64_t d, int64_t e) {}
369
370 void __muloti4(int32_t a, int64_t b, int64_t c, int64_t d, int64_t d_, int32_t e) {}
371
372 int __lttf2(long double a, long double b) {
373 if (a > b) {
374 return 1;
375 } else if (a == b) {
376 return 0;
377 } else {
378 return -1;
379 }
380 }
381
382 void printDebugCallback(
383 const char *str,
384 int len
385 ) __attribute__((
386 used,
387 __import_module__("env"),
388 __import_name__("printDebugCallback")
389 ));
390
391 __attribute__((used))
392 void printDebug(const char *log) {
393 if (strlen(log) > 0) {
394 (* printDebugCallback)(log, strlen(log));
395 }
396 }