diff options
Diffstat (limited to '')
-rw-r--r-- | build.zig | 2 | ||||
-rw-r--r-- | src/brr.zig | 50 | ||||
-rw-r--r-- | src/main.zig | 8 |
3 files changed, 56 insertions, 4 deletions
diff --git a/build.zig b/build.zig index 94ee88f..def28a5 100644 --- a/build.zig +++ b/build.zig @@ -25,6 +25,8 @@ pub fn build(b: *std.Build) void { // linking for linux only // TODO: link for mac later exe.linkSystemLibrary("SDL2"); + exe.linkSystemLibrary("mpg123"); + exe.linkSystemLibrary("alsa"); 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 e69de29..aad554d 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -0,0 +1,50 @@ +const std = @import("std"); +const c = @cImport({ + @cInclude("mpg123.h"); + @cInclude("alsa/asoundlib.h"); +}); + +pub fn brr(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; + }; + defer c.mpg123_delete(handle); + + const file_path = file.ptr; + if (c.mpg123_open(handle, file_path) != 0) { + std.log.warn("Filed to open the file: {}\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; + } + defer c.snd_pcm_close(pcm); + + var params: *c.snd_pcm_hw_params_t = undefined; + c.snd_pcm_hw_params_malloc(¶ms); + defer c.snd_pcm_hw_params_free(params); + + c.snd_pcm_hw_params(pcm, params); + 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(pcm, params); + + var buffer: [4096]u8 = undefined; + + while (true) { + var done: c.size_t = 0; + c.mpg123_read(handle, &buffer[0], buffer.len, &done); + + if (done == 0) break; + + c.snd_pcm_writei(pcm, &buffer[0], done / 4); + } +} diff --git a/src/main.zig b/src/main.zig index 5d5f734..498b928 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); 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 commands = [_]struct { name: []const u8, @@ -48,7 +49,7 @@ pub fn main() !void { return; } - if (args.len == 2) { + if (args.len >= 2) { var argument = Arg{ .name = args[1], }; @@ -57,9 +58,8 @@ pub fn main() !void { .LS => try ls.ls(), .TCP => try socket_server.start_server(), .WINDOW => try sdl_window.present_sdl_window(), - else => { - return; - }, + // TODO: parse options for arguments + .BRR => try brr.brr(args[2]), } } |