1 /******************************************/
4 by Gary P. Scavone, 2001
6 Play sawtooth waveforms of distinct frequency.
7 Takes number of channels and sample rate as
8 input arguments. Uses blocking functionality.
10 /******************************************/
16 typedef signed long MY_TYPE;
17 #define FORMAT RtAudio::RTAUDIO_SINT24
18 #define SCALE 2147483647.0
22 #define FORMAT RtAudio::RTAUDIO_SINT8
26 typedef signed short MY_TYPE;
27 #define FORMAT RtAudio::RTAUDIO_SINT16
31 typedef signed long MY_TYPE;
32 #define FORMAT RtAudio::RTAUDIO_SINT32
33 #define SCALE 2147483647.0
35 typedef float MY_TYPE;
36 #define FORMAT RtAudio::RTAUDIO_FLOAT32
39 typedef double MY_TYPE;
40 #define FORMAT RtAudio::RTAUDIO_FLOAT64
44 #define BASE_RATE 0.005
48 // Error function in case of incorrect command-line
49 // argument specifications.
50 cout << "\nuseage: play_saw N fs <device>\n";
51 cout << " where N = number of channels,\n";
52 cout << " fs = the sample rate,\n";
53 cout << " and device = the device to use (default = 0).\n\n";
57 int main(int argc, char *argv[])
59 int chans, fs, buffer_size, stream, device = 0;
60 long frames, counter = 0, i, j;
65 // minimal command-line checking
66 if (argc != 3 && argc != 4 ) usage();
68 chans = (int) atoi(argv[1]);
69 fs = (int) atoi(argv[2]);
71 device = (int) atoi(argv[3]);
73 // Open the realtime output device
76 audio = new RtAudio(&stream, device, chans, 0, 0,
77 FORMAT, fs, &buffer_size, 4);
83 frames = (long) (fs * TIME);
84 data = (double *) calloc(chans, sizeof(double));
87 buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
88 audio->startStream(stream);
94 cout << "\nPlaying for " << TIME << " seconds ... buffer size = " << buffer_size << "." << endl;
95 while (counter < frames) {
96 for (i=0; i<buffer_size; i++) {
97 for (j=0; j<chans; j++) {
98 buffer[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
99 data[j] += BASE_RATE * (j+1+(j*0.1));
100 if (data[j] >= 1.0) data[j] -= 2.0;
105 //cout << "frames until no block = " << audio->streamWillBlock(stream) << endl;
106 audio->tickStream(stream);
112 counter += buffer_size;
116 audio->stopStream(stream);
122 audio->closeStream(stream);
124 if (data) free(data);