forked from agiz/switchaudio-osx
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterface.c
276 lines (240 loc) · 9.35 KB
/
interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* interface.c
* AudioSwitch
*
Copyright (c) 2008 Devon Weller <[email protected]>
Copyright (c) 2015 Ziga Zupanec <[email protected]>
Copyright (c) 2016 Mahmood S. Zargar <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "device.h"
#include "interface.h"
void printProperties(AudioDeviceID deviceID, ASDeviceType typeRequested, ASOutputFormat outputFormat) {
char deviceName[DEVICE_NAME_LEN];
float vol_left, vol_right;
ASDeviceType device_type;
UInt32 transportType;
switch (typeRequested) {
case kAudioTypeInput:
if (!isAnInputDevice(deviceID)) return;
device_type = kAudioTypeInput;
break;
case kAudioTypeOutput:
if (!isAnOutputDevice(deviceID)) return;
device_type = kAudioTypeOutput;
break;
case kAudioTypeSystemOutput:
device_type = getDeviceType(deviceID);
if (device_type != kAudioTypeOutput) return;
break;
default:
break;
}
getDeviceName(deviceID, deviceName);
getDeviceVolume(deviceID, &vol_left, &vol_right);
getDeviceTransportType(deviceID, &transportType);
switch (outputFormat) {
case kOutputFormatName:
printf("%s\n", deviceName);
break;
default:
printf("[%3u] - %6s %-26s", (unsigned int) deviceID, deviceTypeName(device_type), deviceName);
if (vol_left < -0.1 || vol_right < -0.1) {
printf("\n");
}
else {
printf(" :: [%.3f:%.3f]\n", vol_left, vol_right);
}
if (transportType == kAudioDeviceTransportTypeAggregate) {
AudioObjectID sub_device[32];
UInt32 outSize = sizeof(sub_device);
getAggregateDeviceSubDeviceList(deviceID, sub_device, &outSize);
for (int j = 0; j < outSize / sizeof(AudioObjectID); j++) {
printf("\t");
printProperties(sub_device[j], device_type, outputFormat);
}
}
}
}
void showUsage(const char *appName) {
printf("Usage: %s [-a] [-c] [-t type] [-n] -s device_name\n"
" -a : shows all devices\n"
" -c : shows current device\n"
" -t type : device type (input/output/system). Defaults to all.\n"
" -f format : output format (name/full). Defaults to full.\n"
" -n : cycles the audio device to the next one\n"
" -s device_name : sets the audio device to the given device by name\n"
" -e device_id1=vol1,vol2:device_id2=vol1,vol2 : sets audio device volume for 1st and 2nd channel. Multiple device can be separated with ':'.\n\n",
appName);
}
int runAudioSwitch(int argc, const char *argv[]) {
char requestedDeviceName[DEVICE_NAME_LEN];
char volume_arg[VOL_ARG_LEN];
AudioDeviceID chosenDeviceID = kAudioDeviceUnknown;
ASDeviceType typeRequested = kAudioTypeUnknown;
ASOutputFormat outputFormat = kOutputFormatDefault;
int function = 0;
int c;
while ((c = getopt(argc, (char **) argv, "hacnf:t:s:e:")) != -1) {
switch (c) {
case 'a':
// show all
function = kFunctionShowAll;
break;
case 'c':
// get current device
function = kFunctionShowCurrent;
break;
case 'e':
// set device volume
function = kFunctionSetVolume;
strncpy(volume_arg, optarg, VOL_ARG_LEN);
break;
case 'h':
// show help
function = kFunctionShowHelp;
break;
case 'n':
// cycle to the next audio device
function = kFunctionCycleNext;
break;
case 's':
// set the requestedDeviceName
function = kFunctionSetDevice;
strncpy(requestedDeviceName, optarg, DEVICE_NAME_LEN);
break;
case 'f':
// set the output format
if (strcmp(optarg, "full") == 0) {
outputFormat = kOutputFormatDefault;
}
else if (strcmp(optarg, "name") == 0) {
outputFormat = kOutputFormatName;
}
else {
printf("Invalid output format \"%s\" specified.\n", optarg);
showUsage(argv[0]);
return 1;
}
break;
case 't':
// set the requestedDeviceName
if (strcmp(optarg, "input") == 0) {
typeRequested = kAudioTypeInput;
}
else if (strcmp(optarg, "output") == 0) {
typeRequested = kAudioTypeOutput;
}
else if (strcmp(optarg, "system") == 0) {
typeRequested = kAudioTypeSystemOutput;
}
else {
printf("Invalid device type \"%s\" specified.\n", optarg);
showUsage(argv[0]);
return 1;
}
break;
}
}
if (function == kFunctionShowAll) {
switch (typeRequested) {
case kAudioTypeInput:
case kAudioTypeOutput:
showAllDevices(typeRequested, outputFormat);
break;
case kAudioTypeSystemOutput:
showAllDevices(kAudioTypeOutput, outputFormat);
break;
default:
showAllDevices(kAudioTypeInput, outputFormat);
showAllDevices(kAudioTypeOutput, outputFormat);
}
return 0;
}
if (function == kFunctionShowHelp) {
showUsage(argv[0]);
return 0;
}
if (function == kFunctionShowCurrent) {
if (typeRequested == kAudioTypeUnknown) typeRequested = kAudioTypeOutput;
showCurrentlySelectedDeviceID(typeRequested, outputFormat);
return 0;
}
if (function == kFunctionSetVolume) {
double vol1, vol2;
int dev_id;
int i = 0;
char *pch = strtok(volume_arg, VOL_DELIMITERS);
while (pch != NULL) {
if (i % 3 == 0) {
dev_id = atoi(pch);
}
else if (i % 3 == 2) {
i = -1;
vol2 = atof(pch);
setDeviceVolume(dev_id, vol1, vol2);
}
else {
vol1 = atof(pch);
}
pch = strtok(NULL, VOL_DELIMITERS);
i++;
}
return 0;
}
if (typeRequested == kAudioTypeUnknown) typeRequested = kAudioTypeOutput;
if (function == kFunctionCycleNext) {
// get current device of requested type
chosenDeviceID = getCurrentlySelectedDeviceID(typeRequested);
if (chosenDeviceID == kAudioDeviceUnknown) {
printf("Could not find current audio device of type %s. Nothing was changed.\n",
deviceTypeName(typeRequested));
return 1;
}
// find next device to current device
chosenDeviceID = getNextDeviceID(chosenDeviceID, typeRequested);
if (chosenDeviceID == kAudioDeviceUnknown) {
printf("Could not find next audio device of type %s. Nothing was changed.\n",
deviceTypeName(typeRequested));
return 1;
}
// choose the requested audio device
setDevice(chosenDeviceID, typeRequested);
getDeviceName(chosenDeviceID, requestedDeviceName);
printf("%s audio device set to \"%s\"\n", deviceTypeName(typeRequested), requestedDeviceName);
return 0;
}
if (function != kFunctionSetDevice) {
printf("Please specify audio device.\n");
showUsage(argv[0]);
return 1;
}
// find the id of the requested device
chosenDeviceID = getRequestedDeviceID(requestedDeviceName, typeRequested);
if (chosenDeviceID == kAudioDeviceUnknown) {
printf("Could not find an audio device named \"%s\" of type %s. Nothing was changed.\n", requestedDeviceName,
deviceTypeName(typeRequested));
return 1;
}
// choose the requested audio device
setDevice(chosenDeviceID, typeRequested);
printf("%s audio device set to \"%s\"\n", deviceTypeName(typeRequested), requestedDeviceName);
return 0;
}