CSL  6.0
BlockResizer.cpp
Go to the documentation of this file.
1 //
2 // BlockResizer.cpp -- Regularizes the amount of data called for,
3 //
4 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 //
6 
7 #include "BlockResizer.h"
8 #include <string.h> // memcpy
9 
10 using namespace csl;
11 
12 // Constructor - set up cache buffer for 1 block at the input's block size
13 
14 BlockResizer::BlockResizer(UnitGenerator & input, unsigned blockSize)
15  : mInput(&input), mBufferSize(blockSize), mFramePointer(-1) {
16  mNumChannels = mInput->numChannels(); // remember client's # channels
17  // set up & allocate my cache buffer
19  mInputBuffer.allocateBuffers(); // make space for my input
20 }
21 
22 // Destructor free cache buffer
23 
26 }
27 
28 // nextBuffer() calls the up-hill graph as needed
29 
30 void BlockResizer::nextBuffer(Buffer & outBuffer) throw(CException) {
31  unsigned numFrames = outBuffer.mNumFrames;
32  unsigned copiedSoFar = 0;
33 
34 // if (outBuffer.mNumChannels != mNumChannels) { // I handle channel mis-match now
35 // logMsg(kLogError, "BlockResizer channel # mismatch: %d : %d", outBuffer.mNumChannels, mNumChannels);
36 // throw RunTimeError("BlockResizer channel # mismatch");
37 // return;
38 // }
39 // outBuffer.zeroBuffers(); // necessary?
40 
41  while (1) { // loop for multiple input reads if necessary
42  if (mFramePointer == -1) { // if it's time to get more input
43 // mInputBuffer.zeroBuffers(); // necessary?
44 // logMsg(" Read %d", mInputBuffer.mNumFrames);
45  mInput->nextBuffer(mInputBuffer); // grab more input
46  mFramePointer = 0; // set ptr to the start of the buffer
47  }
48  // now we know there's data available
49  unsigned toCopy = csl_min((mBufferSize - mFramePointer),
50  (numFrames - copiedSoFar));
51  // channel buffer copy loop
52  for (unsigned i = 0; i < outBuffer.mNumChannels; i++) {
53  unsigned whichIn = csl_min(i, (mNumChannels - 1)); // handle mono/stereo
54  SampleBuffer src = mInputBuffer.buffer(whichIn) + mFramePointer;
55  SampleBuffer dest = outBuffer.buffer(i) + copiedSoFar;
56  memcpy (dest, src, toCopy * sizeof(sample));
57  }
58 // logMsg("r %d s %d p %d", toCopy, copiedSoFar, mFramePointer);
59 
60  mFramePointer += toCopy; // now update pointers
61  if (mFramePointer >= (int) mBufferSize)
62  mFramePointer = -1; // time to grab more input
63  copiedSoFar += toCopy;
64  if (copiedSoFar >= numFrames) // return if we're done
65  return; // maybe without reading at all
66  } // while loop
67 }
sample * SampleBuffer
1-channel buffer data type, vector of (sample)
Definition: CSL_Types.h:194
#define csl_min(a, b)
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
void freeBuffers()
fcn to free them
Definition: CSL_Core.cpp:141
void setSize(unsigned numChannels, unsigned numFrames)
Definition: CSL_Core.cpp:75
UnitGenerator * mInput
my input unit generator (pointer)
Definition: BlockResizer.h:33
Buffer mInputBuffer
buffer used to pull input
Definition: BlockResizer.h:34
virtual unsigned numChannels()
Definition: CSL_Core.h:252
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
unsigned mNumChannels
my "expected" number of output channels
Definition: CSL_Core.h:292
void nextBuffer(Buffer &outputBuffer)
the work-horse method calls the up-hill graph as needed
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
BlockResizer(UnitGenerator &input, unsigned blockSize)
ctor / dtor
void allocateBuffers()
fcn to malloc storage buffers
Definition: CSL_Core.cpp:122
forward declaration
Definition: CSL_Core.h:241
Base class of CSL exceptions (written upper-case). Has a string message.
unsigned mBufferSize
fixed buffer size of the up-hill graph
Definition: BlockResizer.h:35