CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Variable.h
Go to the documentation of this file.
1 //
2 // Variable.h -- the abstract external variable (plug or port) class
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5
6 #ifndef CSL_Variable_H
7 #define CSL_Variable_H
8
9 #include "CSL_Core.h"
10
11 //#define USE_RANDOMS // broken at present (can't link for libnewran.a)
12
13 #ifdef USE_RANDOMS
14 #include "newran/newran.h" // random variables from Robert Davies
15 #endif
16
17 namespace csl {
18
19 ///
20 /// CVariable is the the abstract external variable (plug or port) class.
21 /// This is the abstract class for static and dynamic variables; it's a sample stream.
22 /// Clients of Variable (oscillators, dsp units, etc) first ask whether
23 /// its values are fixed over the length of the buffer or not.
24 /// If the value is fixed, the client calls next_sample() to get the next sample
25 /// and use it the DSP loop. If the value is not fixed, the client calls
26 /// next_buffer() to get the next buffer of values.
27 ///
28
29 class CVariable { // abstract class
30
31 protected:
32 float mValue; ///< the value I represent
33
34 public: /// Constructors
35 CVariable() { };
36 CVariable(float tvalue) : mValue(tvalue) { };
37 /// Accessors
38 float value() { return(mValue); };
39 void setValue(float x) { mValue = x; };
40 void setValue(int x) { mValue = (float) x; };
41 void setValue(double x) { mValue = (float) x; };
42 };
43
44 ///
45 /// StaticVariable -- he static external variable (plug) class.
46 /// This is a kind of variable that holds onto floating-point value that is
47 /// fixed each control rate period (e.g., changes at most once per control rate).
48 ///
49
50 class StaticVariable : public CVariable, public UnitGenerator {
51
52 public: /// Constructors
53 StaticVariable(float x) : CVariable(x), UnitGenerator() { };
54 StaticVariable(int x) : CVariable((float) x), UnitGenerator() { };
55 StaticVariable(double x) : CVariable((float) x), UnitGenerator() { };
56
57 /// this what being a static variable means!
58 bool isFixed() { return true; };
59 /// versions of nextBuffer
60 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
61
62 float value() { return(CVariable::value()); };
63 void setValue(float x) { CVariable::setValue(x); };
64 void setValue(int x) { CVariable::setValue(x); };
65 void setValue(double x) { CVariable::setValue(x); };
66 };
67
68 // An enumeration for the possible arithmetic operations
69
70 #ifdef CSL_ENUMS
71 typedef enum {
72 kOpPlus,
73 kOpTimes,
74 kOpMinus,
75 kOpDivided,
76 kOpNegated
77 } VOperator;
78 #else
79 #define kOpPlus 1
80 #define kOpTimes 2
81 #define kOpMinus 3
82 #define kOpDivided 4
83 #define kOpNegated 5
84 typedef int VOperator;
85 #endif
86
87 ///
88 /// The DynamicVariable class is a changing variable that can perform an
89 /// operation (e.g., scaling) on another unit generator
90 ///
91
92 class DynamicVariable : public CVariable, public Effect {
93
94 protected:
95 VOperator mMode; ///< the operation I perform '+', '*', etc.
96
97 public: /// Constructors
98 DynamicVariable(UnitGenerator & vox, float val) : CVariable(val), Effect(vox), mMode(kOpTimes) { };
99 DynamicVariable(UnitGenerator & vox, double val) : CVariable((float) val), Effect(vox), mMode(kOpTimes) { };
100 DynamicVariable(float val, UnitGenerator & vox) : CVariable(val), Effect(vox), mMode(kOpTimes) { };
101 DynamicVariable(float val, UnitGenerator & vox, VOperator m) : CVariable(val), Effect(vox), mMode(m) { };
102 DynamicVariable(UnitGenerator & vox, float val, VOperator m) : CVariable(val), Effect(vox), mMode(m) { };
103
104 DynamicVariable(int val, UnitGenerator & vox) : CVariable((float) val), Effect(vox), mMode(kOpTimes) { };
105 DynamicVariable(UnitGenerator & vox, int val) : CVariable((float) val), Effect(vox), mMode(kOpTimes) { };
106 DynamicVariable(int val, UnitGenerator & vox, VOperator m) : CVariable((float) val), Effect(vox), mMode(m) { };
107 DynamicVariable(UnitGenerator & vox, int val, VOperator m) : CVariable((float) val), Effect(vox), mMode(m) { };
108
109 /// my main operations
110 // void nextBuffer(Buffer & outputBuffer) throw (CException);
111 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
112
113 void setValue(float x) { CVariable::setValue(x); };
114 void setValue(int x) { CVariable::setValue(x); };
115 void setValue(double x) { CVariable::setValue(x); };
116 };
117
118 #ifdef USE_RANDOMS
119
120 #ifdef CSL_ENUMS
121
122 ///
123 /// An enumeration for the possible Random Variable distributions
124 ///
125
126 typedef enum {
127 kUniform,
128 kExponential,
129 kCauchy,
130 kNormal,
131 kChiSq,
132 kGamma,
133 kPareto,
134 kPoisson,
135 kBinomial,
136 kNegativeBinomial
137 } Distribution;
138
139 #endif
140
141 ///
142 /// RandomVariable class
143 ///
144
145 class RandomVariable : public CVariable, public UnitGenerator {
146
147 private: // Newran stuff
148 const static bool copySeedFromDisk = false; ///< set to true to get seed from disk file
149 MotherOfAll urng; ///< declare uniform random number generator
150 // Local stuff
151 Random * mVar; ///< my random generator
152 Distribution mDist; ///< my probability distribution type
153 float mMean; ///< my statistical mean
154 float mVariance; ///< my statistical variance
155
156 public: /// Constructors
157 RandomVariable(Distribution d, float mean, float variance, float v1, float v2);
158 ~RandomVariable();
159 /// my main operations
160 void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException);
161 };
162
163 #endif // USE_RANDOMS
164
165 }
166
167 #endif