diff options
Diffstat (limited to '')
-rw-r--r-- | build.zig | 5 | ||||
-rw-r--r-- | csrc/cbrr.c | 87 | ||||
-rw-r--r-- | csrc/cbrr.h | 1 | ||||
-rw-r--r-- | src/brr.zig | 66 | ||||
-rw-r--r-- | src/main.zig | 7 | ||||
-rw-r--r-- | static/badum.mp3 | bin | 0 -> 61793 bytes |
6 files changed, 139 insertions, 27 deletions
diff --git a/build.zig b/build.zig index def28a5..5a00841 100644 --- a/build.zig +++ b/build.zig @@ -26,8 +26,11 @@ 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(); + 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 <alsa/asoundlib.h> +#include <mpg123.h> +#include <stdio.h> +#include <stdlib.h> + +#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/brr.zig b/src/brr.zig index 74ee419..bbacdd7 100644 --- a/src/brr.zig +++ b/src/brr.zig @@ -4,17 +4,23 @@ const c = @cImport({ @cInclude("alsa/asoundlib.h"); }); -pub fn brr(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}); + if (c.mpg123_open(handle, file) != c.MPG123_OK) { + std.log.warn("Failed to open the file: {s}\n", .{file}); + 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; } @@ -24,37 +30,47 @@ pub fn brr(file: []const u8) !void { return; } + var dir: c_int = 0; 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; + } - _ = 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_any(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_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; + } - _ = c.snd_pcm_hw_params(pcm, params); + const buffer_size = c.mpg123_outblock(handle); + var done: usize = 0; - var buffer: [4096]u8 = undefined; + var mpg123_buffer: []u8 = try allocator.alloc(u8, buffer_size); + _ = &mpg123_buffer; + defer allocator.free(mpg123_buffer); + var result: c_int = 0; while (true) { - var done: usize = 0; - const result = c.mpg123_read(handle, &buffer[0], buffer.len, &done); + result = c.mpg123_read(handle, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), buffer_size, &done); switch (result) { c.MPG123_OK => { - std.log.info("Reading successfule", .{}); + _ = c.snd_pcm_writei(pcm, @as(?*anyopaque, @ptrCast(mpg123_buffer.ptr)), done / 4); }, else => { - std.log.err("Decode error {}", .{result}); + const err_str = c.mpg123_strerror(handle); + std.log.warn("Failed to read from file\n {}, {s}", .{ result, err_str }); + break; }, } - - 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); } + + _ = c.mpg123_delete(handle); + _ = c.snd_pcm_hw_params_free(params); + _ = c.snd_pcm_close(pcm); + _ = c.mpg123_exit(); } diff --git a/src/main.zig b/src/main.zig index 5609376..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(args[2]); + brr_c(args[2]); return; } } diff --git a/static/badum.mp3 b/static/badum.mp3 new file mode 100644 index 0000000..30a9202 --- /dev/null +++ b/static/badum.mp3 Binary files differ |