andrewm@5
|
1 /*
|
andrewm@5
|
2 * RTAudioCommandLine.cpp
|
andrewm@5
|
3 *
|
andrewm@5
|
4 * Created on: Nov 8, 2014
|
andrewm@5
|
5 * Author: parallels
|
andrewm@5
|
6 */
|
andrewm@5
|
7
|
andrewm@5
|
8 #include <iostream>
|
andrewm@5
|
9 #include <cstdlib>
|
andrewm@5
|
10 #include <cstring>
|
andrewm@5
|
11 #include <getopt.h>
|
andrewm@45
|
12 #include "../include/BeagleRT.h"
|
giuliomoro@24
|
13
|
giuliomoro@171
|
14 #define OPT_PRU_FILE 1000
|
giuliomoro@171
|
15 #define OPT_PGA_GAIN_LEFT 1001
|
giuliomoro@171
|
16 #define OPT_PGA_GAIN_RIGHT 1002
|
andrewm@280
|
17 #define OPT_PRU_NUMBER 1003
|
giuliomoro@171
|
18
|
andrewm@5
|
19
|
andrewm@47
|
20 enum {
|
andrewm@47
|
21 kAmplifierMutePin = 61 // P8-26 controls amplifier mute
|
andrewm@47
|
22 };
|
andrewm@47
|
23
|
andrewm@5
|
24 // Default command-line options for RTAudio
|
andrewm@5
|
25 struct option gDefaultLongOptions[] =
|
andrewm@5
|
26 {
|
andrewm@5
|
27 {"period", 1, NULL, 'p'},
|
andrewm@5
|
28 {"verbose", 0, NULL, 'v'},
|
andrewm@50
|
29 {"use-analog", 1, NULL, 'N'},
|
andrewm@50
|
30 {"use-digital", 1, NULL, 'G'},
|
giuliomoro@19
|
31 {"analog-channels", 1, NULL, 'C'},
|
andrewm@50
|
32 {"digital-channels", 1, NULL, 'B'},
|
andrewm@5
|
33 {"mute-speaker", 1, NULL, 'M'},
|
andrewm@5
|
34 {"dac-level", 1, NULL, 'D'},
|
andrewm@5
|
35 {"adc-level", 1, NULL, 'A'},
|
giuliomoro@171
|
36 {"pga-gain-left", 1, NULL, OPT_PGA_GAIN_LEFT},
|
giuliomoro@171
|
37 {"pga-gain-right", 1, NULL, OPT_PGA_GAIN_RIGHT},
|
andrewm@5
|
38 {"hp-level", 1, NULL, 'H'},
|
andrewm@50
|
39 {"receive-port", 1, NULL, 'R'},
|
andrewm@50
|
40 {"transmit-port", 1, NULL, 'T'},
|
andrewm@50
|
41 {"server-name", 1, NULL, 'S'},
|
andrewm@45
|
42 {"pru-file", 1, NULL, OPT_PRU_FILE},
|
andrewm@280
|
43 {"pru-number", 1, NULL, OPT_PRU_NUMBER},
|
andrewm@5
|
44 {NULL, 0, NULL, 0}
|
andrewm@5
|
45 };
|
giuliomoro@24
|
46
|
andrewm@50
|
47 const char gDefaultShortOptions[] = "p:vN:M:C:D:A:H:G:B:R:T:S:";
|
andrewm@5
|
48
|
andrewm@45
|
49 // This function sets the default settings for the BeagleRTInitSettings structure
|
andrewm@45
|
50 void BeagleRT_defaultSettings(BeagleRTInitSettings *settings)
|
andrewm@5
|
51 {
|
andrewm@5
|
52 // Set default values for settings
|
giuliomoro@178
|
53 settings->periodSize = 16;
|
andrewm@45
|
54 settings->useAnalog = 1;
|
andrewm@45
|
55 settings->useDigital = 1;
|
andrewm@45
|
56 settings->numAnalogChannels = 8;
|
andrewm@45
|
57 settings->numDigitalChannels = 16;
|
andrewm@45
|
58
|
andrewm@5
|
59 settings->beginMuted = 0;
|
andrewm@5
|
60 settings->dacLevel = DEFAULT_DAC_LEVEL;
|
andrewm@5
|
61 settings->adcLevel = DEFAULT_ADC_LEVEL;
|
giuliomoro@171
|
62 for(int n = 0; n < 2; n++)
|
giuliomoro@171
|
63 settings->pgaGain[n] = DEFAULT_PGA_GAIN;
|
andrewm@5
|
64 settings->headphoneLevel = DEFAULT_HP_LEVEL;
|
andrewm@280
|
65 settings->numMuxChannels = 0;
|
andrewm@45
|
66
|
andrewm@5
|
67 settings->verbose = 0;
|
andrewm@280
|
68 settings->pruNumber = 0;
|
andrewm@45
|
69 settings->pruFilename[0] = '\0';
|
andrewm@45
|
70
|
andrewm@45
|
71 // These two deliberately have no command-line flags by default.
|
andrewm@45
|
72 // A given program might prefer one mode or another, but it's unlikely
|
andrewm@45
|
73 // the user would want to switch at runtime
|
andrewm@45
|
74 settings->interleave = 1;
|
andrewm@45
|
75 settings->analogOutputsPersist = 1;
|
andrewm@45
|
76
|
andrewm@5
|
77 settings->codecI2CAddress = CODEC_I2C_ADDRESS;
|
andrewm@45
|
78 settings->receivePort = 9998;
|
andrewm@45
|
79 settings->transmitPort = 9999;
|
giuliomoro@24
|
80 strcpy(settings->serverName, "127.0.0.1");
|
andrewm@5
|
81 settings->ampMutePin = kAmplifierMutePin;
|
andrewm@5
|
82 }
|
andrewm@5
|
83
|
andrewm@5
|
84 // This function drops in place of getopt() in the main() function
|
andrewm@5
|
85 // and handles the initialisation of the RTAudio settings using
|
andrewm@5
|
86 // standard command-line arguments. System default arguments will
|
andrewm@5
|
87 // be stored in settings, otherwise arguments will be returned
|
andrewm@5
|
88 // as getopt() normally does.
|
andrewm@5
|
89
|
andrewm@45
|
90 int BeagleRT_getopt_long(int argc, char *argv[], const char *customShortOptions, const struct option *customLongOptions, BeagleRTInitSettings *settings)
|
andrewm@5
|
91 {
|
andrewm@5
|
92 static int firstRun = 1;
|
andrewm@5
|
93 static char totalShortOptions[256];
|
andrewm@5
|
94 static struct option totalLongOptions[256];
|
andrewm@5
|
95
|
andrewm@5
|
96 int c;
|
andrewm@5
|
97
|
andrewm@5
|
98 // Prep total option string the first time this is
|
andrewm@5
|
99 // run. As a getopt() substitute, it will be called repeatedly working its
|
andrewm@5
|
100 // way through argc and argv.
|
andrewm@5
|
101 if(firstRun) {
|
andrewm@5
|
102 firstRun = 0;
|
andrewm@5
|
103
|
andrewm@5
|
104 // Copy short options into one string
|
andrewm@5
|
105 strcpy(totalShortOptions, gDefaultShortOptions);
|
andrewm@5
|
106 strncat(totalShortOptions, customShortOptions, 256 - strlen(gDefaultShortOptions) - 1);
|
andrewm@5
|
107
|
andrewm@5
|
108 // Copy long options into one array
|
andrewm@5
|
109 int n = 0;
|
andrewm@5
|
110 while(1) {
|
andrewm@5
|
111 if(gDefaultLongOptions[n].name == NULL)
|
andrewm@5
|
112 break;
|
andrewm@5
|
113 totalLongOptions[n].name = gDefaultLongOptions[n].name;
|
andrewm@5
|
114 totalLongOptions[n].has_arg = gDefaultLongOptions[n].has_arg;
|
andrewm@5
|
115 totalLongOptions[n].flag = gDefaultLongOptions[n].flag;
|
andrewm@5
|
116 totalLongOptions[n].val = gDefaultLongOptions[n].val;
|
andrewm@5
|
117 n++;
|
andrewm@5
|
118 }
|
andrewm@5
|
119
|
andrewm@5
|
120 // Copy custom options into the array, if present
|
andrewm@5
|
121 if(customLongOptions == 0) {
|
andrewm@5
|
122 // Terminate the array
|
andrewm@5
|
123 totalLongOptions[n].name = NULL;
|
andrewm@5
|
124 totalLongOptions[n].has_arg = 0;
|
andrewm@5
|
125 totalLongOptions[n].flag = NULL;
|
andrewm@5
|
126 totalLongOptions[n].val = 0;
|
andrewm@5
|
127 }
|
andrewm@5
|
128 else {
|
andrewm@5
|
129 int customIndex = 0;
|
andrewm@5
|
130 while(n < 256) {
|
andrewm@5
|
131 if(customLongOptions[customIndex].name == NULL)
|
andrewm@5
|
132 break;
|
andrewm@5
|
133 totalLongOptions[n].name = customLongOptions[customIndex].name;
|
andrewm@5
|
134 totalLongOptions[n].has_arg = customLongOptions[customIndex].has_arg;
|
andrewm@5
|
135 totalLongOptions[n].flag = customLongOptions[customIndex].flag;
|
andrewm@5
|
136 totalLongOptions[n].val = customLongOptions[customIndex].val;
|
andrewm@5
|
137 n++;
|
andrewm@5
|
138 customIndex++;
|
andrewm@5
|
139 }
|
andrewm@5
|
140
|
andrewm@5
|
141 // Terminate the array
|
andrewm@5
|
142 totalLongOptions[n].name = NULL;
|
andrewm@5
|
143 totalLongOptions[n].has_arg = 0;
|
andrewm@5
|
144 totalLongOptions[n].flag = NULL;
|
andrewm@5
|
145 totalLongOptions[n].val = 0;
|
andrewm@5
|
146 }
|
andrewm@5
|
147 }
|
andrewm@5
|
148
|
andrewm@5
|
149 while(1) {
|
andrewm@5
|
150 if ((c = getopt_long(argc, argv, totalShortOptions, totalLongOptions, NULL)) < 0)
|
andrewm@5
|
151 return c;
|
andrewm@5
|
152
|
andrewm@5
|
153 switch (c) {
|
andrewm@5
|
154 case 'p':
|
andrewm@5
|
155 settings->periodSize = atoi(optarg);
|
andrewm@5
|
156 if(settings->periodSize < 1)
|
andrewm@5
|
157 settings->periodSize = 1;
|
andrewm@5
|
158 break;
|
andrewm@5
|
159 case 'v':
|
andrewm@5
|
160 settings->verbose = 1;
|
andrewm@5
|
161 break;
|
andrewm@50
|
162 case 'N':
|
giuliomoro@19
|
163 settings->useAnalog = atoi(optarg);
|
andrewm@5
|
164 break;
|
andrewm@50
|
165 case 'G':
|
giuliomoro@19
|
166 settings->useDigital = atoi(optarg);
|
giuliomoro@240
|
167 if(settings->useDigital == 0){
|
giuliomoro@240
|
168 settings->numDigitalChannels = 0;
|
giuliomoro@240
|
169 }
|
giuliomoro@16
|
170 break;
|
andrewm@12
|
171 case 'C':
|
giuliomoro@19
|
172 settings->numAnalogChannels = atoi(optarg);
|
andrewm@280
|
173 if(settings->numAnalogChannels >= 8) {
|
andrewm@280
|
174 // Use multiplexer capelet to run larger numbers of channels
|
andrewm@280
|
175 if(settings->numAnalogChannels >= 64)
|
andrewm@280
|
176 settings->numMuxChannels = 8;
|
andrewm@280
|
177 else if(settings->numAnalogChannels >= 32)
|
andrewm@280
|
178 settings->numMuxChannels = 4;
|
andrewm@280
|
179 else if(settings->numAnalogChannels >= 16)
|
andrewm@280
|
180 settings->numMuxChannels = 2;
|
giuliomoro@19
|
181 settings->numAnalogChannels = 8;
|
andrewm@280
|
182 }
|
giuliomoro@19
|
183 else if(settings->numAnalogChannels >= 4)
|
giuliomoro@19
|
184 settings->numAnalogChannels = 4;
|
andrewm@12
|
185 else
|
giuliomoro@19
|
186 settings->numAnalogChannels = 2;
|
andrewm@12
|
187 break;
|
andrewm@50
|
188 case 'B':
|
giuliomoro@19
|
189 settings->numDigitalChannels = atoi(optarg);
|
giuliomoro@19
|
190 if(settings->numDigitalChannels >= 16)
|
giuliomoro@19
|
191 settings->numDigitalChannels = 16;
|
giuliomoro@19
|
192 else if (settings->numDigitalChannels < 1){
|
giuliomoro@19
|
193 settings->numDigitalChannels = 0;
|
giuliomoro@19
|
194 settings->useDigital = 0; //TODO: this actually works only if -G 0 is specified after -g 1.
|
giuliomoro@19
|
195 //No worries, though: disabling numDigital will only prevent the pins from being exported.
|
giuliomoro@16
|
196 }
|
giuliomoro@16
|
197 break;
|
andrewm@5
|
198 case 'M':
|
andrewm@5
|
199 settings->beginMuted = atoi(optarg);
|
andrewm@5
|
200 break;
|
andrewm@5
|
201 case 'D':
|
andrewm@5
|
202 settings->dacLevel = atof(optarg);
|
andrewm@5
|
203 break;
|
andrewm@5
|
204 case 'A':
|
andrewm@5
|
205 settings->adcLevel = atof(optarg);
|
andrewm@5
|
206 break;
|
andrewm@5
|
207 case 'H':
|
andrewm@5
|
208 settings->headphoneLevel = atof(optarg);
|
andrewm@5
|
209 break;
|
andrewm@50
|
210 case 'R':
|
giuliomoro@24
|
211 settings->receivePort = atoi(optarg);
|
giuliomoro@24
|
212 break;
|
andrewm@50
|
213 case 'T':
|
giuliomoro@24
|
214 settings->transmitPort = atoi(optarg);
|
giuliomoro@24
|
215 break;
|
andrewm@50
|
216 case 'S':
|
giuliomoro@24
|
217 if(strlen(optarg)<MAX_SERVERNAME_LENGTH)
|
giuliomoro@24
|
218 strcpy(settings->serverName, optarg);
|
giuliomoro@24
|
219 else
|
giuliomoro@24
|
220 std::cerr << "Warning: server name is too long (>" << MAX_SERVERNAME_LENGTH << " characters)."
|
giuliomoro@24
|
221 " Using default severName Instead ( " << settings->serverName << " ).\n";
|
giuliomoro@24
|
222 break;
|
giuliomoro@29
|
223 case OPT_PRU_FILE:
|
andrewm@45
|
224 if(strlen(optarg) < MAX_PRU_FILENAME_LENGTH)
|
giuliomoro@16
|
225 strcpy(settings->pruFilename, optarg);
|
giuliomoro@16
|
226 else
|
giuliomoro@16
|
227 std::cerr << "Warning: filename for the PRU code is too long (>" << MAX_PRU_FILENAME_LENGTH << " characters). Using embedded PRU code instead\n";
|
giuliomoro@16
|
228 break;
|
giuliomoro@171
|
229 case OPT_PGA_GAIN_LEFT:
|
giuliomoro@171
|
230 settings->pgaGain[0] = atof(optarg);
|
giuliomoro@171
|
231 break;
|
giuliomoro@171
|
232 case OPT_PGA_GAIN_RIGHT:
|
giuliomoro@171
|
233 settings->pgaGain[1] = atof(optarg);
|
giuliomoro@171
|
234 break;
|
andrewm@280
|
235 case OPT_PRU_NUMBER:
|
andrewm@280
|
236 settings->pruNumber = atoi(optarg);
|
andrewm@280
|
237 break;
|
andrewm@5
|
238 case '?':
|
andrewm@5
|
239 default:
|
andrewm@5
|
240 return c;
|
andrewm@5
|
241 }
|
andrewm@5
|
242 }
|
andrewm@5
|
243 }
|
andrewm@5
|
244
|
andrewm@5
|
245 // This function prints standard usage information for default arguments
|
andrewm@5
|
246 // Call from within your own usage function
|
andrewm@5
|
247 void BeagleRT_usage()
|
andrewm@5
|
248 {
|
giuliomoro@19
|
249 std::cerr << " --period [-p] period: Set the hardware period (buffer) size in analog samples\n";
|
giuliomoro@16
|
250 std::cerr << " --dac-level [-D] dBs: Set the DAC output level (0dB max; -63.5dB min)\n";
|
giuliomoro@16
|
251 std::cerr << " --adc-level [-A] dBs: Set the ADC input level (0dB max; -12dB min)\n";
|
giuliomoro@171
|
252 std::cerr << " --pga-gain-left dBs: Set the Programmable Gain Amplifier for the left audio channel (0dBmin; 59.5dB max; default: 16dB)\n";
|
giuliomoro@171
|
253 std::cerr << " --pga-gain-right dBs: Set the Programmable Gain Amplifier for the right audio channel (0dBmin; 59.5dB max; default: 16dB)\n";
|
giuliomoro@16
|
254 std::cerr << " --hp-level [-H] dBs: Set the headphone output level (0dB max; -63.5dB min)\n";
|
giuliomoro@16
|
255 std::cerr << " --mute-speaker [-M] val: Set whether to mute the speaker initially (default: no)\n";
|
andrewm@52
|
256 std::cerr << " --use-analog [-N] val: Set whether to use ADC/DAC analog (default: yes)\n";
|
andrewm@52
|
257 std::cerr << " --use-digital [-G] val: Set whether to use digital GPIO channels (default: yes)\n";
|
giuliomoro@19
|
258 std::cerr << " --analog-channels [-C] val: Set the number of ADC/DAC channels (default: 8)\n";
|
andrewm@52
|
259 std::cerr << " --digital-channels [-B] val: Set the number of GPIO channels (default: 16)\n";
|
andrewm@52
|
260 std::cerr << " --receive-port [-R] val: Set the receive port (default: 9998)\n";
|
andrewm@52
|
261 std::cerr << " --transmit-port [-T] val: Set the transmit port (default: 9999)\n";
|
andrewm@52
|
262 std::cerr << " --server-name [-S] val: Set the destination server name (default: '127.0.0.1')\n";
|
giuliomoro@24
|
263 std::cerr << " --pru-file val: Set an optional external file to use for the PRU binary code\n";
|
andrewm@280
|
264 std::cerr << " --pru-number val: Set the PRU to use for I/O (options: 0 or 1, default: 0)\n";
|
giuliomoro@16
|
265 std::cerr << " --verbose [-v]: Enable verbose logging information\n";
|
andrewm@5
|
266 }
|
giuliomoro@16
|
267
|