about summary refs log tree commit diff
path: root/src/brr.zig
blob: 3a0c94ca1da0b7a00d154d69d282ea0ad9405d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
    };

    const file_path = file.ptr;
    if (c.mpg123_open(handle, file_path) != 0) {
        std.log.warn("Filed 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 params: ?*c.snd_pcm_hw_params_t = null;
    _ = c.snd_pcm_hw_params_malloc(&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: usize = 0;
        _ = c.mpg123_read(handle, &buffer[0], buffer.len, &done);

        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);
    }
}