Debian Codegraph @

wasm/browser/src/modules/rtmidi.js

raw
1 import { range } from "rambda/dist/rambda.esm.js";
2 import { freeStringPtr } from "../utils/string-pointers.js";
3 import { trimNull } from "../utils/trim-null.js";
4 import { structBufferToObject } from "../utils/structure-buffer-to-object.js";
5 import { sizeofStruct } from "../utils/native-sizes.js";
6 import { uint2String } from "../utils/text-encoders.js";
7 import { CS_MIDIDEVICE } from "../structures.js";
8
9 export const csoundSetMidiCallbacks = (wasm) => (csound /* CsoundInst */) => {
10 wasm.exports.csoundSetMidiCallbacks(csound);
11 };
12
13 /**
14 * This function can be called to obtain a list of available input or output midi devices.
15 * If list is NULL, the function will only return the number of devices
16 * (isOutput=1 for out devices, 0 for in devices).
17 * @function
18 */
19 // eslint-disable-next-line unicorn/prevent-abbreviations
20 export const csoundGetMIDIDevList = (wasm) => (csound /* CsoundInst */, isOutput /* number */) => {
21 const { buffer } = wasm.wasi.memory;
22 const numberOfDevices = wasm.exports.csoundGetMIDIDevList(csound, undefined, isOutput ? 1 : 0);
23 if (numberOfDevices === 0) return [];
24 const structLength = sizeofStruct(CS_MIDIDEVICE);
25 const structOffset = wasm.exports.allocCsMidiDeviceStruct(numberOfDevices);
26 wasm.exports.csoundGetMIDIDevList(csound, structOffset, isOutput ? 1 : 0);
27 const structBuffer = new Uint8Array(buffer, structOffset, structLength * numberOfDevices);
28 /** @type CS_MIDIDEVICE */
29 const out = range(0, numberOfDevices).map((index) =>
30 structBufferToObject(CS_MIDIDEVICE, structBuffer.subarray(index * structLength, structLength)),
31 );
32 wasm.exports.freeCsMidiDeviceStruct(structOffset);
33 return out;
34 };
35
36 csoundGetMIDIDevList.toString = () => "getMIDIDevList = async (isOutput) => Object;";
37
38 /**
39 * This function can be called to obtain a list of available input or output midi devices.
40 * If list is NULL, the function will only return the number of devices
41 * (isOutput=1 for out devices, 0 for in devices).
42 * @function
43 */
44 export const csoundGetRtMidiName = (wasm) => (csound /* CsoundInst */) => {
45 const { buffer } = wasm.wasi.memory;
46 const ptr = wasm.exports.getRtMidiName(csound);
47 const stringBuffer = new Uint8Array(buffer, ptr, 128);
48 return trimNull(uint2String(stringBuffer)) || "";
49 };
50
51 csoundGetRtMidiName.toString = () => "getRtMidiName = async () => String;";
52
53 export const csoundGetMidiOutFileName = (wasm) => (csound /* CsoundInst */) => {
54 const { buffer } = wasm.wasi.memory;
55 const ptr = wasm.exports.getMidiOutFileName(csound);
56 const stringBuffer = new Uint8Array(buffer, ptr, 128);
57 ptr && ptr.length > 0 && freeStringPtr(wasm, ptr);
58 return trimNull(uint2String(stringBuffer)) || "";
59 };
60
61 export const _isRequestingRtMidiInput = (wasm) => (csound /* CsoundInst */) => {
62 return wasm.exports.isRequestingRtMidiInput(csound);
63 };
64
65 /**
66 * Emit a midi message with a given triplet of values
67 * in the range of 0 to 127.
68 * @function
69 */
70 export const csoundPushMidiMessage =
71 (wasm) =>
72 (csound /* CsoundInst */, status /* number */, data1 /* number */, data2 /* number */) => {
73 wasm.exports.pushMidiMessage(csound, status, data1, data2);
74 };
75
76 csoundPushMidiMessage.toString = () => "midiMessage = async (status, data1, data2) => undefined;";
77
78 // PUBLIC void csoundSetMIDIModule (CSOUND *csound, const char *module)
79 // PUBLIC void csoundSetHostImplementedMIDIIO (CSOUND *csound, int state)
80 // PUBLIC int csoundGetMIDIDevList (CSOUND *csound, CS_MIDIDEVICE *list, int isOutput)
81 // PUBLIC void csoundSetExternalMidiInOpenCallback (CSOUND *, int(*func)(CSOUND *, void **userData, const char *devName))
82 // PUBLIC void csoundSetExternalMidiReadCallback (CSOUND *, int(*func)(CSOUND *, void *userData, unsigned char *buf, int nBytes))
83 // PUBLIC void csoundSetExternalMidiInCloseCallback (CSOUND *, int(*func)(CSOUND *, void *userData))
84 // PUBLIC void csoundSetExternalMidiOutOpenCallback (CSOUND *, int(*func)(CSOUND *, void **userData, const char *devName))
85 // PUBLIC void csoundSetExternalMidiWriteCallback (CSOUND *, int(*func)(CSOUND *, void *userData, const unsigned char *buf, int nBytes))
86 // PUBLIC void csoundSetExternalMidiOutCloseCallback (CSOUND *, int(*func)(CSOUND *, void *userData))
87 // PUBLIC void csoundSetExternalMidiErrorStringCallback (CSOUND *, const char *(*func)(int))
88 // PUBLIC void csoundSetMIDIDeviceListCallback (CSOUND *csound, int(*mididevlist__)(CSOUND *, CS_MIDIDEVICE *list, int isOutput))