Skip to content

Latest commit

 

History

History
528 lines (438 loc) · 11 KB

mloader.c

File metadata and controls

528 lines (438 loc) · 11 KB
 
Dec 27, 1999
Dec 27, 1999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* MikMod sound library
(c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
complete list.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
Oct 21, 1999
Oct 21, 1999
20
Dec 27, 1999
Dec 27, 1999
21
/*==============================================================================
Oct 21, 1999
Oct 21, 1999
22
Dec 27, 1999
Dec 27, 1999
23
$Id$
Oct 21, 1999
Oct 21, 1999
24
Dec 27, 1999
Dec 27, 1999
25
These routines are used to access the available module loaders
Oct 21, 1999
Oct 21, 1999
26
Dec 27, 1999
Dec 27, 1999
27
==============================================================================*/
Oct 21, 1999
Oct 21, 1999
28
Dec 27, 1999
Dec 27, 1999
29
30
31
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Oct 21, 1999
Oct 21, 1999
32
Dec 27, 1999
Dec 27, 1999
33
34
35
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
Oct 21, 1999
Oct 21, 1999
36
Dec 27, 1999
Dec 27, 1999
37
#include "mikmod_internals.h"
Oct 21, 1999
Oct 21, 1999
38
Dec 27, 1999
Dec 27, 1999
39
#include <string.h>
Oct 21, 1999
Oct 21, 1999
40
Dec 27, 1999
Dec 27, 1999
41
42
MREADER *modreader;
MODULE of;
Oct 21, 1999
Oct 21, 1999
43
Dec 27, 1999
Dec 27, 1999
44
static MLOADER *firstloader=NULL;
Oct 21, 1999
Oct 21, 1999
45
Dec 27, 1999
Dec 27, 1999
46
47
48
49
UWORD finetune[16]={
8363,8413,8463,8529,8581,8651,8723,8757,
7895,7941,7985,8046,8107,8169,8232,8280
};
Oct 21, 1999
Oct 21, 1999
50
Dec 27, 1999
Dec 27, 1999
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
CHAR* MikMod_InfoLoader(void)
{
int len=0;
MLOADER *l;
CHAR *list=NULL;
MUTEX_LOCK(lists);
/* compute size of buffer */
for(l=firstloader;l;l=l->next) len+=1+(l->next?1:0)+strlen(l->version);
if(len)
if((list=_mm_malloc(len*sizeof(CHAR)))) {
list[0]=0;
/* list all registered module loders */
for(l=firstloader;l;l=l->next)
sprintf(list,(l->next)?"%s%s\n":"%s%s",list,l->version);
}
MUTEX_UNLOCK(lists);
return list;
Oct 21, 1999
Oct 21, 1999
70
71
}
Dec 27, 1999
Dec 27, 1999
72
void _mm_registerloader(MLOADER* ldr)
Oct 21, 1999
Oct 21, 1999
73
{
Dec 27, 1999
Dec 27, 1999
74
MLOADER *cruise=firstloader;
Oct 21, 1999
Oct 21, 1999
75
Dec 27, 1999
Dec 27, 1999
76
77
78
79
80
if(cruise) {
while(cruise->next) cruise = cruise->next;
cruise->next=ldr;
} else
firstloader=ldr;
Oct 21, 1999
Oct 21, 1999
81
82
}
Dec 27, 1999
Dec 27, 1999
83
84
85
86
87
88
89
90
91
92
93
void MikMod_RegisterLoader(struct MLOADER* ldr)
{
/* if we try to register an invalid loader, or an already registered loader,
ignore this attempt */
if ((!ldr)||(ldr->next))
return;
MUTEX_LOCK(lists);
_mm_registerloader(ldr);
MUTEX_UNLOCK(lists);
}
Oct 21, 1999
Oct 21, 1999
94
95
96
BOOL ReadComment(UWORD len)
{
Dec 27, 1999
Dec 27, 1999
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
if(len) {
int i;
if(!(of.comment=(CHAR*)_mm_malloc(len+1))) return 0;
_mm_read_UBYTES(of.comment,len,modreader);
/* translate IT linefeeds */
for(i=0;i<len;i++)
if(of.comment[i]=='\r') of.comment[i]='\n';
of.comment[len]=0; /* just in case */
}
if(!of.comment[0]) {
free(of.comment);
of.comment=NULL;
}
return 1;
Oct 21, 1999
Oct 21, 1999
114
115
}
Dec 27, 1999
Dec 27, 1999
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
BOOL ReadLinedComment(UWORD lines,UWORD linelen)
{
CHAR *tempcomment,*line,*storage;
UWORD total=0,t,len=lines*linelen;
int i;
if (lines) {
if(!(tempcomment=(CHAR*)_mm_malloc(len+1))) return 0;
if(!(storage=(CHAR*)_mm_malloc(linelen+1))) {
free(tempcomment);
return 0;
}
_mm_read_UBYTES(tempcomment,len,modreader);
/* compute message length */
for(line=tempcomment,total=t=0;t<lines;t++,line+=linelen) {
for(i=linelen;(i>=0)&&(line[i]==' ');i--) line[i]=0;
for(i=0;i<linelen;i++) if (!line[i]) break;
total+=1+i;
}
if(total>lines) {
if(!(of.comment=(CHAR*)_mm_malloc(total+1))) {
free(storage);
free(tempcomment);
return 0;
}
/* convert message */
for(line=tempcomment,t=0;t<lines;t++,line+=linelen) {
for(i=0;i<linelen;i++) if(!(storage[i]=line[i])) break;
storage[i]=0; /* if (i==linelen) */
strcat(of.comment,storage);strcat(of.comment,"\r");
}
free(storage);
free(tempcomment);
}
}
return 1;
}
Oct 21, 1999
Oct 21, 1999
156
157
158
BOOL AllocPositions(int total)
{
Dec 27, 1999
Dec 27, 1999
159
160
161
162
163
164
if(!total) {
_mm_errno=MMERR_NOT_A_MODULE;
return 0;
}
if(!(of.positions=_mm_calloc(total,sizeof(UWORD)))) return 0;
return 1;
Oct 21, 1999
Oct 21, 1999
165
166
167
168
}
BOOL AllocPatterns(void)
{
Dec 27, 1999
Dec 27, 1999
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
int s,t,tracks = 0;
if((!of.numpat)||(!of.numchn)) {
_mm_errno=MMERR_NOT_A_MODULE;
return 0;
}
/* Allocate track sequencing array */
if(!(of.patterns=(UWORD*)_mm_calloc((ULONG)(of.numpat+1)*of.numchn,sizeof(UWORD)))) return 0;
if(!(of.pattrows=(UWORD*)_mm_calloc(of.numpat+1,sizeof(UWORD)))) return 0;
for(t=0;t<=of.numpat;t++) {
of.pattrows[t]=64;
for(s=0;s<of.numchn;s++)
of.patterns[(t*of.numchn)+s]=tracks++;
}
return 1;
Oct 21, 1999
Oct 21, 1999
186
187
188
189
}
BOOL AllocTracks(void)
{
Dec 27, 1999
Dec 27, 1999
190
191
192
193
194
195
if(!of.numtrk) {
_mm_errno=MMERR_NOT_A_MODULE;
return 0;
}
if(!(of.tracks=(UBYTE **)_mm_calloc(of.numtrk,sizeof(UBYTE *)))) return 0;
return 1;
Oct 21, 1999
Oct 21, 1999
196
197
198
199
}
BOOL AllocInstruments(void)
{
Dec 27, 1999
Dec 27, 1999
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
int t,n;
if(!of.numins) {
_mm_errno=MMERR_NOT_A_MODULE;
return 0;
}
if(!(of.instruments=(INSTRUMENT*)_mm_calloc(of.numins,sizeof(INSTRUMENT))))
return 0;
for(t=0;t<of.numins;t++) {
for(n=0;n<INSTNOTES;n++) {
/* Init note / sample lookup table */
of.instruments[t].samplenote[n] = n;
of.instruments[t].samplenumber[n] = t;
}
of.instruments[t].globvol = 64;
}
return 1;
Oct 21, 1999
Oct 21, 1999
218
219
220
221
}
BOOL AllocSamples(void)
{
Dec 27, 1999
Dec 27, 1999
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
UWORD u;
if(!of.numsmp) {
_mm_errno=MMERR_NOT_A_MODULE;
return 0;
}
if(!(of.samples=(SAMPLE*)_mm_calloc(of.numsmp,sizeof(SAMPLE)))) return 0;
for(u=0;u<of.numsmp;u++) {
of.samples[u].panning = 128; /* center */
of.samples[u].handle = -1;
of.samples[u].globvol = 64;
of.samples[u].volume = 64;
}
return 1;
Oct 21, 1999
Oct 21, 1999
237
238
}
Dec 27, 1999
Dec 27, 1999
239
static BOOL ML_LoadSamples(void)
Oct 21, 1999
Oct 21, 1999
240
{
Dec 27, 1999
Dec 27, 1999
241
242
SAMPLE *s;
int u;
Oct 21, 1999
Oct 21, 1999
243
Dec 27, 1999
Dec 27, 1999
244
245
for(u=of.numsmp,s=of.samples;u;u--,s++)
if(s->length) SL_RegisterSample(s,MD_MUSIC,modreader);
Oct 21, 1999
Oct 21, 1999
246
Dec 27, 1999
Dec 27, 1999
247
return 1;
Oct 21, 1999
Oct 21, 1999
248
249
}
Dec 27, 1999
Dec 27, 1999
250
251
252
/* Creates a CSTR out of a character buffer of 'len' bytes, but strips any
terminating non-printing characters like 0, spaces etc. */
CHAR *DupStr(CHAR* s,UWORD len,BOOL strict)
Oct 21, 1999
Oct 21, 1999
253
{
Dec 27, 1999
Dec 27, 1999
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
UWORD t;
CHAR *d=NULL;
/* Scan for last printing char in buffer [includes high ascii up to 254] */
while(len) {
if(s[len-1]>0x20) break;
len--;
}
/* Scan forward for possible NULL character */
if(strict) {
for(t=0;t<len;t++) if (!s[t]) break;
if (t<len) len=t;
}
/* When the buffer wasn't completely empty, allocate a cstring and copy the
buffer into that string, except for any control-chars */
if((d=(CHAR*)_mm_malloc(sizeof(CHAR)*(len+1)))) {
for(t=0;t<len;t++) d[t]=(s[t]<32)?'.':s[t];
d[len]=0;
}
return d;
Oct 21, 1999
Oct 21, 1999
276
277
278
279
}
static void ML_XFreeSample(SAMPLE *s)
{
Dec 27, 1999
Dec 27, 1999
280
281
282
if(s->handle>=0)
MD_SampleUnload(s->handle);
if(s->samplename) free(s->samplename);
Oct 21, 1999
Oct 21, 1999
283
284
285
286
}
static void ML_XFreeInstrument(INSTRUMENT *i)
{
Dec 27, 1999
Dec 27, 1999
287
if(i->insname) free(i->insname);
Oct 21, 1999
Oct 21, 1999
288
289
}
Dec 27, 1999
Dec 27, 1999
290
static void ML_FreeEx(MODULE *mf)
Oct 21, 1999
Oct 21, 1999
291
{
Dec 27, 1999
Dec 27, 1999
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
UWORD t;
if(mf->songname) free(mf->songname);
if(mf->comment) free(mf->comment);
if(mf->modtype) free(mf->modtype);
if(mf->positions) free(mf->positions);
if(mf->patterns) free(mf->patterns);
if(mf->pattrows) free(mf->pattrows);
if(mf->tracks) {
for(t=0;t<mf->numtrk;t++)
if(mf->tracks[t]) free(mf->tracks[t]);
free(mf->tracks);
}
if(mf->instruments) {
for(t=0;t<mf->numins;t++)
ML_XFreeInstrument(&mf->instruments[t]);
free(mf->instruments);
}
if(mf->samples) {
for(t=0;t<mf->numsmp;t++)
if(mf->samples[t].length) ML_XFreeSample(&mf->samples[t]);
free(mf->samples);
}
memset(mf,0,sizeof(MODULE));
if(mf!=&of) free(mf);
Oct 21, 1999
Oct 21, 1999
319
320
}
Dec 27, 1999
Dec 27, 1999
321
static MODULE *ML_AllocUniMod(void)
Oct 21, 1999
Oct 21, 1999
322
{
Dec 27, 1999
Dec 27, 1999
323
MODULE *mf;
Oct 21, 1999
Oct 21, 1999
324
Dec 27, 1999
Dec 27, 1999
325
326
return (mf=_mm_malloc(sizeof(MODULE)));
}
Oct 21, 1999
Oct 21, 1999
327
Dec 27, 1999
Dec 27, 1999
328
void Player_Free_internal(MODULE *mf)
Oct 21, 1999
Oct 21, 1999
329
{
Dec 27, 1999
Dec 27, 1999
330
331
332
333
if(mf) {
Player_Exit_internal(mf);
ML_FreeEx(mf);
}
Oct 21, 1999
Oct 21, 1999
334
335
}
Dec 27, 1999
Dec 27, 1999
336
void Player_Free(MODULE *mf)
Oct 21, 1999
Oct 21, 1999
337
{
Dec 27, 1999
Dec 27, 1999
338
339
340
341
MUTEX_LOCK(vars);
Player_Free_internal(mf);
MUTEX_UNLOCK(vars);
}
Oct 21, 1999
Oct 21, 1999
342
Dec 27, 1999
Dec 27, 1999
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
static CHAR* Player_LoadTitle_internal(MREADER *reader)
{
MLOADER *l;
modreader=reader;
_mm_errno = 0;
_mm_critical = 0;
_mm_iobase_setcur(modreader);
/* Try to find a loader that recognizes the module */
for(l=firstloader;l;l=l->next) {
_mm_rewind(modreader);
if(l->Test()) break;
}
if(!l) {
_mm_errno = MMERR_NOT_A_MODULE;
if(_mm_errorhandler) _mm_errorhandler();
return NULL;
}
return l->LoadTitle();
}
Oct 21, 1999
Oct 21, 1999
366
Dec 27, 1999
Dec 27, 1999
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
CHAR* Player_LoadTitle(CHAR* filename)
{
CHAR* result=NULL;
FILE* fp;
MREADER* reader;
if((fp=_mm_fopen(filename,"rb"))) {
if((reader=_mm_new_file_reader(fp))) {
MUTEX_LOCK(lists);
result=Player_LoadTitle_internal(reader);
MUTEX_UNLOCK(lists);
_mm_delete_file_reader(reader);
}
fclose(fp);
}
return result;
}
Oct 21, 1999
Oct 21, 1999
384
Dec 27, 1999
Dec 27, 1999
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* Loads a module given an reader */
MODULE* Player_LoadGeneric_internal(MREADER *reader,int maxchan,BOOL curious)
{
int t;
MLOADER *l;
BOOL ok;
MODULE *mf;
modreader = reader;
_mm_errno = 0;
_mm_critical = 0;
_mm_iobase_setcur(modreader);
/* Try to find a loader that recognizes the module */
for(l=firstloader;l;l=l->next) {
_mm_rewind(modreader);
if(l->Test()) break;
}
if(!l) {
_mm_errno = MMERR_NOT_A_MODULE;
if(_mm_errorhandler) _mm_errorhandler();
_mm_rewind(modreader);_mm_iobase_revert();
return NULL;
}
/* init unitrk routines */
if(!UniInit()) {
if(_mm_errorhandler) _mm_errorhandler();
_mm_rewind(modreader);_mm_iobase_revert();
return NULL;
}
/* load the song using the song's loader variable */
memset(&of,0,sizeof(MODULE));
of.initvolume = 128;
/* init panning array */
for(t=0; t<64; t++) of.panning[t] = ((t+1)&2) ? 255 : 0;
for(t=0; t<64; t++) of.chanvol[t] = 64;
/* init module loader and load the header / patterns */
if(l->Init()) {
_mm_rewind(modreader);
ok = l->Load(curious);
} else
ok = 0;
/* free loader and unitrk allocations */
l->Cleanup();
UniCleanup();
if(!ok) {
ML_FreeEx(&of);
if(_mm_errorhandler) _mm_errorhandler();
_mm_rewind(modreader);_mm_iobase_revert();
return NULL;
}
if(!ML_LoadSamples()) {
ML_FreeEx(&of);
if(_mm_errorhandler) _mm_errorhandler();
_mm_rewind(modreader);_mm_iobase_revert();
return NULL;
}
if(!(mf=ML_AllocUniMod())) {
ML_FreeEx(&of);
_mm_rewind(modreader);_mm_iobase_revert();
if(_mm_errorhandler) _mm_errorhandler();
return NULL;
}
/* Copy the static MODULE contents into the dynamic MODULE struct. */
memcpy(mf,&of,sizeof(MODULE));
_mm_iobase_revert();
if(maxchan>0) {
if(!(mf->flags&UF_NNA)&&(mf->numchn<maxchan))
maxchan = mf->numchn;
else
if((mf->numvoices)&&(mf->numvoices<maxchan))
maxchan = mf->numvoices;
if(maxchan<mf->numchn) mf->flags |= UF_NNA;
if(MikMod_SetNumVoices_internal(maxchan,-1)) {
Player_Free(mf);
return NULL;
}
}
if(SL_LoadSamples()) {
Player_Free_internal(mf);
return NULL;
}
if(Player_Init(mf)) {
Player_Free_internal(mf);
mf=NULL;
}
return mf;
Oct 21, 1999
Oct 21, 1999
485
486
}
Dec 27, 1999
Dec 27, 1999
487
488
489
MODULE* Player_LoadGeneric(MREADER *reader,int maxchan,BOOL curious)
{
MODULE* result;
Oct 21, 1999
Oct 21, 1999
490
Dec 27, 1999
Dec 27, 1999
491
492
493
494
495
MUTEX_LOCK(vars);
MUTEX_LOCK(lists);
result=Player_LoadGeneric_internal(reader,maxchan,curious);
MUTEX_UNLOCK(lists);
MUTEX_UNLOCK(vars);
Oct 21, 1999
Oct 21, 1999
496
Dec 27, 1999
Dec 27, 1999
497
498
return result;
}
Oct 21, 1999
Oct 21, 1999
499
Dec 27, 1999
Dec 27, 1999
500
501
502
/* Loads a module given a file pointer.
File is loaded from the current file seek position. */
MODULE* Player_LoadFP(FILE* fp,int maxchan,BOOL curious)
Oct 21, 1999
Oct 21, 1999
503
{
Dec 27, 1999
Dec 27, 1999
504
505
506
507
508
509
510
511
MODULE* result=NULL;
struct MREADER* reader=_mm_new_file_reader (fp);
if (reader) {
result=Player_LoadGeneric(reader,maxchan,curious);
_mm_delete_file_reader(reader);
}
return result;
Oct 21, 1999
Oct 21, 1999
512
513
}
Dec 27, 1999
Dec 27, 1999
514
515
516
/* Open a module via its filename. The loader will initialize the specified
song-player 'player'. */
MODULE* Player_Load(CHAR* filename,int maxchan,BOOL curious)
Oct 21, 1999
Oct 21, 1999
517
{
Dec 27, 1999
Dec 27, 1999
518
519
520
521
522
523
524
525
FILE *fp;
MODULE *mf=NULL;
if((fp=_mm_fopen(filename,"rb"))) {
mf=Player_LoadFP(fp,maxchan,curious);
fclose(fp);
}
return mf;
Oct 21, 1999
Oct 21, 1999
526
527
}
Dec 27, 1999
Dec 27, 1999
528
/* ex:set ts=4: */