about summary refs log tree commit diff
path: root/src/al.zig
blob: 1fa11df5260afec75042faac3156597d37d2c046 (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// OpenAL wrapper
// Copyright (C) 2021-2023, 2025  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 Child = meta.Child;
const Tag = meta.Tag;
const meta = @import("std").meta;

const c = @import("cimport.zig");

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 FALSE = c.AL_FALSE;
pub const TRUE = c.AL_TRUE;
pub const AUTO = c.AL_AUTO_SOFT;

/// Convert bool to AL enumeration.
pub fn boolToEnum(value: bool) c_int {
    return if (value) TRUE else FALSE;
}

pub const listener = struct {
    const Property = enum(c.ALenum) {
        gain = c.AL_GAIN,
        position = c.AL_POSITION,
        velocity = c.AL_VELOCITY,
        orientation = c.AL_ORIENTATION,
        meters_per_unit = c.AL_METERS_PER_UNIT,
    };

    /// Set a property for the listener.
    pub fn set(property: Property, value: anytype) Error!void {
        const param = @intFromEnum(property);
        const T = @TypeOf(value);
        switch (T) {
            f32 => c.alListenerf(param, value),
            i32 => c.alListeneri(param, value),
            else => switch (Child(T)) {
                f32 => c.alListenerfv(param, value[0..]),
                i32 => c.alListeneriv(param, value[0..]),
                else => unreachable,
            },
        }

        switch (c.alGetError()) {
            c.AL_NO_ERROR => {},
            c.AL_INVALID_VALUE => return Error.InvalidValue,
            c.AL_INVALID_ENUM => return Error.InvalidEnum,
            c.AL_INVALID_OPERATION => return Error.InvalidOperation,
            else => unreachable,
        }
    }
};

pub const Data = union(enum) {
    /// Unsigned 8-bit mono.
    mono8: []const u8,
    /// Signed 16-bit mono.
    mono16: []const i16,
    /// Unsigned 8-bit stereo.
    stereo8: []const u8,
    /// Signed 16-bit stereo.
    stereo16: []const i16,
};

pub const buffer = struct {
    /// Generate one buffer for audio data and return its reference.
    pub fn create() Error!u32 {
        var reference: u32 = undefined;
        c.alGenBuffers(1, &reference);
        return switch (c.alGetError()) {
            c.AL_NO_ERROR => reference,
            c.AL_INVALID_VALUE => Error.InvalidValue,
            c.AL_OUT_OF_MEMORY => Error.OutOfMemory,
            else => unreachable,
        };
    }

    /// Free resources used by one buffer.
    /// Buffers attached to a source cannot be deleted.
    pub fn destroy(reference: *const u32) Error!void {
        c.alDeleteBuffers(1, reference);
        switch (c.alGetError()) {
            c.AL_NO_ERROR => {},
            c.AL_INVALID_OPERATION => return Error.InvalidOperation,
            c.AL_INVALID_NAME => return Error.InvalidName,
            c.AL_INVALID_VALUE => return Error.InvalidValue,
            else => unreachable,
        }
    }

    /// Fill a buffer with audio data.
    pub fn fill(reference: u32, data: Data, freq: i32) Error!void {
        const format = switch (data) {
            .mono8 => c.AL_FORMAT_MONO8,
            .mono16 => c.AL_FORMAT_MONO16,
            .stereo8 => c.AL_FORMAT_STEREO8,
            .stereo16 => c.AL_FORMAT_STEREO16,
        };

        switch (data) {
            .mono8, .stereo8 => |slice|
                c.alBufferData(reference, format,
                               slice.ptr, @intCast(slice.len), freq),
            .mono16, .stereo16 => |slice|
                c.alBufferData(reference, format,
                               slice.ptr, @intCast(slice.len * 2), freq),
        }

        switch (c.alGetError()) {
            c.AL_NO_ERROR => {},
            c.AL_OUT_OF_MEMORY => return Error.OutOfMemory,
            c.AL_INVALID_VALUE => return Error.InvalidValue,
            c.AL_INVALID_ENUM => return Error.InvalidEnum,
            else => unreachable,
        }
    }
};

pub const source = struct {
    /// Generate one source for audio data and return its reference.
    pub fn create() Error!u32 {
        var reference: u32 = undefined;
        c.alGenSources(1, &reference);
        return switch (c.alGetError()) {
            c.AL_NO_ERROR => reference,
            c.AL_INVALID_VALUE => Error.InvalidValue,
            c.AL_OUT_OF_MEMORY => Error.OutOfMemory,
            else => unreachable,
        };
    }

    /// Free resources used by one source.
    /// Sources attached to a source cannot be deleted.
    pub fn destroy(reference: *const u32) Error!void {
        c.alDeleteSources(1, reference);
        switch (c.alGetError()) {
            c.AL_NO_ERROR => {},
            c.AL_INVALID_OPERATION => return Error.InvalidOperation,
            c.AL_INVALID_NAME => return Error.InvalidName,
            c.AL_INVALID_VALUE => return Error.InvalidValue,
            else => unreachable,
        }
    }

    const Property = enum(c.ALenum) {
        pitch = c.AL_PITCH,
        gain = c.AL_GAIN,
        position = c.AL_POSITION,
        velocity = c.AL_VELOCITY,
        direction = c.AL_DIRECTION,
        relative = c.AL_SOURCE_RELATIVE,
        looping = c.AL_LOOPING,
        buffer = c.AL_BUFFER,
        state = c.AL_SOURCE_STATE,
        sec_offset = c.AL_SEC_OFFSET,
        spatialize = c.AL_SOURCE_SPATIALIZE_SOFT,
    };

    pub const State = enum(i32) {
        initial = c.AL_INITIAL,
        playing = c.AL_PLAYING,
        paused = c.AL_PAUSED,
        stopped = c.AL_STOPPED,
    };

    /// Set a property for the source.
    pub fn set(reference: u32, property: Property, value: anytype) Error!void {
        const param = @intFromEnum(property);
        const T = @TypeOf(value);
        switch (T) {
            f32 => c.alSourcef(reference, param, value),
            i32 => c.alSourcei(reference, param, value),
            else => switch (@typeInfo(T)) {
                .@"enum" => c.alSourcei(reference, param, @intFromEnum(value)),
                else => switch (Child(T)) {
                    f32 => c.alSourcefv(reference, param, value[0..]),
                    i32 => c.alSourceiv(reference, param, value[0..]),
                    else => unreachable,
                },
            },
        }

        switch (c.alGetError()) {
            c.AL_NO_ERROR => {},
            c.AL_INVALID_VALUE => return Error.InvalidValue,
            c.AL_INVALID_ENUM => return Error.InvalidEnum,
            c.AL_INVALID_NAME => return Error.InvalidName,
            c.AL_INVALID_OPERATION => return Error.InvalidOperation,
            else => unreachable,
        }
    }

    /// Get a scalar property from the source.
    pub fn get(comptime T: type, reference: u32, property: Property) Error!T {
        const param = @intFromEnum(property);
        var value: T = undefined;
        switch (T) {
            f32 => c.alGetSourcef(reference, param, &value),
            i32 => c.alGetSourcei(reference, param, &value),
            else => switch (@typeInfo(T)) {
                .@"enum" => {
                    var raw: i32 = undefined;
                    c.alGetSourcei(reference, param, &raw);
                    value = @enumFromInt(raw);
                },
                else => unreachable,
            },
        }

        return switch (c.alGetError()) {
            c.AL_NO_ERROR => value,
            c.AL_INVALID_VALUE => return Error.InvalidValue,
            c.AL_INVALID_ENUM => return Error.InvalidEnum,
            c.AL_INVALID_NAME => return Error.InvalidName,
            c.AL_INVALID_OPERATION => return Error.InvalidOperation,
            else => unreachable,
        };
    }

    /// Play the source.
    pub fn play(reference: u32) Error!void {
        c.alSourcePlay(reference);
        switch (c.alGetError()) {
            c.AL_NO_ERROR => {},
            c.AL_INVALID_NAME => return Error.InvalidName,
            c.AL_INVALID_OPERATION => return Error.InvalidOperation,
            else => unreachable,
        }
    }
};

// alSourcePause
// alSourceStop