diff options
author | zlago | 2024-09-01 18:14:38 +0200 |
---|---|---|
committer | zlago | 2024-09-01 18:14:38 +0200 |
commit | 90b967d9d1b5970969f758c7993d244936fff324 (patch) | |
tree | 275c8b45d9dd7a1e6022d4b3929dd1f064f8caaa | |
parent | 2eab12e43c040d369b685efc13e2452405cf6451 (diff) |
libfluidsynth
-rw-r--r-- | GNUmakefile | 2 | ||||
-rw-r--r-- | src/modules/fluidsynth.c | 57 | ||||
-rw-r--r-- | src/sdl.c | 19 |
3 files changed, 75 insertions, 3 deletions
diff --git a/GNUmakefile b/GNUmakefile index 6f110de..58990fd 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,7 +3,7 @@ GLAD ?= glad MKDIR ?= mkdir -p -libs ::= SDL2 openmpt +libs ::= SDL2 openmpt fluidsynth cflags ::= -I . -g -Og ${CFLAGS} ldflags ::= -Wl,--rpath,'$$ORIGIN' $(addprefix -l,${libs}) ${LDFLAGS} diff --git a/src/modules/fluidsynth.c b/src/modules/fluidsynth.c new file mode 100644 index 0000000..fd3c874 --- /dev/null +++ b/src/modules/fluidsynth.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <stdlib.h> +#include "../include.h" + +#include <fluidsynth.h> + +struct fluidsynth_userdata { + fluid_settings_t *settings; + fluid_synth_t *synth; + fluid_player_t *player; + int soundfont; +}; + + +static void fluidsynth_callback(void *userdata, unsigned char *stream, int const length) { + struct fluidsynth_userdata *fs = userdata; + fluid_synth_write_float(fs->synth, length / sizeof (float) / 2, stream, 0, 2, stream, 1, 2); +} + +static void fluidsynth_free(void *ptr) { + struct fluidsynth_userdata *fs = ptr; + fluid_player_stop(fs->player); + delete_fluid_player(fs->player); + fluid_player_play(fs->player); + fluid_synth_sfunload(fs->synth, fs->soundfont, 0); + delete_fluid_synth(fs->synth); + delete_fluid_settings(fs->settings); + free(fs); +} + +int module_fluidsynth(struct blob *file, struct userdata *userdata) { + struct fluidsynth_userdata *fs = malloc(sizeof (struct fluidsynth_userdata)); + if (fs == NULL) { + return 1; + } + fs->settings = new_fluid_settings(); // wasteful, but when trying to 'fix' it it just caused more errors + fluid_settings_setnum(fs->settings, "synth.gain", 0.5); + fluid_settings_setint(fs->settings, "player.reset-synth", 0); + fluid_settings_setnum(fs->settings, "synth.sample-rate", 48000); + fs->synth = new_fluid_synth(fs->settings); + char *soundfont_path = getenv("SOUNDFONT"); + if (soundfont_path == NULL) { + soundfont_path = "/usr/share/sounds/sf2/default-GM.sf2"; + } + fs->soundfont = fluid_synth_sfload(fs->synth, soundfont_path, 1); // ugly hack + fs->player = new_fluid_player(fs->synth); + fluid_player_set_loop(fs->player, -1); + if (fluid_player_add_mem(fs->player, file->data, file->size) == FLUID_FAILED) { + fluidsynth_free(fs); + return 1; + } + fluid_player_play(fs->player); + userdata->callback = fluidsynth_callback; + userdata->user = fs; + userdata->freefunc = fluidsynth_free; + return 0; +} diff --git a/src/sdl.c b/src/sdl.c index 875cde5..8deb8ca 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -8,6 +8,7 @@ #include "include.h" int module_openmpt(struct blob *file, struct userdata *userdata); +int module_fluidsynth(struct blob *file, struct userdata *userdata); #define eprintf(...) fprintf(stderr, __VA_ARGS__) @@ -103,9 +104,22 @@ int main(void) { SDL_free(evt.drop.file); break; } - SDL_free(evt.drop.file); struct userdata newuser = {.callback = NULL, .freefunc = NULL}; - if (module_openmpt(&file, audio, &newuser)) { + int (*module_func)(struct blob *, struct userdata *) = NULL; + size_t len = strlen(evt.drop.file); + #define ext(extension) memcmp(evt.drop.file + len - sizeof (extension) + 1, extension, sizeof (extension)) + if ((ext(".mptm") && ext(".mod") && ext(".xm") && ext(".s3m") && ext(".it")) == 0) { + module_func = module_openmpt; + } else if ((ext(".mid") && ext(".midi")) == 0) { + module_func = module_fluidsynth; + } + if (module_func == NULL) { + eprintf("%s: unrecognized file extension\n", evt.drop.file); + free(file.data); + break; + } + SDL_free(evt.drop.file); + if (module_func(&file, &newuser)) { // error } else { SDL_LockAudioDevice(audio); @@ -126,6 +140,7 @@ int main(void) { if (userdata.freefunc != NULL) { SDL_LockAudioDevice(audio); userdata.freefunc(userdata.user); + SDL_PauseAudioDevice(audio, 1); SDL_UnlockAudioDevice(audio); } |