57f225c91f17cfbc29ed5daa845b0c628759b280
[rtaudio-cdist.git] / tests / call_saw.cpp
1 /******************************************/
2 /*
3   call_saw.c
4   by Gary P. Scavone, 2001
5
6   Play sawtooth waveforms of distinct frequency.
7   Takes number of channels and sample rate as
8   input arguments.  Use callback functionality.
9 */
10 /******************************************/
11
12 #include "RtAudio.h"
13 #include <iostream>
14
15 /*
16 typedef signed long  MY_TYPE;
17 #define FORMAT RTAUDIO_SINT24
18 #define SCALE  2147483647.0
19
20 typedef char  MY_TYPE;
21 #define FORMAT RTAUDIO_SINT8
22 #define SCALE  127.0
23
24 typedef signed short  MY_TYPE;
25 #define FORMAT RTAUDIO_SINT16
26 #define SCALE  32767.0
27
28 typedef signed long  MY_TYPE;
29 #define FORMAT RTAUDIO_SINT32
30 #define SCALE  2147483647.0
31 */
32 typedef float  MY_TYPE;
33 #define FORMAT RTAUDIO_FLOAT32
34 #define SCALE  1.0
35
36 /*
37 typedef double  MY_TYPE;
38 #define FORMAT RTAUDIO_FLOAT64
39 #define SCALE  1.0
40 */
41
42 #define BASE_RATE 0.005
43 #define TIME   1.0
44
45 void usage(void) {
46   /* Error function in case of incorrect command-line
47      argument specifications
48   */
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";
53   exit(0);
54 }
55
56 int chans;
57
58 int saw(char *buffer, int buffer_size, void *data)
59 {
60   int i, j;
61   extern int chans;
62   MY_TYPE *my_buffer = (MY_TYPE *) buffer;
63   double *my_data = (double *) data;
64
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;
70     }
71   }
72
73   return 0;
74 }
75
76 int main(int argc, char *argv[])
77 {
78   int buffer_size, fs, device = 0;
79   RtAudio *audio;
80   double *data;
81   char input;
82
83   // minimal command-line checking
84   if (argc != 3 && argc != 4 ) usage();
85
86   chans = (int) atoi(argv[1]);
87   fs = (int) atoi(argv[2]);
88   if ( argc == 4 )
89     device = (int) atoi(argv[3]);
90
91   // Open the realtime output device
92   buffer_size = 1024;
93   try {
94     audio = new RtAudio(device, chans, 0, 0,
95                         FORMAT, fs, &buffer_size, 4);
96   }
97   catch (RtError &error) {
98     error.printMessage();
99     exit(EXIT_FAILURE);
100   }
101
102   data = (double *) calloc(chans, sizeof(double));
103
104   try {
105     audio->setStreamCallback(&saw, (void *)data);
106     audio->startStream();
107   }
108   catch (RtError &error) {
109     error.printMessage();
110     goto cleanup;
111   }
112
113   std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << buffer_size << ").\n";
114   std::cin.get(input);
115
116   // Stop the stream.
117   try {
118     audio->stopStream();
119   }
120   catch (RtError &error) {
121     error.printMessage();
122   }
123
124  cleanup:
125   audio->closeStream();
126   delete audio;
127   if (data) free(data);
128
129   return 0;
130 }