CSL  6.0
BinaryOp.cpp
Go to the documentation of this file.
1 ///
2 /// BinaryOp.cpp -- The implementation file for simple arithmetic operations on UnitGenerators.
3 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT.
4 ///
5 
6 #include "BinaryOp.h"
7 #include "CGestalt.h"
8 
9 using namespace csl;
10 
12  this->addInput(CSL_OPERAND, op2);
13 #ifdef CSL_DEBUG
14  logMsg("BinaryOp::add input and operand UGs");
15 #endif
16 }
17 
18 BinaryOp::BinaryOp(UnitGenerator & op1, float op2) : Effect(op1) {
19  this->addInput(CSL_OPERAND, op2);
20 #ifdef CSL_DEBUG
21  logMsg("BinaryOp::add input UG and float operand");
22 #endif
23 }
24 
25 BinaryOp::BinaryOp(float op1, UnitGenerator & op2) : Effect(op2) {
26  this->addInput(CSL_OPERAND, op1); // Set the operand port's UGen
27 #ifdef CSL_DEBUG
28  logMsg("BinaryOp::add operand UG and float input");
29 #endif
30 }
31 
33  // mInputs.erase(CSL_OPERAND); // Is this right?
34 }
35 
36 // This function returns whether or not its values are dynamic
37 // Again, these might not be necessary due to ports doing dynamic/static checks automatically
38 
40  return inputIsFixed() && operandIsFixed();
41 }
42 
43 // Set the operand from a fixed float
44 
45 void BinaryOp::setOperand(float op) {
46  Port * opPort = mInputs[CSL_OPERAND];
47  if (opPort->mUGen != 0)
48  throw RunTimeError("Can't set value of UGen port");
49  opPort->mValue = op;
50 }
51 
52 // print info about this instance
53 
55  fprintf(stderr, "BinaryOp left: ");
56  Effect::inPort()->dump();
57  fprintf(stderr, "right: ");
58  mInputs[CSL_OPERAND]->dump();
59  fprintf(stderr, "\n");
60 }
61 
62 // AddOp
63 
64 
65 AddOp::AddOp(UnitGenerator & op1, UnitGenerator & op2) : BinaryOp(op1, op2) { }
66 
67 AddOp::AddOp(UnitGenerator & op1, float op2) : BinaryOp(op1, op2) { }
68 
69 AddOp::AddOp(float op1, UnitGenerator & op2) : BinaryOp(op1, op2) { }
70 
71 void AddOp::nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException) {
72 #ifdef CSL_DEBUG
73  logMsg("AddOp nextBuffer"); // for verbose debugging (define CSL_DEBUG in CSL_Core.h)
74 #endif
75  SampleBuffer outputBufferPtr = outputBuffer.buffer(outBufNum);
76  unsigned numFrames = outputBuffer.mNumFrames;
77  DECLARE_OPERAND_CONTROLS; // Declare and load the operand
79  for (unsigned i = 0; i < numFrames; i++) {
80  *outputBufferPtr++ = *inValue + opValue; // Do the actual add
81  UPDATE_OPERAND_CONTROLS; // Update both the operand
82  }
83 }
84 // MulOp
85 
86 MulOp::MulOp(UnitGenerator & op1, UnitGenerator & op2) : BinaryOp(op1, op2) { }
87 
88 MulOp::MulOp(UnitGenerator & op1, float op2) : BinaryOp(op1, op2) { }
89 
90 MulOp::MulOp(float op1, UnitGenerator & op2) : BinaryOp(op1, op2) { }
91 
92 void MulOp::nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException) {
93 #ifdef CSL_DEBUG
94  logMsg("MulOp nextBuffer"); // for verbose debugging (define CSL_DEBUG in CSL_Core.h)
95 #endif
96  SampleBuffer outputBufferPtr = outputBuffer.buffer(outBufNum);
97  unsigned numFrames = outputBuffer.mNumFrames;
98  DECLARE_OPERAND_CONTROLS; // Declare and load the operand
100 
101  for (unsigned i = 0; i < numFrames; i++) {
102  *outputBufferPtr++ = * inValue * opValue; // Do the actual multiply
103  UPDATE_OPERAND_CONTROLS; // Update both the operand
104  }
105 }
sample * SampleBuffer
1-channel buffer data type, vector of (sample)
Definition: CSL_Types.h:194
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
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
#define DECLARE_OPERAND_CONTROLS
Declare the operand port (accessing the mInputs map) and current value.
Definition: BinaryOp.h:61
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
Illegal operation at run time.
void dump()
pretty-print the receiver
Definition: CSL_Core.cpp:827
float mValue
my value (in case I'm fixed [mUGen == NULL])
Definition: CSL_Core.h:322
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
MulOp(UnitGenerator &op1, UnitGenerator &op2)
The constructor for dual-UnitGenerator operation.
Definition: BinaryOp.cpp:86
Port * inPort()
Definition: CSL_Core.h:485
#define UPDATE_OPERAND_CONTROLS
Update the op-related value in the loop.
Definition: BinaryOp.h:77
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
#define LOAD_OPERAND_CONTROLS
Load the op-related values at the start of the callback; if the operand is a dynamic UGen input...
Definition: BinaryOp.h:69
BinaryOp – An Effect that takes two input sources (UnitGenerators or scalars) and gives a single Uni...
Definition: BinaryOp.h:25
UnitGenerator * mUGen
my unit generator (pointer or NULL)
Definition: CSL_Core.h:320
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
Port – used to represent constant, control-rate or signal inputs and outputs in named maps; holds a ...
Definition: CSL_Core.h:312
void addInput(CSL_MAP_KEY name, UnitGenerator &ugen)
Plug in a unit generator to the named input slot.
Definition: CSL_Core.cpp:894
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
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.