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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
//
// Audio Overload
// Emulated music player
//
// (C) 2000-2008 Richard F. Bannister
//
//
// eng_dsf.c
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ao.h"
#include "eng_protos.h"
#include "corlett.h"
#include "dc_hw.h"
#include "cpuintrf.h"
#include "aica.h"
#include "aicadsp.h"
#define DEBUG_LOADER (1)
#define DK_CORE (1)
#if DK_CORE
#include "arm7.h"
#else
#include "arm7core.h"
#endif
typedef struct {
corlett_t *c;
char psfby[256];
uint32 decaybegin, decayend, total_samples;
struct sARM7 *cpu;
uint8 init_dc_ram[8*1024*1024];
} dsf_synth_t;
void *dsf_start(const char *path, uint8 *buffer, uint32 length)
{
dsf_synth_t *s = malloc (sizeof (dsf_synth_t));
memset (s, 0, sizeof (dsf_synth_t));
uint8 *file, *lib_decoded, *lib_raw_file;
uint32 offset, plength, lengthMS, fadeMS;
uint64 file_len, lib_len, lib_raw_length;
corlett_t *lib;
char *libfile;
int i;
// Decode the current SSF
if (corlett_decode(buffer, length, &file, &file_len, &s->c) != AO_SUCCESS)
{
dsf_stop (s);
return NULL;
}
#if DEBUG_LOADER
printf("%d bytes decoded\n", file_len);
#endif
s->cpu = ARM7_Alloc ();
// Get the library file, if any
for (i=0; i<9; i++) {
libfile = i ? s->c->libaux[i-1] : s->c->lib;
if (libfile[0] != 0)
{
uint64 tmp_length;
char libpath[PATH_MAX];
const char *e = path + strlen(path);
while (e > path && *e != '/') {
e--;
}
if (*e == '/') {
e++;
memcpy (libpath, path, e-path);
libpath[e-path] = 0;
strcat (libpath, libfile);
}
else {
strcpy (libpath, libfile);
}
#if DEBUG_LOADER
printf("Loading library: %s\n", libpath);
#endif
if (ao_get_lib(libpath, &lib_raw_file, &tmp_length) != AO_SUCCESS)
{
dsf_stop (s);
return NULL;
}
lib_raw_length = tmp_length;
if (corlett_decode(lib_raw_file, lib_raw_length, &lib_decoded, &lib_len, &lib) != AO_SUCCESS)
{
free(lib_raw_file);
dsf_stop (s);
return NULL;
}
// Free up raw file
free(lib_raw_file);
// patch the file into ram
offset = lib_decoded[0] | lib_decoded[1]<<8 | lib_decoded[2]<<16 | lib_decoded[3]<<24;
memcpy(&s->cpu->dc_ram[offset], lib_decoded+4, lib_len-4);
// Dispose the corlett structure for the lib - we don't use it
free(lib);
}
}
// now patch the file into RAM over the libraries
offset = file[3]<<24 | file[2]<<16 | file[1]<<8 | file[0];
memcpy(&s->cpu->dc_ram[offset], file+4, file_len-4);
free(file);
// Finally, set psfby/ssfby tag
strcpy(s->psfby, "n/a");
if (s->c)
{
for (i = 0; i < MAX_UNKNOWN_TAGS; i++)
{
if ((!strcasecmp(s->c->tag_name[i], "psfby")) || (!strcasecmp(s->c->tag_name[i], "ssfby")))
strcpy(s->psfby, s->c->tag_data[i]);
}
}
#if DEBUG_LOADER && 0
{
FILE *f;
f = fopen("dcram.bin", "wb");
fwrite(s->cpu->dc_ram, 2*1024*1024, 1, f);
fclose(f);
}
#endif
memcpy (s->init_dc_ram, s->cpu->dc_ram, sizeof (s->init_dc_ram));
#if DK_CORE
ARM7_Init(s->cpu);
#else
arm7_init(cpu, 0, 45000000, NULL, NULL);
arm7_reset(s->cpu);
#endif
dc_hw_init(s->cpu);
// now figure out the time in samples for the length/fade
lengthMS = psfTimeToMS(s->c->inf_length);
fadeMS = psfTimeToMS(s->c->inf_fade);
s->total_samples = 0;
if (lengthMS == 0)
{
lengthMS = ~0;
}
if (lengthMS == ~0)
{
s->decaybegin = lengthMS;
}
else
{
lengthMS = (lengthMS * 441) / 10;
fadeMS = (fadeMS * 441) / 10;
s->decaybegin = lengthMS;
s->decayend = lengthMS + fadeMS;
}
return s;
}
int32 dsf_gen(void *handle, int16 *buffer, uint32 samples)
{
dsf_synth_t *s = handle;
int i;
int16 output[44100/30], output2[44100/30];
int16 *stereo[2];
int16 *outp = buffer;
int opos;
opos = 0;
for (i = 0; i < samples; i++)
{
#if DK_CORE
ARM7_Execute(s->cpu, (33000000 / 60 / 4) / 735);
#else
arm7_execute(s->cpu, (33000000 / 60 / 4) / 735);
#endif
stereo[0] = &output[opos];
stereo[1] = &output2[opos];
AICA_Update(s->cpu->AICA, NULL, NULL, stereo, 1);
opos++;
}
for (i = 0; i < samples; i++)
{
// process the fade tags
if (s->total_samples >= s->decaybegin)
{
if (s->total_samples >= s->decayend)
{
// song is done here, signal your player appropriately!
// ao_song_done = 1;
output[i] = 0;
output2[i] = 0;
}
else
{
int32 fader = 256 - (256*(s->total_samples - s->decaybegin)/(s->decayend-s->decaybegin));
output[i] = (output[i] * fader)>>8;
output2[i] = (output2[i] * fader)>>8;
s->total_samples++;
}
}
else
{
s->total_samples++;
}
*outp++ = output[i];
*outp++ = output2[i];
}
return AO_SUCCESS;
}
int32 dsf_stop(void *handle)
{
return AO_SUCCESS;
}
int32 dsf_command(void *handle, int32 command, int32 parameter)
{
dsf_synth_t *s = handle;
switch (command)
{
case COMMAND_RESTART:
dc_hw_free (s->cpu);
memcpy (s->cpu->dc_ram, s->init_dc_ram, sizeof (s->init_dc_ram));
ARM7_Init(s->cpu);
dc_hw_init (s->cpu);
s->total_samples = 0;
return AO_SUCCESS;
}
return AO_FAIL;
}
int32 dsf_fill_info(void *handle, ao_display_info *info)
{
dsf_synth_t *s = handle;
if (s->c == NULL)
return AO_FAIL;
strcpy(info->title[1], "Name: ");
sprintf(info->info[1], "%s", s->c->inf_title);
strcpy(info->title[2], "Game: ");
sprintf(info->info[2], "%s", s->c->inf_game);
strcpy(info->title[3], "Artist: ");
sprintf(info->info[3], "%s", s->c->inf_artist);
strcpy(info->title[4], "Copyright: ");
sprintf(info->info[4], "%s", s->c->inf_copy);
strcpy(info->title[5], "Year: ");
sprintf(info->info[5], "%s", s->c->inf_year);
strcpy(info->title[6], "Length: ");
sprintf(info->info[6], "%s", s->c->inf_length);
strcpy(info->title[7], "Fade: ");
sprintf(info->info[7], "%s", s->c->inf_fade);
strcpy(info->title[8], "Ripper: ");
sprintf(info->info[8], "%s", s->psfby);
return AO_SUCCESS;
}
|