From 5358b1c15d94296cc213712b3ad0fcdc3aa65169 Mon Sep 17 00:00:00 2001 From: alichraghi Date: Sun, 26 Jun 2022 23:06:45 +0430 Subject: [PATCH] audio: soundio error handling --- audio/soundio/error.zig | 62 +++++++++++++++++++++++++++++++++++++++++ audio/soundio/main.zig | 4 +-- 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 audio/soundio/error.zig diff --git a/audio/soundio/error.zig b/audio/soundio/error.zig new file mode 100644 index 00000000..e367f957 --- /dev/null +++ b/audio/soundio/error.zig @@ -0,0 +1,62 @@ +const std = @import("std"); +const c = @import("c.zig"); + +pub const Error = error{ + OutOfMemory, + /// The backend does not appear to be active or running. + InitAudioBackend, + /// A system resource other than memory was not available. + SystemResources, + /// Attempted to open a device and failed. + OpeningDevice, + NoSuchDevice, + /// The programmer did not comply with the API. + Invalid, + /// libsoundio was compiled without support for that backend. + BackendUnavailable, + /// An open stream had an error that can only be recovered from by + /// destroying the stream and creating it again. + Streaming, + /// Attempted to use a device with parameters it cannot support. + IncompatibleDevice, + /// When JACK returns `JackNoSuchClient` + NoSuchClient, + /// Attempted to use parameters that the backend cannot support. + IncompatibleBackend, + /// Backend server shutdown or became inactive. + BackendDisconnected, + Interrupted, + /// Buffer underrun occurred. + Underflow, + /// Unable to convert to or from UTF-8 to the native string format. + EncodingString, +}; + +pub fn intToError(err: c_int) Error!void { + return switch (err) { + c.SoundIoErrorNone => {}, + c.SoundIoErrorNoMem => Error.OutOfMemory, + c.SoundIoErrorInitAudioBackend => Error.InitAudioBackend, + c.SoundIoErrorSystemResources => Error.SystemResources, + c.SoundIoErrorOpeningDevice => Error.OpeningDevice, + c.SoundIoErrorNoSuchDevice => Error.NoSuchDevice, + c.SoundIoErrorInvalid => Error.Invalid, + c.SoundIoErrorBackendUnavailable => Error.BackendUnavailable, + c.SoundIoErrorStreaming => Error.Streaming, + c.SoundIoErrorIncompatibleDevice => Error.IncompatibleDevice, + c.SoundIoErrorNoSuchClient => Error.NoSuchClient, + c.SoundIoErrorIncompatibleBackend => Error.IncompatibleBackend, + c.SoundIoErrorBackendDisconnected => Error.BackendDisconnected, + c.SoundIoErrorInterrupted => Error.Interrupted, + c.SoundIoErrorUnderflow => Error.Underflow, + c.SoundIoErrorEncodingString => Error.EncodingString, + else => unreachable, + }; +} + +test "error convertion" { + const expectError = @import("std").testing.expectError; + + try intToError(c.SoundIoErrorNone); + try expectError(Error.OutOfMemory, intToError(c.SoundIoErrorNoMem)); +} diff --git a/audio/soundio/main.zig b/audio/soundio/main.zig index d78c5866..47fe5abe 100644 --- a/audio/soundio/main.zig +++ b/audio/soundio/main.zig @@ -1,3 +1 @@ -test { - _ = @import("c.zig"); -} +pub const Error = @import("error.zig").Error;