commit 88245add6512f3b9a56d1da40a7dcba2fcd97fed
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 19 Sep 2022 19:53:46 +0200
Initial version
Diffstat:
6 files changed, 132 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,2 @@
+.cache
+build/mplay
diff --git a/.gitmodules b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "lib/minimp3"]
+ path = lib/minimp3
+ url = git@github.com:lieff/minimp3.git
diff --git a/Makefile b/Makefile
@@ -0,0 +1,19 @@
+CFLAGS = -g -I src -I lib/minimp3/
+LDLIBS =
+
+.PHONY: all clean
+
+all: build/mplay
+
+clean:
+ rm -rf build
+
+build:
+ mkdir build
+
+build/%.o: src/%.c src/%.h | build
+ $(CC) -o $@ -c $< $(CFLAGS) $(LDLIBS)
+
+build/mplay: src/main.c | build
+ $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS)
+
diff --git a/compile_commands.json b/compile_commands.json
@@ -0,0 +1,18 @@
+[
+ {
+ "arguments": [
+ "cc",
+ "-c",
+ "-g",
+ "-I",
+ "src",
+ "-I",
+ "lib/minimp3/",
+ "-o",
+ "build/mplay",
+ "src/main.c"
+ ],
+ "directory": "/snxdata/lib/dev/mplay",
+ "file": "src/main.c"
+ }
+]
+\ No newline at end of file
diff --git a/lib/minimp3 b/lib/minimp3
@@ -0,0 +1 @@
+Subproject commit afb604c06bc8beb145fecd42c0ceb5bda8795144
diff --git a/src/main.c b/src/main.c
@@ -0,0 +1,88 @@
+#define MINIMP3_IMPLEMENTATION
+#include "minimp3.h"
+#include "pulse/error.h"
+#include "pulse/simple.h"
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static uint8_t *read_file(const char *filename, size_t *len);
+static void play_file(const char *filename);
+static void daemon(void);
+
+uint8_t *
+read_file(const char *filename, size_t *len)
+{
+ FILE *f;
+ uint8_t *buf;
+ size_t start;
+
+ f = fopen(filename, "r");
+ if (f == NULL)
+ ERROR("Failed to open file\n");
+
+ fseek(f, 0, SEEK_END);
+ *len = ftell(f) - start;
+
+ buf = malloc(*len);
+ OOM_CHECK(buf);
+
+ fseek(f, 0, SEEK_SET);
+ if (fread(buf, 1, *len, f) != len)
+ ERROR("Failed to read file contents\n");
+
+ fclose(f);
+
+ return buf;
+}
+
+void
+play_file(const char *filename)
+{
+ static const pa_sample_spec spec = {
+ .format = PA_SAMPLE_S16LE,
+ .rate = 44100,
+ .channels = 2
+ };
+ mp3dec_frame_info_t info;
+ mp3d_sample_t sample;
+ mp3dec_t dec;
+ pa_simple *stream;
+ uint8_t *data;
+ size_t len;
+ int status, ret;
+
+ data = read_file(filename, &len);
+
+ mp3dec_init(&dec);
+
+ stream = pa_simple_new(NULL, "MPLAY", PA_STREAM_PLAYBACK, NULL,
+ "playback", &spec, NULL, NULL, &status);
+ // check status
+
+ while (mp3dec_decode_frame(&dec, data, len, &sample, &info)) {
+ pa_simple_write();
+ }
+
+ ret = pa_simple_drain(stream, &status);
+ if (!ret) ERROR("Failed to drain rest of audio\n");
+
+ pa_simple_free(stream);
+}
+
+void
+daemon(void)
+{
+
+}
+
+int
+main(int argc, const char **argv)
+{
+ if (argc != 2)
+ ERROR("USAGE: mplay FILE\n");
+
+ play_file(argv[1]);
+}