CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Test_Support.cpp
Go to the documentation of this file.
1 //
2 // Test_Support.cpp -- some support functions for the CSL testing main()s
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 // Note that this file is not normally put into project or makefiles,
6 // since it is #included in the test's main program explicitly (once).
7 //
8 // Global IO and argument-processing support; usage() and run_tests() functions
9 //
10 // Default CSL main() options:
11 //
12 // -r srate -b blksiz -o noch -i nich -v verbosity -l logperiod -p outport -u
13 //
14 // -r srate set frame rate to srate Hz
15 // -b blksiz set buffer block size to blksiz
16 // -o nch set # of output channels to nch
17 // -i nch set # of input channels to nch
18 // -v verbos set verbosity level to verbos: 0/1/2
19 // -l logpd set logging period to logpd seconds
20 // -p port set the RemoteIO output port to port
21 // -u print usage message and exit
22 //
23
24 #include "Test_Support.h" // include my stuff
25 #include <stdlib.h>
26
27 using namespace csl;
28
29 IO_CLASS * theIO = 0; // Here's the global IO instance
30
31 #ifdef USE_TEST_MAIN // use the generic main() function here
32
33 extern void runTests(); // this is the list of tests to run (see the test files)
34
35 ////// MAIN //////
36
37 int main (int argc, const char * argv[]) {
38 printf("\n");
39 logMsg("Running CSL5 tests\n"); // print a log message
40 theIO = new IO_CLASS; // create the IO object
41
42 theIO->open(); // open & start it
43 theIO->start();
44 runTests(); // call the function with the list of unit tests
45 theIO->stop(); // stop & close PortAudio
46 theIO->close();
47 exit(0);
48 }
49
50 #endif // USE_TEST_MAIN
51
52 // Test the given DSP graph for the given amount of time
53
54 void runTest(UnitGenerator & vox, double dur) {
55 // theIO = new IO_CLASS; // create the IO object
56 theIO->setRoot(vox); // make some sound
57 sleepSec(dur); // sleep for the desired duration
58 theIO->clearRoot(); // make some silence
59 sleepSec(0.1); // wait for it to stop
60 }
61
62 // The default is to play for 3 seconds
63
64 void runTest(UnitGenerator & vox) {
65 runTest(vox, (double) 3.0);
66 }
67
68 // dump-test -- good for debugging
69
70 void dumpTest(UnitGenerator & vox) {
71 theIO->stop(); // stop it
72 theIO->setRoot(vox); // set the root of the graph
73 vox.dump(); // dump the root
74 theIO->test(); // and test the callback
75 theIO->clearRoot(); // make some silence
76 theIO->start(); // stop it
77 }
78
79 //
80 // Command-line option parsing
81 //
82 // find_option() -- locate an option in the argv[] list
83 // find_option(argc, argv, 't' str)
84 // returns -1 if -t not found;
85 // returns 0 of "-t" opt found without arg,
86 // returns 1 and sets ptr "str" to first opt after "-t" if opt and arg found
87 //
88 // Usage example:
89 // if (find_option(argc, argv, 't' str) > 0)
90 // float value = atof(str)'
91 //
92
93 int find_option(int argc, const char ** argv, char flag, unsigned * datum) {
94 for (int i = 1; i < argc; i++ ) {
95 if ((argv[i]) && (argv[i][0] == '-')) { // linear search, slow, but simple
96 if (argv[i][1] == flag) { // if flag found
97 if ((i + 1) == argc) { // if it's the last cmd-line option
98 datum = NULL;
99 return 0;
100 } else { // else of there's a possible follow-on arg
101 *datum = i+1;
102 return 1;
103 }
104 }
105 }
106 }
107 return -1; // if not found
108 }
109
110 //
111 // READ_CSL_OPTS macro -- Parse the standard CSL cmd-line options and set globals based on their values
112 //
113
114 #define READ_CSL_OPTS \
115 unsigned val; \
116 if (find_option(argc, argv, 'r', &val) > 0) { \
117 CGestalt::setFrameRate(atoi(argv[val])); \
118 printf("Setting frame rate to: %d\n", \
119 CGestalt::frameRate()); } \
120 if (find_option(argc, argv, 'b', &val) > 0) { \
121 CGestalt::setBlockSize(atoi(argv[val])); \
122 printf("Setting block size to: %d\n", \
123 CGestalt::blockSize()); } \
124 if (find_option(argc, argv, 'o', &val) > 0) { \
125 CGestalt::setNumOutChannels(atoi(argv[val])); \
126 printf("Setting numOutChannels to: %d\n", \
127 CGestalt::numOutChannels()); } \
128 if (find_option(argc, argv, 'i', &val) > 0) { \
129 CGestalt::setNumInChannels(atoi(argv[val])); \
130 printf("Setting numInChannels to: %d\n", \
131 CGestalt::numInChannels()); } \
132 if (find_option(argc, argv, 'l', &val) > 0) { \
133 CGestalt::setLoggingPeriod(atoi(argv[val])); \
134 printf("Setting loggingPeriod to: %d\n", \
135 CGestalt::loggingPeriod()); } \
136 if (find_option(argc, argv, 'p', &val) > 0) { \
137 CGestalt::setOutPort(atoi(argv[val])); \
138 printf("Setting outPort to: %d\n", \
139 CGestalt::outPort()); } \
140 if ((find_option(argc, argv, 'u', &val) >= 0) || \
141 (find_option(argc, argv, 'h', &val) >= 0)) { \
142 usage(argv[0]); \
143 exit(0); } \
144 if (find_option(argc, argv, 'v', &val) > 0) \
145 if (argv[val][0] == '-') \
146 CGestalt::setVerbosity(2); \
147 else \
148 CGestalt::setVerbosity(atoi(argv[val]))
149
150 // Usage function
151
152 void usage(const char * name) {
153 printf("\n%s: %s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\n", name,
154 "-r srate -b blksiz -o noch -i nich -v verbosity -l logperiod -p outport",
155 "-r srate set frame rate to srate Hz",
156 "-b blksiz set buffer block size to blksiz",
157 "-o nch set # of output channels to nch",
158 "-i nch set # of input channels to nch",
159 "-v verbos set verbosity level to 0-3",
160 "-l logpd set logging period to logpd seconds",
161 "-p pt set the output port to pt");
162 exit(0);
163 }