Debian Codegraph @

wasm/browser/src/modules/performance.js

raw
1 import { freeStringPtr, string2ptr } from "../utils/string-pointers.js";
2
3 /*
4 csound performance module from <csound.h>
5 https://csound.com/docs/api/modules.html
6 */
7
8 /**
9 * Parses a csound orchestra string
10 * @async
11 * @function
12 * @name parseOrc
13 * @memberof CsoundObj
14 * @param {string} orc
15 * @return {Promise.<object>}
16 */
17 export const csoundParseOrc = (wasm) => (csound, orc) => wasm.exports.csoundParseOrc(csound, orc);
18
19 csoundParseOrc.toString = () => "parseOrc = async (orchestra) => Object;";
20
21 /**
22 * Compiles AST tree
23 * @async
24 * @function
25 * @name compileTree
26 * @memberof CsoundObj
27 * @param {object} tree
28 * @return {Promise.<number>}
29 */
30 export const csoundCompileTree = (wasm) => (csound, tree) =>
31 wasm.exports.csoundCompileTree(csound, tree);
32
33 csoundCompileTree.toString = () => "compileTree = async (tree) => Number;";
34
35 // TODO
36 // csoundDeleteTree (CSOUND *csound, TREE *tree)
37
38 /**
39 * Compiles a csound orchestra string
40 * @async
41 * @function
42 * @name compileOrc
43 * @memberof CsoundObj
44 * @param {string} orc
45 * @return {Promise.<number>}
46 */
47 export const csoundCompileOrc = (wasm) => (csound, orc) => {
48 const stringPtr = string2ptr(wasm, orc);
49 const result = wasm.exports.csoundCompileOrc(csound, stringPtr);
50 freeStringPtr(wasm, stringPtr);
51 return result;
52 };
53
54 csoundCompileOrc.toString = () => "compileOrc = async (orchestra) => Number;";
55
56 /**
57 * Compiles a csound orchestra string
58 * @async
59 * @function
60 * @name evalCode
61 * @memberof CsoundObj
62 * @param {string} orc
63 * @return {Promise.<number>}
64 */
65 export const csoundEvalCode = (wasm) => (csound, orc) => {
66 const stringPtr = string2ptr(wasm, orc);
67 const result = wasm.exports.csoundEvalCode(csound, stringPtr);
68 freeStringPtr(wasm, stringPtr);
69 return result;
70 };
71
72 csoundEvalCode.toString = () => "csoundEvalCode = async (orchestra) => Number;";
73
74 // TODO
75 // csoundInitializeCscore (CSOUND *, FILE *insco, FILE *outsco)
76
77 // TODO
78 // csoundCompileArgs (CSOUND *, int argc, const char **argv)
79
80 /**
81 * Prepares Csound for performance
82 * @async
83 * @function
84 * @name start
85 * @memberof CsoundObj
86 * @return {Promise.<number>}
87 */
88 export const csoundStart = (wasm) => (csound) => wasm.exports.csoundStartWasi(csound);
89
90 csoundStart.toString = () => "start = async () => Number;";
91
92 // TODO
93 // csoundCompile (CSOUND *, int argc, const char **argv)
94
95 /**
96 * Compiles a Csound input file but does not perform it.
97 * @async
98 * @function
99 * @name compileCsd
100 * @memberof CsoundObj
101 * @param {string} path
102 * @return {Promise.<number>}
103 */
104 export const csoundCompileCsd = (wasm) => (csound, path) => {
105 const stringPtr = string2ptr(wasm, path);
106
107 let result;
108 try {
109 result = wasm.exports.csoundCompileCsd(csound, stringPtr);
110 } catch (error) {
111 console.error(error);
112 }
113 freeStringPtr(wasm, stringPtr);
114 return result;
115 };
116
117 csoundCompileCsd.toString = () => "compileCsd = async (path) => Number;";
118
119 /**
120 * Compiles a CSD string but does not perform it.
121 * @async
122 * @function
123 * @name compileCsdText
124 * @memberof CsoundObj
125 * @param {string} orc
126 * @return {Promise.<number>}
127 */
128 export const csoundCompileCsdText = (wasm) => (csound, orc) => {
129 const stringPtr = string2ptr(wasm, orc);
130 const result = wasm.exports.csoundCompileCsdText(csound, stringPtr);
131 freeStringPtr(wasm, stringPtr);
132 return result;
133 };
134
135 csoundCompileCsdText.toString = () => "compileCsdText = async (csoundDocument) => Number;";
136
137 /**
138 * Performs(plays) audio until end is reached
139 * @async
140 * @function
141 * @name perform
142 * @memberof CsoundObj
143 * @return {Promise.<number>}
144 */
145 export const csoundPerform = (wasm) => (csound) => wasm.exports.csoundPerform(csound);
146
147 csoundPerform.toString = () => "perform = async () => Number;";
148
149 /**
150 * Performs(plays) 1 ksmps worth of sample(s)
151 * @async
152 * @function
153 * @name performKsmps
154 * @memberof CsoundObj
155 * @return {Promise.<number>}
156 */
157 export const csoundPerformKsmps = (wasm) => (csound) => wasm.exports.csoundPerformKsmpsWasi(csound);
158
159 csoundPerformKsmps.toString = () => "performKsmps = async (csound) => Number;";
160
161 /**
162 * Performs(plays) 1 buffer worth of audio
163 * @async
164 * @function
165 * @name performBuffer
166 * @memberof CsoundObj
167 * @return {Promise.<number>}
168 */
169 export const csoundPerformBuffer = (wasm) => (csound) => wasm.exports.csoundPerformBuffer(csound);
170
171 csoundPerformBuffer.toString = () => "performBuffer = async (csound) => Number;";
172
173 /**
174 * Stops a csoundPerform
175 * @async
176 * @function
177 * @name stop
178 * @memberof CsoundObj
179 * @return {Promise.<undefined>}
180 */
181 export const csoundStop = (wasm) => (csound) => wasm.exports.csoundStop(csound);
182
183 csoundStop.toString = () => "stop = async () => undefined;";
184
185 /**
186 * Prints information about the end of a performance,
187 * and closes audio and MIDI devices.
188 * @async
189 * @function
190 * @name cleanup
191 * @memberof CsoundObj
192 * @return {Promise.<number>}
193 */
194 export const csoundCleanup = (wasm) => (csound) => wasm.exports.csoundCleanup(csound);
195
196 csoundCleanup.toString = () => "cleanup = async () => Number;";
197
198 /**
199 * Prints information about the end of a performance,
200 * and closes audio and MIDI devices.
201 * @async
202 * @function
203 * @name reset
204 * @memberof CsoundObj
205 * @return {Promise.<number>}
206 */
207 export const csoundReset = (wasm) => (csound) => wasm.exports.csoundResetWasi(csound);
208
209 csoundReset.toString = () => "reset = async () => Number;";