Version 3.0
[rtaudio.git] / tests / in_out.cpp
1 /******************************************/
2 /*
3   in_out.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   Use blocking functionality.
10 */
11 /******************************************/
12
13 #include "RtAudio.h"
14 #include <iostream>
15
16 /*
17 typedef signed long  MY_TYPE;
18 #define FORMAT RTAUDIO_SINT24
19
20 typedef char  MY_TYPE;
21 #define FORMAT RTAUDIO_SINT8
22
23 typedef signed short  MY_TYPE;
24 #define FORMAT RTAUDIO_SINT16
25
26 typedef signed long  MY_TYPE;
27 #define FORMAT RTAUDIO_SINT32
28
29 typedef float  MY_TYPE;
30 #define FORMAT RTAUDIO_FLOAT32
31 */
32
33 typedef double  MY_TYPE;
34 #define FORMAT RTAUDIO_FLOAT64
35
36 #define TIME   4.0
37
38 void usage(void) {
39   /* Error function in case of incorrect command-line
40      argument specifications
41   */
42   std::cout << "\nuseage: in_out N fs <device>\n";
43   std::cout << "    where N = number of channels,\n";
44   std::cout << "    fs = the sample rate,\n";
45   std::cout << "    and device = the device to use (default = 0).\n\n";
46   exit(0);
47 }
48
49 int main(int argc, char *argv[])
50 {
51   int chans, fs, buffer_size, device = 0;
52   long frames, counter = 0;
53   MY_TYPE *buffer;
54   RtAudio *audio;
55
56   // minimal command-line checking
57   if (argc != 3 && argc != 4 ) usage();
58
59   chans = (int) atoi(argv[1]);
60   fs = (int) atoi(argv[2]);
61   if ( argc == 4 )
62     device = (int) atoi(argv[3]);
63
64   // Open the realtime output device
65   buffer_size = 512;
66   try {
67     audio = new RtAudio(device, chans, device, chans,
68                         FORMAT, fs, &buffer_size, 8);
69   }
70   catch (RtError &error) {
71     error.printMessage();
72     exit(EXIT_FAILURE);
73   }
74
75   frames = (long) (fs * TIME);
76
77   try {
78     buffer = (MY_TYPE *) audio->getStreamBuffer();
79     audio->startStream();
80   }
81   catch (RtError &error) {
82     error.printMessage();
83     goto cleanup;
84   }
85
86   std::cout << "\nRunning for " << TIME << " seconds ... fragment_size = " << buffer_size << std::endl;
87   while (counter < frames) {
88
89     try {
90       audio->tickStream();
91     }
92     catch (RtError &error) {
93       error.printMessage();
94       goto cleanup;
95     }
96     counter += buffer_size;
97   }
98
99   try {
100     audio->stopStream();
101   }
102   catch (RtError &error) {
103     error.printMessage();
104   }
105
106  cleanup:
107   audio->closeStream();
108   delete audio;
109
110   return 0;
111 }