forked from stsaz/phiola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.h
140 lines (116 loc) · 2.65 KB
/
device.h
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
/** phiola: executor: 'device' command
2023, Simon Zolin */
static int dev_help()
{
static const char s[] = "\
List audio devices:\n\
phiola device list [OPTIONS]\n\
\n\
Options:\n\
-audio STRING Audio library name (e.g. alsa)\n\
-capture Show capture devices only\n\
-playback Show playback devices only\n\
";
ffstdout_write(s, FFS_LEN(s));
x->exit_code = 0;
return 1;
}
struct cmd_dev_list {
const char *audio;
ffbyte capture;
ffbyte playback;
};
static int dev_list_once(ffvec *buf, const phi_adev_if *adev, uint flags)
{
struct phi_adev_ent *ents;
uint ndev = adev->list(&ents, flags);
if (ndev == ~0U)
return -1;
const char *title = (flags == PHI_ADEV_PLAYBACK) ? "Playback/Loopback" : "Capture";
ffvec_addfmt(buf, "%s:\n", title);
for (uint i = 0; i != ndev; i++) {
ffstr def = {};
if (ents[i].default_device)
ffstr_setz(&def, " - Default");
ffvec_addfmt(buf, " %u: %s%S\n", i + 1, ents[i].name, &def);
}
adev->list_free(ents);
return 0;
}
static const void* dev_find_mod()
{
static const char mods[][20] = {
#if defined FF_WIN
"wasapi.dev",
"direct-sound.dev",
#elif defined FF_BSD
"oss.dev",
#elif defined FF_APPLE
"coreaudio.dev",
#elif defined FF_ANDROID
"aaudio.dev",
#else
"pulse.dev",
"alsa.dev",
#endif
};
for (uint i = 0; i < FF_COUNT(mods); i++) {
const void *f;
if (NULL != (f = x->core->mod(mods[i])))
return f;
}
return NULL;
}
static int dev_list_action(struct cmd_dev_list *l)
{
char sbuf[1000];
ffvec buf = {};
const phi_adev_if *adev;
if (l->audio) {
ffsz_format(sbuf, sizeof(sbuf), "%s.dev", l->audio);
adev = x->core->mod(sbuf);
} else {
adev = dev_find_mod();
}
if (!adev) return -1;
uint f = 3;
if (l->playback)
f = 1;
else if (l->capture)
f = 2;
if (f & 1)
dev_list_once(&buf, adev, PHI_ADEV_PLAYBACK);
if (f & 2)
dev_list_once(&buf, adev, PHI_ADEV_CAPTURE);
ffstdout_write(buf.ptr, buf.len);
ffvec_free(&buf);
x->core->sig(PHI_CORE_STOP);
x->exit_code = 0;
return 0;
}
static int dev_list_prepare()
{
return 0;
}
#define O(m) (void*)FF_OFF(struct cmd_dev_list, m)
static const struct ffarg cmd_dev_list[] = {
{ "-audio", 's', O(audio) },
{ "-capture", '1', O(capture) },
{ "-help", '1', dev_help },
{ "-playback", '1', O(playback) },
{ "", 0, dev_list_prepare },
};
#undef O
static void cmd_dev_list_free(struct cmd_dev_list *l)
{
ffmem_free(l);
}
static struct ffarg_ctx cmd_dev_list_init()
{
return SUBCMD_INIT(ffmem_new(struct cmd_dev_list), cmd_dev_list_free, dev_list_action, cmd_dev_list);
}
static const struct ffarg cmd_dev[] = {
{ "-help", '1', dev_help },
{ "list", '{', cmd_dev_list_init },
{ "", 0, dev_help },
};