Version 2.0
[rtaudio-cdist.git] / tests / call_twostreams.cpp
1 /******************************************/
2 /*
3   twostreams.cpp
4   by Gary P. Scavone, 2001
5
6   Text 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
40 void usage(void) {
41   /* Error function in case of incorrect command-line
42      argument specifications
43   */
44   cout << "\nuseage: call_twostreams N fs\n";
45   cout << "    where N = number of channels,\n";
46   cout << "    and fs = the sample rate.\n\n";
47   exit(0);
48 }
49
50 int chans;
51
52 int in(char *buffer, int buffer_size, void *data)
53 {
54   extern int chans;
55   MY_TYPE *my_buffer = (MY_TYPE *) buffer;
56   MY_TYPE *my_data = (MY_TYPE *) data;
57   long buffer_bytes = buffer_size * chans * sizeof(MY_TYPE);
58
59   memcpy(my_data, my_buffer, buffer_bytes);
60
61   return 0;
62 }
63
64 int out(char *buffer, int buffer_size, void *data)
65 {
66   extern int chans;
67   MY_TYPE *my_buffer = (MY_TYPE *) buffer;
68   MY_TYPE *my_data = (MY_TYPE *) data;
69   long buffer_bytes = buffer_size * chans * sizeof(MY_TYPE);
70
71   memcpy(my_buffer, my_data, buffer_bytes);
72
73   return 0;
74 }
75
76 int main(int argc, char *argv[])
77 {
78   int device, buffer_size, stream1, stream2, fs;
79   MY_TYPE *data = 0;
80   RtAudio *audio;
81   char input;
82
83   // minimal command-line checking
84   if (argc != 3) usage();
85
86   chans = (int) atoi(argv[1]);
87   fs = (int) atoi(argv[2]);
88
89   // Open the realtime output device
90   buffer_size = 512;
91   device = 0; // default device
92   try {
93     audio = new RtAudio();
94   }
95   catch (RtAudioError &m) {
96     m.printMessage();
97     exit(EXIT_FAILURE);
98   }
99
100   try {
101     stream1 = audio->openStream(0, 0, device, chans,
102                                 FORMAT, fs, &buffer_size, 8);
103     stream2 = audio->openStream(device, chans, 0, 0,
104                                 FORMAT, fs, &buffer_size, 8);
105   }
106   catch (RtAudioError &m) {
107     m.printMessage();
108     goto cleanup;
109   }
110
111   data = (MY_TYPE *) calloc(chans*buffer_size, sizeof(MY_TYPE));
112   try {
113     audio->setStreamCallback(stream1, &in, (void *)data);
114     audio->setStreamCallback(stream2, &out, (void *)data);
115     audio->startStream(stream1);
116     audio->startStream(stream2);
117   }
118   catch (RtAudioError &m) {
119     m.printMessage();
120     goto cleanup;
121   }
122
123   cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
124   cin.get(input);
125
126   cout << "\nStopping both streams.\n";
127   try {
128     audio->stopStream(stream1);
129     audio->stopStream(stream2);
130   }
131   catch (RtAudioError &m) {
132     m.printMessage();
133     goto cleanup;
134   }
135
136   cout << "\nPress <enter> to restart streams:\n";
137   cin.get(input);
138
139   try {
140     audio->startStream(stream1);
141     audio->startStream(stream2);
142   }
143   catch (RtAudioError &m) {
144     m.printMessage();
145     goto cleanup;
146   }
147
148   cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
149   cin.get(input);
150
151   try {
152     audio->stopStream(stream1);
153     audio->stopStream(stream2);
154   }
155   catch (RtAudioError &m) {
156     m.printMessage();
157   }
158
159  cleanup:
160   audio->closeStream(stream1);
161   audio->closeStream(stream2);
162   delete audio;
163   if (data) free(data);
164
165   return 0;
166 }