summary refs log tree commit diff
path: root/examples/hrtf.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/hrtf.zig')
-rw-r--r--examples/hrtf.zig67
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/hrtf.zig b/examples/hrtf.zig
new file mode 100644
index 0000000..723571d
--- /dev/null
+++ b/examples/hrtf.zig
@@ -0,0 +1,67 @@
+// Positional audio example with HRTF
+// 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 std = @import("std");
+const allocator = std.heap.c_allocator;
+const args = std.process.args;
+const print = std.debug.print;
+const sleep = std.time.sleep;
+
+const zeal = @import("zeal");
+const al = zeal.al;
+const alc = zeal.alc;
+const Device = zeal.Device;
+const Context = zeal.Context;
+const Audio = zeal.Audio;
+const useContext = zeal.useContext;
+const Buffer = zeal.Buffer;
+const Source = zeal.Source;
+
+pub fn main() !void {
+    const device = try Device.init(null);
+    defer device.deinit() catch unreachable;
+
+    const context = try Context.init(device, &.{ alc.HRTF, alc.TRUE });
+    if (try device.enabledHrtf())
+        print("HRTF enabled!\n", .{})
+    else
+        print("HRTF not enabled!\n", .{});
+    defer context.deinit() catch unreachable;
+
+    var argv = args();
+    allocator.free(try argv.next(allocator).?);
+    const path = try argv.next(allocator).?;
+    defer allocator.free(path);
+    const audio = try Audio.read(allocator, path);
+    defer audio.free();
+
+    try useContext(context);
+    const buffer = try Buffer.init(context, audio);
+    defer buffer.deinit() catch unreachable;
+
+    const source = try Source.init(context);
+    defer source.deinit() catch unreachable;
+    try source.setSpatialize(al.TRUE);
+    try source.play(buffer);
+    defer print("\n", .{});
+    while (try source.isPlaying()) {
+        const offset = try source.getSecOffset();
+        try source.setPosition(.{ @sin(offset), 0, @cos(offset) });
+        sleep(10_000_000);
+    }
+}