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
289
290
|
// MODplay for the PS1
// Music Module Player
// Supports ProTracker (.mod) module format
// Requires libADPCM!
// If NO_PSX_LIB is defined, no parts using PSXSDK functions are compiled
// This is useful if you want to use the library to handle module files in tools
#ifndef NO_PSX_LIB
#include <psx.h>
#endif
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#ifndef NO_PSX_LIB
#include <adpcm.h>
#endif
#include "modplay.h"
#include "modplay_int.h"
// Configuration defines
// Size of ADPCM buffer used by MODUploadSamples when
// converting 8-bit unsigned PCM samples to PS1 ADPCM format
// By default it is set to 0x4000, 16 kilobytes
#define ADPCM_BUFFER_SIZE 0x4000
int modplay_base_voice = 0;
int modplay_max_vol = 0x3fff;
int modplay_chan_vols[8];
int modplay_int_cnt = 0;
int modplay_samples_off[32];
int modplay_chan_mask = 0;
int modplay_is_mono = 0;
unsigned char modplay_adpcm_buffer[ADPCM_BUFFER_SIZE];
unsigned int modload_flags = 0;
ModMusic *MODLoad(void *d)
{
return MODLoadEx(d, 0);
}
ModMusic *MODLoadEx(void *d, unsigned int flags)
{
modload_flags = flags;
// If the module file was in no other format, assume the module file is
// in ProTracker format. There's no real way to detect a ProTracker module
// file 100% correctly so this will do.
return MODLoad_MOD(d);
}
void MODUnload(ModMusic *m)
{
int x;
MODStop(m);
switch(m->fmt)
{
case MOD_FMT_MOD:
free(m->pattern_data);
for(x = 0; x < m->sample_num; x++)
{
if(m->sample[x].data != NULL)
free(m->sample[x].data);
}
free(m->sample);
free(m);
break;
}
}
#ifdef NO_PSX_LIB
void MODPlay_func(ModMusic *m, int c, int s, int p, int vl, int vr)
{
// Just a stub
}
#else
void MODPlay_func(ModMusic *m, int c, int s, int p, int vl, int vr)
{
int v = c + modplay_base_voice;
// static int mask = 0;
// if(s != -1)
// {
// SsKeyOff(v);
if(p != -1)
SsVoicePitch(v, p);
// }
if(modplay_max_vol != 0x3fff)
{
vl = (modplay_max_vol * vl) / 0x4000;
vr = (modplay_max_vol * vr) / 0x4000;
vl&=0x3fff;
vr&=0x3fff;
}
if(modplay_is_mono)
{
if(vl>vr)
vr=vl;
else
vl=vr;
}
SsVoiceVol(v, vl, vr);
if(s != -1)
{
if(modplay_samples_off[s] != -1)
{
SsVoiceStartAddr(v, modplay_samples_off[s]);
modplay_chan_mask|=(1<<v);
}
}
}
#endif
void MODPlay(ModMusic *m, int *t)
{
modplay_chan_mask = 0;
switch(m->fmt)
{
case MOD_FMT_MOD:
MODPlay_MOD(m, t);
break;
}
//printf("modplay_chan_mask = %d\n", modplay_chan_mask);
#ifndef NO_PSX_LIB
SsKeyOnMask(modplay_chan_mask);
#endif
}
void MODStop(ModMusic *m)
{
#ifndef NO_PSX_LIB
int mask = 0;
int x;
for(x = 0; x<m->channel_num; x++)
mask|=1<<(modplay_base_voice+x);
SsKeyOffMask(mask);
#endif
}
#ifndef NO_PSX_LIB
int MODUploadSamples(ModMusic *m, int base_addr)
{
int x, b;
if(base_addr == -1)
base_addr = SPU_DATA_BASE_ADDR;
modplay_samples_off[0] = base_addr;
for(x = 0; x < m->sample_num; x++)
{
b = SsAdpcmPack(m->sample[x].data, modplay_adpcm_buffer,
m->sample[x].length, FMT_U8, sizeof(modplay_adpcm_buffer), 0);
SsUpload(modplay_adpcm_buffer, b, modplay_samples_off[x]);
if(x!=30)
modplay_samples_off[x+1] = modplay_samples_off[x]+b;
}
return modplay_samples_off[x];
}
int MOD4PSX_Upload(void *d, int base_addr)
{
unsigned char *c = d;
int x;
int o;
int sz;
int n;
int smpOff;
// Check magic string
if(strncmp((char*)c, "_mod4psx", 8) != 0)
return -1;
o = 12;
n = *((int*)(c+8));
if(base_addr == -1)
smpOff = SPU_DATA_BASE_ADDR;
else
smpOff = base_addr;
//smpOff = modplay_samples_off[0];
printf("Number of samples: %d\n", n);
for(x = 0; x < n; x++)
{
// Get size
sz = *((int*)(c+o));
printf("Size: %d\n", sz);
// Ignore eight reserved bytes (for future expension)
o+=12;
if(sz > 0)
{
modplay_samples_off[x] = smpOff;
SsUpload(c+o, sz, modplay_samples_off[x]);
smpOff+=sz;
}
else
modplay_samples_off[x] = -1;
o += sz;
}
return modplay_samples_off[x];
}
#endif
void MODSetBaseVoice(int base_voice)
{
modplay_base_voice = base_voice;
}
void MODSetMaxVolume(unsigned short max_volume)
{
// Default is 0x3fff.
// Valid values 0 (MUTE) - 0x3FFF (MAX)
modplay_max_vol = max_volume & 0x3fff;
}
void MODRewind(ModMusic *m)
{
MODStop(m);
m->song_pos = 0;
m->pat_pos = 0;
if(m->fmt == MOD_FMT_MOD)
{
m->divisions_sec = 7;
m->beats_minute = 125;
m->ticks_division = 6;
}
}
void MODSetTranspose(ModMusic *m, short transpose)
{
m->transpose = transpose;
}
void MODSetMono(int value)
{
/* Sets mono audio mode
left volume = right volume */
modplay_is_mono = value;
}
#ifdef NO_PSX_LIB
// Some code might use this, so use a stub.
unsigned short SsFreqToPitch(int hz)
{
return 0;
}
#endif
|