CSL  5.2
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 }