CSL  6.0
BinaryOp.h
Go to the documentation of this file.
1 ///
2 /// BinaryOp.h -- The specification file for simple arithmetic operations on UnitGenerators.
3 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 ///
5 /// What's here:
6 ///
7 /// BinaryOp -- an abstract class that specific binary operators (add, mul, etc.) inherit from)
8 /// AddOp -- Adds two UnitGenerators (or a UnitGenerator and a scalar) together
9 /// MulOp -- Multiplies two UnitGenerators (or a UnitGenerator and a scalar) together
10 ///
11 
12 #ifndef CSL_BINARYOP_H
13 #define CSL_BINARYOP_H
14 
15 #include "CSL_Core.h"
16 
17 namespace csl {
18 
19 ///
20 /// BinaryOp -- An Effect that takes two input sources (UnitGenerators or scalars) and
21 /// gives a single UnitGenerator as its output. BinaryOp is an abstract class who's nextBuffer
22 /// method is to be implemented by subclasses
23 ///
24 
25 class BinaryOp : public Effect {
26 
27 public: /// The constructor for dual-UnitGenerator operation
28  BinaryOp(UnitGenerator & op1, UnitGenerator & op2);
29  /// Constructuctor for operating a UnitGenerator and a fixed float
30  BinaryOp(UnitGenerator & op1, float op2);
31  /// The same constructor, but with UGen and float inputs
32  BinaryOp(float op1, UnitGenerator & op2);
33  virtual ~BinaryOp(); ///< The destructor
34 
35  void dump(); ///< Prints instance info
36  void setOperand(float op); ///< Set the operand from a fixed float
37 
38 protected:
39  /// Abstract function that will do the processing for each buffer
40  virtual void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw(CException) = 0;
41  /// Is the input fixed or a dynamic UnitGenerator?
42  inline bool isFixed();
43  inline bool inputIsFixed();
44  /// Is the operand fixed or a dynamic UnitGenerator?
45  inline bool operandIsFixed();
46  /// Convert the dynamic operand to a fixed sample (if operand is fixed)
47  inline sample operandFixedValue(Buffer & outputBuffer);
48 
49 };
50 
51 inline bool BinaryOp::inputIsFixed() {
52  return (Effect::inPort())->isFixed();
53 }
54 
55 inline bool BinaryOp::operandIsFixed() {
56  return mInputs[CSL_OPERAND]->isFixed();
57 }
58 
59 /// Declare the operand port (accessing the mInputs map) and current value.
60 
61 #define DECLARE_OPERAND_CONTROLS \
62  Port * opPort = mInputs[CSL_OPERAND]; \
63  SampleBuffer inValue; \
64  sample opValue
65 
66 /// Load the op-related values at the start of the callback; if the operand is a dynamic UGen input,
67 /// then pull its value, get the pointer to its buffer, and set the first value, otherwise store the constant value
68 
69 #define LOAD_OPERAND_CONTROLS \
70  Controllable::pullInput(opPort, numFrames); \
71  Effect::pullInput(numFrames); \
72  inValue = mInputPtr; \
73  opValue = opPort->nextValue()
74 
75 /// Update the op-related value in the loop
76 
77 #define UPDATE_OPERAND_CONTROLS \
78  opValue = opPort->nextValue() ; \
79  inValue++
80 
81 ///
82 /// AddOp -- A BinaryOp that adds two UnitGenerators or fixed constants together.
83 ///
84 
85 class AddOp : public BinaryOp {
86 
87 public: /// The constructor for dual-UnitGenerator operation
88  AddOp(UnitGenerator & op1, UnitGenerator & op2);
89  /// Constructuctor for operating a UnitGenerator and a fixed float
90  AddOp(UnitGenerator & op1, float op2);
91  /// Returns the next individual frame (for fixed values)
92  AddOp(float op1, UnitGenerator & op2);
93 
94 // float nextValue() throw (CException);
95 
96 protected: /// Function that implements the adding routine
97  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException) ;
98 };
99 
100 ///
101 /// MulOp -- A BinaryOp that multiplies two UnitGenerators or fixed constants together, sample-by-sample.
102 ///
103 
104 class MulOp : public BinaryOp {
105 
106 public: /// The constructor for dual-UnitGenerator operation
107  MulOp(UnitGenerator & op1, UnitGenerator & op2);
108  /// Constructuctor for operating a UnitGenerator and a fixed float
109  MulOp(UnitGenerator & op1, float op2);
110 
111  MulOp(float op2, UnitGenerator & op1);
112  void setScale();
113  /// Returns the next individual frame (for fixed values)
114 // float nextValue() throw (CException);
115 
116 //protected: /// Function that implements the multiplying routine
117  void nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException) ;
118 
119 };
120 
121 }
122 
123 #endif // CSL_BINARYOP_H
124 
void dump()
Prints instance info.
Definition: BinaryOp.cpp:54
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
bool isFixed()
Is the input fixed or a dynamic UnitGenerator?
Definition: BinaryOp.cpp:39
BinaryOp(UnitGenerator &op1, UnitGenerator &op2)
The constructor for dual-UnitGenerator operation.
Definition: BinaryOp.cpp:11
void setScale()
virtual void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)=0
Abstract function that will do the processing for each buffer.
AddOp(UnitGenerator &op1, UnitGenerator &op2)
The constructor for dual-UnitGenerator operation.
Definition: BinaryOp.cpp:65
virtual ~BinaryOp()
The destructor.
Definition: BinaryOp.cpp:32
bool inputIsFixed()
Definition: BinaryOp.h:51
sample operandFixedValue(Buffer &outputBuffer)
Convert the dynamic operand to a fixed sample (if operand is fixed)
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
MulOp(UnitGenerator &op1, UnitGenerator &op2)
The constructor for dual-UnitGenerator operation.
Definition: BinaryOp.cpp:86
Port * inPort()
Definition: CSL_Core.h:485
void setOperand(float op)
Set the operand from a fixed float.
Definition: BinaryOp.cpp:45
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
Returns the next individual frame (for fixed values)
Definition: BinaryOp.cpp:92
MulOp – A BinaryOp that multiplies two UnitGenerators or fixed constants together, sample-by-sample.
Definition: BinaryOp.h:104
BinaryOp – An Effect that takes two input sources (UnitGenerators or scalars) and gives a single Uni...
Definition: BinaryOp.h:25
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
Function that implements the adding routine.
Definition: BinaryOp.cpp:71
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
PortMap mInputs
the map of my inputs or controls (used by the mix-in classes)
Definition: CSL_Core.h:378
forward declaration
Definition: CSL_Core.h:241
#define CSL_OPERAND
Definition: CSL_Types.h:276
AddOp – A BinaryOp that adds two UnitGenerators or fixed constants together.
Definition: BinaryOp.h:85
bool operandIsFixed()
Is the operand fixed or a dynamic UnitGenerator?
Definition: BinaryOp.h:55
Base class of CSL exceptions (written upper-case). Has a string message.