about summary refs log tree commit diff
path: root/examples/play.zig
blob: 60256c6aec7be25a5ad07f4c4a28bb73328c7557 (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
// Playing audio example
// Copyright (C) 2021, 2023  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, .{});
    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.bind(buffer);
    try source.play();
    defer print("\n", .{});
    while (try source.isPlaying()) {
        print("\r{d:.1} s", .{ try source.getSecOffset() });
        sleep(10_000_000);
    }
}