summary refs log tree commit diff
path: root/src/modules
diff options
context:
space:
mode:
authorzlago2024-09-01 12:57:57 +0200
committerzlago2024-09-01 12:57:57 +0200
commit519e350739f55cfd3ef8b9a97e241b61ca9ed1f9 (patch)
treed3a1b251544ae5a3b85fe9f16c4a39fde1f46e78 /src/modules
parenteb0194ee748ab3707ade5318e8d336bbbed81594 (diff)
libopenmpt
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/openmpt.c72
-rw-r--r--src/modules/openmpt.h2
2 files changed, 74 insertions, 0 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);