mirror of
https://github.com/Ryujinx/libsoundio.git
synced 2024-12-22 21:55:36 +00:00
sine example: add parameter to specify device
This commit is contained in:
parent
bc7b22f61c
commit
2eb8ce24cf
|
@ -69,9 +69,6 @@ backend:
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
__attribute__ ((cold))
|
|
||||||
__attribute__ ((noreturn))
|
|
||||||
__attribute__ ((format (printf, 1, 2)))
|
|
||||||
static void panic(const char *format, ...) {
|
static void panic(const char *format, ...) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
|
|
|
@ -23,8 +23,9 @@ static void panic(const char *format, ...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int usage(char *exe) {
|
static int usage(char *exe) {
|
||||||
fprintf(stderr, "Usage: %s [--dummy] [--alsa] [--pulseaudio] [--jack]\n", exe);
|
fprintf(stderr, "Usage: %s [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi] [--device id] [--raw]\n",
|
||||||
return 1;
|
exe);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const float PI = 3.1415926535f;
|
static const float PI = 3.1415926535f;
|
||||||
|
@ -78,16 +79,40 @@ static void underflow_callback(struct SoundIoOutStream *outstream) {
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
char *exe = argv[0];
|
char *exe = argv[0];
|
||||||
enum SoundIoBackend backend = SoundIoBackendNone;
|
enum SoundIoBackend backend = SoundIoBackendNone;
|
||||||
|
char *device_id = NULL;
|
||||||
|
bool raw = false;
|
||||||
for (int i = 1; i < argc; i += 1) {
|
for (int i = 1; i < argc; i += 1) {
|
||||||
char *arg = argv[i];
|
char *arg = argv[i];
|
||||||
if (strcmp("--dummy", arg) == 0) {
|
if (arg[0] == '-' && arg[1] == '-') {
|
||||||
backend = SoundIoBackendDummy;
|
if (strcmp(arg, "--raw") == 0) {
|
||||||
} else if (strcmp("--alsa", arg) == 0) {
|
raw = true;
|
||||||
backend = SoundIoBackendAlsa;
|
} else {
|
||||||
} else if (strcmp("--pulseaudio", arg) == 0) {
|
i += 1;
|
||||||
backend = SoundIoBackendPulseAudio;
|
if (i >= argc) {
|
||||||
} else if (strcmp("--jack", arg) == 0) {
|
return usage(exe);
|
||||||
backend = SoundIoBackendJack;
|
} else if (strcmp(arg, "--backend") == 0) {
|
||||||
|
if (strcmp(argv[i], "dummy") == 0) {
|
||||||
|
backend = SoundIoBackendDummy;
|
||||||
|
} else if (strcmp(argv[i], "alsa") == 0) {
|
||||||
|
backend = SoundIoBackendAlsa;
|
||||||
|
} else if (strcmp(argv[i], "pulseaudio") == 0) {
|
||||||
|
backend = SoundIoBackendPulseAudio;
|
||||||
|
} else if (strcmp(argv[i], "jack") == 0) {
|
||||||
|
backend = SoundIoBackendJack;
|
||||||
|
} else if (strcmp(argv[i], "coreaudio") == 0) {
|
||||||
|
backend = SoundIoBackendCoreAudio;
|
||||||
|
} else if (strcmp(argv[i], "wasapi") == 0) {
|
||||||
|
backend = SoundIoBackendWasapi;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
} else if (strcmp(arg, "--device") == 0) {
|
||||||
|
device_id = argv[i];
|
||||||
|
} else {
|
||||||
|
return usage(exe);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return usage(exe);
|
return usage(exe);
|
||||||
}
|
}
|
||||||
|
@ -100,16 +125,33 @@ int main(int argc, char **argv) {
|
||||||
int err = (backend == SoundIoBackendNone) ?
|
int err = (backend == SoundIoBackendNone) ?
|
||||||
soundio_connect(soundio) : soundio_connect_backend(soundio, backend);
|
soundio_connect(soundio) : soundio_connect_backend(soundio, backend);
|
||||||
|
|
||||||
if (err)
|
if (err) {
|
||||||
panic("error connecting: %s", soundio_strerror(err));
|
fprintf(stderr, "Unable to connect to backend: %s\n", soundio_strerror(err));
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
soundio_flush_events(soundio);
|
soundio_flush_events(soundio);
|
||||||
|
|
||||||
int default_out_device_index = soundio_default_output_device_index(soundio);
|
int selected_device_index = -1;
|
||||||
if (default_out_device_index < 0)
|
if (device_id) {
|
||||||
panic("no output device found");
|
int device_count = soundio_output_device_count(soundio);
|
||||||
|
for (int i = 0; i < device_count; i += 1) {
|
||||||
|
struct SoundIoDevice *device = soundio_get_output_device(soundio, i);
|
||||||
|
if (strcmp(device->id, device_id) == 0 && device->is_raw == raw) {
|
||||||
|
selected_device_index = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
selected_device_index = soundio_default_output_device_index(soundio);
|
||||||
|
}
|
||||||
|
|
||||||
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
|
if (selected_device_index < 0) {
|
||||||
|
fprintf(stderr, "Output device not found\n");
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SoundIoDevice *device = soundio_get_output_device(soundio, selected_device_index);
|
||||||
if (!device)
|
if (!device)
|
||||||
panic("out of memory");
|
panic("out of memory");
|
||||||
|
|
||||||
|
@ -120,8 +162,10 @@ int main(int argc, char **argv) {
|
||||||
outstream->write_callback = write_callback;
|
outstream->write_callback = write_callback;
|
||||||
outstream->underflow_callback = underflow_callback;
|
outstream->underflow_callback = underflow_callback;
|
||||||
|
|
||||||
if ((err = soundio_outstream_open(outstream)))
|
if ((err = soundio_outstream_open(outstream))) {
|
||||||
panic("unable to open device: %s", soundio_strerror(err));
|
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (outstream->layout_error)
|
if (outstream->layout_error)
|
||||||
fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));
|
fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));
|
||||||
|
@ -135,5 +179,5 @@ int main(int argc, char **argv) {
|
||||||
soundio_outstream_destroy(outstream);
|
soundio_outstream_destroy(outstream);
|
||||||
soundio_device_unref(device);
|
soundio_device_unref(device);
|
||||||
soundio_destroy(soundio);
|
soundio_destroy(soundio);
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue