CSL  6.0
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,
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
The DynamicVariable class is a changing variable that can perform an operation (e.g., scaling) on another unit generator.
Definition: Variable.h:92
void setValue(double x)
Definition: Variable.h:115
DynamicVariable(int val, UnitGenerator &vox)
Definition: Variable.h:104
float value()
Accessors.
Definition: Variable.h:38
DynamicVariable(float val, UnitGenerator &vox)
Definition: Variable.h:100
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
Effect – mix-in for classes that have unit generators as inputs (like filters).
Definition: CSL_Core.h:466
VOperator mMode
the operation I perform '+', '*', etc.
Definition: Variable.h:95
CVariable(float tvalue)
Definition: Variable.h:36
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
versions of nextBuffer
Definition: Variable.cpp:13
bool isFixed()
this what being a static variable means!
Definition: Variable.h:58
void setValue(double x)
Definition: Variable.h:41
DynamicVariable(UnitGenerator &vox, float val, VOperator m)
Definition: Variable.h:102
CVariable is the the abstract external variable (plug or port) class. This is the abstract class for ...
Definition: Variable.h:29
#define kOpTimes
Definition: Variable.h:80
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
my main operations
Definition: Variable.cpp:23
DynamicVariable(float val, UnitGenerator &vox, VOperator m)
Definition: Variable.h:101
StaticVariable – he static external variable (plug) class. This is a kind of variable that holds ont...
Definition: Variable.h:50
void setValue(float x)
set/get the value (not allowed in the abstract, useful for static values)
Definition: Variable.h:63
DynamicVariable(UnitGenerator &vox, int val)
Definition: Variable.h:105
float mValue
the value I represent
Definition: Variable.h:32
DynamicVariable(UnitGenerator &vox, int val, VOperator m)
Definition: Variable.h:107
void setValue(int x)
Definition: Variable.h:64
CVariable()
Constructors.
Definition: Variable.h:35
void setValue(float x)
Definition: Variable.h:39
StaticVariable(float x)
Constructors.
Definition: Variable.h:53
int VOperator
Definition: Variable.h:84
void setValue(int x)
Definition: Variable.h:114
DynamicVariable(UnitGenerator &vox, float val)
Constructors.
Definition: Variable.h:98
void setValue(float x)
set/get the value (not allowed in the abstract, useful for static values)
Definition: Variable.h:113
#define kOpMinus
Definition: Variable.h:81
#define kOpDivided
Definition: Variable.h:82
void setValue(int x)
Definition: Variable.h:40
void setValue(double x)
Definition: Variable.h:65
StaticVariable(int x)
Definition: Variable.h:54
#define kOpNegated
Definition: Variable.h:83
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
DynamicVariable(UnitGenerator &vox, double val)
Definition: Variable.h:99
forward declaration
Definition: CSL_Core.h:241
StaticVariable(double x)
Definition: Variable.h:55
Base class of CSL exceptions (written upper-case). Has a string message.
DynamicVariable(int val, UnitGenerator &vox, VOperator m)
Definition: Variable.h:106
#define kOpPlus
Definition: Variable.h:79