summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/include.h3
-rw-r--r--src/modules/openmpt.c8
-rw-r--r--src/modules/openmpt.h2
-rw-r--r--src/sdl.c12
4 files changed, 15 insertions, 10 deletions
diff --git a/src/include.h b/src/include.h
index a8d48bf..389aaf2 100644
--- a/src/include.h
+++ b/src/include.h
@@ -1,4 +1,5 @@
 #pragma once
+#include <stddef.h>
 
 struct blob {
 	void *data;
@@ -6,7 +7,7 @@ struct blob {
 };
 
 struct userdata {
-	SDL_AudioCallback callback;
+	void (*callback)(void *, unsigned char *, int);
 	void *user;
 	void (*freefunc)(void *);
 };
diff --git a/src/modules/openmpt.c b/src/modules/openmpt.c
index ee63731..58c3994 100644
--- a/src/modules/openmpt.c
+++ b/src/modules/openmpt.c
@@ -1,9 +1,9 @@
-#include <SDL2/SDL.h>
+#include <stdio.h>
 #include "../include.h"
 
 #include <libopenmpt/libopenmpt.h>
 
-void openmpt_callback(void *mod, unsigned char *stream, int const length) {
+static void openmpt_callback(void *mod, unsigned char *stream, int const length) {
 	openmpt_module_read_interleaved_float_stereo(mod, 48000, length / sizeof (float) / 2, (float *) stream);
 }
 
@@ -11,7 +11,7 @@ static void libopenmpt_example_logfunc( const char * message, void * userdata );
 static int libopenmpt_example_errfunc( int error, void * userdata );
 static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str );
 
-int module_openmpt(struct blob *file, SDL_AudioDeviceID audio, struct userdata *userdata) {
+int module_openmpt(struct blob *file, struct userdata *userdata) {
 	int mod_err = OPENMPT_ERROR_OK;
 	const char *mod_err_str = NULL;
 	openmpt_module *mod = openmpt_module_create_from_memory2(file->data, file->size, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL);
@@ -21,11 +21,9 @@ int module_openmpt(struct blob *file, SDL_AudioDeviceID audio, struct userdata *
 		return 1;
 	}
 	openmpt_module_set_repeat_count(mod, -1);
-	SDL_LockAudioDevice(audio);
 	userdata->callback = openmpt_callback;
 	userdata->user = mod;
 	userdata->freefunc = (void (*)(void *)) openmpt_module_destroy;
-	SDL_UnlockAudioDevice(audio);
 	return 0;
 }
 
diff --git a/src/modules/openmpt.h b/src/modules/openmpt.h
deleted file mode 100644
index 3daa7cd..0000000
--- a/src/modules/openmpt.h
+++ /dev/null
@@ -1,2 +0,0 @@
-#include "../include.h"
-int module_openmpt(struct blob *file, SDL_AudioDeviceID audio, struct userdata *userdata);
diff --git a/src/sdl.c b/src/sdl.c
index e780906..875cde5 100644
--- a/src/sdl.c
+++ b/src/sdl.c
@@ -5,7 +5,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#include "modules/openmpt.h"
+#include "include.h"
+
+int module_openmpt(struct blob *file, struct userdata *userdata);
 
 #define eprintf(...) fprintf(stderr, __VA_ARGS__)
 
@@ -15,7 +17,6 @@ SDL_Window *window = NULL;
 
 struct blob load_file(char const *const name);
 
-#include "include.h"
 struct userdata userdata = {
 	.callback = NULL,
 	.user = NULL,
@@ -107,10 +108,12 @@ int main(void) {
 					if (module_openmpt(&file, audio, &newuser)) {
 						// error
 					} else {
+						SDL_LockAudioDevice(audio);
 						if (userdata.freefunc != NULL) {
 							userdata.freefunc(userdata.user);
 						}
 						userdata = newuser;
+						SDL_UnlockAudioDevice(audio);
 					}
 					free(file.data);
 					break;
@@ -120,6 +123,11 @@ int main(void) {
 		}
 	}
 	done:
+	if (userdata.freefunc != NULL) {
+		SDL_LockAudioDevice(audio);
+		userdata.freefunc(userdata.user);
+		SDL_UnlockAudioDevice(audio);
+	}
 	
 	SDL_DestroyRenderer(renderer);