Debian Codegraph @

wasm/browser/src/modules/attributes.js

raw
1 /* eslint-disable unicorn/prevent-abbreviations */
2 import { freeStringPtr, string2ptr } from "../utils/string-pointers.js";
3 import { structBufferToObject } from "../utils/structure-buffer-to-object.js";
4 import { sizeofStruct } from "../utils/native-sizes.js";
5 import { CSOUND_PARAMS } from "../structures.js";
6
7 /*
8 csound attribute module from <csound.h>
9 https://csound.com/docs/api/modules.html
10 */
11
12 /**
13 * Returns the sample rate from Csound instance
14 * @async
15 * @function
16 * @name getSr
17 * @memberof CsoundObj
18 * @return {Promise.<number>}
19 */
20 export const csoundGetSr = (wasm) => (csound) => wasm.exports.csoundGetSr(csound);
21
22 csoundGetSr.toString = () => "getSr = async () => Number;";
23
24 /**
25 * Returns the control rate from Csound instance
26 * @async
27 * @function
28 * @name getKr
29 * @memberof CsoundObj
30 * @return {Promise.<number>}
31 */
32 export const csoundGetKr = (wasm) => (csound) => wasm.exports.csoundGetKr(csound);
33
34 csoundGetKr.toString = () => "getKr = async () => Number;";
35
36 /**
37 * Returns the ksmps value (kr/sr) from Csound instance
38 * @async
39 * @function
40 * @name getKsmps
41 * @memberof CsoundObj
42 * @return {Promise.<number>}
43 */
44 export const csoundGetKsmps = (wasm) => (csound) => wasm.exports.csoundGetKsmps(csound);
45
46 csoundGetKsmps.toString = () => "getKsmps = async () => Number;";
47
48 /**
49 * Returns the number of output channels from Csound instance
50 * @async
51 * @function
52 * @name getNchnls
53 * @memberof CsoundObj
54 * @return {Promise.<number>}
55 */
56 export const csoundGetNchnls = (wasm) => (csound) => wasm.exports.csoundGetNchnls(csound);
57
58 csoundGetNchnls.toString = () => "getNchnls = async () => Number;";
59
60 /**
61 * Returns the number of input channels from Csound instance
62 * @async
63 * @function
64 * @name getNchnlsInput
65 * @memberof CsoundObj
66 * @return {Promise.<number>}
67 */
68 export const csoundGetNchnlsInput = (wasm) => (csound) => wasm.exports.csoundGetNchnlsInput(csound);
69
70 csoundGetNchnlsInput.toString = () => "getNchnlsInput = async () => Number;";
71
72 /**
73 * Returns the value of csoundGet0dBFS
74 * @async
75 * @function
76 * @name get0dBFS
77 * @memberof CsoundObj
78 * @return {Promise.<number>}
79 */
80 export const csoundGet0dBFS = (wasm) => (csound) => wasm.exports.csoundGet0dBFS(csound);
81
82 csoundGet0dBFS.toString = () => "get0dBFS = async () => Number;";
83
84 /**
85 * Returns the A4 frequency reference
86 * @async
87 * @function
88 * @name getA4
89 * @memberof CsoundObj
90 * @return {Promise.<number>}
91 */
92 export const csoundGetA4 = (wasm) => (csound) => wasm.exports.csoundGetA4(csound);
93
94 csoundGetA4.toString = () => "getA4 = async () => Number;";
95
96 /**
97 * Return the current performance time in samples
98 * @async
99 * @function
100 * @name getCurrentTimeSamples
101 * @memberof CsoundObj
102 * @return {Promise.<number>}
103 */
104 export const csoundGetCurrentTimeSamples = (wasm) => (csound) =>
105 wasm.exports.csoundGetCurrentTimeSamples(csound);
106
107 csoundGetCurrentTimeSamples.toString = () => "getCurrentTimeSamples = async () => Number;";
108
109 /**
110 * Return the size of MYFLT in number of bytes
111 * @async
112 * @function
113 * @name getSizeOfMYFLT
114 * @memberof CsoundObj
115 * @return {Promise.<number>}
116 */
117 export const csoundGetSizeOfMYFLT = (wasm) => (csound) => wasm.exports.csoundGetSizeOfMYFLT(csound);
118
119 csoundGetSizeOfMYFLT.toString = () => "getSizeOfMYFLT = async () => Number;";
120
121 // TODO (do these make any sense in wasm?)
122 // csoundGetHostData
123 // csoundSetHostData
124
125 /**
126 * Set a single csound option (flag),
127 * no spaces are allowed in the string.
128 * @async
129 * @function
130 * @name setOption
131 * @memberof CsoundObj
132 * @param {string} option
133 * @return {Promise.<number>}
134 */
135 export const csoundSetOption = (wasm) => (csound, option) => {
136 const stringPtr = string2ptr(wasm, option);
137 const result = wasm.exports.csoundSetOption(csound, stringPtr);
138 freeStringPtr(wasm, stringPtr);
139 return result;
140 };
141
142 csoundSetOption.toString = () => "setOption = async (option) => Number;";
143
144 /**
145 * Configure Csound with a given set of
146 * parameters defined in the CSOUND_PARAMS structure.
147 * These parameters are the part of the OPARMS struct
148 * that are configurable through command line flags.
149 * The CSOUND_PARAMS structure can be obtained using
150 * csoundGetParams().
151 * These options should only be changed before
152 * performance has started.
153 * @async
154 * @function
155 * @name setParams
156 * @memberof CsoundObj
157 * @param {CSOUND_PARAMS} csoundParams - csoundParams object
158 * @return {Promise.<undefined>}
159 */
160 export const csoundSetParams = (wasm) => (csound, csoundParameters) => {
161 wasm.exports.csoundSetParams(csound, csoundParameters);
162 };
163
164 csoundSetParams.toString = () => "setParams = async (csoundParams) => undefined;";
165
166 /**
167 * Get the current set of parameters
168 * from a Csound instance
169 * in a CSOUND_PARAMS structure.
170 * @async
171 * @function
172 * @name getParams
173 * @memberof CsoundObj
174 * @return {Promise.<CSOUND_PARAMS>} - CSOUND_PARAMS object
175 */
176 export const csoundGetParams = (wasm) => (csound) => {
177 const { buffer } = wasm.wasi.memory;
178 const structLength = sizeofStruct(CSOUND_PARAMS);
179 const structOffset = wasm.exports.allocCsoundParamsStruct();
180 const structBuffer = new Uint8Array(buffer, structOffset, structLength);
181 wasm.exports.csoundGetParams(csound, structOffset);
182 const currentCsoundParameters = structBufferToObject(CSOUND_PARAMS, structBuffer);
183 wasm.exports.freeCsoundParams(structOffset);
184 return currentCsoundParameters;
185 };
186
187 csoundGetParams.toString = () => "getParams = async () => CSOUND_PARAMS;";
188
189 /**
190 * Returns whether Csound is set to print debug messages
191 * sent through the DebugMsg() internal API function.
192 * Anything different to 0 means true.
193 * @async
194 * @function
195 * @name getDebug
196 * @memberof CsoundObj
197 * @return {Promise.<number>}
198 */
199 export const csoundGetDebug = (wasm) => (csound) => wasm.exports.csoundGetDebug(csound);
200
201 csoundGetDebug.toString = () => "getDebug = async () => Number;";
202
203 /**
204 * Return the size of MYFLT in number of bytes
205 * @async
206 * @function
207 * @name setDebug
208 * @memberof CsoundObj
209 * @param {number} debug
210 * @return {Promise.<undefined>}
211 */
212 export const csoundSetDebug = (wasm) => (csound, debug) => {
213 wasm.exports.csoundSetDebug(csound, debug);
214 };
215
216 csoundSetDebug.toString = () => "setDebug = async (number) => undefined;";