summary refs log tree commit diff
path: root/src/alc.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/alc.zig')
-rw-r--r--src/alc.zig93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/alc.zig b/src/alc.zig
new file mode 100644
index 0000000..3a6d9a8
--- /dev/null
+++ b/src/alc.zig
@@ -0,0 +1,93 @@
+// Audio Library Context wrapper
+// Copyright (C) 2021  Nguyễn Gia Phong
+//
+// This file is part of zeal.
+//
+// Zeal is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Zeal is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with zeal.  If not, see <https://www.gnu.org/licenses/>.
+
+usingnamespace @cImport({ @cInclude("AL/alc.h"); });
+
+pub const Device = ALCdevice;
+pub const Context = ALCcontext;
+pub const Error = error {
+    InvalidDevice,
+    InvalidContext,
+    InvalidEnum,
+    InvalidValue,
+    OutOfMemory,
+};
+
+/// Create and attach a context to the given device.
+pub fn createContext(device: *Device, attrlist: [*c]const ALCint) !*Context {
+    if (alcCreateContext(device, attrlist)) |context|
+        return context;
+    return switch (alcGetError(device)) {
+        ALC_INVALID_DEVICE => Error.InvalidDevice,
+        ALC_INVALID_VALUE => Error.InvalidValue,
+        else => unreachable,
+    };
+}
+
+/// Make the given context the active process-wide context.
+/// Passing NULL clears the active context.
+pub fn makeContextCurrent(context: ?*Context) !void {
+    if (alcMakeContextCurrent(context) == ALC_TRUE)
+        return;
+    if (alcGetError(null) == ALC_INVALID_CONTEXT)
+        return Error.InvalidContext;
+    unreachable;
+}
+
+/// Remove a context from its device and destroys it.
+pub fn destroyContext(context: *Context) !void {
+    alcDestroyContext(context);
+    if (alcGetError(null) == ALC_INVALID_CONTEXT)
+        return Error.InvalidContext;
+}
+
+/// Returns the currently active context.
+pub fn getCurrentContext() *Context {
+    if (alcGetCurrentContext()) |context|
+        return context;
+    unreachable;
+}
+
+/// Return the device that a particular context is attached to.
+pub fn getContextsDevice(context: *Context) !*Device {
+    if (alcGetContextsDevice(context)) |device|
+        return device;
+    if (alcGetError(null) == ALC_INVALID_CONTEXT)
+        return Error.InvalidContext;
+    unreachable;
+}
+
+/// Open the named playback device.
+pub fn openDevice(devicename: [*c]const u8) !*Device {
+    if (alcOpenDevice(devicename)) |device|
+        return device;
+    return switch (alcGetError(null)) {
+        ALC_INVALID_VALUE => Error.InvalidValue,
+        ALC_OUT_OF_MEMORY => Error.OutOfMemory,
+        else => unreachable,
+    };
+}
+
+/// Close the given playback device.
+pub fn closeDevice(device: *Device) !void {
+    if (alcCloseDevice(device) == ALC_TRUE)
+        return;
+    if (alcGetError(device) == ALC_INVALID_DEVICE)
+        return Error.InvalidDevice;
+    unreachable;
+}