List the available input and output devices on the system and their properties. Supports watching for changes and specifying backend to use.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
static int usage(char *exe) {
fprintf(stderr, "Usage: %s [options]\n"
"Options:\n"
" [--watch]\n"
" [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n"
" [--short]\n", exe);
return 1;
}
fprintf(stderr,
"%s", layout->
name);
} else {
}
}
}
static bool short_output = false;
static void print_device(
struct SoundIoDevice *device,
bool is_default) {
const char *default_str = is_default ? " (default)" : "";
const char *raw_str = device->
is_raw ?
" (raw)" :
"";
fprintf(stderr,
"%s%s%s\n", device->
name, default_str, raw_str);
if (short_output)
return;
fprintf(stderr,
" id: %s\n", device->
id);
} else {
fprintf(stderr, " channel layouts:\n");
fprintf(stderr, " ");
print_channel_layout(&device->
layouts[i]);
fprintf(stderr, "\n");
}
fprintf(stderr, " current layout: ");
fprintf(stderr, "\n");
}
fprintf(stderr, " sample rates:\n");
fprintf(stderr,
" %d - %d\n", range->
min, range->
max);
}
fprintf(stderr, " formats: ");
const char *comma = (i == device->
format_count - 1) ?
"" :
", ";
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
static int list_devices(
struct SoundIo *soundio) {
fprintf(stderr, "--------Input Devices--------\n\n");
for (int i = 0; i < input_count; i += 1) {
print_device(device, default_input == i);
}
fprintf(stderr, "\n--------Output Devices--------\n\n");
for (int i = 0; i < output_count; i += 1) {
print_device(device, default_output == i);
}
fprintf(stderr, "\n%d devices found\n", input_count + output_count);
return 0;
}
static void on_devices_change(
struct SoundIo *soundio) {
fprintf(stderr, "devices changed\n");
list_devices(soundio);
}
int main(int argc, char **argv) {
char *exe = argv[0];
bool watch = false;
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (strcmp("--watch", arg) == 0) {
watch = true;
} else if (strcmp("--short", arg) == 0) {
short_output = true;
} else if (arg[0] == '-' && arg[1] == '-') {
i += 1;
if (i >= argc) {
return usage(exe);
} else if (strcmp(arg, "--backend") == 0) {
if (strcmp("-dummy", argv[i]) == 0) {
} else if (strcmp("alsa", argv[i]) == 0) {
} else if (strcmp("pulseaudio", argv[i]) == 0) {
} else if (strcmp("jack", argv[i]) == 0) {
} else if (strcmp("coreaudio", argv[i]) == 0) {
} else if (strcmp("wasapi", argv[i]) == 0) {
} else {
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
return 1;
}
} else {
return usage(exe);
}
} else {
return usage(exe);
}
}
if (!soundio) {
fprintf(stderr, "out of memory\n");
return 1;
}
if (err) {
return err;
}
if (watch) {
for (;;) {
}
} else {
int err = list_devices(soundio);
return err;
}
}