about summary refs log tree commit diff
diff options
context:
space:
mode:
authormakefunstuff <[email protected]>2024-07-04 23:32:55 +0200
committermakefunstuff <[email protected]>2024-07-04 23:32:55 +0200
commita9029ce5cfe75e9bb9f732ceae27871c441112e6 (patch)
tree1faf17db712b88975f3d4c225468fa43e53c5ba1
parent30b080dc8a67228792bc1a4898d1fdc160182e9b (diff)
downloadtinkerbunk-a9029ce5cfe75e9bb9f732ceae27871c441112e6.tar.gz
getting closer
Diffstat (limited to '')
-rw-r--r--src/brr.zig50
-rw-r--r--src/main.zig2
2 files changed, 19 insertions, 33 deletions
diff --git a/src/brr.zig b/src/brr.zig
index e11a69c..aa46bf0 100644
--- a/src/brr.zig
+++ b/src/brr.zig
@@ -4,7 +4,7 @@ const c = @cImport({
     @cInclude("alsa/asoundlib.h");
 });
 
-pub fn brr(file: []const u8) !void {
+pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void {
     _ = c.mpg123_init();
 
     const handle = c.mpg123_new(null, null) orelse {
@@ -12,6 +12,8 @@ pub fn brr(file: []const u8) !void {
         return;
     };
 
+    const buffer_size = c.mpg123_outblock(handle);
+
     const file_path: [*c]const u8 = @ptrCast(file);
     if (c.mpg123_open(handle, file_path) != c.MPG123_OK) {
         std.log.warn("Failed to open the file: {s}\n", .{file_path});
@@ -41,17 +43,17 @@ pub fn brr(file: []const u8) !void {
 
     _ = c.snd_pcm_hw_params(pcm, params);
     _ = c.snd_pcm_hw_params_set_access(pcm, params, c.SND_PCM_ACCESS_RW_INTERLEAVED);
-    _ = c.snd_pcm_hw_params_set_access(pcm, params, c.SND_PCM_FORMAT_S16_LE);
-    _ = c.snd_pcm_hw_params_set_channels(pcm, params, @intCast(channels));
-    _ = c.snd_pcm_hw_params_set_rate(pcm, params, @intCast(rate), 0);
+    _ = c.snd_pcm_hw_params_set_channels(pcm, params, @as(c_uint, @intCast(channels)));
+    _ = c.snd_pcm_hw_params_set_rate(pcm, params, @as(c_uint, @intCast(rate)), 0);
 
     _ = c.snd_pcm_hw_params(pcm, params);
 
-    var buffer: [4096]u8 = undefined;
+    var buffer: []u8 = try allocator.alloc(u8, buffer_size);
+    defer allocator.free(buffer);
 
     while (true) {
         var done: usize = 0;
-        const result = c.mpg123_read(handle, &buffer[0], buffer.len, &done);
+        const result = c.mpg123_read(handle, &buffer[0], buffer_size, &done);
         switch (result) {
             c.MPG123_OK => {},
             c.MPG123_DONE => {
@@ -67,33 +69,17 @@ pub fn brr(file: []const u8) !void {
             break;
         }
 
-        const frames_sent = c.snd_pcm_writei(pcm, &buffer[0], done / 4);
-        if (frames_sent < 0) {
-            std.log.err("Failed to write to ALSA device", .{});
-            var status: ?*c.snd_pcm_status_t = null;
-            _ = c.snd_pcm_status_malloc(&status);
-            defer c.snd_pcm_status_free(status);
-
-            _ = c.snd_pcm_status(pcm, status);
-            const state = c.snd_pcm_status_get_state(status);
-
-            switch (state) {
-                c.SND_PCM_STATE_SUSPENDED => {
-                    std.log.warn("ALSA device is suspended", .{});
-                    _ = c.snd_pcm_resume(pcm);
-                },
-                c.SND_PCM_STATE_XRUN => {
-                    std.log.warn("ALSA device is in an XRUN state", .{});
-                    _ = c.snd_pcm_prepare(pcm);
-                },
-                else => {
-                    std.log.err("ALSA device is in an unknown state", .{});
-                },
-            }
+        const write_state = c.snd_pcm_writei(pcm, &buffer[0], done / 2);
 
-            return;
-        } else {
-            std.log.info("Frames sent: {d}", .{frames_sent});
+        switch (write_state) {
+            c.SND_ERROR_BEGIN => {
+                std.log.debug("SND_ERROR_BEGIN", .{});
+            },
+            else => {
+                const error_code: c_int = @as(c_int, @intCast(write_state));
+                const error_string = c.snd_strerror(error_code);
+                std.log.err("Failed {s}", .{error_string});
+            },
         }
     }
     _ = c.mpg123_delete(handle);
diff --git a/src/main.zig b/src/main.zig
index 5609376..15df5c9 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -51,7 +51,7 @@ pub fn main() !void {
 
     if (args.len == 3) {
         if (std.mem.eql(u8, args[1], "brr")) {
-            try brr.brr(args[2]);
+            try brr.brr(allocator, args[2]);
             return;
         }
     }