/
sloader.c
407 lines (313 loc) · 9.52 KB
/
sloader.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
--> Sample Loaders and Sample Processing
Name: sloader.c
Description:
Routines for loading samples. The sample loader utilizes the routines
provided by the "registered" sample loader. See SAMPLELOADER in
MIKMOD.H for the sample loader structure.
Portability:
All systems - all compilers
*/
#include "mikmod.h"
static int sl_rlength;
static SWORD sl_old;
static SWORD *sl_buffer = NULL;
static SAMPLOAD *musiclist = NULL,
*sndfxlist = NULL;
BOOL SL_Init(SAMPLOAD *s) /* returns 0 on error! */
{
if(sl_buffer==NULL)
if((sl_buffer=_mm_malloc(4100)) == NULL) return 0;
sl_rlength = s->length;
if(s->infmt & SF_16BITS) sl_rlength>>=1;
sl_old = 0;
return 1;
}
void SL_Exit(SAMPLOAD *s)
{
if(sl_rlength > 0) _mm_fseek(s->fp,sl_rlength,SEEK_CUR);
}
void SL_Reset(void)
{
sl_old = 0;
}
void SL_Load(void *buffer, SAMPLOAD *smp, int length)
{
UWORD infmt = smp->infmt, outfmt = smp->outfmt;
SBYTE *bptr = (SBYTE *)buffer;
SWORD *wptr = (SWORD *)buffer;
int stodo;
int t, u;
while(length)
{ stodo = (length<2048) ? length : 2048;
if(infmt & SF_16BITS)
{ if(infmt & SF_BIG_ENDIAN)
_mm_read_M_SWORDS(sl_buffer,stodo,smp->fp);
else
_mm_read_I_SWORDS(sl_buffer,stodo,smp->fp);
} else
{ SBYTE *s;
SWORD *d;
fread(sl_buffer,sizeof(SBYTE),stodo,smp->fp);
s = (SBYTE *)sl_buffer;
d = sl_buffer;
s += stodo;
d += stodo;
for(t=0; t<stodo; t++)
{ s--;
d--;
*d = (*s)<<8;
}
}
if(infmt & SF_DELTA)
{ for(t=0; t<stodo; t++)
{ sl_buffer[t] += sl_old;
sl_old = sl_buffer[t];
}
}
if((infmt^outfmt) & SF_SIGNED)
{ for(t=0; t<stodo; t++)
sl_buffer[t] ^= 0x8000;
}
if(smp->scalefactor)
{ int idx = 0;
SLONG scaleval;
/* Sample Scaling... average values for better results. */
t = 0;
while(t<stodo && length)
{ scaleval = 0;
for(u=smp->scalefactor; u && t<stodo; u--, t++)
scaleval += sl_buffer[t];
sl_buffer[idx++] = scaleval / (smp->scalefactor-u);
length--;
}
sl_rlength -= stodo;
stodo = idx;
} else
{ length -= stodo;
sl_rlength -= stodo;
}
if(outfmt & SF_16BITS)
{ for(t=0; t<stodo; t++) *(wptr++) = sl_buffer[t];
} else
{ for(t=0; t<stodo; t++) *(bptr++) = sl_buffer[t]>>8;
}
}
}
void SL_LoadStream(void *buffer, UWORD infmt, UWORD outfmt, int length, FILE *fp)
/* This is like SL_Load, but does not perform sample scaling, and does not */
/* require calls to SL_Init / SL_Exit. */
{
SBYTE *bptr = (SBYTE *)buffer;
SWORD *wptr = (SWORD *)buffer;
int stodo;
int t;
/* compute number of samples to load */
if(sl_buffer==NULL)
if((sl_buffer=_mm_malloc(4100)) == NULL) return;
while(length)
{ stodo = (length<2048) ? length : 2048;
if(infmt & SF_16BITS)
{ if(infmt & SF_BIG_ENDIAN)
_mm_read_M_SWORDS(sl_buffer,stodo,fp);
else
_mm_read_I_SWORDS(sl_buffer,stodo,fp);
} else
{ SBYTE *s;
SWORD *d;
fread(sl_buffer,sizeof(SBYTE),stodo,fp);
s = (SBYTE *)sl_buffer;
d = sl_buffer;
s += stodo;
d += stodo;
for(t=0; t<stodo; t++)
{ s--;
d--;
*d = (*s)<<8;
}
}
if(infmt & SF_DELTA)
{ for(t=0; t<stodo; t++)
{ sl_buffer[t] += sl_old;
sl_old = sl_buffer[t];
}
}
if((infmt^outfmt) & SF_SIGNED)
{ for(t=0; t<stodo; t++)
sl_buffer[t] ^= 0x8000;
}
length -= stodo;
if((infmt & SF_STEREO) && !(outfmt & SF_STEREO))
{ /* Dither stereo to mono .. average together every two samples */
SLONG avgval;
int idx = 0;
t = 0;
while(t<stodo && length)
{ avgval = sl_buffer[t++];
avgval += sl_buffer[t++];
sl_buffer[idx++] = avgval >> 1;
length-=2;
}
stodo = idx;
}
if(outfmt & SF_16BITS)
{ for(t=0; t<stodo; t++) *(wptr++) = sl_buffer[t];
} else
{ for(t=0; t<stodo; t++) *(bptr++) = sl_buffer[t]>>8;
}
}
}
SAMPLOAD *SL_RegisterSample(SAMPLE *s, int type, FILE *fp) /* Returns 1 on error! */
/* Registers a sample for loading when SL_LoadSamples() is called. */
/* type - type of sample to be loaded .. */
/* MD_MUSIC, MD_SNDFX */
{
SAMPLOAD *news, **samplist, *cruise;
if(type==MD_MUSIC)
{ samplist = & musiclist;
cruise = musiclist;
} else
{ samplist = &sndfxlist;
cruise = sndfxlist;
}
/* Allocate and add structure to the END of the list */
if((news=(SAMPLOAD *)_mm_calloc(1,sizeof(SAMPLOAD))) == NULL) return NULL;
if(cruise!=NULL)
{ while(cruise->next!=NULL) cruise = cruise->next;
cruise->next = news;
} else
*samplist = news;
news->infmt = s->flags & 31;
news->outfmt = news->infmt;
news->fp = fp;
news->sample = s;
news->length = s->length;
news->loopstart = s->loopstart;
news->loopend = s->loopend;
return news;
}
static void FreeSampleList(SAMPLOAD *s)
{
SAMPLOAD *old;
while(s!=NULL)
{ old = s;
s = s->next;
free(old);
}
}
static ULONG SampleTotal(SAMPLOAD *samplist, int type)
/* Returns the total amount of memory required by the samplelist queue. */
{
int total = 0;
while(samplist!=NULL)
{ samplist->sample->flags = (samplist->sample->flags&~31) | samplist->outfmt;
total += MD_SampleLength(type,samplist->sample);
samplist = samplist->next;
}
return total;
}
static ULONG RealSpeed(SAMPLOAD *s)
{
return(s->sample->speed / ((s->scalefactor==0) ? 1 : s->scalefactor));
}
static BOOL DitherSamples(SAMPLOAD *samplist, int type)
{
SAMPLOAD *c2smp;
ULONG maxsize, speed;
SAMPLOAD *s;
if(samplist==NULL) return 0;
/* make sure the samples will fit inside available RAM */
if((maxsize = MD_SampleSpace(type)*1024) != 0)
{ while(SampleTotal(samplist, type) > maxsize)
{ /* First Pass - check for any 16 bit samples */
s = samplist;
while(s!=NULL)
{ if(s->outfmt & SF_16BITS)
{ SL_Sample16to8(s);
break;
}
s = s->next;
}
/* Second pass (if no 16bits found above) is to take the sample */
/* with the highest speed and dither it by half. */
if(s==NULL)
{ s = samplist;
speed = 0;
while(s!=NULL)
{ if((s->sample->length) && (RealSpeed(s) > speed))
{ speed = RealSpeed(s);
c2smp = s;
}
s = s->next;
}
SL_HalveSample(c2smp);
}
}
}
/* Samples dithered, now load them! */
/* ================================ */
s = samplist;
while(s != NULL)
{ /* sample has to be loaded ? -> increase number of */
/* samples, allocate memory and load sample. */
if(s->sample->length)
{ if(s->sample->seekpos)
_mm_fseek(s->fp, s->sample->seekpos, SEEK_SET);
/* Call the sample load routine of the driver module. */
/* It has to return a 'handle' (>=0) that identifies */
/* the sample. */
s->sample->handle = MD_SampleLoad(s, type, s->fp);
s->sample->flags = (s->sample->flags & ~31) | s->outfmt;
if(s->sample->handle < 0)
{ FreeSampleList(samplist);
if(_mm_errorhandler!=NULL) _mm_errorhandler();
return 1;
}
}
s = s->next;
}
FreeSampleList(samplist);
return 0;
}
BOOL SL_LoadSamples(void) /* Returns 1 on error! */
{
BOOL ok;
_mm_critical = 0;
if((musiclist==NULL) && (sndfxlist==NULL)) return 0;
/* MikMod_Exit(); */
/* exit(1); */
ok = DitherSamples(musiclist,MD_MUSIC) || DitherSamples(sndfxlist,MD_SNDFX);
musiclist = sndfxlist = NULL;
return ok;
}
void SL_Sample16to8(SAMPLOAD *s)
{
s->outfmt &= ~SF_16BITS;
s->sample->flags = (s->sample->flags&~31) | s->outfmt;
}
void SL_Sample8to16(SAMPLOAD *s)
{
s->outfmt |= SF_16BITS;
s->sample->flags = (s->sample->flags&~31) | s->outfmt;
}
void SL_SampleSigned(SAMPLOAD *s)
{
s->outfmt |= SF_SIGNED;
s->sample->flags = (s->sample->flags&~31) | s->outfmt;
}
void SL_SampleUnsigned(SAMPLOAD *s)
{
s->outfmt &= ~SF_SIGNED;
s->sample->flags = (s->sample->flags&~31) | s->outfmt;
}
void SL_HalveSample(SAMPLOAD *s)
{
if(s->scalefactor)
s->scalefactor++;
else
s->scalefactor = 2;
s->sample->divfactor = s->scalefactor;
s->sample->length = s->length / s->scalefactor;
s->sample->loopstart = s->loopstart / s->scalefactor;
s->sample->loopend = s->loopend / s->scalefactor;
}