rtgetin() differs from rtaddout in that it reads a block of samples instead of a single "frame" (1-sample-step * number-of-output-channels) of samples. The buffer used by rtgetin() has to be dimensioned large enough to hold the number of samples that will be buffered by RTcmix. Since this depends on how the buffers were set in the rtsetparams score directive, this usually needs to be allocated dynamically.
Here's an example of how to do this:
float in[]; int MYINSTRUMENT::run() { int rsamps; ... if (in == NULL) // first time, we need to allocate the buffer memory in = new float[RTBUFSAMPS*inputchans] rsamps = chunksamps*inputchans; rtgetin(in, this, rsamps); ... }Where do all these variable RTBUFSAMPS, inputchans, and chunksamps come from? They are all class variables that are part of the general Instrument object, used as the base class to create user-written instruments.
Because rtgetin() reads an entire buffer, this buffer needs to be 'stepped through' inside the INSTRUMENT::run() method. The step size depends on how many channels (inputchans) are in the input stream, as samples are interleaved in the buffer. This means, for example, that a stereo input stream will set up the buffer-array with left0, right0, left1, right1, left2, right2 ... leftN, rightN.
Generally rtgetin is used in the INSTRUMENT::run() member function, just before the sample-computing loop. It can also be used to read in a buffer of samples from an input file at any point inside an INSTRUMENT. rtgetin assumes that rtsetinput was called previously.
This function replaces
the older GETIN macro used in
disk-only cmix.
The rtgetin() function returns the number of actual samples read into the buffer.
#include <Instrument.h> float in[]; int MYINSTRUMENT::run() { float out[2]; // stereo output array int rsamps; // rsamps is the "real" number of samples read ... if (in == NULL) // first time, so allocate it in = new float [RTBUFSAMPS*inputchans]; rsamps = chunksamps*inputchans; rtgetin(in, this, rsamps); // skip through the buffer a frame at a time, rather than a sample at a time for (i = 0; i < rsamps; i += inputchans) { ... out[0] = in[i]; out[1] = in[i+1]; rtaddout(out); ... } }