-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin-vio2sf.c
232 lines (200 loc) · 5.4 KB
/
plugin-vio2sf.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "plugin-api.h"
#include "vio2sf/vio2sf.h"
#define CONTAINER_STRING "PSF Song Archive"
#define CONTAINER_STRING_SIZE 16
#define INDEX_RECORD_SIZE 12
#define NDS_VOICE_COUNT 16
typedef struct
{
uint8_t *dataBuffer;
int dataBufferSize;
int trackCount;
int currentTrack;
int initialized;
} twosfContext;
/* obviously not thread-safe */
static twosfContext *currentTwosfContext;
static const char *channelStrings[NDS_VOICE_COUNT] = {
"ch 01",
"ch 02",
"ch 03",
"ch 04",
"ch 05",
"ch 06",
"ch 07",
"ch 08",
"ch 09",
"ch 10",
"ch 11",
"ch 12",
"ch 13",
"ch 14",
"ch 15",
"ch 16"
};
int xsf_get_lib(char *pfilename, void **ppbuffer, unsigned int *plength)
{
unsigned int offset;
int i;
int found = 0;
unsigned int nameOffset;
char *nameRecord;
unsigned int filePtrOffset = 0;
unsigned int fileSize = 0;
unsigned char *data = currentTwosfContext->dataBuffer;
unsigned char *dataCopy = NULL;
offset = 20;
for (i = 0; i < currentTwosfContext->trackCount; i++)
{
nameOffset =
(data[offset + 8] << 24) |
(data[offset + 9] << 16) |
(data[offset + 10] << 8) |
(data[offset + 11] << 0);
nameRecord = (char*)&data[nameOffset];
/* should be a binary search, ideally */
if (strncasecmp(nameRecord, pfilename, strlen(pfilename)) == 0)
{
found = 1;
filePtrOffset =
(data[offset + 0] << 24) |
(data[offset + 1] << 16) |
(data[offset + 2] << 8) |
(data[offset + 3] << 0);
fileSize =
(data[offset + 4] << 24) |
(data[offset + 5] << 16) |
(data[offset + 6] << 8) |
(data[offset + 7] << 0);
break;
}
offset += INDEX_RECORD_SIZE;
}
if (found)
{
dataCopy = (unsigned char*)malloc(fileSize);
memcpy(dataCopy, &data[filePtrOffset], fileSize);
}
*ppbuffer = dataCopy;
*plength = fileSize;
return found;
}
static int TwosfInitPlugin(void *privateData, uint8_t *data, int size)
{
twosfContext *cxt = (twosfContext*)privateData;
cxt->dataBuffer = data;
cxt->dataBufferSize = size;
cxt->trackCount = 0;
cxt->currentTrack = 0;
/* check for special container format */
if (cxt->dataBufferSize < CONTAINER_STRING_SIZE ||
strncmp((char*)cxt->dataBuffer, CONTAINER_STRING, CONTAINER_STRING_SIZE) == 1)
cxt->initialized = 0;
else
{
cxt->initialized = 1;
cxt->trackCount =
(cxt->dataBuffer[16] << 24) | (cxt->dataBuffer[17] << 16) |
(cxt->dataBuffer[18] << 8) | (cxt->dataBuffer[19]);
}
return cxt->initialized;
}
static int TwosfStartTrack(void *privateData, int trackNumber)
{
twosfContext *cxt = (twosfContext*)privateData;
unsigned int offset;
unsigned int fileIndex;
unsigned char *filePtr;
unsigned int fileSize;
unsigned char *dataCopy = NULL;
if (trackNumber == -1)
trackNumber = cxt->currentTrack;
offset = 20 + (trackNumber * INDEX_RECORD_SIZE);
fileIndex =
(cxt->dataBuffer[offset + 0] << 24) |
(cxt->dataBuffer[offset + 1] << 16) |
(cxt->dataBuffer[offset + 2] << 8) |
(cxt->dataBuffer[offset + 3] << 0);
filePtr = &cxt->dataBuffer[fileIndex];
offset += 4;
fileSize =
(cxt->dataBuffer[offset + 0] << 24) |
(cxt->dataBuffer[offset + 1] << 16) |
(cxt->dataBuffer[offset + 2] << 8) |
(cxt->dataBuffer[offset + 3] << 0);
dataCopy = (unsigned char*)malloc(fileSize);
if (dataCopy)
{
memcpy(dataCopy, filePtr, fileSize);
currentTwosfContext = cxt;
return xsf_start(dataCopy, fileSize);
}
else
return 0;
}
static int TwosfGenerateStereoFrames(void *privateData, int16_t *samples, int frameCount)
{
int status;
status = xsf_gen(samples, frameCount);
return (status == XSF_TRUE);
}
static int TwosfGetTrackCount(void *privateData)
{
twosfContext *cxt = (twosfContext*)privateData;
return cxt->trackCount;
}
static int TwosfGetCurrentTrack(void *privateData)
{
twosfContext *cxt = (twosfContext*)privateData;
return cxt->currentTrack;
}
static int TwosfNextTrack(void *privateData)
{
twosfContext *cxt = (twosfContext*)privateData;
cxt->currentTrack = (cxt->currentTrack + 1) % cxt->trackCount;
return cxt->currentTrack;
}
static int TwosfPreviousTrack(void *privateData)
{
twosfContext *cxt = (twosfContext*)privateData;
cxt->currentTrack--;
if (cxt->currentTrack < 0)
cxt->currentTrack = cxt->trackCount - 1;
return cxt->currentTrack;
}
static int TwosfGetVoiceCount(void *privateData)
{
return 1;
}
static const char* TwosfGetVoiceName(void *privateData, int voiceNumber)
{
return "Vio2sf Engine";
}
static int TwosfVoicesCanBeToggled(void *privateData)
{
/* do not allow individual voices to be toggled; the voice model doesn't
* really support it properly */
return 0;
}
static int TwosfSetVoiceState(void *privateData, int voice, int enabled)
{
return 0;
}
pluginInfo pluginVio2sf =
{
.initPlugin = TwosfInitPlugin,
.startTrack = TwosfStartTrack,
.generateStereoFrames = TwosfGenerateStereoFrames,
.getTrackCount = TwosfGetTrackCount,
.getCurrentTrack = TwosfGetCurrentTrack,
.nextTrack = TwosfNextTrack,
.previousTrack = TwosfPreviousTrack,
.getVoiceCount = TwosfGetVoiceCount,
.getVoiceName = TwosfGetVoiceName,
.voicesCanBeToggled = TwosfVoicesCanBeToggled,
.setVoiceState = TwosfSetVoiceState,
.contextSize = sizeof(twosfContext)
};