summary refs log tree commit diff
path: root/examples/play.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/play.zig')
-rw-r--r--examples/play.zig53
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/play.zig b/examples/play.zig
new file mode 100644
index 0000000..81403a0
--- /dev/null
+++ b/examples/play.zig
@@ -0,0 +1,53 @@
+// Playing audio example
+// 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 sleep = std.time.sleep;
+
+const zeal = @import("zeal");
+const alc = zeal.alc;
+const Device = zeal.Device;
+const Context = zeal.Context;
+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, &.{});
+    defer context.deinit() catch unreachable;
+    try useContext(context);
+
+    var argv = args();
+    _ = argv.next(allocator);
+    const filename = try argv.next(allocator).?;
+    const buffer = try Buffer.init(allocator, context, filename);
+    defer buffer.deinit() catch unreachable;
+
+    const source = try Source.init(context);
+    defer source.deinit() catch unreachable;
+    try source.play(buffer);
+    defer std.debug.print("\n", .{});
+    while (try source.isPlaying()) {
+        sleep(10_000_000);
+        defer std.debug.print("\r{d:.1} s", .{ source.getSecOffset() });
+    }
+}