Mercurial > hg > beaglert
comparison core/RTAudioCommandLine.cpp @ 5:09f03ac40fcc
API improvements and cleanups. Now all common audio command-line options can be parsed automatically.
author | andrewm |
---|---|
date | Sat, 08 Nov 2014 16:16:55 +0100 |
parents | |
children | a6beeba3a648 |
comparison
equal
deleted
inserted
replaced
4:f34c63568523 | 5:09f03ac40fcc |
---|---|
1 /* | |
2 * RTAudioCommandLine.cpp | |
3 * | |
4 * Created on: Nov 8, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 #include <iostream> | |
9 #include <cstdlib> | |
10 #include <cstring> | |
11 #include <getopt.h> | |
12 | |
13 #include "../include/RTAudio.h" | |
14 | |
15 // Default command-line options for RTAudio | |
16 struct option gDefaultLongOptions[] = | |
17 { | |
18 {"period", 1, NULL, 'p'}, | |
19 {"verbose", 0, NULL, 'v'}, | |
20 {"use-matrix", 1, NULL, 'm'}, | |
21 {"mute-speaker", 1, NULL, 'M'}, | |
22 {"dac-level", 1, NULL, 'D'}, | |
23 {"adc-level", 1, NULL, 'A'}, | |
24 {"hp-level", 1, NULL, 'H'}, | |
25 {NULL, 0, NULL, 0} | |
26 }; | |
27 | |
28 const char gDefaultShortOptions[] = "p:vm:M:D:A:H:"; | |
29 | |
30 // This function sets the default settings for the RTAudioSettings structure | |
31 void BeagleRT_defaultSettings(RTAudioSettings *settings) | |
32 { | |
33 // Set default values for settings | |
34 settings->periodSize = 8; | |
35 settings->beginMuted = 0; | |
36 settings->dacLevel = DEFAULT_DAC_LEVEL; | |
37 settings->adcLevel = DEFAULT_ADC_LEVEL; | |
38 settings->headphoneLevel = DEFAULT_HP_LEVEL; | |
39 settings->useMatrix = 1; | |
40 settings->verbose = 0; | |
41 settings->codecI2CAddress = CODEC_I2C_ADDRESS; | |
42 settings->ampMutePin = kAmplifierMutePin; | |
43 } | |
44 | |
45 // This function drops in place of getopt() in the main() function | |
46 // and handles the initialisation of the RTAudio settings using | |
47 // standard command-line arguments. System default arguments will | |
48 // be stored in settings, otherwise arguments will be returned | |
49 // as getopt() normally does. | |
50 | |
51 int BeagleRT_getopt_long(int argc, char *argv[], const char *customShortOptions, const struct option *customLongOptions, RTAudioSettings *settings) | |
52 { | |
53 static int firstRun = 1; | |
54 static char totalShortOptions[256]; | |
55 static struct option totalLongOptions[256]; | |
56 | |
57 int c; | |
58 | |
59 // Prep total option string the first time this is | |
60 // run. As a getopt() substitute, it will be called repeatedly working its | |
61 // way through argc and argv. | |
62 if(firstRun) { | |
63 firstRun = 0; | |
64 | |
65 // Copy short options into one string | |
66 strcpy(totalShortOptions, gDefaultShortOptions); | |
67 strncat(totalShortOptions, customShortOptions, 256 - strlen(gDefaultShortOptions) - 1); | |
68 | |
69 // Copy long options into one array | |
70 int n = 0; | |
71 while(1) { | |
72 if(gDefaultLongOptions[n].name == NULL) | |
73 break; | |
74 totalLongOptions[n].name = gDefaultLongOptions[n].name; | |
75 totalLongOptions[n].has_arg = gDefaultLongOptions[n].has_arg; | |
76 totalLongOptions[n].flag = gDefaultLongOptions[n].flag; | |
77 totalLongOptions[n].val = gDefaultLongOptions[n].val; | |
78 n++; | |
79 } | |
80 | |
81 // Copy custom options into the array, if present | |
82 if(customLongOptions == 0) { | |
83 // Terminate the array | |
84 totalLongOptions[n].name = NULL; | |
85 totalLongOptions[n].has_arg = 0; | |
86 totalLongOptions[n].flag = NULL; | |
87 totalLongOptions[n].val = 0; | |
88 } | |
89 else { | |
90 int customIndex = 0; | |
91 while(n < 256) { | |
92 if(customLongOptions[customIndex].name == NULL) | |
93 break; | |
94 totalLongOptions[n].name = customLongOptions[customIndex].name; | |
95 totalLongOptions[n].has_arg = customLongOptions[customIndex].has_arg; | |
96 totalLongOptions[n].flag = customLongOptions[customIndex].flag; | |
97 totalLongOptions[n].val = customLongOptions[customIndex].val; | |
98 n++; | |
99 customIndex++; | |
100 } | |
101 | |
102 // Terminate the array | |
103 totalLongOptions[n].name = NULL; | |
104 totalLongOptions[n].has_arg = 0; | |
105 totalLongOptions[n].flag = NULL; | |
106 totalLongOptions[n].val = 0; | |
107 } | |
108 } | |
109 | |
110 while(1) { | |
111 if ((c = getopt_long(argc, argv, totalShortOptions, totalLongOptions, NULL)) < 0) | |
112 return c; | |
113 | |
114 switch (c) { | |
115 case 'p': | |
116 settings->periodSize = atoi(optarg); | |
117 if(settings->periodSize < 1) | |
118 settings->periodSize = 1; | |
119 break; | |
120 case 'v': | |
121 settings->verbose = 1; | |
122 break; | |
123 case 'm': | |
124 settings->useMatrix = atoi(optarg); | |
125 break; | |
126 case 'M': | |
127 settings->beginMuted = atoi(optarg); | |
128 break; | |
129 case 'D': | |
130 settings->dacLevel = atof(optarg); | |
131 break; | |
132 case 'A': | |
133 settings->adcLevel = atof(optarg); | |
134 break; | |
135 case 'H': | |
136 settings->headphoneLevel = atof(optarg); | |
137 break; | |
138 case '?': | |
139 default: | |
140 return c; | |
141 } | |
142 } | |
143 } | |
144 | |
145 // This function prints standard usage information for default arguments | |
146 // Call from within your own usage function | |
147 void BeagleRT_usage() | |
148 { | |
149 std::cerr << " --period [-p] period: Set the hardware period (buffer) size in matrix samples\n"; | |
150 std::cerr << " --dac-level [-D] dBs: Set the DAC output level (0dB max; -63.5dB min)\n"; | |
151 std::cerr << " --adc-level [-A] dBs: Set the ADC input level (0dB max; -12dB min)\n"; | |
152 std::cerr << " --hp-level [-H] dBs: Set the headphone output level (0dB max; -63.5dB min)\n"; | |
153 std::cerr << " --mute-speaker [-M] val: Set whether to mute the speaker initially (default: no)\n"; | |
154 std::cerr << " --use-matrix [-m] val: Set whether to use ADC/DAC matrix\n"; | |
155 std::cerr << " --verbose [-v]: Enable verbose logging information\n"; | |
156 } |