cleanup code techniques (original) (raw)
Star (12) You must be signed in to star a gist
Fork (0) You must be signed in to fork a gist
Clone this repository at <script src="https://gist.github.com/andrewrk/d285c8f912169329e5e28c3d0a63c1d8.js"></script>
Save andrewrk/d285c8f912169329e5e28c3d0a63c1d8 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/andrewrk/d285c8f912169329e5e28c3d0a63c1d8.js"></script>
Save andrewrk/d285c8f912169329e5e28c3d0a63c1d8 to your computer and use it in GitHub Desktop.
cleanup code techniques
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
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); |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
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; |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
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); |
} |