1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22 #ifndef _SIGNALS_
23 #define _SIGNALS_
24
25 #include <vector>
26
27 #include "binop.hh"
28 #include "exception.hh"
29 #include "tlib.hh"
30 #include "export.hh"
31
32 using namespace std;
33
34 #if defined(WIN32) && !defined(__GNUC__)
35 #pragma warning(disable : 4800)
36 #endif
37
38 ////////////////////////////////////////////////////////////////////////
39 /**
40 * Signals
41 * Block-diagrams are converted into signal expressions via
42 * symbolic propagation
43 */
44 ///////////////////////////////////////////////////////////////////////
45
46 typedef std::vector<Tree> siglist;
47
48 // Constant signals : for all t, x(t)=n
49 EXPORT Tree sigInt(int n);
50 EXPORT Tree sigReal(double n);
51
52 bool isSigInt(Tree t, int* i);
53 bool isSigReal(Tree t, double* r);
54
55 // Waveforms
56 EXPORT Tree sigWaveform(const tvec& wf);
57 bool isSigWaveform(Tree s);
58
59 // Inputs and outputs
60 EXPORT Tree sigInput(int i);
61 Tree sigOutput(int i, Tree t);
62
63 bool isSigInput(Tree t, int* i);
64 bool isSigOutput(Tree t, int* i, Tree& t0);
65
66 // Delay
67 Tree sigDelay0(Tree t);
68 Tree sigDelay1(Tree t);
69 bool isSigDelay1(Tree t, Tree& t0);
70
71 EXPORT Tree sigDelay(Tree t0, Tree t1);
72 bool isSigDelay(Tree t, Tree& t0, Tree& t1);
73
74 Tree sigPrefix(Tree t0, Tree t1);
75 bool isSigPrefix(Tree t, Tree& t0, Tree& t1);
76
77 Tree sigIota(Tree t0);
78 bool isSigIota(Tree t, Tree& t0);
79
80 // Int and Double casting
81 EXPORT Tree sigIntCast(Tree t);
82 EXPORT Tree sigFloatCast(Tree t);
83
84 bool isSigIntCast(Tree t);
85 bool isSigFloatCast(Tree t);
86
87 bool isSigIntCast(Tree t, Tree& x);
88 bool isSigFloatCast(Tree t, Tree& x);
89
90 // Tables
91 Tree sigRDTbl(Tree t, Tree i);
92 Tree sigWRTbl(Tree id, Tree t, Tree i, Tree s);
93 Tree sigTable(Tree id, Tree n, Tree sig);
94 Tree sigGen(Tree content);
95
96 bool isSigRDTbl(Tree s, Tree& t, Tree& i);
97 bool isSigWRTbl(Tree u, Tree& id, Tree& t, Tree& i, Tree& s);
98 bool isSigTable(Tree t, Tree& id, Tree& n, Tree& sig);
99 bool isSigGen(Tree t, Tree& content);
100 bool isSigGen(Tree t);
101
102 EXPORT Tree sigWriteReadTable(Tree n, Tree init, Tree widx, Tree wsig, Tree ridx);
103 EXPORT Tree sigReadOnlyTable(Tree n, Tree init, Tree ridx);
104
105 // Tables for documentator
106 // used to replace real tables for documentation purposes only
107 Tree sigDocConstantTbl(Tree n, Tree init);
108 Tree sigDocWriteTbl(Tree n, Tree init, Tree widx, Tree wsig);
109 Tree sigDocAccessTbl(Tree doctbl, Tree ridx);
110
111 bool isSigDocConstantTbl(Tree s, Tree& n, Tree& init);
112 bool isSigDocWriteTbl(Tree s, Tree& n, Tree& init, Tree& widx, Tree& wsig);
113 bool isSigDocAccessTbl(Tree s, Tree& doctbl, Tree& ridx);
114
115 // Selectors
116 EXPORT Tree sigSelect2(Tree selector, Tree s1, Tree s2);
117 EXPORT Tree sigSelect3(Tree selector, Tree s1, Tree s2, Tree s3);
118
119 bool isSigSelect2(Tree t, Tree& selector, Tree& s1, Tree& s2);
120
121 // Interval annotation
122 Tree sigAssertBounds(Tree s1, Tree s2, Tree s3);
123 Tree sigLowest(Tree s);
124 Tree sigHighest(Tree s);
125
126 bool isSigAssertBounds(Tree t, Tree& s1, Tree& s2, Tree& s3);
127 bool isSigLowest(Tree t, Tree& s);
128 bool isSigHighest(Tree t, Tree& s);
129
130 // Arithmetical operations
131 EXPORT Tree sigBinOp(int op, Tree x, Tree y);
132 bool isSigBinOp(Tree s, int* op, Tree& x, Tree& y);
133
134 // Foreign functions
135 Tree sigFFun(Tree ff, Tree largs);
136 bool isSigFFun(Tree s, Tree& ff, Tree& largs);
137
138 // Foreign constants
139 EXPORT Tree sigFConst(Tree type, Tree name, Tree file);
140 bool isSigFConst(Tree s);
141 bool isSigFConst(Tree s, Tree& type, Tree& name, Tree& file);
142
143 // Foreign variables
144 EXPORT Tree sigFVar(Tree type, Tree name, Tree file);
145 bool isSigFVar(Tree s);
146 bool isSigFVar(Tree s, Tree& type, Tree& name, Tree& file);
147
148 // Emulation of all fonctions
149 typedef Tree (* sigFun)(Tree, Tree);
150
151 EXPORT Tree sigAdd(Tree x, Tree y);
152 EXPORT Tree sigSub(Tree x, Tree y);
153 EXPORT Tree sigMul(Tree x, Tree y);
154 EXPORT Tree sigDiv(Tree x, Tree y);
155 Tree sigRem(Tree x, Tree y);
156
157 EXPORT Tree sigAND(Tree x, Tree y);
158 EXPORT Tree sigOR(Tree x, Tree y);
159 EXPORT Tree sigXOR(Tree x, Tree y);
160 EXPORT Tree sigLeftShift(Tree x, Tree y);
161 EXPORT Tree sigLRightShift(Tree x, Tree y);
162 EXPORT Tree sigARightShift(Tree x, Tree y);
163 EXPORT Tree sigGT(Tree x, Tree y);
164 EXPORT Tree sigLT(Tree x, Tree y);
165 EXPORT Tree sigGE(Tree x, Tree y);
166 EXPORT Tree sigLE(Tree x, Tree y);
167 EXPORT Tree sigEQ(Tree x, Tree y);
168 EXPORT Tree sigNE(Tree x, Tree y);
169
170 // Extended math functions
171 EXPORT Tree sigAbs(Tree x);
172 EXPORT Tree sigAcos(Tree x);
173 EXPORT Tree sigTan(Tree x);
174 EXPORT Tree sigSqrt(Tree x);
175 EXPORT Tree sigSin(Tree x);
176 EXPORT Tree sigRint(Tree x);
177 EXPORT Tree sigRemainder(Tree x, Tree y);
178 EXPORT Tree sigPow(Tree x, Tree y);
179 EXPORT Tree sigMin(Tree x, Tree y);
180 EXPORT Tree sigMax(Tree x, Tree y);
181 EXPORT Tree sigLog(Tree x);
182 EXPORT Tree sigLog10(Tree x);
183 EXPORT Tree sigFmod(Tree x, Tree y);
184 EXPORT Tree sigFloor(Tree x);
185 EXPORT Tree sigExp(Tree x);
186 EXPORT Tree sigExp10(Tree x);
187 EXPORT Tree sigCos(Tree x);
188 EXPORT Tree sigCeil(Tree x);
189 EXPORT Tree sigAtan(Tree x);
190 EXPORT Tree sigAtan2(Tree x, Tree y);
191 EXPORT Tree sigAsin(Tree x);
192
193 // Pattern matching for old fonctions
194 bool isSigAdd(Tree a, Tree& x, Tree& y);
195 bool isSigMul(Tree a, Tree& x, Tree& y);
196 bool isSigSub(Tree a, Tree& x, Tree& y);
197 bool isSigDiv(Tree a, Tree& x, Tree& y);
198
199 // Operations on tree representing numbers
200 bool sameMagnitude(Tree a, Tree b);
201
202 Tree addNums(Tree a, Tree b);
203 Tree subNums(Tree a, Tree b);
204 Tree mulNums(Tree a, Tree b);
205 Tree divExtendedNums(Tree a, Tree b);
206 Tree minusNum(Tree a);
207 Tree inverseNum(Tree a);
208
209 // Tests on constant signals
isNum(Tree a)210 inline bool isNum(Tree a)
211 {
212 faustassert(a);
213 return isNum(a->node());
214 }
isZero(Tree a)215 inline bool isZero(Tree a)
216 {
217 faustassert(a);
218 return isZero(a->node());
219 }
isGTZero(Tree a)220 inline bool isGTZero(Tree a)
221 {
222 faustassert(a);
223 return isGTZero(a->node());
224 }
isGEZero(Tree a)225 inline bool isGEZero(Tree a)
226 {
227 faustassert(a);
228 return isGEZero(a->node());
229 }
isOne(Tree a)230 inline bool isOne(Tree a)
231 {
232 faustassert(a);
233 return isOne(a->node());
234 }
isMinusOne(Tree a)235 inline bool isMinusOne(Tree a)
236 {
237 faustassert(a);
238 return isMinusOne(a->node());
239 }
240
241 // Projection for recursive groups
242 Tree sigProj(int i, Tree rgroup);
243 bool isProj(Tree t, int* i, Tree& rgroup);
244
isNum(const Tree & t,num & n)245 inline bool isNum(const Tree& t, num& n)
246 {
247 double f;
248 int i;
249
250 if (isDouble(t->node(), &f)) {
251 n = f;
252 return true;
253 }
254 if (isInt(t->node(), &i)) {
255 n = i;
256 return true;
257 }
258 return false;
259 }
260
261 /*****************************************************************************
262 User Interface Elements
263 *****************************************************************************/
264
265 EXPORT Tree sigButton(Tree label);
266 bool isSigButton(Tree s);
267 bool isSigButton(Tree s, Tree& label);
268
269 EXPORT Tree sigCheckbox(Tree label);
270 bool isSigCheckbox(Tree s);
271 bool isSigCheckbox(Tree s, Tree& label);
272
273 EXPORT Tree sigVSlider(Tree label, Tree init, Tree min, Tree max, Tree step);
274 bool isSigVSlider(Tree s);
275 bool isSigVSlider(Tree s, Tree& label, Tree& init, Tree& min, Tree& max, Tree& step);
276
277 EXPORT Tree sigHSlider(Tree label, Tree init, Tree min, Tree max, Tree step);
278 bool isSigHSlider(Tree s);
279 bool isSigHSlider(Tree s, Tree& label, Tree& init, Tree& min, Tree& max, Tree& step);
280
281 EXPORT Tree sigNumEntry(Tree label, Tree init, Tree min, Tree max, Tree step);
282 bool isSigNumEntry(Tree s);
283 bool isSigNumEntry(Tree s, Tree& label, Tree& init, Tree& min, Tree& max, Tree& step);
284
285 // Output elements
286 EXPORT Tree sigVBargraph(Tree label, Tree min, Tree max, Tree t0);
287 bool isSigVBargraph(Tree s);
288 bool isSigVBargraph(Tree s, Tree& label, Tree& min, Tree& max, Tree& t0);
289
290 EXPORT Tree sigHBargraph(Tree label, Tree min, Tree max, Tree t0);
291 bool isSigHBargraph(Tree s);
292 bool isSigHBargraph(Tree s, Tree& label, Tree& min, Tree& max, Tree& t0);
293
294 EXPORT Tree sigAttach(Tree x, Tree y);
295 bool isSigAttach(Tree s);
296 bool isSigAttach(Tree s, Tree& x, Tree& y);
297
298 Tree sigEnable(Tree x, Tree y);
299 bool isSigEnable(Tree s);
300 bool isSigEnable(Tree s, Tree& x, Tree& y);
301
302 Tree sigControl(Tree x, Tree y);
303 bool isSigControl(Tree s);
304 bool isSigControl(Tree s, Tree& x, Tree& y);
305
306 /*****************************************************************************
307 Sounfiles (also UI elements)
308 *****************************************************************************/
309 /*
310 A boxSounfile(label,c) has 2 inputs and c+3 outputs:
311 0 sigSoundfileLength(label, part): the number of frames of the soundfile part (NK)
312 1 sigSoundfileRate(label): the sampling rate encoded in the file (NK)
313 2.. sigSoundfileBuffer(label, c, part, ridx): the cth channel content (RK or RS)
314 */
315
316 EXPORT Tree sigSoundfile(Tree label);
317 EXPORT Tree sigSoundfileLength(Tree sf, Tree part);
318 EXPORT Tree sigSoundfileRate(Tree sf, Tree part);
319 EXPORT Tree sigSoundfileBuffer(Tree sf, Tree chan, Tree part, Tree ridx);
320
321 bool isSigSoundfile(Tree s, Tree& label);
322 bool isSigSoundfileLength(Tree s, Tree& sf, Tree& part);
323 bool isSigSoundfileRate(Tree s, Tree& sf, Tree& part);
324 bool isSigSoundfileBuffer(Tree s, Tree& sf, Tree& chan, Tree& part, Tree& ridx);
325
326 /*****************************************************************************
327 matrix extension
328 *****************************************************************************/
329
330 // A tuple of signals is basically a list of signals.
331 // mode = 0 means normal, mode = 1 means blocked
332 Tree sigTuple(int mode, Tree ls);
333 bool isSigTuple(Tree s, int* mode, Tree& ls);
334
335 // Access the components of a tuple.
336 // ts is tuple of signals, idx is a scalar signal between 0..n
337 Tree sigTupleAccess(Tree ts, Tree idx);
338 bool isSigTupleAccess(Tree s, Tree& ts, Tree& idx);
339
340 // Create a tuple of signals
341 Tree sigCartesianProd(Tree s1, Tree s2);
342
343 /*****************************************************************************
344 FTZ wrapping
345 Add FTZ wrapping to a signal
346 *****************************************************************************/
347
348 Tree sigFTZ(Tree s);
349
350 /*****************************************************************************
351 Access to sub signals of a signal
352 *****************************************************************************/
353
354 int getSubSignals(Tree sig, tvec& vsigs, bool visitgen = true);
355
356 /**
357 * Test if exp is very simple that is it
358 * can't be considered a real component
359 * @param exp the signal we want to test
360 * @return true if it a very simple signal
361 */
362 bool verySimple(Tree exp);
363
364 bool sigList2vecInt(Tree ls, vector<int>& v);
365
366 /**
367 * Convert an stl vector of signals into a tree list of signals
368 */
369 Tree listConvert(const siglist& a);
370
371 #endif
372