/
modplayer.c
135 lines (111 loc) · 2.5 KB
/
modplayer.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* modplayer.c
* zerobutanmod
*
* Created by Bryce Kerley on 8/6/05.
* Copyright 2005 __MyCompanyName__. All rights reserved.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <libmikmod-coreaudio/mikmod.h>
#include "modplayer.h"
#include "umx.h"
MODULE *module;
int playerReady = 0;
int initPlayer() {
if (playerReady == 1) return 0;
MikMod_RegisterAllDrivers();
MikMod_RegisterAllLoaders();
md_mode |= DMODE_16BITS |
DMODE_HQMIXER |
DMODE_SURROUND |
DMODE_INTERP |
DMODE_FLOAT;
if(MikMod_Init("")) {
fprintf(stderr, "Error: %s\n",
MikMod_strerror(MikMod_errno)
);
return -1;
}
playerReady = 1;
return 0;
}
trackinfo playFD(int fd, unsigned long long insize) {
BOOL handleUMX;
FILE * temp;
FILE * in;
int ret;
unsigned long i;
trackinfo info = {
NULL, NULL, NULL
};
in = fdopen(fd, "rb");
handleUMX = checkUMX(in);
switch(handleUMX) {
case UMXCHK_E_GENERR :
fprintf(stderr,"Warning: A non-error non-eof read problem occurred.\n");
return info;
case UMXCHK_E_NOREAD :
fprintf(stderr,"Error: Couldn't read file for UMX signature checking.\n");
return info;
case UMXCHK_ISUMX :
rewind(in);
/* make a temporary filehandle */
temp = tmpfile();
switch (CopyData(in, temp, insize)) {
case CUMX_E_MEM:
fprintf(stderr, "Error: Not enough memory for UMX buffer\n");
fclose(in); fclose(temp);
return info;
case CUMX_E_NOMOD:
fprintf(stderr, "Error: No module data in this Unreal package\n");
fclose(in); fclose(temp);
return info;
case CUMX_E_NORW:
fprintf(stderr, "Error: Couldn't read from UMX or write to tmpfile\n");
fclose(in); fclose(temp);
return info;
}
printf("done!\n");
in = temp;
break;
default:
break;
}
rewind(in);
ret = startPlay(in);
info.comment = module->comment;
info.modtype = module->modtype;
info.trackname = module->songname;
info.numIns = module->numsmp;
info.insNames = calloc(info.numIns, sizeof(char*));
for (i = 0; i < info.numIns; i++) {
info.insNames[i] = module->samples[i].samplename;
}
fclose(in);
return info;
}
int startPlay(FILE* in) {
module = Player_LoadFP(in,256,4);
usleep(100);
Player_Start(module);
if (NOLOOP == 1) {
module->wrap = 0;
module->loop = 0;
}
return 0;
}
int endPlay() {
Player_Stop();
Player_Free(module);
module = NULL;
MikMod_Reset(NULL);
return 0;
}
void feedPlayer() {
while(Player_Active()) {
usleep(10000);
MikMod_Update();
}
}