summary refs log tree commit diff
path: root/src/main.zig
blob: fc13fa80bbfe67dcec0b26b836fe7fe3bf093c29 (plain) (blame)
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
// 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 expect = std.testing.expect;
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);
        al.listener.set([3]f32, 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] };
        al.listener.set(@TypeOf(orientation), al.ORIENTATION, orientation);
    }
};

test "temporary" {
    const device = try Device.init("OpenAL Soft");
    defer device.deinit() catch unreachable;
    const context = try Context.init(device, &.{ alc.HRTF, alc.TRUE });
    defer context.deinit() catch unreachable;

    try expect(null == try currentContext());
    if (checkContext(context)) |_| unreachable else |err| switch (err) {
        error.UncurrentContext => {},
    }

    try useContext(context);
    const current_context = (try currentContext()).?;
    try expect(eql(current_context, context));
    try expect(eql(current_context.device, device));
    try checkContext(context);

    const listener = Listener{ .context = context };
    try listener.setPosition(.{ 0, 0, 0 });
    try listener.setOrientation(.{ 0, 0, 0 }, .{ 0, 0, 0 });
}