CSL  6.0
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 
void pullInput(Buffer &outputBuffer)
Definition: CSL_Core.cpp:1122
BufferCMap mMap
the mapped buffer pointers for the output channels
Definition: InOut.h:59
#define kNoProc
Definition: InOut.h:25
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
void setChanGains(float *values)
set gain array
Definition: InOut.cpp:66
void setGain(unsigned index, float value)
set gain value at index
Definition: InOut.cpp:71
unsigned mOutChans
out chans
Definition: InOut.h:61
virtual SampleBuffer buffer(unsigned bufNum)
convenience accessors for sample buffers
Definition: CSL_Core.cpp:66
virtual void nextBuffer(Buffer &outputBuffer)
get a buffer of Frames – this is the core CSL "pull" function; the given buffer can be written into...
Definition: InOut.cpp:77
bool mInterleaved
flag if IO is interleaved
Definition: CSL_Core.h:792
std::vector< int > mChannelMap
the map between virtual and real channels
Definition: CSL_Core.h:199
void setChanMap(unsigned *chans)
set channel map
Definition: InOut.cpp:61
int InOutFlags
Definition: InOut.h:30
The CSL system defaults class.
Definition: CGestalt.h:39
float * mGains
amplitude scales
Definition: InOut.h:63
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
IO – the abstract I/O scheduling class; subclasses interface to specific I/O APIs.
Definition: CSL_Core.h:752
~InOut()
Definition: InOut.cpp:59
#define CSL_INPUT
Definition: CSL_Types.h:275
#define kR2M
Definition: InOut.h:28
#define kN2M
Definition: InOut.h:29
#define kL2M
Definition: InOut.h:27
#define kLR2M
Definition: InOut.h:26
Buffer * mBuffer
the buffer used to hold my output
Definition: CSL_Core.h:321
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
forward declaration
Definition: CSL_Core.h:241
InOut(IO *anIO, unsigned inChans, unsigned outChans, InOutFlags f=kNoProc)
Constructor with IO, number of channels in & out, and processing.
Definition: InOut.cpp:17
Base class of CSL exceptions (written upper-case). Has a string message.