Version 2.1.1
[rtaudio-cdist.git] / tests / call_playtwo.cpp
1 /******************************************/
2 /*
3   call_playtwo.cpp
4   by Gary P. Scavone, 2002.
5
6   Test executable using two streams with
7   callbacks.
8 */
9 /******************************************/
10
11 #include "RtAudio.h"
12 #include <iostream.h>
13
14 /*
15 typedef signed long  MY_TYPE;
16 #define FORMAT RtAudio::RTAUDIO_SINT24
17 #define SCALE  2147483647.0
18
19 typedef char  MY_TYPE;
20 #define FORMAT RtAudio::RTAUDIO_SINT8
21 #define SCALE  127.0
22
23 typedef signed short  MY_TYPE;
24 #define FORMAT RtAudio::RTAUDIO_SINT16
25 #define SCALE  32767.0
26
27 typedef signed long  MY_TYPE;
28 #define FORMAT RtAudio::RTAUDIO_SINT32
29 #define SCALE  2147483647.0
30
31 typedef float  MY_TYPE;
32 #define FORMAT RtAudio::RTAUDIO_FLOAT32
33 #define SCALE  1.0
34 */
35
36 typedef double  MY_TYPE;
37 #define FORMAT RtAudio::RTAUDIO_FLOAT64
38 #define SCALE  1.0
39 #define BASE_RATE1 0.005
40 #define BASE_RATE2 0.004
41
42 void usage(void) {
43   /* Error function in case of incorrect command-line
44      argument specifications
45   */
46   cout << "\nuseage: call_twostreams N fs\n";
47   cout << "    where N = number of channels,\n";
48   cout << "    and fs = the sample rate.\n\n";
49   exit(0);
50 }
51
52 int chans;
53
54 int saw1(char *buffer, int buffer_size, void *data)
55 {
56   int i, j;
57   extern int chans;
58   MY_TYPE *my_buffer = (MY_TYPE *) buffer;
59   double *my_data = (double *) data;
60
61   for (i=0; i<buffer_size; i++) {
62     for (j=0; j<chans; j++) {
63       *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
64       my_data[j] += BASE_RATE1 * (j+1+(j*0.1));
65       if (my_data[j] >= 1.0) my_data[j] -= 2.0;
66     }
67   }
68
69   return 0;
70 }
71
72 int saw2(char *buffer, int buffer_size, void *data)
73 {
74   int i, j;
75   extern int chans;
76   MY_TYPE *my_buffer = (MY_TYPE *) buffer;
77   double *my_data = (double *) data;
78
79   for (i=0; i<buffer_size; i++) {
80     for (j=0; j<chans; j++) {
81       *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
82       my_data[j] += BASE_RATE2 * (j+1+(j*0.1));
83       if (my_data[j] >= 1.0) my_data[j] -= 2.0;
84     }
85   }
86
87   return 0;
88 }
89
90 int main(int argc, char *argv[])
91 {
92   int device, buffer_size, stream1 = 0, stream2 = 0, fs;
93   double *data1 = 0;
94   double *data2 = 0;
95   RtAudio *audio;
96   char input;
97
98   // minimal command-line checking
99   if (argc != 3) usage();
100
101   chans = (int) atoi(argv[1]);
102   fs = (int) atoi(argv[2]);
103
104   // Open the realtime output device
105   buffer_size = 512;
106   device = 0; // default device
107   try {
108     audio = new RtAudio();
109   }
110   catch (RtError &) {
111     exit(EXIT_FAILURE);
112   }
113
114   try {
115     stream1 = audio->openStream(device, chans, 0, 0,
116                                 FORMAT, fs, &buffer_size, 8);
117     stream2 = audio->openStream(device, chans, 0, 0,
118                                 FORMAT, fs, &buffer_size, 8);
119   }
120   catch (RtError &) {
121     goto cleanup;
122   }
123
124   data1 = (double *) calloc(chans, sizeof(double));
125   data2 = (double *) calloc(chans, sizeof(double));
126
127   try {
128     audio->setStreamCallback(stream1, &saw1, (void *)data1);
129     audio->setStreamCallback(stream2, &saw2, (void *)data2);
130     audio->startStream(stream1);
131     audio->startStream(stream2);
132   }
133   catch (RtError &) {
134     goto cleanup;
135   }
136
137   cout << "\nRunning two streams ... press <enter> to quit.\n";
138   cin.get(input);
139
140   cout << "\nStopping both streams.\n";
141   try {
142     audio->stopStream(stream1);
143     audio->stopStream(stream2);
144   }
145   catch (RtError &) {
146     goto cleanup;
147   }
148
149   cout << "\nPress <enter> to restart streams:\n";
150   cin.get(input);
151
152   try {
153     audio->startStream(stream1);
154     audio->startStream(stream2);
155   }
156   catch (RtError &) {
157     goto cleanup;
158   }
159
160   cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
161   cin.get(input);
162
163   try {
164     audio->stopStream(stream1);
165     audio->stopStream(stream2);
166   }
167   catch (RtError &) {
168   }
169
170  cleanup:
171   audio->closeStream(stream1);
172   audio->closeStream(stream2);
173   delete audio;
174   if (data1) free(data1);
175   if (data2) free(data2);
176
177   return 0;
178 }