summary refs log tree commit diff
path: root/src/modules/openmpt.c
blob: 027519f7498f0d9fd7b3e762f4cbd49a9fa0bf6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include "../include.h"

#include <libopenmpt/libopenmpt.h>

static void openmpt_callback(void *mod, unsigned char *stream, int const length) {
	openmpt_module_read_interleaved_float_stereo(mod, SAMPLE_RATE, 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, 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);
	userdata->callback = openmpt_callback;
	userdata->user = mod;
	userdata->freefunc = (void (*)(void *)) openmpt_module_destroy;
	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 );
	}
}