CSL 5.2

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

- File List
- File Members

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
11 BinaryOp::BinaryOp(UnitGenerator & op1, UnitGenerator & op2) : Effect(op1) {
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
32 BinaryOp::~BinaryOp() {
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
39 bool BinaryOp::isFixed() {
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
54 void BinaryOp::dump() {
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
78 LOAD_OPERAND_CONTROLS;
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
99 LOAD_OPERAND_CONTROLS;
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 }