1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Zig easy audio library
// 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/>.
const al = @import("al.zig");
const alc = @import("alc.zig");
const expectEqual = std.testing.expectEqual;
const eql = std.meta.eql;
const std = @import("std");
pub const Device = struct {
pimpl: *alc.Device,
pub fn init(name: ?[]const u8) alc.Error!Device {
return Device{ .pimpl = try alc.openDevice(name) };
}
pub fn deinit(self: Device) alc.Error!void {
try alc.closeDevice(self.pimpl);
}
};
pub const Context = struct {
pimpl: *alc.Context,
device: Device,
pub fn init(device: Device, attributes: [:0]const i32) alc.Error!Context {
return Context {
.pimpl = try alc.createContext(device.pimpl, attributes),
.device = device,
};
}
pub fn deinit(self: Context) alc.Error!void {
try alc.makeContextCurrent(null);
try alc.destroyContext(self.pimpl);
}
};
pub fn useContext(context: ?Context) alc.Error!void {
try alc.makeContextCurrent(if (context == null) null else context.?.pimpl);
}
pub fn currentContext() alc.Error!?Context {
if (alc.getCurrentContext()) |pimpl|
return Context {
.pimpl = pimpl,
.device = Device { .pimpl = try alc.getContextsDevice(pimpl) },
};
return null;
}
fn checkContext(context: Context) !void {
if (context.pimpl != alc.getCurrentContext())
return error.UncurrentContext;
}
pub const Listener = struct {
context: Context,
pub fn setPosition(self: Listener, position: [3]f32) !void {
try checkContext(self.context);
try al.listener.set(al.POSITION, position);
}
pub fn setOrientation(self: Listener, at: [3]f32, up: [3]f32) !void {
try checkContext(self.context);
const orientation = [_]f32{ at[0], at[1], at[2], up[0], up[1], up[2] };
try al.listener.set(al.ORIENTATION, orientation);
}
};
pub const Buffer = struct {
context: Context,
reference: u32,
pub fn init(context: Context, name: []const u8) !Buffer {
try checkContext(context);
const reference = try al.genBuffer();
return Buffer{ .context = context, .reference = reference };
}
pub fn deinit(self: Buffer) !void {
try checkContext(self.context);
try al.deleteBuffer(&self.reference);
}
};
test "Device" {
const device = try Device.init("OpenAL Soft");
try device.deinit();
}
test "Context" {
const device = try Device.init(null);
defer device.deinit() catch unreachable;
const context = try Context.init(device, &.{ alc.HRTF, alc.TRUE });
defer context.deinit() catch unreachable;
try expectEqual(try currentContext(), null);
if (checkContext(context)) unreachable else |err| switch (err) {
error.UncurrentContext => {},
}
try useContext(context);
const current_context = (try currentContext()).?;
try expectEqual(current_context, context);
try expectEqual(current_context.device, device);
try checkContext(context);
}
test "Listener" {
const context = try Context.init(try Device.init(null), &.{});
defer context.device.deinit() catch unreachable;
defer context.deinit() catch unreachable;
try useContext(context);
const listener = Listener{ .context = context };
try listener.setPosition(.{ 1, 2, 3 });
try listener.setOrientation(.{ 4, 5, 6 }, .{ 7, 8, 9 });
}
test "Buffer" {
const context = try Context.init(try Device.init(null), &.{});
defer context.device.deinit() catch unreachable;
defer context.deinit() catch unreachable;
try useContext(context);
const buffer = try Buffer.init(context, "");
defer buffer.deinit() catch unreachable;
}
|