twostreams.cpp
by Gary P. Scavone, 2001
- Text executable for audio playback,
- recording, duplex operation, stopping,
- starting, and aborting operations.
- Takes number of channels and sample
- rate as input arguments. Runs input
- and output through two separate streams.
+ Test executable for audio playback, recording,
+ duplex operation, stopping, starting, and
+ aborting operations. Takes number of channels
+ and sample rate as input arguments. Runs input
+ and output through two separate instances of RtAudio.
Uses blocking functionality.
*/
/******************************************/
#include "RtAudio.h"
-#include <iostream.h>
-#include <stdio.h>
+#include <iostream>
/*
typedef signed long MY_TYPE;
-#define FORMAT RtAudio::RTAUDIO_SINT24
+#define FORMAT RTAUDIO_SINT24
#define SCALE 2147483647.0
typedef char MY_TYPE;
-#define FORMAT RtAudio::RTAUDIO_SINT8
+#define FORMAT RTAUDIO_SINT8
#define SCALE 127.0
typedef signed short MY_TYPE;
-#define FORMAT RtAudio::RTAUDIO_SINT16
+#define FORMAT RTAUDIO_SINT16
#define SCALE 32767.0
typedef signed long MY_TYPE;
-#define FORMAT RtAudio::RTAUDIO_SINT32
+#define FORMAT RTAUDIO_SINT32
#define SCALE 2147483647.0
+*/
typedef float MY_TYPE;
-#define FORMAT RtAudio::RTAUDIO_FLOAT32
+#define FORMAT RTAUDIO_FLOAT32
#define SCALE 1.0
-*/
+/*
typedef double MY_TYPE;
-#define FORMAT RtAudio::RTAUDIO_FLOAT64
+#define FORMAT RTAUDIO_FLOAT64
#define SCALE 1.0
+*/
#define BASE_RATE 0.005
#define TIME 2.0
/* Error function in case of incorrect command-line
argument specifications
*/
- cout << "\nuseage: twostreams N fs\n";
- cout << " where N = number of channels,\n";
- cout << " and fs = the sample rate.\n\n";
+ std::cout << "\nuseage: twostreams N fs <device>\n";
+ std::cout << " where N = number of channels,\n";
+ std::cout << " fs = the sample rate,\n";
+ std::cout << " and device = the device to use (default = 0).\n\n";
exit(0);
}
int main(int argc, char *argv[])
{
- int chans, fs, device, buffer_size, stream1, stream2;
+ int chans, fs, buffer_size, device = 0;
long frames, counter = 0, i, j;
MY_TYPE *buffer1, *buffer2;
- RtAudio *audio;
+ RtAudio *stream1, *stream2;
FILE *fd;
- double *data;
+ double *data = 0;
// minimal command-line checking
- if (argc != 3) usage();
+ if (argc != 3 && argc != 4 ) usage();
chans = (int) atoi(argv[1]);
fs = (int) atoi(argv[2]);
+ if ( argc == 4 )
+ device = (int) atoi(argv[3]);
// Open the realtime output device
buffer_size = 512;
- device = 0; // default device
try {
- audio = new RtAudio();
+ stream1 = new RtAudio(device, chans, 0, 0,
+ FORMAT, fs, &buffer_size, 8);
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
exit(EXIT_FAILURE);
}
try {
- stream1 = audio->openStream(device, chans, 0, 0,
- FORMAT, fs, &buffer_size, 8);
- stream2 = audio->openStream(0, 0, device, chans,
- FORMAT, fs, &buffer_size, 8);
- buffer1 = (MY_TYPE *) audio->getStreamBuffer(stream1);
- buffer2 = (MY_TYPE *) audio->getStreamBuffer(stream2);
- }
- catch (RtAudioError &m) {
- m.printMessage();
+ stream2 = new RtAudio(0, 0, device, chans,
+ FORMAT, fs, &buffer_size, 8);
+ }
+ catch (RtError &error) {
+ delete stream1;
+ error.printMessage();
+ exit(EXIT_FAILURE);
+ }
+
+ try {
+ buffer1 = (MY_TYPE *) stream1->getStreamBuffer();
+ buffer2 = (MY_TYPE *) stream2->getStreamBuffer();
+ }
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
data = (double *) calloc(chans, sizeof(double));
try {
- audio->startStream(stream1);
+ stream1->startStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
- cout << "\nStarting sawtooth playback stream for " << TIME << " seconds." << endl;
+ std::cout << "\nStarting sawtooth playback stream for " << TIME << " seconds." << std::endl;
while (counter < frames) {
for (i=0; i<buffer_size; i++) {
for (j=0; j<chans; j++) {
}
try {
- audio->tickStream(stream1);
+ stream1->tickStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
counter += buffer_size;
}
- cout << "\nStopping playback stream." << endl;
+ std::cout << "\nStopping playback stream." << std::endl;
try {
- audio->stopStream(stream1);
- audio->startStream(stream2);
+ stream1->stopStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
fd = fopen("test.raw","wb");
+ try {
+ stream2->startStream();
+ }
+ catch (RtError &error) {
+ error.printMessage();
+ goto cleanup;
+ }
+
counter = 0;
- cout << "\nStarting recording stream for " << TIME << " seconds." << endl;
+ std::cout << "\nStarting recording stream for " << TIME << " seconds." << std::endl;
while (counter < frames) {
try {
- audio->tickStream(stream2);
+ stream2->tickStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
}
fclose(fd);
- cout << "\nAborting recording." << endl;
+ std::cout << "\nAborting recording." << std::endl;
try {
- audio->abortStream(stream2);
- audio->startStream(stream1);
- audio->startStream(stream2);
+ stream2->abortStream();
+ stream1->startStream();
+ stream2->startStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
counter = 0;
- cout << "\nStarting playback and record streams (quasi-duplex) for " << TIME << " seconds." << endl;
+ std::cout << "\nStarting playback and record streams (quasi-duplex) for " << TIME << " seconds." << std::endl;
while (counter < frames) {
try {
- audio->tickStream(stream2);
+ stream2->tickStream();
memcpy(buffer1, buffer2, sizeof(MY_TYPE) * chans * buffer_size);
- audio->tickStream(stream1);
+ stream1->tickStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
goto cleanup;
}
counter += buffer_size;
}
- cout << "\nStopping both streams." << endl;
+ std::cout << "\nStopping both streams." << std::endl;
try {
- audio->stopStream(stream1);
- audio->stopStream(stream2);
+ stream1->stopStream();
+ stream2->stopStream();
}
- catch (RtAudioError &m) {
- m.printMessage();
+ catch (RtError &error) {
+ error.printMessage();
}
cleanup:
- audio->closeStream(stream1);
- audio->closeStream(stream2);
- delete audio;
+ stream1->closeStream();
+ stream2->closeStream();
+ delete stream1;
+ delete stream2;
if (data) free(data);
return 0;