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_SINT24
18 #define SCALE 2147483647.0
21 #define FORMAT RTAUDIO_SINT8
25 typedef signed short MY_TYPE;
26 #define FORMAT RTAUDIO_SINT16
30 typedef signed long MY_TYPE;
31 #define FORMAT RTAUDIO_SINT32
32 #define SCALE 2147483647.0
34 typedef float MY_TYPE;
35 #define FORMAT RTAUDIO_FLOAT32
38 typedef double MY_TYPE;
39 #define FORMAT RTAUDIO_FLOAT64
43 #define BASE_RATE 0.005
47 // Error function in case of incorrect command-line
48 // argument specifications.
49 std::cout << "\nuseage: play_saw N fs <device>\n";
50 std::cout << " where N = number of channels,\n";
51 std::cout << " fs = the sample rate,\n";
52 std::cout << " and device = the device to use (default = 0).\n\n";
56 int main(int argc, char *argv[])
58 int chans, fs, buffer_size, device = 0;
59 long frames, counter = 0, i, j;
64 // minimal command-line checking
65 if (argc != 3 && argc != 4 ) usage();
67 chans = (int) atoi(argv[1]);
68 fs = (int) atoi(argv[2]);
70 device = (int) atoi(argv[3]);
72 // Open the realtime output device
75 audio = new RtAudio(device, chans, 0, 0,
76 FORMAT, fs, &buffer_size, 4);
78 catch (RtError &error) {
83 frames = (long) (fs * TIME);
84 data = (double *) calloc(chans, sizeof(double));
87 buffer = (MY_TYPE *) audio->getStreamBuffer();
90 catch (RtError &error) {
95 std::cout << "\nPlaying for " << TIME << " seconds ... buffer size = " << buffer_size << "." << std::endl;
96 while (counter < frames) {
97 for (i=0; i<buffer_size; i++) {
98 for (j=0; j<chans; j++) {
99 buffer[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
100 data[j] += BASE_RATE * (j+1+(j*0.1));
101 if (data[j] >= 1.0) data[j] -= 2.0;
108 catch (RtError &error) {
109 error.printMessage();
113 counter += buffer_size;
119 catch (RtError &error) {
120 error.printMessage();
124 audio->closeStream();
126 if (data) free(data);