Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cleanup code techniques
int main(int argc, char **argv) {
struct SoundIo *soundio = soundio_create();
if (!soundio) {
fprintf(stderr, "out of memory\n");
return 1;
}
int err;
if ((err = soundio_connect(soundio))) {
fprintf(stderr, "unable to connect: %s\n", soundio_strerror(err));
soundio_destroy(soundio);
return 1;
}
soundio_flush_events(soundio);
int default_output_index = soundio_default_output_device_index(soundio);
if (default_output_index < 0) {
fprintf(stderr, "No output device\n");
soundio_destroy(soundio);
return 1;
}
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_output_index);
if (!device) {
fprintf(stderr, "out of memory\n");
soundio_destroy(soundio);
return 1;
}
struct SoundIoOutStream *outstream = soundio_outstream_create(device);
if (!outstream) {
fprintf(stderr, "out of memory\n");
soundio_device_unref(device);
soundio_destroy(soundio);
return 1;
}
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
if ((err = soundio_outstream_open(outstream))) {
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
soundio_outstream_destroy(outstream);
soundio_device_unref(device);
soundio_destroy(soundio);
return 1;
}
if ((err = soundio_outstream_start(outstream))) {
fprintf(stderr, "unable to start device: %s\n", soundio_strerror(err));
soundio_outstream_destroy(outstream);
soundio_device_unref(device);
soundio_destroy(soundio);
return 1;
}
for (;;) soundio_wait_events(soundio);
}
int main(int argc, char **argv) {
struct SoundIo *soundio = NULL;
struct SoundIoOutStream *outstream = NULL;
struct SoundIoDevice *device = NULL;
int err;
soundio = soundio_create();
if (!soundio) {
err = SoundIoErrorNoMem;
goto cleanup;
}
if ((err = soundio_connect(soundio))) {
goto cleanup;
}
soundio_flush_events(soundio);
int default_output_index = soundio_default_output_device_index(soundio);
if (default_output_index < 0) {
err = SoundIoErrorNoSuchDevice;
goto cleanup;
}
device = soundio_get_output_device(soundio, default_output_index);
if (!device) {
goto cleanup;
}
outstream = soundio_outstream_create(device);
if (!outstream) {
err = SoundIoErrorNoMem;
goto cleanup;
}
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
if ((err = soundio_outstream_open(outstream))) {
goto cleanup;
}
if ((err = soundio_outstream_start(outstream))) {
goto cleanup;
}
for (;;) soundio_wait_events(soundio);
cleanup:
fprintf(stderr, "error: %s\n", soundio_strerror(err));
if (outstream) soundio_outstream_destroy(outstream);
if (device) soundio_device_unref(device);
if (soundio) soundio_destroy(soundio);
return 1;
}
pub fn main() !void {
const soundio = c.soundio_create() orelse return error.OutOfMemory;
defer c.soundio_destroy(soundio);
try sio_err(c.soundio_connect(soundio));
c.soundio_flush_events(soundio);
const default_output_index = c.soundio_default_output_device_index(soundio);
if (default_output_index < 0) return error.NoOutputDevice;
const device = c.soundio_get_output_device(soundio, default_output_index) orelse return error.OutOfMemory;
defer c.soundio_device_unref(device);
const outstream = c.soundio_outstream_create(device) orelse return error.OutOfMemory;
defer c.soundio_outstream_destroy(outstream);
outstream.format = c.SoundIoFormatFloat32NE;
outstream.write_callback = write_callback;
try sio_err(c.soundio_outstream_open(outstream));
try sio_err(c.soundio_outstream_start(outstream));
while (true) c.soundio_wait_events(soundio);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.