about summary refs log tree commit diff
path: root/examples/hrtf.zig
blob: 43e83c0c1bb468a2e37d68d6481a49955e47d836 (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
// Positional audio example with HRTF
// 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 std = @import("std");
const allocator = std.heap.c_allocator;
const args = std.process.argsWithAllocator;
const print = std.debug.print;
const sleep = std.time.sleep;

const zeal = @import("zeal");
const Device = zeal.Device;
const Context = zeal.Context;
const Audio = zeal.Audio;
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, .{ .hrtf = .true });
    if (try device.get(.hrtf))
        print("HRTF enabled!\n", .{})
    else
        print("HRTF not enabled!\n", .{});
    defer context.deinit() catch unreachable;

    var argv = try args(allocator);
    defer argv.deinit();
    _ = argv.next().?;
    const audio = try Audio.read(allocator, argv.next().?);
    defer audio.free();

    try context.makeCurrent();
    const buffer = try Buffer.init(audio);
    defer buffer.deinit() catch unreachable;

    const source = try Source.init();
    defer source.deinit() catch unreachable;
    try source.setSpatialize(true);
    try source.bind(buffer);
    try source.play();
    defer print("\n", .{});
    while (try source.isPlaying()) {
        const offset = try source.getSecOffset();
        try source.setPosition(.{ @sin(offset), 0, @cos(offset) });
        print("\r{d:.1} s", .{ try source.getSecOffset() });
        sleep(10_000_000);
    }
}