1
/*******************************************/
2
/* RtWvIn Input Class, */
3
/* by Gary P. Scavone, 1999-2000 */
5
/* This object inherits from WvIn and is */
6
/* used to read in realtime 16-bit data */
7
/* from a computer's audio port. */
9
/* NOTE: This object is NOT intended for */
10
/* use in achieving simultaneous realtime */
11
/* audio input/output (together with */
12
/* RtWvOut). Under certain circumstances */
13
/* such a scheme is possible, though you */
14
/* should definitely know what you are */
15
/* doing before trying. For safer "full- */
16
/* duplex" operation, use the RtDuplex */
18
/*******************************************/
22
RtWvIn :: RtWvIn(int chans, MY_FLOAT srate, int device)
26
sound_dev = new RtAudio(chans, srate, "record", device);
28
bufferSize = RT_BUFFER_SIZE;
30
rtdata = (INT16 *) new INT16[(bufferSize+1)*channels];
32
lastSamples = (INT16 *) new INT16[channels];
33
for (int i=0;i<channels;i++) {
34
lastSamples[i] = (INT16) 0.0;
39
rate = (MY_FLOAT) srate / SRATE;
40
if (fmod(rate, 1.0) > 0.0) interpolate = 1;
42
phaseOffset = (MY_FLOAT) 0.0;
43
lastOutput = (MY_FLOAT *) new MY_FLOAT[channels];
47
#if (defined(__STK_REALTIME_) && defined(__OS_IRIX_))
48
// This is necessary under IRIX because it scales the input by 0.5
49
// when using single-channel input.
50
if (channels == 1) gain *= 2;
62
delete [ ] lastSamples;
67
void RtWvIn :: setRate(MY_FLOAT aRate)
69
// Negative rates not allowed for realtime input
72
if (fmod(rate, 1.0) != 0.0) interpolate = 1;
76
void RtWvIn :: addTime(MY_FLOAT aTime)
78
// Negative time shift no allowed for realtime input
82
void RtWvIn :: setLooping(int aLoopStatus)
84
// No looping for realtime data.
88
long RtWvIn :: getSize()
93
void RtWvIn :: getData(long index)
95
static long temp = RT_BUFFER_SIZE*channels;
96
sound_dev->recordBuffer(&rtdata[channels],temp);
98
/* Fill in the extra sample frame for interpolation.
99
* We do this by pre-pending the last sample frame
100
* from the previous input buffer to the current one.
102
for (int i=0;i<channels;i++) {
103
rtdata[i] = lastSamples[i];
104
lastSamples[i] = rtdata[temp+i];
108
int RtWvIn :: informTick()
110
static MY_FLOAT alpha;
113
if (time >= bufferSize) {
115
while (time >= bufferSize)
119
// integer part of time address
123
// fractional part of time address
124
alpha = time - (MY_FLOAT) index;
126
for (int i=0;i<channels;i++) {
127
// Do linear interpolation
128
lastOutput[i] = (MY_FLOAT) rtdata[index];
129
lastOutput[i] += alpha * ((MY_FLOAT) rtdata[index+channels] - lastOutput[i]);
130
lastOutput[i] *= gain;
136
for (int i=0;i<channels;i++) {
137
lastOutput[i] = rtdata[index++];
138
lastOutput[i] *= gain;
142
// increment time, which can be negative