CSL  5.2
InOut.cpp
Go to the documentation of this file.
1 //
2 // InOut.h -- implementation of the class that copies the input buffer to the output buffer
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "InOut.h"
7 #include <math.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 using namespace csl;
12 
13 // Generic InOut implementation
14 
15 // Arguments passed to the constructor are optional. Defaults to 2 channels in and out.
16 
17 InOut::InOut(IO * anIO, unsigned inChans, unsigned outChans, InOutFlags f)
18  : mIO(anIO), mMap(outChans, CGestalt::blockSize()),
19  mInChans(inChans), mOutChans(outChans),
20  mFlags(f) {
21  anIO->mInterleaved = false;
22  mGains = (float *) malloc(mOutChans * sizeof(float));
23 }
24 
25 InOut::InOut(IO * anIO, unsigned inChans, unsigned outChans, InOutFlags f, ...)
26  : mIO(anIO), mMap(outChans, CGestalt::blockSize()),
27  mInChans(inChans), mOutChans(outChans),
28  mFlags(f) {
29  anIO->mInterleaved = false;
30  mGains = (float *) malloc(mOutChans * sizeof(float));
31  va_list ap;
32  va_start(ap, f);
33  for (unsigned i = 0; i < outChans; i++)
34  mMap.mChannelMap.push_back(va_arg(ap, unsigned));
35  va_end(ap);
36 }
37 
38 InOut::InOut(UnitGenerator & myInput, unsigned inChans, unsigned outChans, InOutFlags f)
39  : mIO(NULL), mMap(outChans, CGestalt::blockSize()),
40  mInChans(inChans), mOutChans(outChans),
41  mFlags(f) {
42  this->addInput(CSL_INPUT, myInput);
43  mGains = (float *) malloc(mOutChans * sizeof(float));
44 }
45 
46 InOut::InOut(UnitGenerator & myInput, unsigned inChans, unsigned outChans, InOutFlags f, ...)
47  : mIO(NULL), mMap(outChans, CGestalt::blockSize()),
48  mInChans(inChans), mOutChans(outChans),
49  mFlags(f) {
50  this->addInput(CSL_INPUT, myInput);
51  mGains = (float *) malloc(mOutChans * sizeof(float));
52  va_list ap;
53  va_start(ap, f);
54  for (unsigned i = 0; i < outChans; i++)
55  mMap.mChannelMap[i] = va_arg(ap, unsigned);
56  va_end(ap);
57 }
58 
60 
61 void InOut::setChanMap(unsigned * chans) { ///< set channel map
62  for (unsigned i = 0; i < mOutChans; i++)
63  mMap.mChannelMap[i] = chans[i];
64 }
65 
66 void InOut::setChanGains(float * values) { ///< set gain array
67  for (unsigned i = 0; i < mOutChans; i++)
68  mGains[i] = values[i];
69 }
70 
71 void InOut::setGain(unsigned index, float tvalue) { ///< set gain value at index
72  mGains[index] = tvalue;
73 }
74 
75 // nextBuffer simply grabs the IO's input buffer
76 
77 void InOut::nextBuffer(Buffer & outputBuffer) throw (CException) {
78  unsigned numFrames = outputBuffer.mNumFrames;
79  Buffer *inputBuffer;
80 
81  if (mIO) { // either grab the mic input, or my effect in chain
82  mIO->getInput(outputBuffer.mNumFrames, outputBuffer.mNumChannels);
83  inputBuffer = &(mIO->mInputBuffer);
84  } else {
85  Effect::pullInput(numFrames);
86  Port * tinPort = mInputs[CSL_INPUT];
87  inputBuffer = tinPort->mBuffer;
88  }
89 
90  switch (mFlags) {
91  case kNoProc:
92  for (unsigned i = 0; i < mOutChans; i++)
93  memcpy(outputBuffer.buffer(i), inputBuffer->buffer(i % mInChans), outputBuffer.mMonoBufferByteSize);
94  break;
95  case kLR2M:
96  for (unsigned i = 0; i < mOutChans; i++) {
97  sample * outPtr = outputBuffer.buffer(i);
98  sample * inPtr1 = inputBuffer->buffer(i % mInChans);
99  sample * inPtr2 = inputBuffer->buffer((i+1) % mInChans);
100  for (unsigned j = 0; j < numFrames; j++)
101  *outPtr++ = (*inPtr1++ * mGains[i]) + (*inPtr2++* mGains[i]);
102  }
103  break;
104  case kL2M:
105  case kR2M:
106  break;
107  case kN2M: // N-to-M-channel mapping with bufferCMap
108  for (unsigned i = 0; i < mOutChans; i++) {
109  sample * outPtr = outputBuffer.buffer(i);
110  int which = mMap.mChannelMap[i];
111  if (which < 0) continue;
112  sample * inPtr1 = inputBuffer->buffer(which);
113  for (unsigned j = 0; j < numFrames; j++)
114  *outPtr++ = *inPtr1++ * mGains[i];
115  }
116  break;
117  }
118 }
119