787a85658a023cf574fbb30360ccb1c6a66c2247
[rtaudio.git] / tests / call_inout.cpp
1 /******************************************/
2 /*
3   call_inout.c
4   by Gary P. Scavone, 2001
5
6   Records from default input and passes it
7   through to the output.  Takes number of
8   channels and sample rate as input arguments.
9   Uses callback functionality.
10 */
11 /******************************************/
12
13 #include "RtAudio.h"
14 #include <iostream.h>
15
16 /*
17 typedef signed long  MY_TYPE;
18 #define FORMAT RtAudio::RTAUDIO_SINT24
19
20 typedef char  MY_TYPE;
21 #define FORMAT RtAudio::RTAUDIO_SINT8
22
23 typedef signed short  MY_TYPE;
24 #define FORMAT RtAudio::RTAUDIO_SINT16
25
26 typedef signed long  MY_TYPE;
27 #define FORMAT RtAudio::RTAUDIO_SINT32
28
29 typedef float  MY_TYPE;
30 #define FORMAT RtAudio::RTAUDIO_FLOAT32
31 */
32
33 typedef double  MY_TYPE;
34 #define FORMAT RtAudio::RTAUDIO_FLOAT64
35
36 void usage(void) {
37   /* Error function in case of incorrect command-line
38      argument specifications
39   */
40   cout << "\nuseage: call_inout N fs device\n";
41   cout << "    where N = number of channels,\n";
42   cout << "    fs = the sample rate,\n";
43   cout << "    and device = the device to use (default = 0).\n\n";
44   exit(0);
45 }
46
47 int inout(char *buffer, int buffer_size, void *)
48 {
49   // Surprise!!  We do nothing to pass the data through.
50   return 0;
51 }
52
53 int main(int argc, char *argv[])
54 {
55   int stream, chans, fs, device = 0;
56   RtAudio *audio;
57   char input;
58
59   // minimal command-line checking
60   if (argc != 3 && argc != 4 ) usage();
61
62   chans = (int) atoi(argv[1]);
63   fs = (int) atoi(argv[2]);
64   if ( argc == 4 )
65     device = (int) atoi(argv[3]);
66
67   // Open the realtime output device
68   int buffer_size = 512;
69   try {
70     audio = new RtAudio(&stream, device, chans, device, chans,
71                         FORMAT, fs, &buffer_size, 8);
72   }
73   catch (RtError &) {
74     exit(EXIT_FAILURE);
75   }
76
77   try {
78     audio->setStreamCallback(stream, &inout, NULL);
79     audio->startStream(stream);
80   }
81   catch (RtError &) {
82     goto cleanup;
83   }
84
85   cout << "\nRunning ... press <enter> to quit (buffer size = " << buffer_size << ").\n";
86   cin.get(input);
87
88   try {
89     audio->stopStream(stream);
90   }
91   catch (RtError &) {
92   }
93
94  cleanup:
95   audio->closeStream(stream);
96   delete audio;
97
98   return 0;
99 }