mirror of
https://github.com/Ryujinx/libsoundio.git
synced 2024-12-23 02:45:29 +00:00
sine example: remove panics
This commit is contained in:
parent
65416669d3
commit
8c9c4f85c2
63
README.md
63
README.md
|
@ -64,20 +64,10 @@ backend:
|
||||||
#include <soundio/soundio.h>
|
#include <soundio/soundio.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
static void panic(const char *format, ...) {
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, format);
|
|
||||||
vfprintf(stderr, format, ap);
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
va_end(ap);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
static const float PI = 3.1415926535f;
|
static const float PI = 3.1415926535f;
|
||||||
static float seconds_offset = 0.0f;
|
static float seconds_offset = 0.0f;
|
||||||
static void write_callback(struct SoundIoOutStream *outstream,
|
static void write_callback(struct SoundIoOutStream *outstream,
|
||||||
|
@ -93,8 +83,10 @@ static void write_callback(struct SoundIoOutStream *outstream,
|
||||||
while (frames_left > 0) {
|
while (frames_left > 0) {
|
||||||
int frame_count = frames_left;
|
int frame_count = frames_left;
|
||||||
|
|
||||||
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count)))
|
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
|
||||||
panic("%s", soundio_strerror(err));
|
fprintf(stderr, "%s\n", soundio_strerror(err));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if (!frame_count)
|
if (!frame_count)
|
||||||
break;
|
break;
|
||||||
|
@ -110,8 +102,10 @@ static void write_callback(struct SoundIoOutStream *outstream,
|
||||||
}
|
}
|
||||||
seconds_offset += seconds_per_frame * frame_count;
|
seconds_offset += seconds_per_frame * frame_count;
|
||||||
|
|
||||||
if ((err = soundio_outstream_end_write(outstream)))
|
if ((err = soundio_outstream_end_write(outstream))) {
|
||||||
panic("%s", soundio_strerror(err));
|
fprintf(stderr, "%s\n", soundio_strerror(err));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
frames_left -= frame_count;
|
frames_left -= frame_count;
|
||||||
}
|
}
|
||||||
|
@ -120,21 +114,29 @@ static void write_callback(struct SoundIoOutStream *outstream,
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int err;
|
int err;
|
||||||
struct SoundIo *soundio = soundio_create();
|
struct SoundIo *soundio = soundio_create();
|
||||||
if (!soundio)
|
if (!soundio) {
|
||||||
panic("out of memory");
|
fprintf(stderr, "out of memory\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if ((err = soundio_connect(soundio)))
|
if ((err = soundio_connect(soundio))) {
|
||||||
panic("error connecting: %s", soundio_strerror(err));
|
fprintf(stderr, "error connecting: %s", soundio_strerror(err));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
soundio_flush_events(soundio);
|
soundio_flush_events(soundio);
|
||||||
|
|
||||||
int default_out_device_index = soundio_default_output_device_index(soundio);
|
int default_out_device_index = soundio_default_output_device_index(soundio);
|
||||||
if (default_out_device_index < 0)
|
if (default_out_device_index < 0) {
|
||||||
panic("no output device found");
|
fprintf(stderr, "no output device found");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
|
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
|
||||||
if (!device)
|
if (!device) {
|
||||||
panic("out of memory");
|
fprintf(stderr, "out of memory");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Output device: %s\n", device->name);
|
fprintf(stderr, "Output device: %s\n", device->name);
|
||||||
|
|
||||||
|
@ -142,14 +144,18 @@ int main(int argc, char **argv) {
|
||||||
outstream->format = SoundIoFormatFloat32NE;
|
outstream->format = SoundIoFormatFloat32NE;
|
||||||
outstream->write_callback = write_callback;
|
outstream->write_callback = write_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 1;
|
||||||
|
}
|
||||||
|
|
||||||
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));
|
||||||
|
|
||||||
if ((err = soundio_outstream_start(outstream)))
|
if ((err = soundio_outstream_start(outstream))) {
|
||||||
panic("unable to start device: %s", soundio_strerror(err));
|
fprintf(stderr, "unable to start device: %s", soundio_strerror(err));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
soundio_wait_events(soundio);
|
soundio_wait_events(soundio);
|
||||||
|
@ -262,11 +268,6 @@ For each backend, do the following:
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
0. implement WASAPI (Windows) backend, get examples working
|
|
||||||
- move the bulk of the `outstream_open_wasapi` code to the thread and
|
|
||||||
have them communicate back and forth. because the thread has to do
|
|
||||||
weird thread-local com stuff, and all that com stuff really needs to be
|
|
||||||
called from the same thread.
|
|
||||||
0. Make sure PulseAudio can handle refresh devices crashing before
|
0. Make sure PulseAudio can handle refresh devices crashing before
|
||||||
block_until_have_devices
|
block_until_have_devices
|
||||||
0. Integrate into libgroove and test with Groove Basin
|
0. Integrate into libgroove and test with Groove Basin
|
||||||
|
|
|
@ -8,21 +8,11 @@
|
||||||
#include <soundio/soundio.h>
|
#include <soundio/soundio.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
static void panic(const char *format, ...) {
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, format);
|
|
||||||
vfprintf(stderr, format, ap);
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
va_end(ap);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
static int usage(char *exe) {
|
static int usage(char *exe) {
|
||||||
fprintf(stderr, "Usage: %s [options]\n"
|
fprintf(stderr, "Usage: %s [options]\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
|
@ -31,7 +21,7 @@ static int usage(char *exe) {
|
||||||
" [--raw]\n"
|
" [--raw]\n"
|
||||||
" [--name stream_name]\n"
|
" [--name stream_name]\n"
|
||||||
, exe);
|
, exe);
|
||||||
return EXIT_FAILURE;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_sample_s16ne(char *ptr, double sample) {
|
static void write_sample_s16ne(char *ptr, double sample) {
|
||||||
|
@ -71,8 +61,10 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int frame_count = frames_left;
|
int frame_count = frames_left;
|
||||||
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count)))
|
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
|
||||||
panic("%s", soundio_strerror(err));
|
fprintf(stderr, "%s\n", soundio_strerror(err));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if (!frame_count)
|
if (!frame_count)
|
||||||
break;
|
break;
|
||||||
|
@ -93,7 +85,8 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m
|
||||||
if ((err = soundio_outstream_end_write(outstream))) {
|
if ((err = soundio_outstream_end_write(outstream))) {
|
||||||
if (err == SoundIoErrorUnderflow)
|
if (err == SoundIoErrorUnderflow)
|
||||||
return;
|
return;
|
||||||
panic("%s", soundio_strerror(err));
|
fprintf(stderr, "%s\n", soundio_strerror(err));
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
frames_left -= frame_count;
|
frames_left -= frame_count;
|
||||||
|
@ -137,7 +130,7 @@ int main(int argc, char **argv) {
|
||||||
backend = SoundIoBackendWasapi;
|
backend = SoundIoBackendWasapi;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
|
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
|
||||||
return EXIT_FAILURE;
|
return 1;
|
||||||
}
|
}
|
||||||
} else if (strcmp(arg, "--device") == 0) {
|
} else if (strcmp(arg, "--device") == 0) {
|
||||||
device_id = argv[i];
|
device_id = argv[i];
|
||||||
|
@ -153,15 +146,17 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SoundIo *soundio = soundio_create();
|
struct SoundIo *soundio = soundio_create();
|
||||||
if (!soundio)
|
if (!soundio) {
|
||||||
panic("out of memory");
|
fprintf(stderr, "out of memory\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
fprintf(stderr, "Unable to connect to backend: %s\n", soundio_strerror(err));
|
fprintf(stderr, "Unable to connect to backend: %s\n", soundio_strerror(err));
|
||||||
return EXIT_FAILURE;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
soundio_flush_events(soundio);
|
soundio_flush_events(soundio);
|
||||||
|
@ -182,12 +177,14 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (selected_device_index < 0) {
|
if (selected_device_index < 0) {
|
||||||
fprintf(stderr, "Output device not found\n");
|
fprintf(stderr, "Output device not found\n");
|
||||||
return EXIT_SUCCESS;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SoundIoDevice *device = soundio_get_output_device(soundio, selected_device_index);
|
struct SoundIoDevice *device = soundio_get_output_device(soundio, selected_device_index);
|
||||||
if (!device)
|
if (!device) {
|
||||||
panic("out of memory");
|
fprintf(stderr, "out of memory\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Output device: %s\n", device->name);
|
fprintf(stderr, "Output device: %s\n", device->name);
|
||||||
|
|
||||||
|
@ -210,20 +207,22 @@ int main(int argc, char **argv) {
|
||||||
write_sample = write_sample_s16ne;
|
write_sample = write_sample_s16ne;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "No suitable device format available.\n");
|
fprintf(stderr, "No suitable device format available.\n");
|
||||||
return EXIT_FAILURE;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((err = soundio_outstream_open(outstream))) {
|
if ((err = soundio_outstream_open(outstream))) {
|
||||||
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
|
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
|
||||||
return EXIT_FAILURE;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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));
|
||||||
|
|
||||||
if ((err = soundio_outstream_start(outstream)))
|
if ((err = soundio_outstream_start(outstream))) {
|
||||||
panic("unable to start device: %s", soundio_strerror(err));
|
fprintf(stderr, "unable to start device: %s\n", soundio_strerror(err));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
soundio_wait_events(soundio);
|
soundio_wait_events(soundio);
|
||||||
|
@ -231,5 +230,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 EXIT_SUCCESS;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue