From dde71be0e70e4a4f4ae6a3af360449039cd2cf49 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Thu, 4 Jul 2024 01:24:08 +0300 Subject: not working, some struct shenanigans --- src/brr.zig | 46 +++++++++++++++++++++++++++++++++++++--------- static/badum.mp3 | Bin 0 -> 61793 bytes 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 static/badum.mp3 diff --git a/src/brr.zig b/src/brr.zig index 74ee419..75dc59d 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -4,6 +4,8 @@ const c = @cImport({ @cInclude("alsa/asoundlib.h"); }); +pub const snd_pcm_info_t = extern struct {}; + pub fn brr(file: []const u8) !void { _ = c.mpg123_init(); @@ -27,10 +29,20 @@ pub fn brr(file: []const u8) !void { var params: ?*c.snd_pcm_hw_params_t = null; _ = c.snd_pcm_hw_params_malloc(¶ms); + var encoding: c_int = 0; + var channels: c_int = 0; + var rate: c_long = 0; + + if (c.mpg123_getformat(handle, &rate, &channels, &encoding) != c.MPG123_OK) { + std.log.warn("Failed to get format\n", .{}); + return; + } + _ = 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, 2); - _ = c.snd_pcm_hw_params_set_rate(pcm, params, 44100, 0); + _ = 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(pcm, params); @@ -40,21 +52,37 @@ pub fn brr(file: []const u8) !void { var done: usize = 0; const result = c.mpg123_read(handle, &buffer[0], buffer.len, &done); switch (result) { - c.MPG123_OK => { - std.log.info("Reading successfule", .{}); + c.MPG123_OK => {}, + c.MPG123_DONE => { + std.log.info("Done reading", .{}); }, else => { - std.log.err("Decode error {}", .{result}); + const plain_error = c.mpg123_plain_strerror(result); + std.log.err("Decode error {s}", .{plain_error}); }, } if (done == 0) { - _ = c.mpg123_delete(handle); - _ = c.snd_pcm_hw_params_free(params); - _ = c.snd_pcm_close(pcm); break; } - _ = c.snd_pcm_writei(pcm, &buffer[0], done / 4); + 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", .{}); + // TODO: figure out how to deal with opaque status + var pcm_info: snd_pcm_info_t = snd_pcm_info_t{}; + _ = c.snd_pcm_info_malloc(&pcm_info); + // _ = c.snd_pcm_info(pcm, &pcm_info); + + defer c.snd_pcm_info_free(&pcm_info); + std.log.debug("pcm status is {any}", .{pcm_info}); + return; + } else { + std.log.info("Frames sent: {d}", .{frames_sent}); + } } + _ = c.mpg123_delete(handle); + _ = c.snd_pcm_hw_params_free(params); + _ = c.snd_pcm_close(pcm); + _ = c.mpg123_exit(); } diff --git a/static/badum.mp3 b/static/badum.mp3 new file mode 100644 index 0000000..30a9202 Binary files /dev/null and b/static/badum.mp3 differ -- cgit 1.4.1-2-gfad0 From c3f6700f383bd1248626071e450eb6a6da08b8d4 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Thu, 4 Jul 2024 10:29:15 +0300 Subject: extending struct --- src/brr.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/brr.zig b/src/brr.zig index 75dc59d..cfd6f1f 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -5,6 +5,9 @@ const c = @cImport({ }); pub const snd_pcm_info_t = extern struct {}; +pub extern fn snd_pcm_info_malloc(pcm_info: *snd_pcm_info_t) void; +pub extern fn snd_pcm_info(pcm: *c.snd_pcm_t, pcm_info: *snd_pcm_info_t) c_int; +pub extern fn snd_pcm_info_free(pcm_info: *snd_pcm_info_t) void; pub fn brr(file: []const u8) !void { _ = c.mpg123_init(); @@ -71,10 +74,10 @@ pub fn brr(file: []const u8) !void { std.log.err("Failed to write to ALSA device", .{}); // TODO: figure out how to deal with opaque status var pcm_info: snd_pcm_info_t = snd_pcm_info_t{}; - _ = c.snd_pcm_info_malloc(&pcm_info); - // _ = c.snd_pcm_info(pcm, &pcm_info); + snd_pcm_info_malloc(&pcm_info); + _ = snd_pcm_info(pcm, &pcm_info); - defer c.snd_pcm_info_free(&pcm_info); + defer snd_pcm_info_free(&pcm_info); std.log.debug("pcm status is {any}", .{pcm_info}); return; } else { -- cgit 1.4.1-2-gfad0 From b695878582fc60e4398f7c4fe486eeedd05dc5de Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Thu, 4 Jul 2024 23:02:40 +0300 Subject: trying hard with alsa --- src/brr.zig | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/brr.zig b/src/brr.zig index cfd6f1f..7bca832 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -4,10 +4,8 @@ const c = @cImport({ @cInclude("alsa/asoundlib.h"); }); -pub const snd_pcm_info_t = extern struct {}; -pub extern fn snd_pcm_info_malloc(pcm_info: *snd_pcm_info_t) void; -pub extern fn snd_pcm_info(pcm: *c.snd_pcm_t, pcm_info: *snd_pcm_info_t) c_int; -pub extern fn snd_pcm_info_free(pcm_info: *snd_pcm_info_t) void; +const _op_snd_pcm_info_t = opaque {}; +const snd_pcm_info_t = ?*_op_snd_pcm_info_t; pub fn brr(file: []const u8) !void { _ = c.mpg123_init(); @@ -72,13 +70,16 @@ pub fn brr(file: []const u8) !void { 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", .{}); - // TODO: figure out how to deal with opaque status - var pcm_info: snd_pcm_info_t = snd_pcm_info_t{}; - snd_pcm_info_malloc(&pcm_info); - _ = snd_pcm_info(pcm, &pcm_info); + var pcm_info: snd_pcm_info_t = undefined; + const alloc_result = c.snd_pcm_info_malloc(@ptrCast(&pcm_info)); + if (alloc_result == 0) { + std.log.err("Failed to allocate pcm info", .{}); + return; + } + _ = c.snd_pcm_info(pcm, @ptrCast(&pcm_info)); - defer snd_pcm_info_free(&pcm_info); - std.log.debug("pcm status is {any}", .{pcm_info}); + std.log.debug("pcm_info {?}", .{pcm_info}); + defer c.snd_pcm_info_free(@ptrCast(&pcm_info)); return; } else { std.log.info("Frames sent: {d}", .{frames_sent}); -- cgit 1.4.1-2-gfad0 From 30b080dc8a67228792bc1a4898d1fdc160182e9b Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Thu, 4 Jul 2024 23:51:10 +0300 Subject: getting better --- src/brr.zig | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/brr.zig b/src/brr.zig index 7bca832..e11a69c 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -4,9 +4,6 @@ const c = @cImport({ @cInclude("alsa/asoundlib.h"); }); -const _op_snd_pcm_info_t = opaque {}; -const snd_pcm_info_t = ?*_op_snd_pcm_info_t; - pub fn brr(file: []const u8) !void { _ = c.mpg123_init(); @@ -28,7 +25,10 @@ pub fn brr(file: []const u8) !void { } var params: ?*c.snd_pcm_hw_params_t = null; - _ = c.snd_pcm_hw_params_malloc(¶ms); + if (c.snd_pcm_hw_params_malloc(¶ms) < 0) { + std.log.warn("Failed to allocate ALSA hardware parameters\n", .{}); + return; + } var encoding: c_int = 0; var channels: c_int = 0; @@ -70,16 +70,27 @@ pub fn brr(file: []const u8) !void { 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 pcm_info: snd_pcm_info_t = undefined; - const alloc_result = c.snd_pcm_info_malloc(@ptrCast(&pcm_info)); - if (alloc_result == 0) { - std.log.err("Failed to allocate pcm info", .{}); - return; + 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", .{}); + }, } - _ = c.snd_pcm_info(pcm, @ptrCast(&pcm_info)); - std.log.debug("pcm_info {?}", .{pcm_info}); - defer c.snd_pcm_info_free(@ptrCast(&pcm_info)); return; } else { std.log.info("Frames sent: {d}", .{frames_sent}); -- cgit 1.4.1-2-gfad0 From a9029ce5cfe75e9bb9f732ceae27871c441112e6 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 00:32:55 +0300 Subject: getting closer --- src/brr.zig | 50 ++++++++++++++++++-------------------------------- src/main.zig | 2 +- 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; } } -- cgit 1.4.1-2-gfad0 From 07e524536fe43efed9da83a8be5024a68d2e4110 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 01:11:36 +0300 Subject: jiggling pointers and casts --- src/brr.zig | 50 ++++++++++++++------------------------------------ 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/src/brr.zig b/src/brr.zig index aa46bf0..be5168b 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -26,12 +26,6 @@ pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { return; } - var params: ?*c.snd_pcm_hw_params_t = null; - if (c.snd_pcm_hw_params_malloc(¶ms) < 0) { - std.log.warn("Failed to allocate ALSA hardware parameters\n", .{}); - return; - } - var encoding: c_int = 0; var channels: c_int = 0; var rate: c_long = 0; @@ -41,46 +35,30 @@ pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { return; } + var params: ?*c.snd_pcm_hw_params_t = null; + if (c.snd_pcm_hw_params_malloc(¶ms) < 0) { + std.log.warn("Failed to allocate ALSA hardware parameters\n", .{}); + return; + } + _ = c.snd_pcm_hw_params_any(pcm, params); _ = 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_format(pcm, params, c.SND_PCM_FORMAT_S16_LE); _ = 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 done: usize = 0; 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_size, &done); - switch (result) { - c.MPG123_OK => {}, - c.MPG123_DONE => { - std.log.info("Done reading", .{}); - }, - else => { - const plain_error = c.mpg123_plain_strerror(result); - std.log.err("Decode error {s}", .{plain_error}); - }, - } - - if (done == 0) { - break; - } - - const write_state = c.snd_pcm_writei(pcm, &buffer[0], done / 2); + const buffer_ptr = @as([*c]u8, @ptrCast(&buffer[0])); - 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}); - }, - } + while (c.mpg123_read(handle, buffer_ptr, buffer_size, &done) == c.MPG123_OK) { + const frames = @divExact(@as(c_ulong, @intCast(done)), @as(c_ulong, @intCast(channels * 2))); + std.log.debug("Starting sending", .{}); + const write_state = c.snd_pcm_writei(pcm, buffer_ptr, @as(c_ulong, frames)); + std.log.info("Written pcm frames {}", .{write_state}); } _ = c.mpg123_delete(handle); _ = c.snd_pcm_hw_params_free(params); -- cgit 1.4.1-2-gfad0 From de19dea8068d20051fe8400aa16c7584b4ac6193 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 01:32:19 +0300 Subject: debug --- src/brr.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/brr.zig b/src/brr.zig index be5168b..106da09 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -55,9 +55,19 @@ pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { const buffer_ptr = @as([*c]u8, @ptrCast(&buffer[0])); while (c.mpg123_read(handle, buffer_ptr, buffer_size, &done) == c.MPG123_OK) { - const frames = @divExact(@as(c_ulong, @intCast(done)), @as(c_ulong, @intCast(channels * 2))); + std.log.info("Read {} bytes from MP3 file", .{done}); + const frames = done / 4; std.log.debug("Starting sending", .{}); const write_state = c.snd_pcm_writei(pcm, buffer_ptr, @as(c_ulong, frames)); + if (write_state < 0) { + if (write_state == -c.EPIPE) { // Buffer underrun + _ = c.snd_pcm_prepare(pcm); + } else { + break; + } + } else if (write_state != frames) { + std.log.warn("Short write, expected {} frames but wrote {}\n", .{ frames, write_state }); + } std.log.info("Written pcm frames {}", .{write_state}); } _ = c.mpg123_delete(handle); -- cgit 1.4.1-2-gfad0 From 85fd03f41fa8f6ac21ae849b710fc06f34200830 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 16:32:04 +0300 Subject: upd --- src/brr.zig | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/src/brr.zig b/src/brr.zig index 106da09..6e63a68 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -6,69 +6,58 @@ const c = @cImport({ pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { _ = c.mpg123_init(); - const handle = c.mpg123_new(null, null) orelse { std.log.warn("Failed to create mpg123 handle\n", .{}); 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}); return; } - var pcm: ?*c.snd_pcm_t = undefined; - if (c.snd_pcm_open(&pcm, "default", c.SND_PCM_STREAM_PLAYBACK, 0) < 0) { - std.log.warn("Failed to open ALSA device\n", .{}); - return; - } - var encoding: c_int = 0; var channels: c_int = 0; var rate: c_long = 0; - if (c.mpg123_getformat(handle, &rate, &channels, &encoding) != c.MPG123_OK) { std.log.warn("Failed to get format\n", .{}); return; } + var pcm: ?*c.snd_pcm_t = undefined; + if (c.snd_pcm_open(&pcm, "default", c.SND_PCM_STREAM_PLAYBACK, 0) < 0) { + std.log.warn("Failed to open ALSA device\n", .{}); + return; + } + + var dir: c_int = 0; var params: ?*c.snd_pcm_hw_params_t = null; if (c.snd_pcm_hw_params_malloc(¶ms) < 0) { std.log.warn("Failed to allocate ALSA hardware parameters\n", .{}); return; } + _ = c.snd_pcm_hw_params_any(pcm, params); _ = 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_format(pcm, params, c.SND_PCM_FORMAT_S16_LE); _ = 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); + _ = c.snd_pcm_hw_params_set_rate_near(pcm, params, @as(*c_uint, @ptrCast(&rate)), &dir); + if (c.snd_pcm_hw_params(pcm, params) < 0) { + std.log.warn("Failed to set ALSA hardware parameters\n", .{}); + return; + } + const buffer_size = c.mpg123_outblock(handle); var done: usize = 0; - var buffer: []u8 = try allocator.alloc(u8, buffer_size); - defer allocator.free(buffer); - const buffer_ptr = @as([*c]u8, @ptrCast(&buffer[0])); + var mpg123_buffer: []u8 = try allocator.alloc(u8, buffer_size); + _ = &mpg123_buffer; + defer allocator.free(mpg123_buffer); - while (c.mpg123_read(handle, buffer_ptr, buffer_size, &done) == c.MPG123_OK) { - std.log.info("Read {} bytes from MP3 file", .{done}); - const frames = done / 4; - std.log.debug("Starting sending", .{}); - const write_state = c.snd_pcm_writei(pcm, buffer_ptr, @as(c_ulong, frames)); - if (write_state < 0) { - if (write_state == -c.EPIPE) { // Buffer underrun - _ = c.snd_pcm_prepare(pcm); - } else { - break; - } - } else if (write_state != frames) { - std.log.warn("Short write, expected {} frames but wrote {}\n", .{ frames, write_state }); - } - std.log.info("Written pcm frames {}", .{write_state}); + while (c.mpg123_read(handle, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), buffer_size, &done) == c.MPG123_OK) { + _ = c.snd_pcm_writei(pcm, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), done / 4); } _ = c.mpg123_delete(handle); _ = c.snd_pcm_hw_params_free(params); -- cgit 1.4.1-2-gfad0 From 3aa30a037b4e636d4e314f4f8b9a73fd6584ba56 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 17:41:48 +0300 Subject: upd --- src/brr.zig | 26 +++++++++++++++++++------- src/main.zig | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/brr.zig b/src/brr.zig index 6e63a68..9c57076 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -4,16 +4,17 @@ const c = @cImport({ @cInclude("alsa/asoundlib.h"); }); -pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { +pub fn brr(allocator: std.mem.Allocator, file: [*:0]const u8) !void { _ = c.mpg123_init(); const handle = c.mpg123_new(null, null) orelse { std.log.warn("Failed to create mpg123 handle\n", .{}); return; }; - 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}); + std.log.debug("file {s}", .{file}); + + if (c.mpg123_open(handle, file) != c.MPG123_OK) { + std.log.warn("Failed to open the file: {s}\n", .{file}); return; } @@ -39,7 +40,6 @@ pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { } _ = c.snd_pcm_hw_params_any(pcm, params); - _ = 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_format(pcm, params, c.SND_PCM_FORMAT_S16_LE); _ = c.snd_pcm_hw_params_set_channels(pcm, params, @as(c_uint, @intCast(channels))); @@ -56,9 +56,21 @@ pub fn brr(allocator: std.mem.Allocator, file: []const u8) !void { _ = &mpg123_buffer; defer allocator.free(mpg123_buffer); - while (c.mpg123_read(handle, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), buffer_size, &done) == c.MPG123_OK) { - _ = c.snd_pcm_writei(pcm, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), done / 4); + var result: c_int = 0; + while (true) { + result = c.mpg123_read(handle, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), buffer_size, &done); + switch (result) { + c.MPG123_OK => { + _ = c.snd_pcm_writei(pcm, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), done / 4); + }, + else => { + const err_str = c.mpg123_strerror(handle); + std.log.warn("Failed to read from file\n {}, {s}", .{ result, err_str }); + break; + }, + } } + _ = c.mpg123_delete(handle); _ = c.snd_pcm_hw_params_free(params); _ = c.snd_pcm_close(pcm); diff --git a/src/main.zig b/src/main.zig index 15df5c9..590f3b3 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(allocator, args[2]); + try brr.brr(std.heap.c_allocator, args[2]); return; } } -- cgit 1.4.1-2-gfad0 From ed3734032e704b26a4dea9304f72e9fac2350e52 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 18:24:00 +0300 Subject: playing but not everything --- build.zig | 2 +- src/brr.zig | 2 -- src/main.zig | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index def28a5..ba4d2a6 100644 --- a/build.zig +++ b/build.zig @@ -26,7 +26,7 @@ pub fn build(b: *std.Build) void { // TODO: link for mac later exe.linkSystemLibrary("SDL2"); exe.linkSystemLibrary("mpg123"); - exe.linkSystemLibrary("alsa"); + exe.linkSystemLibrary("asound"); exe.linkLibC(); // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default diff --git a/src/brr.zig b/src/brr.zig index 9c57076..bbacdd7 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -11,8 +11,6 @@ pub fn brr(allocator: std.mem.Allocator, file: [*:0]const u8) !void { return; }; - std.log.debug("file {s}", .{file}); - if (c.mpg123_open(handle, file) != c.MPG123_OK) { std.log.warn("Failed to open the file: {s}\n", .{file}); return; diff --git a/src/main.zig b/src/main.zig index 590f3b3..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(std.heap.c_allocator, args[2]); + try brr.brr(allocator, args[2]); return; } } -- cgit 1.4.1-2-gfad0 From 2e6e2c5bc415460a66517dd786091c63189b66dc Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Fri, 5 Jul 2024 19:46:43 +0300 Subject: update --- build.zig | 3 +++ csrc/cbrr.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ csrc/cbrr.h | 1 + src/main.zig | 7 ++++- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 csrc/cbrr.c create mode 100644 csrc/cbrr.h diff --git a/build.zig b/build.zig index ba4d2a6..5a00841 100644 --- a/build.zig +++ b/build.zig @@ -28,6 +28,9 @@ pub fn build(b: *std.Build) void { exe.linkSystemLibrary("mpg123"); exe.linkSystemLibrary("asound"); exe.linkLibC(); + exe.addCSourceFile(.{ .file = b.path("csrc/cbrr.c"), .flags = &.{} }); + exe.addIncludePath(b.path("./csrc")); + // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). diff --git a/csrc/cbrr.c b/csrc/cbrr.c new file mode 100644 index 0000000..f1eb2bd --- /dev/null +++ b/csrc/cbrr.c @@ -0,0 +1,87 @@ + +#include "cbrr.h" +#include +#include +#include +#include + +#define PCM_DEVICE "default" + +void brr_c(char *filename) { + // Initialize the MPG123 library + mpg123_init(); + mpg123_handle *mh = mpg123_new(NULL, NULL); + if (mpg123_open(mh, filename) != MPG123_OK) { + fprintf(stderr, "Error opening %s: %s\n", filename, mpg123_strerror(mh)); + return; + } + + // Retrieve the format of the MP3 file + long rate; + int channels, encoding; + if (mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { + fprintf(stderr, "Error getting format: %s\n", mpg123_strerror(mh)); + return; + } + + // Set the output format + snd_pcm_t *pcm_handle; + snd_pcm_hw_params_t *params; + int pcm, dir; + snd_pcm_uframes_t frames; + char *buffer; + int size; + + // Open the PCM device + if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < + 0) { + fprintf(stderr, "Error opening PCM device %s: %s\n", PCM_DEVICE, + snd_strerror(pcm)); + return; + } + + // Allocate hardware parameters object + snd_pcm_hw_params_alloca(¶ms); + snd_pcm_hw_params_any(pcm_handle, params); + + // Set the desired hardware parameters + snd_pcm_hw_params_set_access(pcm_handle, params, + SND_PCM_ACCESS_RW_INTERLEAVED); + snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE); + snd_pcm_hw_params_set_channels(pcm_handle, params, channels); + snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, &dir); + + // Write the parameters to the driver + if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0) { + fprintf(stderr, "Error setting HW params: %s\n", snd_strerror(pcm)); + return; + } + + // Use a buffer large enough to hold one period + snd_pcm_hw_params_get_period_size(params, &frames, &dir); + size = frames * channels * 2; // 2 bytes/sample, 2 channels + buffer = (char *)malloc(size); + + // Decode and play the MP3 file + size_t buffer_size = mpg123_outblock(mh); + unsigned char *mpg123_buffer = + (unsigned char *)malloc(buffer_size * sizeof(unsigned char)); + size_t done; + int err; + + while ((err = mpg123_read(mh, mpg123_buffer, buffer_size, &done)) == + MPG123_OK) { + snd_pcm_writei(pcm_handle, mpg123_buffer, done / 4); + } + + // Clean up + free(buffer); + free(mpg123_buffer); + snd_pcm_drain(pcm_handle); + snd_pcm_close(pcm_handle); + mpg123_close(mh); + mpg123_delete(mh); + mpg123_exit(); + + return; +} diff --git a/csrc/cbrr.h b/csrc/cbrr.h new file mode 100644 index 0000000..e57fc64 --- /dev/null +++ b/csrc/cbrr.h @@ -0,0 +1 @@ +void brr_c(char *filename); diff --git a/src/main.zig b/src/main.zig index 15df5c9..1d8af9a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,11 @@ const ls = @import("ls.zig"); const socket_server = @import("socket-server.zig"); const sdl_window = @import("sdl-window.zig"); const brr = @import("brr.zig"); +const cbrr = @cImport({ + @cInclude("cbrr.h"); +}); + +extern fn brr_c(filepath: [*:0]const u8) void; const commands = [_]struct { name: []const u8, @@ -51,7 +56,7 @@ pub fn main() !void { if (args.len == 3) { if (std.mem.eql(u8, args[1], "brr")) { - try brr.brr(allocator, args[2]); + brr_c(args[2]); return; } } -- cgit 1.4.1-2-gfad0