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@5
|
12 #include "../include/RTAudio.h"
|
giuliomoro@23
|
13 #ifndef OPT_PRU_FILE
|
giuliomoro@23
|
14 #define OPT_PRU_FILE -1
|
giuliomoro@23
|
15 #endif
|
andrewm@5
|
16
|
andrewm@5
|
17 // Default command-line options for RTAudio
|
andrewm@5
|
18 struct option gDefaultLongOptions[] =
|
andrewm@5
|
19 {
|
andrewm@5
|
20 {"period", 1, NULL, 'p'},
|
andrewm@5
|
21 {"verbose", 0, NULL, 'v'},
|
giuliomoro@19
|
22 {"use-analog", 1, NULL, 'm'},
|
giuliomoro@23
|
23 {"use-digital-gpio", 1, NULL, 'g'},
|
giuliomoro@19
|
24 {"analog-channels", 1, NULL, 'C'},
|
giuliomoro@23
|
25 {"digital-gpio-channels", 1, NULL, 'G'},
|
andrewm@5
|
26 {"mute-speaker", 1, NULL, 'M'},
|
andrewm@5
|
27 {"dac-level", 1, NULL, 'D'},
|
andrewm@5
|
28 {"adc-level", 1, NULL, 'A'},
|
andrewm@5
|
29 {"hp-level", 1, NULL, 'H'},
|
giuliomoro@23
|
30 {"pru-file",1,NULL,OPT_PRU_FILE},
|
andrewm@5
|
31 {NULL, 0, NULL, 0}
|
andrewm@5
|
32 };
|
giuliomoro@23
|
33 const char gDefaultShortOptions[] = "p:vm:M:C:D:A:H:g:G:";
|
andrewm@5
|
34
|
andrewm@5
|
35 // This function sets the default settings for the RTAudioSettings structure
|
andrewm@5
|
36 void BeagleRT_defaultSettings(RTAudioSettings *settings)
|
andrewm@5
|
37 {
|
andrewm@5
|
38 // Set default values for settings
|
andrewm@5
|
39 settings->periodSize = 8;
|
andrewm@5
|
40 settings->beginMuted = 0;
|
andrewm@5
|
41 settings->dacLevel = DEFAULT_DAC_LEVEL;
|
andrewm@5
|
42 settings->adcLevel = DEFAULT_ADC_LEVEL;
|
andrewm@5
|
43 settings->headphoneLevel = DEFAULT_HP_LEVEL;
|
giuliomoro@19
|
44 settings->useAnalog = 1;
|
giuliomoro@19
|
45 settings->useDigital = 1;
|
giuliomoro@19
|
46 settings->numAnalogChannels = 8;
|
giuliomoro@19
|
47 settings->numDigitalChannels = 16;
|
andrewm@5
|
48 settings->verbose = 0;
|
giuliomoro@16
|
49 settings->pruFilename[0]='\0';
|
andrewm@5
|
50 settings->codecI2CAddress = CODEC_I2C_ADDRESS;
|
andrewm@5
|
51 settings->ampMutePin = kAmplifierMutePin;
|
andrewm@5
|
52 }
|
andrewm@5
|
53
|
andrewm@5
|
54 // This function drops in place of getopt() in the main() function
|
andrewm@5
|
55 // and handles the initialisation of the RTAudio settings using
|
andrewm@5
|
56 // standard command-line arguments. System default arguments will
|
andrewm@5
|
57 // be stored in settings, otherwise arguments will be returned
|
andrewm@5
|
58 // as getopt() normally does.
|
andrewm@5
|
59
|
andrewm@5
|
60 int BeagleRT_getopt_long(int argc, char *argv[], const char *customShortOptions, const struct option *customLongOptions, RTAudioSettings *settings)
|
andrewm@5
|
61 {
|
andrewm@5
|
62 static int firstRun = 1;
|
andrewm@5
|
63 static char totalShortOptions[256];
|
andrewm@5
|
64 static struct option totalLongOptions[256];
|
andrewm@5
|
65
|
andrewm@5
|
66 int c;
|
andrewm@5
|
67
|
andrewm@5
|
68 // Prep total option string the first time this is
|
andrewm@5
|
69 // run. As a getopt() substitute, it will be called repeatedly working its
|
andrewm@5
|
70 // way through argc and argv.
|
andrewm@5
|
71 if(firstRun) {
|
andrewm@5
|
72 firstRun = 0;
|
andrewm@5
|
73
|
andrewm@5
|
74 // Copy short options into one string
|
andrewm@5
|
75 strcpy(totalShortOptions, gDefaultShortOptions);
|
andrewm@5
|
76 strncat(totalShortOptions, customShortOptions, 256 - strlen(gDefaultShortOptions) - 1);
|
andrewm@5
|
77
|
andrewm@5
|
78 // Copy long options into one array
|
andrewm@5
|
79 int n = 0;
|
andrewm@5
|
80 while(1) {
|
andrewm@5
|
81 if(gDefaultLongOptions[n].name == NULL)
|
andrewm@5
|
82 break;
|
andrewm@5
|
83 totalLongOptions[n].name = gDefaultLongOptions[n].name;
|
andrewm@5
|
84 totalLongOptions[n].has_arg = gDefaultLongOptions[n].has_arg;
|
andrewm@5
|
85 totalLongOptions[n].flag = gDefaultLongOptions[n].flag;
|
andrewm@5
|
86 totalLongOptions[n].val = gDefaultLongOptions[n].val;
|
andrewm@5
|
87 n++;
|
andrewm@5
|
88 }
|
andrewm@5
|
89
|
andrewm@5
|
90 // Copy custom options into the array, if present
|
andrewm@5
|
91 if(customLongOptions == 0) {
|
andrewm@5
|
92 // Terminate the array
|
andrewm@5
|
93 totalLongOptions[n].name = NULL;
|
andrewm@5
|
94 totalLongOptions[n].has_arg = 0;
|
andrewm@5
|
95 totalLongOptions[n].flag = NULL;
|
andrewm@5
|
96 totalLongOptions[n].val = 0;
|
andrewm@5
|
97 }
|
andrewm@5
|
98 else {
|
andrewm@5
|
99 int customIndex = 0;
|
andrewm@5
|
100 while(n < 256) {
|
andrewm@5
|
101 if(customLongOptions[customIndex].name == NULL)
|
andrewm@5
|
102 break;
|
andrewm@5
|
103 totalLongOptions[n].name = customLongOptions[customIndex].name;
|
andrewm@5
|
104 totalLongOptions[n].has_arg = customLongOptions[customIndex].has_arg;
|
andrewm@5
|
105 totalLongOptions[n].flag = customLongOptions[customIndex].flag;
|
andrewm@5
|
106 totalLongOptions[n].val = customLongOptions[customIndex].val;
|
andrewm@5
|
107 n++;
|
andrewm@5
|
108 customIndex++;
|
andrewm@5
|
109 }
|
andrewm@5
|
110
|
andrewm@5
|
111 // Terminate the array
|
andrewm@5
|
112 totalLongOptions[n].name = NULL;
|
andrewm@5
|
113 totalLongOptions[n].has_arg = 0;
|
andrewm@5
|
114 totalLongOptions[n].flag = NULL;
|
andrewm@5
|
115 totalLongOptions[n].val = 0;
|
andrewm@5
|
116 }
|
andrewm@5
|
117 }
|
andrewm@5
|
118
|
andrewm@5
|
119 while(1) {
|
andrewm@5
|
120 if ((c = getopt_long(argc, argv, totalShortOptions, totalLongOptions, NULL)) < 0)
|
andrewm@5
|
121 return c;
|
andrewm@5
|
122
|
andrewm@5
|
123 switch (c) {
|
andrewm@5
|
124 case 'p':
|
andrewm@5
|
125 settings->periodSize = atoi(optarg);
|
andrewm@5
|
126 if(settings->periodSize < 1)
|
andrewm@5
|
127 settings->periodSize = 1;
|
andrewm@5
|
128 break;
|
andrewm@5
|
129 case 'v':
|
andrewm@5
|
130 settings->verbose = 1;
|
andrewm@5
|
131 break;
|
andrewm@5
|
132 case 'm':
|
giuliomoro@19
|
133 settings->useAnalog = atoi(optarg);
|
andrewm@5
|
134 break;
|
giuliomoro@16
|
135 case 'g':
|
giuliomoro@19
|
136 settings->useDigital = atoi(optarg);
|
giuliomoro@19
|
137 settings->numDigitalChannels = 0;
|
giuliomoro@16
|
138 break;
|
andrewm@12
|
139 case 'C':
|
giuliomoro@19
|
140 settings->numAnalogChannels = atoi(optarg);
|
giuliomoro@19
|
141 if(settings->numAnalogChannels >= 8)
|
giuliomoro@19
|
142 settings->numAnalogChannels = 8;
|
giuliomoro@19
|
143 else if(settings->numAnalogChannels >= 4)
|
giuliomoro@19
|
144 settings->numAnalogChannels = 4;
|
andrewm@12
|
145 else
|
giuliomoro@19
|
146 settings->numAnalogChannels = 2;
|
andrewm@12
|
147 break;
|
giuliomoro@16
|
148 case 'G':
|
giuliomoro@19
|
149 settings->numDigitalChannels = atoi(optarg);
|
giuliomoro@19
|
150 if(settings->numDigitalChannels >= 16)
|
giuliomoro@19
|
151 settings->numDigitalChannels = 16;
|
giuliomoro@19
|
152 else if (settings->numDigitalChannels < 1){
|
giuliomoro@19
|
153 settings->numDigitalChannels = 0;
|
giuliomoro@19
|
154 settings->useDigital = 0; //TODO: this actually works only if -G 0 is specified after -g 1.
|
giuliomoro@19
|
155 //No worries, though: disabling numDigital will only prevent the pins from being exported.
|
giuliomoro@16
|
156 }
|
giuliomoro@16
|
157 break;
|
andrewm@5
|
158 case 'M':
|
andrewm@5
|
159 settings->beginMuted = atoi(optarg);
|
andrewm@5
|
160 break;
|
andrewm@5
|
161 case 'D':
|
andrewm@5
|
162 settings->dacLevel = atof(optarg);
|
andrewm@5
|
163 break;
|
andrewm@5
|
164 case 'A':
|
andrewm@5
|
165 settings->adcLevel = atof(optarg);
|
andrewm@5
|
166 break;
|
andrewm@5
|
167 case 'H':
|
andrewm@5
|
168 settings->headphoneLevel = atof(optarg);
|
andrewm@5
|
169 break;
|
giuliomoro@23
|
170 case OPT_PRU_FILE:
|
giuliomoro@16
|
171 if(strlen(optarg)<MAX_PRU_FILENAME_LENGTH)
|
giuliomoro@16
|
172 strcpy(settings->pruFilename, optarg);
|
giuliomoro@16
|
173 else
|
giuliomoro@16
|
174 std::cerr << "Warning: filename for the PRU code is too long (>" << MAX_PRU_FILENAME_LENGTH << " characters). Using embedded PRU code instead\n";
|
giuliomoro@16
|
175 break;
|
andrewm@5
|
176 case '?':
|
andrewm@5
|
177 default:
|
andrewm@5
|
178 return c;
|
andrewm@5
|
179 }
|
andrewm@5
|
180 }
|
andrewm@5
|
181 }
|
andrewm@5
|
182
|
andrewm@5
|
183 // This function prints standard usage information for default arguments
|
andrewm@5
|
184 // Call from within your own usage function
|
andrewm@5
|
185 void BeagleRT_usage()
|
andrewm@5
|
186 {
|
giuliomoro@19
|
187 std::cerr << " --period [-p] period: Set the hardware period (buffer) size in analog samples\n";
|
giuliomoro@16
|
188 std::cerr << " --dac-level [-D] dBs: Set the DAC output level (0dB max; -63.5dB min)\n";
|
giuliomoro@16
|
189 std::cerr << " --adc-level [-A] dBs: Set the ADC input level (0dB max; -12dB min)\n";
|
giuliomoro@16
|
190 std::cerr << " --hp-level [-H] dBs: Set the headphone output level (0dB max; -63.5dB min)\n";
|
giuliomoro@16
|
191 std::cerr << " --mute-speaker [-M] val: Set whether to mute the speaker initially (default: no)\n";
|
giuliomoro@19
|
192 std::cerr << " --use-analog [-m] val: Set whether to use ADC/DAC analog (default: yes)\n";
|
giuliomoro@19
|
193 std::cerr << " --use-gpio-analog [-g] val: Set whether to use GPIO analog (default: yes)\n";
|
giuliomoro@19
|
194 std::cerr << " --analog-channels [-C] val: Set the number of ADC/DAC channels (default: 8)\n";
|
giuliomoro@19
|
195 std::cerr << " --analog-gpio-channels [-G] val: Set the number of GPIO channels (default: 16)\n";
|
giuliomoro@23
|
196 std::cerr << " --pru-file val: Set an optional external file to use for the PRU binary code\n";
|
giuliomoro@16
|
197 std::cerr << " --verbose [-v]: Enable verbose logging information\n";
|
andrewm@5
|
198 }
|
giuliomoro@16
|
199
|