diff options
-rw-r--r-- | build.zig | 3 | ||||
-rw-r--r-- | csrc/cbrr.c | 87 | ||||
-rw-r--r-- | csrc/cbrr.h | 1 | ||||
-rw-r--r-- | src/main.zig | 7 |
4 files changed, 97 insertions, 1 deletions
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 <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/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; } } |