1 /************************************************************************
2  ************************************************************************
3  Copyright (C) 2021 GRAME, Centre National de Creation Musicale
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation; either version 2.1 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19  ************************************************************************
20  ************************************************************************/
21 
22 #ifndef LIBFAUSTCOMMON_C_H
23 #define LIBFAUSTCOMMON_C_H
24 
25 /*!
26  \addtogroup signalc C interface for the Signal API.
27  @{
28  */
29 
30 
31 /**
32  * Opaque types.
33  */
34 
35 #ifdef _MSC_VER
36 typedef void CTree;
37 #else
38 typedef struct {} CTree;
39 #endif
40 
41 typedef CTree* Signal;
42 typedef CTree* Box;
43 
44 enum SType { kSInt, kSReal };
45 
46 enum SOperator { kAdd, kSub, kMul, kDiv, kRem, kLsh, kARsh, kLRsh, kGT, kLT, kGE, kLE, kEQ, kNE, kAND, kOR, kXOR };
47 
48 #endif
49 
50 #ifndef LIBFAUSTSIGNAL_C_H
51 #define LIBFAUSTSIGNAL_C_H
52 
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57 
58     /**
59      * Create global compilation context, has to be done first.
60      */
61     void createLibContext();
62 
63     /**
64      * Destroy global compilation context, has to be done last.
65      */
66     void destroyLibContext();
67 
68     /**
69      * Constant integer : for all t, x(t) = n.
70      *
71      * @param n - the integer
72      *
73      * @return the integer value.
74      */
75     Signal CsigInt(int n);
76 
77     /**
78      * Constant real : for all t, x(t) = n.
79      *
80      * @param n - the float/double value (depends of -single or -double compilation parameter)
81      *
82      * @return the float/double signal.
83      */
84     Signal CsigReal(double n);
85 
86     /**
87      * Create an input.
88      *
89      * @param idx - the input index
90      *
91      * @return the input signal.
92      */
93     Signal CsigInput(int idx);
94 
95     /**
96      * Create a delayed signal.
97      *
98      * @param s - the signal to be delayed
99      * @param del - the delay signal that doesn't have to be fixed but must be bounded and cannot be negative
100      *
101      * @return the delayed signal.
102      */
103     Signal CsigDelay(Signal s, Signal del);
104 
105     /**
106      * Create a casted signal.
107      *
108      * @param s - the signal to be casted in integer
109      *
110      * @return the casted signal.
111      */
112     Signal CsigIntCast(Signal s);
113 
114     /**
115      * Create a casted signal.
116      *
117      * @param s - the signal to be casted as float/double value (depends of -single or -double compilation parameter)
118      *
119      * @return the casted signal.
120      */
121     Signal CsigFloatCast(Signal s);
122 
123     /**
124      * Create a read only table.
125      *
126      * @param n - the table size, a constant numerical expression (see [1])
127      * @param init - the table content
128      * @param ridx - the read index (an int between 0 and n-1)
129      *
130      * @return the table signal.
131      */
132     Signal CsigReadOnlyTable(Signal n, Signal init, Signal ridx);
133 
134     /**
135      * Create a read/write table.
136      *
137      * @param n - the table size, a constant numerical expression (see [1])
138      * @param init - the table content
139      * @param widx - the write index (an integer between 0 and n-1)
140      * @param wsig - the input of the table
141      * @param ridx - the read index (an integer between 0 and n-1)
142      *
143      * @return the table signal.
144      */
145     Signal CsigWriteReadTable(Signal n, Signal init, Signal widx, Signal wsig, Signal ridx);
146 
147     /**
148      * Create a waveform.
149      *
150      * @param wf - the content of the waveform as a null terminated array of CsigInt or CsigDouble signals
151      *
152      * @return the waveform signal.
153      */
154     Signal CsigWaveform(Signal* wf);
155     // Use: CsigInt(size of wf); to generate the waveform size signal
156 
157     /**
158      * Create a soundfile block.
159      *
160      * @param label - of form "label[url:{'path1';'path2';'path3'}]" to describe a list of soundfiles
161      *
162      * @return the soundfile block.
163      */
164     Signal CsigSoundfile(const char* label);
165 
166     /**
167      * Create the length signal of a given soundfile in frames.
168      *
169      * @param sf - the soundfile
170      * @param part - in the [0..255] range to select a given sound number, a constant numerical expression (see [1])
171      *
172      * @return the length signal.
173      */
174     Signal CsigSoundfileLength(Signal sf, Signal part);
175 
176     /**
177      * Create the rate signal of a given soundfile in Hz.
178      *
179      * @param sf - the soundfile
180      * @param part - in the [0..255] range to select a given sound number, a constant numerical expression (see [1])
181      *
182      * @return the rate signal.
183      */
184     Signal CsigSoundfileRate(Signal sf, Signal part);
185 
186     /**
187      * Create the buffer signal of a given soundfile.
188      *
189      * @param sf - the soundfile
190      * @param chan - an integer to select a given channel, a constant numerical expression (see [1])
191      * @param part - in the [0..255] range to select a given sound number, a constant numerical expression (see [1])
192      * @param ridx - the read index (an integer between 0 and the selected sound length)
193      *
194      * @return the buffer signal.
195      */
196     Signal CsigSoundfileBuffer(Signal sf, Signal chan, Signal part, Signal ridx);
197 
198     /**
199      * Create a selector between two signals.
200      *
201      * @param selector - when 0 at time t returns s1[t], otherwise returns s2[t]
202      * (selector is automatically wrapped with sigIntCast)
203      * @param s1 - first signal to be selected
204      * @param s2 - second signal to be selected
205      *
206      * @return the selected signal depending of the selector value at each time t.
207      */
208     Signal CsigSelect2(Signal selector, Signal s1, Signal s2);
209 
210     /**
211      * Create a selector between three signals.
212      *
213      * @param selector - when 0 at time t returns s1[t], when 1 at time t returns s2[t], otherwise returns s3[t]
214      * (selector is automatically wrapped with sigIntCast)
215      * @param s1 - first signal to be selected
216      * @param s2 - second signal to be selected
217      * @param s3 - third signal to be selected
218      *
219      * @return the selected signal depending of the selector value at each time t.
220      */
221     Signal CsigSelect3(Signal selector, Signal s1, Signal s2, Signal s3);
222 
223     /**
224      * Create a foreign constant signal.
225      *
226      * @param type - the foreign constant type of SType
227      * @param name - the foreign constant name
228      * @param file - the include file where the foreign constant is defined
229      *
230      * @return the foreign constant signal.
231      */
232     Signal CsigFConst(enum SType type, const char* name, const char* file);
233 
234     /**
235      * Create a foreign variable signal.
236      *
237      * @param type - the foreign variable type of SType
238      * @param name - the foreign variable name
239      * @param file - the include file where the foreign variable is defined
240      *
241      * @return the foreign variable signal.
242      */
243     Signal CsigFVar(enum SType type, const char* name, const char* file);
244 
245     /**
246      * Generic binary mathematical functions.
247      *
248      * @param op - the operator in SOperator set
249      * @param x - first signal
250      * @param y - second signal
251      *
252      * @return the result signal of op(x,y).
253      */
254     Signal CsigBinOp(enum SOperator op, Signal x, Signal y);
255 
256     /**
257      * Specific binary mathematical functions.
258      *
259      * @param x - first signal
260      * @param y - second signal
261      *
262      * @return the result signal of fun(x,y).
263      */
264     Signal CsigAdd(Signal x, Signal y);
265     Signal CsigSub(Signal x, Signal y);
266     Signal CsigMul(Signal x, Signal y);
267     Signal CsigDiv(Signal x, Signal y);
268     Signal CsigRem(Signal x, Signal y);
269 
270     Signal CsigLeftShift(Signal x, Signal y);
271     Signal CsigLRightShift(Signal x, Signal y);
272     Signal CsigARightShift(Signal x, Signal y);
273 
274     Signal CsigGT(Signal x, Signal y);
275     Signal CsigLT(Signal x, Signal y);
276     Signal CsigGE(Signal x, Signal y);
277     Signal CsigLE(Signal x, Signal y);
278     Signal CsigEQ(Signal x, Signal y);
279     Signal CsigNE(Signal x, Signal y);
280 
281     Signal CsigAND(Signal x, Signal y);
282     Signal CsigOR(Signal x, Signal y);
283     Signal CsigXOR(Signal x, Signal y);
284 
285     /**
286      * Extended unary mathematical functions.
287      */
288     Signal CsigAbs(Signal x);
289     Signal CsigAcos(Signal x);
290     Signal CsigTan(Signal x);
291     Signal CsigSqrt(Signal x);
292     Signal CsigSin(Signal x);
293     Signal CsigRint(Signal x);
294     Signal CsigLog(Signal x);
295     Signal CsigLog10(Signal x);
296     Signal CsigFloor(Signal x);
297     Signal CsigExp(Signal x);
298     Signal CsigExp10(Signal x);
299     Signal CsigCos(Signal x);
300     Signal CsigCeil(Signal x);
301     Signal CsigAtan(Signal x);
302     Signal CsigAsin(Signal x);
303 
304     /**
305      * Extended binary mathematical functions.
306      */
307     Signal CsigRemainder(Signal x, Signal y);
308     Signal CsigPow(Signal x, Signal y);
309     Signal CsigMin(Signal x, Signal y);
310     Signal CsigMax(Signal x, Signal y);
311     Signal CsigFmod(Signal x, Signal y);
312     Signal CsigAtan2(Signal x, Signal y);
313 
314     /**
315      * Create a recursive signal inside the sigRecursion expression.
316      *
317      * @return the recursive signal.
318      */
319     Signal CsigSelf();
320 
321     /**
322      * Create a recursive signal. Use sigSelf() to refer to the
323      * recursive signal inside the sigRecursion expression.
324      *
325      * @param s - the signal to recurse on.
326      *
327      * @return the signal with a recursion.
328      */
329     Signal CsigRecursion(Signal s);
330 
331     /**
332      * Create a button signal.
333      *
334      * @param label - the label definition (see [2])
335      *
336      * @return the button signal.
337      */
338     Signal CsigButton(const char* label);
339 
340     /**
341      * Create a checkbox signal.
342      *
343      * @param label - the label definition (see [2])
344      *
345      * @return the checkbox signal.
346      */
347     Signal CsigCheckbox(const char* label);
348 
349     /**
350      * Create a vertical slider signal.
351      *
352      * @param label - the label definition (see [2])
353      * @param init - the init signal, a constant numerical expression (see [1])
354      * @param min - the max signal, a constant numerical expression (see [1])
355      * @param max - the min signal, a constant numerical expression (see [1])
356      * @param step - the step signal, a constant numerical expression (see [1])
357      *
358      * @return the vertical slider signal.
359      */
360     Signal CsigVSlider(const char* label, Signal init, Signal min, Signal max, Signal step);
361 
362     /**
363      * Create an horizontal slider signal.
364      *
365      * @param label - the label definition (see [2])
366      * @param init - the init signal, a constant numerical expression (see [1])
367      * @param min - the max signal, a constant numerical expression (see [1])
368      * @param max - the min signal, a constant numerical expression (see [1])
369      * @param step - the step signal, a constant numerical expression (see [1])
370      *
371      * @return the horizontal slider signal.
372      */
373     Signal CsigHSlider(const char* label, Signal init, Signal min, Signal max, Signal step);
374 
375     /**
376      * Create a num entry signal.
377      *
378      * @param label - the label definition (see [2])
379      * @param init - the init signal, a constant numerical expression (see [1])
380      * @param min - the max signal, a constant numerical expression (see [1])
381      * @param max - the min signal, a constant numerical expression (see [1])
382      * @param step - the step signal, a constant numerical expression (see [1])
383      *
384      * @return the num entry signal.
385      */
386     Signal CsigNumEntry(const char* label, Signal init, Signal min, Signal max, Signal step);
387 
388     /**
389      * Create a vertical bargraph signal.
390      *
391      * @param label - the label definition (see [2])
392      * @param min - the max signal, a constant numerical expression (see [1])
393      * @param max - the min signal, a constant numerical expression (see [1])
394      * @param s - the input signal
395      *
396      * @return the vertical bargraph signal.
397      */
398     Signal CsigVBargraph(const char* label, Signal min, Signal max, Signal s);
399 
400     /**
401      * Create an horizontal bargraph signal.
402      *
403      * @param label - the label definition (see [2])
404      * @param min - the max signal, a constant numerical expression (see [1])
405      * @param max - the min signal, a constant numerical expression (see [1])
406      * @param s - the input signal
407      *
408      * @return the horizontal bargraph signal.
409      */
410     Signal CsigHBargraph(const char* label, Signal min, Signal max, Signal s);
411 
412     /**
413      * Create an attach signal.
414      *
415      * The attach primitive takes two input signals and produces one output signal
416      * which is a copy of the first input. The role of attach is to force
417      * its second input signal to be compiled with the first one.
418      *
419      * @param s1 - the first signal
420      * @param s2 - the second signal
421      *
422      * @return the attach signal.
423      */
424     Signal CsigAttach(Signal s1, Signal s2);
425 
426     /*
427      [1] Constant numerical expression : see https://faustdoc.grame.fr/manual/syntax/#constant-numerical-expressions
428      [2] Label definition : https://faustdoc.grame.fr/manual/syntax/#variable-parts-of-a-label
429      */
430 
431 #ifdef __cplusplus
432 }
433 #endif
434 
435 /*!
436  @}
437  */
438 
439 #endif
440