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. Use callback functionality.
10 /******************************************/
16 typedef signed long MY_TYPE;
17 #define FORMAT RTAUDIO_SINT24
18 #define SCALE 2147483647.0
21 #define FORMAT RTAUDIO_SINT8
24 typedef signed short MY_TYPE;
25 #define FORMAT RTAUDIO_SINT16
28 typedef signed long MY_TYPE;
29 #define FORMAT RTAUDIO_SINT32
30 #define SCALE 2147483647.0
32 typedef float MY_TYPE;
33 #define FORMAT RTAUDIO_FLOAT32
37 typedef double MY_TYPE;
38 #define FORMAT RTAUDIO_FLOAT64
42 #define BASE_RATE 0.005
46 /* Error function in case of incorrect command-line
47 argument specifications
49 std::cout << "\nuseage: call_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";
58 int saw(char *buffer, int buffer_size, void *data)
62 MY_TYPE *my_buffer = (MY_TYPE *) buffer;
63 double *my_data = (double *) data;
65 for (i=0; i<buffer_size; i++) {
66 for (j=0; j<chans; j++) {
67 *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
68 my_data[j] += BASE_RATE * (j+1+(j*0.1));
69 if (my_data[j] >= 1.0) my_data[j] -= 2.0;
76 int main(int argc, char *argv[])
78 int buffer_size, fs, device = 0;
83 // minimal command-line checking
84 if (argc != 3 && argc != 4 ) usage();
86 chans = (int) atoi(argv[1]);
87 fs = (int) atoi(argv[2]);
89 device = (int) atoi(argv[3]);
91 // Open the realtime output device
94 audio = new RtAudio(device, chans, 0, 0,
95 FORMAT, fs, &buffer_size, 4);
97 catch (RtError &error) {
102 data = (double *) calloc(chans, sizeof(double));
105 audio->setStreamCallback(&saw, (void *)data);
106 audio->startStream();
108 catch (RtError &error) {
109 error.printMessage();
113 std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << buffer_size << ").\n";
120 catch (RtError &error) {
121 error.printMessage();
125 audio->closeStream();
127 if (data) free(data);