summary refs log tree commit diff
path: root/src/al.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/al.zig')
-rw-r--r--src/al.zig57
1 files changed, 54 insertions, 3 deletions
diff --git a/src/al.zig b/src/al.zig
index e806e90..bfb93b6 100644
--- a/src/al.zig
+++ b/src/al.zig
@@ -1,4 +1,4 @@
-// Audio Library wrapper
+// OpenAL wrapper
 // Copyright (C) 2021  Nguyễn Gia Phong
 //
 // This file is part of zeal.
@@ -20,11 +20,26 @@ const Child = @import("std").meta.Child;
 
 usingnamespace @cImport(@cInclude("AL/al.h"));
 
+pub const Error = error {
+    /// Bad name (ID) passed to an OpenAL function.
+    InvalidName,
+    /// Invalid enum parameter passed to an OpenAL function.
+    InvalidEnum,
+    /// Invalid value parameter passed to an OpenAL function.
+    InvalidValue,
+    /// Requested operation invalid.
+    InvalidOperation,
+    /// Requested operation resulted in OpenAL running out of memory.
+    OutOfMemory,
+};
+
 pub const POSITION = AL_POSITION;
 pub const ORIENTATION = AL_ORIENTATION;
 
 pub const listener = struct {
-    pub fn set(comptime T: type, param: ALenum, value: T) void {
+    /// Set a property for the listener.
+    pub fn set(param: ALenum, value: anytype) !void {
+        const T = @TypeOf(value);
         switch (T) {
             f32 => alListenerf(param, value),
             i32 => alListeneri(param, value),
@@ -34,12 +49,48 @@ pub const listener = struct {
                 else => unreachable,
             }
         }
+
+        switch (alGetError()) {
+            AL_NO_ERROR => {},
+            AL_INVALID_VALUE => return Error.InvalidValue,
+            AL_INVALID_ENUM => return Error.InvalidEnum,
+            AL_INVALID_OPERATION => return Error.InvalidOperation,
+            else => unreachable,
+        }
     }
 };
 
+/// Generate one buffer for audio data and return its reference.
+pub fn genBuffer() !u32 {
+    var reference: u32 = undefined;
+    alGenBuffers(1, &reference);
+    return switch (alGetError()) {
+        AL_NO_ERROR => reference,
+        AL_INVALID_VALUE => Error.InvalidValue,
+        AL_OUT_OF_MEMORY => Error.OutOfMemory,
+        else => unreachable,
+    };
+}
+
+// TODO: genBuffers
+
+/// Free resources used by one buffer.
+/// Buffers attached to a source cannot be deleted.
+pub fn deleteBuffer(reference: *const u32) !void {
+    alDeleteBuffers(1, reference);
+    switch (alGetError()) {
+        AL_NO_ERROR => {},
+        AL_INVALID_OPERATION => return Error.InvalidOperation,
+        AL_INVALID_NAME => return Error.InvalidName,
+        AL_INVALID_VALUE => return Error.InvalidValue,
+        else => unreachable,
+    }
+}
+
+// TODO: deleteBuffers
+
 // alBufferData
 // alDeleteSources
-// alGenBuffers
 // alGenSources
 // alGetSourcei
 // alSourcef