-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.h
139 lines (106 loc) · 2.6 KB
/
music.h
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
135
136
137
138
139
#define USEMEMLOAD
//#define USEMEMLOADRESOURCE
#define USEFMOD TRUE
#ifdef USEFMOD
#include "minifmod.h"
#endif
// this is if you want to replace the samples with your own (in case you have compressed them)
void sampleloadcallback(void *buff, int lenbytes, int numbits, int instno, int sampno)
{
printf("pointer = %p length = %d bits = %d instrument %d sample %d\n", buff, lenbytes, numbits, instno, sampno);
}
#ifndef USEMEMLOAD
unsigned int fileopen(char *name)
{
return (unsigned int)fopen(name, "rb");
}
void fileclose(unsigned int handle)
{
fclose((FILE *)handle);
}
int fileread(void *buffer, int size, unsigned int handle)
{
return fread(buffer, 1, size, (FILE *)handle);
}
void fileseek(unsigned int handle, int pos, signed char mode)
{
fseek((FILE *)handle, pos, mode);
}
int filetell(unsigned int handle)
{
return ftell((FILE *)handle);
}
#else
typedef struct
{
int length;
int pos;
void *data;
} MEMFILE;
unsigned int memopen(char *name)
{
MEMFILE *memfile;
memfile = (MEMFILE *)calloc(sizeof(MEMFILE),1);
#ifndef USEMEMLOADRESOURCE
{ // load an external file and read it
FILE *fp;
fp = fopen(name, "rb");
if (fp)
{
fseek(fp, 0, SEEK_END);
memfile->length = ftell(fp);
memfile->data = calloc(memfile->length,1);
memfile->pos = 0;
fseek(fp, 0, SEEK_SET);
fread(memfile->data, 1, memfile->length, fp);
fclose(fp);
}
}
#else
{ // hey look some load from resource code!
HRSRC rec;
HGLOBAL handle;
rec = FindResource(NULL, name, RT_RCDATA);
handle = LoadResource(NULL, rec);
memfile->data = LockResource(handle);
memfile->length = SizeofResource(NULL, rec);
memfile->pos = 0;
}
#endif
return (unsigned int)memfile;
}
void memclose(unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;
#ifndef USEMEMLOADRESOURCE
free(memfile->data); // dont free it if it was initialized with LockResource
#endif
free(memfile);
}
int memread(void *buffer, int size, unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;
if (memfile->pos + size >= memfile->length)
size = memfile->length - memfile->pos;
memcpy(buffer, (char *)memfile->data+memfile->pos, size);
memfile->pos += size;
return size;
}
void memseek(unsigned int handle, int pos, signed char mode)
{
MEMFILE *memfile = (MEMFILE *)handle;
if (mode == SEEK_SET)
memfile->pos = pos;
else if (mode == SEEK_CUR)
memfile->pos += pos;
else if (mode == SEEK_END)
memfile->pos = memfile->length + pos;
if (memfile->pos > memfile->length)
memfile->pos = memfile->length;
}
int memtell(unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;
return memfile->pos;
}
#endif