d028be5f4b28984bec293732045e2925b4b6956a
[rtaudio.git] / tests / play_saw.cpp
1 /******************************************/
2 /*
3   play_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.  Uses blocking functionality.
9 */
10 /******************************************/
11
12 #include "RtAudio.h"
13 #include <iostream.h>
14
15 /*
16 typedef signed long  MY_TYPE;
17 #define FORMAT RtAudio::RTAUDIO_SINT24
18 #define SCALE  2147483647.0
19 */
20
21 typedef char  MY_TYPE;
22 #define FORMAT RtAudio::RTAUDIO_SINT8
23 #define SCALE  127.0
24
25 /*
26 typedef signed short  MY_TYPE;
27 #define FORMAT RtAudio::RTAUDIO_SINT16
28 #define SCALE  32767.0
29
30
31 typedef signed long  MY_TYPE;
32 #define FORMAT RtAudio::RTAUDIO_SINT32
33 #define SCALE  2147483647.0
34
35 typedef float  MY_TYPE;
36 #define FORMAT RtAudio::RTAUDIO_FLOAT32
37 #define SCALE  1.0
38
39 typedef double  MY_TYPE;
40 #define FORMAT RtAudio::RTAUDIO_FLOAT64
41 #define SCALE  1.0
42 */
43
44 #define BASE_RATE 0.005
45 #define TIME   1.0
46
47 void usage(void) {
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";
54   exit(0);
55 }
56
57 int main(int argc, char *argv[])
58 {
59   int chans, fs, buffer_size, stream, device = 0;
60   long frames, counter = 0, i, j;
61   MY_TYPE *buffer;
62   RtAudio *audio;
63   double *data;
64
65   // minimal command-line checking
66   if (argc != 3 && argc != 4 ) usage();
67
68   chans = (int) atoi(argv[1]);
69   fs = (int) atoi(argv[2]);
70   if ( argc == 4 )
71     device = (int) atoi(argv[3]);
72
73   // Open the realtime output device
74   buffer_size = 512;
75   try {
76     audio = new RtAudio(&stream, device, chans, 0, 0,
77                         FORMAT, fs, &buffer_size, 4);
78   }
79   catch (RtError &) {
80     exit(EXIT_FAILURE);
81   }
82
83   frames = (long) (fs * TIME);
84   data = (double *) calloc(chans, sizeof(double));
85
86   try {
87     buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
88     audio->startStream(stream);
89   }
90   catch (RtError &) {
91     goto cleanup;
92   }
93
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;
101       }
102     }
103
104     try {
105       //cout << "frames until no block = " << audio->streamWillBlock(stream) << endl;
106       audio->tickStream(stream);
107     }
108     catch (RtError &) {
109       goto cleanup;
110     }
111
112     counter += buffer_size;
113   }
114
115   try {
116     audio->stopStream(stream);
117   }
118   catch (RtError &) {
119   }
120
121  cleanup:
122   audio->closeStream(stream);
123   delete audio;
124   if (data) free(data);
125
126   return 0;
127 }