1 /************************************************************************
2 ************************************************************************
4 Copyright (C) 2003-2004 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.
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.
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 ************************************************************************/
31 #include "smartpointer.hh"
32 #include "interval.hh"
35 /*********************************************************************
37 * Type System for FAUST
39 * <type> ::= <simpletype> || table(<type>)
40 * || <type>|<type> || <type>*<type>
41 * <simpletype> ::= <nature><variability><computability><vectorability>||<boolean>
42 * <nature> ::= TINT || TREAL
43 * <variability> ::= TKONST || TBLOCK || TSAMP
44 * <computability> ::= TCOMP || TINIT || TEXEC
45 * <vectorability> ::= KVECT || KSCAL || KTRUESCAL
46 * <boolean> ::= KNUM || KBOOL
48 **********************************************************************/
51 //--------------------------------------------------
52 // qualite des types simples
54 enum { kInt = 0, kReal = 1 }; ///< nature : integer or floating point values
55 enum { kNum = 0 , kBool = 1}; ///< boolean : when a signal stands for a boolean value ( while being of c-type int or float )
56 enum { kKonst = 0, kBlock = 1, kSamp = 3 }; ///< variability : how fast values change
57 enum { kComp = 0, kInit = 1, kExec = 3 }; ///< computability : when values are available
58 enum { kVect = 0, kScal = 1, kTrueScal = 3/*, kIndex = 4*/};///< vectorability: when a signal can be vectorized ( actually, only kVect and kScal matter; kTrueScal and kIndex don't denote types but are here to simplify code generation )
60 /*---------------------------------------------------------------------
63 The root class for SimpleType, TableType and TupletType
66 A smartPointer to type
68 ----------------------------------------------------------------------*/
74 typedef P<AudioType> Type;
77 * The Root class for all audio data types.
78 * All audio types have a "variability" (how fast the values change) and
79 * a "computability" (when the values are available). Simple types have
80 * also a "nature" (integer or floating point).
85 static int gAllocationCount;
87 int fNature; ///< the kind of data represented
88 int fVariability; ///< how fast values change
89 int fComputability; ///< when are values available
90 int fVectorability; ///< when a signal can be vectorized
91 int fBoolean; ///< when a signal stands for a boolean value
93 interval fInterval; ///< Minimal and maximal values the signal can take
94 Tree fCode; ///< Tree representation (for memoization purposes)
98 AudioType(int n, int v, int c, int vec = kVect, int b = kNum, interval i=interval())
99 : fNature(n), fVariability(v), fComputability(c),
100 fVectorability(vec), fBoolean(b),
101 fInterval(i), fCode(0) {} ///< constructs an abstract audio type
103 virtual ~AudioType() {} ///< not really useful here, but make compiler happier
105 int nature() const { return fNature; } ///< returns the kind of values (integre or floating point)
106 int variability() const { return fVariability; } ///< returns how fast values change (constant, by blocks, by samples)
107 int computability() const { return fComputability;} ///< returns when values are available (compilation, initialisation, execution)
108 int vectorability() const { return fVectorability;} ///< returns when a signal can be vectorized
109 int boolean() const { return fBoolean; } ///< returns when a signal stands for a boolean value
111 interval getInterval() const { return fInterval; } ///< returns the interval (min dn max values) of a signal
113 void setCode(Tree code) { fCode = code; } ///< returns the interval (min dn max values) of a signal
114 Tree getCode() { return fCode; } ///< returns the interval (min dn max values) of a signal
118 virtual AudioType* promoteNature(int n) = 0; ///< promote the nature of a type
119 virtual AudioType* promoteVariability(int n) = 0; ///< promote the variability of a type
120 virtual AudioType* promoteComputability(int n) = 0; ///< promote the computability of a type
121 virtual AudioType* promoteVectorability(int n) = 0; ///< promote the vectorability of a type
122 virtual AudioType* promoteBoolean(int n) = 0; ///< promote the booleanity of a type
123 //virtual AudioType* promoteInterval(const interval& i) = 0; ///< promote the interval of a type
126 virtual ostream& print(ostream& dst) const = 0; ///< print nicely a type
127 virtual bool isMaximal() const = 0; ///< true when type is maximal (and therefore can't change depending of hypothesis)
129 virtual AudioType* dimensions(vector<int>& D) = 0; /// Fill D with the dimensions of the type and returns its base type
132 void setInterval(const interval& r) { fInterval = r;}
137 inline ostream& operator << (ostream& s, const AudioType& n) { return n.print(s); }
141 * Return the nature of a vector of types.
143 inline int mergenature(const vector<Type>& v)
146 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->nature();
153 * Return the variability of a vector of types.
155 inline int mergevariability(const vector<Type>& v)
158 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->variability();
165 * Return the computability of a vector of types.
167 inline int mergecomputability(const vector<Type>& v)
170 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->computability();
177 * Return the vectorability of a vector of types.
179 inline int mergevectorability(const vector<Type>& v)
182 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->vectorability();
189 * Return the booleanity of a vector of types.
191 inline int mergeboolean(const vector<Type>& v)
194 for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->boolean();
201 * Return the interval of a vector of types.
203 inline interval mergeinterval(const vector<Type>& v)
209 for (unsigned int i = 0; i < v.size(); i++) {
210 interval r = v[i]->getInterval();
211 if (!r.valid) return r;
220 return interval(lo, hi);
225 AudioType* makeSimpleType(int n, int v, int c, int vec, int b, const interval& i);
227 AudioType* makeTableType(const Type& ct);
228 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec);
229 AudioType* makeTableType(const Type& ct, int n, int v, int c, int vec, int b, const interval& i);
232 AudioType* makeTupletType(const vector<Type>& vt);
233 AudioType* makeTupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i);
236 * The type of a simple numeric audio signal.
237 * Beside a computability and a variability, SimpleTypes have
238 * a "nature" indicating if they represent an integer or floating
239 * point audio signals.
241 class SimpleType : public AudioType
245 SimpleType(int n, int v, int c, int vec, int b, const interval& i) : AudioType(n,v,c,vec,b,i) {
246 //cerr << "new simple type " << i << " -> " << *this << endl;
247 } ///< constructs a SimpleType from a nature a variability and a computability
249 virtual ostream& print(ostream& dst) const; ///< print a SimpleType
253 virtual AudioType* promoteNature(int n) { return makeSimpleType(n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
254 virtual AudioType* promoteVariability(int v) { return makeSimpleType(fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
255 virtual AudioType* promoteComputability(int c) { return makeSimpleType(fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
256 virtual AudioType* promoteVectorability(int vec) { return makeSimpleType(fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval); } ///< promote the vectorability of a type
257 virtual AudioType* promoteBoolean(int b) { return makeSimpleType(fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
258 // virtual AudioType* promoteInterval(const interval& i) {
259 // cerr << "promote to Interval " << i << endl;
260 // cerr << "for type : " << *this << endl;
261 // Type t = makeSimpleType(fNature, fVariability, fComputability, fVectorability, fBoolean, i); ///< promote the interval of a type
262 // cerr << "gives type " << *t << endl;
265 virtual AudioType* dimensions(vector<int>& D) { D.clear(); return this; } // scalar have no dimensions
267 virtual bool isMaximal() const; ///< true when type is maximal (and therefore can't change depending of hypothesis)
273 inline Type intCast (Type t) { return makeSimpleType(kInt, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
274 inline Type floatCast (Type t) { return makeSimpleType(kReal, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
275 inline Type sampCast (Type t) { return makeSimpleType(t->nature(), kSamp, t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
276 inline Type boolCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kBool, t->getInterval()); }
277 inline Type numCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kNum, t->getInterval()); }
278 inline Type vecCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), kVect, t->boolean(), t->getInterval()); }
279 inline Type scalCast (Type t) { return makeSimpleType(t->nature(), t->variability(), t->computability(), kScal, t->boolean(), t->getInterval()); }
280 inline Type truescalCast (Type t){ return makeSimpleType(t->nature(), t->variability(), t->computability(), kTrueScal, t->boolean(), t->getInterval()); }
282 inline Type castInterval (Type t, const interval& i)
284 return makeSimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean(), i);
288 * The type of a table of audio data.
289 * Beside a computability and a variability, TableTypes have
290 * a "content" indicating the type of the data stored in the table.
292 class TableType : public AudioType
295 const Type fContent; ///< type of that data stored in the table
298 TableType(const Type& t) :
299 AudioType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean()),
300 fContent(t) {} ///< construct a TableType with a content of a type t
302 TableType(const Type& t, int v, int c) :
303 AudioType(t->nature(), t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()),
304 fContent(t) {} ///< construct a TableType with a content of a type t, promoting variability and computability
306 TableType(const Type& t, int n, int v, int c) :
307 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()),
308 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability and computability
310 TableType(const Type& t, int n, int v, int c, int vec, int b) :
311 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b),
312 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability, computability, vectorability and booleanity
315 TableType(const Type& t, int n, int v, int c, int vec, int b, const interval& i) :
316 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b, i),
317 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability, computability, vectorability and booleanity
319 TableType(const Type& t, int n, int v, int c, int vec) :
320 AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()),
321 fContent(t) {} ///< construct a TableType with a content of a type t, promoting nature, variability, computability and vectorability
324 Type content() const { return fContent; } ///< return the type of data store in the table
325 virtual ostream& print(ostream& dst) const; ///< print a TableType
327 virtual AudioType* promoteNature(int n) { return makeTableType(fContent, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
328 virtual AudioType* promoteVariability(int v) { return makeTableType(fContent, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
329 virtual AudioType* promoteComputability(int c) { return makeTableType(fContent, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
330 virtual AudioType* promoteVectorability(int vec) { return makeTableType(fContent, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);}///< promote the vectorability of a type
331 virtual AudioType* promoteBoolean(int b) { return makeTableType(fContent, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
332 //virtual AudioType* promoteInterval(const interval& i) { return makeTableType(fContent, fNature, fVariability, fComputability, fVectorability, fBoolean, i); } ///< promote the interval of a type
334 virtual bool isMaximal() const; ///< true when type is maximal (and therefore can't change depending of hypothesis)
336 virtual AudioType* dimensions(vector<int>& D) { D.clear(); return this; } // tables have no dimensions
343 * The type of a tuplet of data.
344 * Beside a computability and a variability, TupletTypes have
345 * a set of components.
347 class TupletType : public AudioType
350 vector<Type> fComponents;
357 TupletType(const vector<Type>& vt) :
358 AudioType(mergenature(vt),mergevariability(vt),mergecomputability(vt),mergevectorability(vt),mergeboolean(vt), mergeinterval(vt)),
361 TupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i) :
362 AudioType(n|mergenature(vt), v|mergevariability(vt), c|mergecomputability(vt), vec|mergevectorability(vt), b|mergeboolean(vt), i),
365 int arity() const { return fComponents.size(); }
366 Type operator[](unsigned int i) const { return fComponents[i]; }
367 virtual ostream& print(ostream& dst) const;
369 virtual AudioType* promoteNature(int n) { return new TupletType(fComponents, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
370 virtual AudioType* promoteVariability(int v) { return new TupletType(fComponents, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
371 virtual AudioType* promoteComputability(int c) { return new TupletType(fComponents, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
372 virtual AudioType* promoteVectorability(int vec) { return new TupletType(fComponents, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);} ///< promote the vectorability of a type
373 virtual AudioType* promoteBoolean(int b) { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
374 //virtual AudioType* promoteInterval(const interval& i) { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, fBoolean, i); } ///< promote the interval of a type
376 virtual bool isMaximal() const; ///< true when type is maximal (and therefore can't change depending of hypothesis)
377 virtual AudioType* dimensions(vector<int>& D) { D.clear(); return this; } // tuples have no dimensions
384 * The type T = vector(n, T') of a signal of vectors of fixed size n and elements of type T'
385 * The following properties hold :
386 * nature(vector(n,T')) = nature(T')
387 * variability(vector(n,T')) = variability(T')
388 * computability(vector(n,T')) = computability(T')
389 * vectorability(vector(n,T')) = ??? (so probably no)
390 * boolean(vector(n,T')) = false (a vector is not a boolean)
391 * getInterval(vector(n,T')) = getInterval(T') (is it meanuful ?)
393 class VectorType : public AudioType
396 int fSize; ///< size of the vector
397 Type fContent; ///< type of the elements
400 VectorType(int n, const Type& t) : AudioType(t->nature(), t->variability(), t->computability(), t->vectorability(),
401 t->boolean(), t->getInterval()),
407 int size() const { return fSize; }
408 Type content() const { return fContent; }
410 virtual ostream& print(ostream& dst) const;
412 virtual AudioType* promoteNature(int n) { return new VectorType(fSize, fContent->promoteNature(n)); } ///< promote the nature of a type
413 virtual AudioType* promoteVariability(int v) { return new VectorType(fSize, fContent->promoteVariability(v)); } ///< promote the variability of a type
414 virtual AudioType* promoteComputability(int c) { return new VectorType(fSize, fContent->promoteComputability(c)); } ///< promote the computability of a type
415 virtual AudioType* promoteVectorability(int vec) { return new VectorType(fSize, fContent->promoteVectorability(vec)); } ///< promote the vectorability of a type
416 virtual AudioType* promoteBoolean(int b) { return new VectorType(fSize, fContent->promoteBoolean(b)); } ///< promote the booleanity of a type
417 virtual bool isMaximal() const { return false; } ///< true when type is maximal (and therefore can't change depending of hypothesis)
418 virtual AudioType* dimensions(vector<int>& D) { AudioType* t = fContent->dimensions(D); D.push_back(fSize); return t;} ///< vectors have a dimension
423 Type makeVectorType(const Type& b, const vector<int>& dim);
424 bool maxdimensions(const vector<int>& D1, const vector<int>& D2, vector<int>& D3);
429 //-------------------------------------------------
430 //-------------------------------------------------
431 // operations sur les types
432 //-------------------------------------------------
433 //-------------------------------------------------
437 //--------------------------------------------------
438 // liste de types predefinis
454 extern Type INT_TGUI;
458 //--------------------------------------------------
461 Type table (const Type& t);
462 Type operator| (const Type& t1, const Type& t2);
463 Type operator* (const Type& t1, const Type& t2);
466 //--------------------------------------------------
467 // comparaison de types
469 bool operator==(const Type& t1, const Type& t2);
470 bool operator<=(const Type& t1, const Type& t2);
472 inline bool operator!=(const Type& t1, const Type& t2) { return !(t1==t2); }
473 inline bool operator< (const Type& t1, const Type& t2) { return t1<=t2 && t1!=t2; }
474 inline bool operator> (const Type& t1, const Type& t2) { return t2<t1; }
475 inline bool operator>=(const Type& t1, const Type& t2) { return t2<=t1; }
478 //--------------------------------------------------
479 // predicats-conversion de types
481 SimpleType* isSimpleType (AudioType* t);
482 TableType* isTableType (AudioType* t);
483 TupletType* isTupletType (AudioType* t);
484 VectorType* isVectorType (AudioType* t);
487 //--------------------------------------------------
488 // impressions de types
490 ostream& operator<<(ostream& dst, const SimpleType& t);
491 ostream& operator<<(ostream& dst, const Type& t);
492 ostream& operator<<(ostream& dst, const TableType& t);
493 ostream& operator<<(ostream& dst, const TupletType& t);
494 ostream& operator<<(ostream& dst, const VectorType& t);
497 //--------------------------------------------------
498 // verification de type
500 Type checkInt(Type t); ///< verifie que t est entier
501 Type checkKonst(Type t); ///< verifie que t est constant
502 Type checkInit(Type t); ///< verifie que t est connu a l'initialisation
504 Type checkIntParam(Type t); ///< verifie que t est connu a l'initialisation, constant et entier
506 Type checkWRTbl(Type tbl, Type wr); ///< verifie que wr est compatible avec le contenu de tbl
508 int checkDelayInterval(Type t); ///< Check if the interval of t is appropriate for a delay
511 //--------------------------------------------------
512 // conversion de type
514 string cType (Type t);
516 Tree codeAudioType(AudioType* t); ///< Code an audio type as a tree (memoization)