summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/modules/openmpt.c72
-rw-r--r--src/modules/openmpt.h2
-rw-r--r--src/sdl.c14
3 files changed, 84 insertions, 4 deletions
diff --git a/src/modules/openmpt.c b/src/modules/openmpt.c
new file mode 100644
index 0000000..ee63731
--- /dev/null
+++ b/src/modules/openmpt.c
@@ -0,0 +1,72 @@
+#include <SDL2/SDL.h>
+#include "../include.h"
+
+#include <libopenmpt/libopenmpt.h>
+
+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);
+}
+
+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 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);
+	if (mod == NULL) {
+		libopenmpt_example_print_error("openmpt_module_create_from_memory2()", mod_err, mod_err_str);
+		openmpt_free_string(mod_err_str);
+		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;
+}
+
+// copy pasted
+static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
+	(void)userdata;
+	if ( message ) {
+		fprintf( stderr, "openmpt: %s\n", message );
+	}
+}
+ 
+static int libopenmpt_example_errfunc( int error, void * userdata ) {
+	(void)userdata;
+	(void)error;
+	return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG;
+}
+ 
+static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) {
+	if ( !func_name ) {
+		func_name = "unknown function";
+	}
+	if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) {
+		mod_err_str = openmpt_error_string( mod_err );
+		if ( !mod_err_str ) {
+			fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" );
+		} else {
+			fprintf( stderr, "Error: %s\n", mod_err_str );
+			openmpt_free_string( mod_err_str );
+			mod_err_str = NULL;
+		}
+	} else {
+		if ( !mod_err_str ) {
+			mod_err_str = openmpt_error_string( mod_err );
+			if ( !mod_err_str ) {
+				fprintf( stderr, "Error: %s failed.\n", func_name );
+			} else {
+				fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
+			}
+			openmpt_free_string( mod_err_str );
+			mod_err_str = NULL;
+		}
+		fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
+	}
+}
diff --git a/src/modules/openmpt.h b/src/modules/openmpt.h
new file mode 100644
index 0000000..3daa7cd
--- /dev/null
+++ b/src/modules/openmpt.h
@@ -0,0 +1,2 @@
+#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 d99fc34..e780906 100644
--- a/src/sdl.c
+++ b/src/sdl.c
@@ -5,7 +5,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#include <libopenmpt/libopenmpt.h>
+#include "modules/openmpt.h"
 
 #define eprintf(...) fprintf(stderr, __VA_ARGS__)
 
@@ -103,9 +103,15 @@ int main(void) {
 						break;
 					}
 					SDL_free(evt.drop.file);
-					//SDL_LockAudioDevice(audio);
-					//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);
-					//SDL_UnlockAudioDevice(audio);
+					struct userdata newuser = {.callback = NULL, .freefunc = NULL};
+					if (module_openmpt(&file, audio, &newuser)) {
+						// error
+					} else {
+						if (userdata.freefunc != NULL) {
+							userdata.freefunc(userdata.user);
+						}
+						userdata = newuser;
+					}
 					free(file.data);
 					break;