1
/*******************************************/
2
/* Real-Time Duplex Input/Output Class, */
3
/* by Gary P. Scavone, 1999-2000 */
5
/* This object opens the sound i/o */
6
/* device, reads buffers in from it, and */
7
/* pokes buffers of samples out to it. */
9
/* At the moment, duplex mode is possible */
10
/* only on Linux (OSS), IRIX, and */
11
/* Windows95/98 platforms. */
12
/*******************************************/
16
#if (defined(__STK_REALTIME_) )
18
RtDuplex :: RtDuplex(int chans, MY_FLOAT srate, int device)
20
// We'll let RTSoundIO deal with channel and srate limitations.
22
sound_dev = new RtAudio(channels, srate, "duplex", device);
25
data_length = RT_BUFFER_SIZE*channels;
26
indata = (INT16 *) new INT16[data_length+10];
27
outdata = (INT16 *) new INT16[data_length+10];
28
insamples = new MY_FLOAT[channels];
29
#if (defined(__STK_REALTIME_) && defined(__OS_IRIX_))
30
// This is necessary under IRIX because it scales the input by 0.5
31
// when using single-channel input.
32
if (channels == 1) gain *= 2;
34
// Read half a buffer to get started
35
readCounter = data_length / 2;
36
sound_dev->recordBuffer(&indata[readCounter],(data_length/2));
39
RtDuplex :: ~RtDuplex()
41
sound_dev->playBuffer(outdata,writeCounter);
43
while (writeCounter<data_length) {
44
outdata[writeCounter++] = 0;
46
sound_dev->playBuffer(outdata,writeCounter);
47
sound_dev->playBuffer(outdata,writeCounter); // Are these extra writes necessary?
48
sound_dev->playBuffer(outdata,writeCounter);
55
MY_FLOAT RtDuplex :: tick(MY_FLOAT outsample)
57
// We offset the data read and data write calls by RT_BUFFER_SIZE / 2
58
if (readCounter >= data_length) {
59
sound_dev->recordBuffer(indata,data_length);
62
*insamples = (MY_FLOAT) indata[readCounter++];
65
for (i=1;i<channels;i++)
66
*insamples += (MY_FLOAT) indata[readCounter++];
71
for (int i=0;i<channels;i++)
72
outdata[writeCounter++] = (short) (outsample * 32000.0);
74
if (writeCounter >= data_length) {
75
sound_dev->playBuffer(outdata,data_length);
81
MY_MULTI RtDuplex :: mtick(MY_MULTI outsamples)
84
// We offset the data read and data write calls by RT_BUFFER_SIZE / 2
85
if (readCounter >= data_length) {
86
sound_dev->recordBuffer(indata,data_length);
89
for (i=0;i<channels;i++)
90
insamples[i] = (MY_FLOAT) (indata[readCounter++]*gain);
92
for (i=0;i<channels;i++)
93
outdata[writeCounter++] = (short) (*outsamples++ * 32000.0);
95
if (writeCounter >= data_length) {
96
sound_dev->playBuffer(outdata,data_length);