1 /******************************************/
4 by Gary P. Scavone, 2001
6 Text executable for audio playback,
7 recording, duplex operation, stopping,
8 starting, and aborting operations.
9 Takes number of channels and sample
10 rate as input arguments. Runs input
11 and output through two separate streams.
12 Uses blocking functionality.
14 /******************************************/
21 typedef signed long MY_TYPE;
22 #define FORMAT RtAudio::RTAUDIO_SINT24
23 #define SCALE 2147483647.0
26 #define FORMAT RtAudio::RTAUDIO_SINT8
29 typedef signed short MY_TYPE;
30 #define FORMAT RtAudio::RTAUDIO_SINT16
33 typedef signed long MY_TYPE;
34 #define FORMAT RtAudio::RTAUDIO_SINT32
35 #define SCALE 2147483647.0
37 typedef float MY_TYPE;
38 #define FORMAT RtAudio::RTAUDIO_FLOAT32
42 typedef double MY_TYPE;
43 #define FORMAT RtAudio::RTAUDIO_FLOAT64
46 #define BASE_RATE 0.005
50 /* Error function in case of incorrect command-line
51 argument specifications
53 cout << "\nuseage: twostreams N fs\n";
54 cout << " where N = number of channels,\n";
55 cout << " and fs = the sample rate.\n\n";
59 int main(int argc, char *argv[])
61 int chans, fs, device, buffer_size, stream1, stream2;
62 long frames, counter = 0, i, j;
63 MY_TYPE *buffer1, *buffer2;
68 // minimal command-line checking
69 if (argc != 3) usage();
71 chans = (int) atoi(argv[1]);
72 fs = (int) atoi(argv[2]);
74 // Open the realtime output device
76 device = 0; // default device
78 audio = new RtAudio();
85 stream1 = audio->openStream(device, chans, 0, 0,
86 FORMAT, fs, &buffer_size, 8);
87 stream2 = audio->openStream(0, 0, device, chans,
88 FORMAT, fs, &buffer_size, 8);
89 buffer1 = (MY_TYPE *) audio->getStreamBuffer(stream1);
90 buffer2 = (MY_TYPE *) audio->getStreamBuffer(stream2);
96 frames = (long) (fs * TIME);
97 data = (double *) calloc(chans, sizeof(double));
100 audio->startStream(stream1);
106 cout << "\nStarting sawtooth playback stream for " << TIME << " seconds." << endl;
107 while (counter < frames) {
108 for (i=0; i<buffer_size; i++) {
109 for (j=0; j<chans; j++) {
110 buffer1[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
111 data[j] += BASE_RATE * (j+1+(j*0.1));
112 if (data[j] >= 1.0) data[j] -= 2.0;
117 audio->tickStream(stream1);
123 counter += buffer_size;
126 cout << "\nStopping playback stream." << endl;
128 audio->stopStream(stream1);
129 audio->startStream(stream2);
135 fd = fopen("test.raw","wb");
138 cout << "\nStarting recording stream for " << TIME << " seconds." << endl;
139 while (counter < frames) {
142 audio->tickStream(stream2);
148 fwrite(buffer2, sizeof(MY_TYPE), chans * buffer_size, fd);
149 counter += buffer_size;
153 cout << "\nAborting recording." << endl;
156 audio->abortStream(stream2);
157 audio->startStream(stream1);
158 audio->startStream(stream2);
165 cout << "\nStarting playback and record streams (quasi-duplex) for " << TIME << " seconds." << endl;
166 while (counter < frames) {
169 audio->tickStream(stream2);
170 memcpy(buffer1, buffer2, sizeof(MY_TYPE) * chans * buffer_size);
171 audio->tickStream(stream1);
177 counter += buffer_size;
180 cout << "\nStopping both streams." << endl;
182 audio->stopStream(stream1);
183 audio->stopStream(stream2);
189 audio->closeStream(stream1);
190 audio->closeStream(stream2);
192 if (data) free(data);