Skip to content

Latest commit

 

History

History
382 lines (319 loc) · 10.1 KB

load_mod.c

File metadata and controls

382 lines (319 loc) · 10.1 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
Generic MOD loader (Protracker, StarTracker, FastTracker, etc)
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
33
34
#include <string.h>
Dec 27, 1999
Dec 27, 1999
35
#include "mikmod_internals.h"
Oct 21, 1999
Oct 21, 1999
36
Dec 27, 1999
Dec 27, 1999
37
/*========== Module structure */
Oct 21, 1999
Oct 21, 1999
38
Dec 27, 1999
Dec 27, 1999
39
40
41
42
43
44
45
typedef struct MSAMPINFO {
CHAR samplename[23]; /* 22 in module, 23 in memory */
UWORD length;
UBYTE finetune;
UBYTE volume;
UWORD reppos;
UWORD replen;
Oct 21, 1999
Oct 21, 1999
46
47
} MSAMPINFO;
Dec 27, 1999
Dec 27, 1999
48
49
50
51
52
53
54
typedef struct MODULEHEADER {
CHAR songname[21]; /* the songname.. 20 in module, 21 in memory */
MSAMPINFO samples[31]; /* all sampleinfo */
UBYTE songlength; /* number of patterns used */
UBYTE magic1; /* should be 127 */
UBYTE positions[128]; /* which pattern to play at pos */
UBYTE magic2[4]; /* string "M.K." or "FLT4" or "FLT8" */
Oct 21, 1999
Oct 21, 1999
55
56
} MODULEHEADER;
Dec 27, 1999
Dec 27, 1999
57
58
59
60
typedef struct MODTYPE {
CHAR id[5];
UBYTE channels;
CHAR *name;
Oct 21, 1999
Oct 21, 1999
61
62
} MODTYPE;
Dec 27, 1999
Dec 27, 1999
63
64
typedef struct MODNOTE {
UBYTE a,b,c,d;
Oct 21, 1999
Oct 21, 1999
65
66
} MODNOTE;
Dec 27, 1999
Dec 27, 1999
67
68
69
/*========== Loader variables */
#define MODULEHEADERSIZE 1084
Oct 21, 1999
Oct 21, 1999
70
Dec 27, 1999
Dec 27, 1999
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
static CHAR protracker[] = "Protracker";
static CHAR startracker[] = "Startracker";
static CHAR fasttracker[] = "Fasttracker";
static CHAR ins15tracker[] = "15-instrument";
static CHAR oktalyzer[] = "Oktalyzer";
static CHAR taketracker[] = "TakeTracker";
static CHAR orpheus[] = "Imago Orpheus (MOD format)";
#define MODTYPE_COUNT 24
static MODTYPE modtypes[MODTYPE_COUNT+1]={
{"M.K.", 4, protracker}, /* protracker 4 channel */
{"M!K!", 4, protracker}, /* protracker 4 channel */
{"FLT4", 4, startracker}, /* startracker 4 channel */
{"2CHN", 2, fasttracker}, /* fasttracker 2 channel */
{"4CHN", 4, fasttracker}, /* fasttracker 4 channel */
{"6CHN", 6, fasttracker}, /* fasttracker 6 channel */
{"8CHN", 8, fasttracker}, /* fasttracker 8 channel */
{"10CH",10, fasttracker}, /* fasttracker 10 channel */
{"12CH",12, fasttracker}, /* fasttracker 12 channel */
{"14CH",14, fasttracker}, /* fasttracker 14 channel */
{"15CH",15, fasttracker}, /* fasttracker 15 channel */
{"16CH",16, fasttracker}, /* fasttracker 16 channel */
{"18CH",18, fasttracker}, /* fasttracker 18 channel */
{"20CH",20, fasttracker}, /* fasttracker 20 channel */
{"22CH",22, fasttracker}, /* fasttracker 22 channel */
{"24CH",24, fasttracker}, /* fasttracker 24 channel */
{"26CH",26, fasttracker}, /* fasttracker 26 channel */
{"28CH",28, fasttracker}, /* fasttracker 28 channel */
{"30CH",30, fasttracker}, /* fasttracker 30 channel */
{"32CH",32, fasttracker}, /* fasttracker 32 channel */
{"CD81", 8, oktalyzer}, /* atari oktalyzer 8 channel */
{"OKTA", 8, oktalyzer}, /* atari oktalyzer 8 channel */
{"16CN",16, taketracker}, /* taketracker 16 channel */
{"32CN",32, taketracker}, /* taketracker 32 channel */
{" ", 4,ins15tracker} /* 15-instrument 4 channel */
Oct 21, 1999
Oct 21, 1999
106
107
};
Dec 27, 1999
Dec 27, 1999
108
static MODULEHEADER *mh = NULL;
Oct 21, 1999
Oct 21, 1999
109
110
111
static MODNOTE *patbuf = NULL;
static int modtype = 0;
Dec 27, 1999
Dec 27, 1999
112
/*========== Loader code */
Oct 21, 1999
Oct 21, 1999
113
Dec 27, 1999
Dec 27, 1999
114
115
116
static BOOL MOD_Test(void)
{
UBYTE id[4];
Oct 21, 1999
Oct 21, 1999
117
Dec 27, 1999
Dec 27, 1999
118
119
_mm_fseek(modreader,MODULEHEADERSIZE-4,SEEK_SET);
if(!_mm_read_UBYTES(id,4,modreader)) return 0;
Oct 21, 1999
Oct 21, 1999
120
Dec 27, 1999
Dec 27, 1999
121
122
123
/* find out which ID string */
for(modtype=0;modtype<MODTYPE_COUNT;modtype++)
if(!memcmp(id,modtypes[modtype].id,4)) return 1;
Oct 21, 1999
Oct 21, 1999
124
Dec 27, 1999
Dec 27, 1999
125
return 0;
Oct 21, 1999
Oct 21, 1999
126
127
}
Dec 27, 1999
Dec 27, 1999
128
static BOOL MOD_Init(void)
Oct 21, 1999
Oct 21, 1999
129
{
Dec 27, 1999
Dec 27, 1999
130
131
if( !(mh=(MODULEHEADER*)_mm_malloc(sizeof(MODULEHEADER)))) return 0;
return 1;
Oct 21, 1999
Oct 21, 1999
132
133
}
Dec 27, 1999
Dec 27, 1999
134
static void MOD_Cleanup(void)
Oct 21, 1999
Oct 21, 1999
135
{
Dec 27, 1999
Dec 27, 1999
136
137
_mm_free(mh);
_mm_free(patbuf);
Oct 21, 1999
Oct 21, 1999
138
139
140
141
142
}
/*
Old (amiga) noteinfo:
Dec 27, 1999
Dec 27, 1999
143
_____byte 1_____ byte2_ _____byte 3_____ byte4_
Oct 21, 1999
Oct 21, 1999
144
145
146
147
148
149
150
151
152
/ \ / \ / \ / \
0000 0000-00000000 0000 0000-00000000
Upper four 12 bits for Lower four Effect command.
bits of sam- note period. bits of sam-
ple number. ple number.
*/
Dec 27, 1999
Dec 27, 1999
153
static void ConvertNote(MODNOTE* n)
Oct 21, 1999
Oct 21, 1999
154
{
Dec 27, 1999
Dec 27, 1999
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
UBYTE instrument,effect,effdat,note;
UWORD period;
UBYTE lastnote=0;
/* extract the various information from the 4 bytes that make up a note */
instrument = (n->a&0x10)|(n->c>>4);
period = (((UWORD)n->a&0xf)<<8)+n->b;
effect = n->c&0xf;
effdat = n->d;
/* Convert the period to a note number */
note=0;
if(period) {
for(note=0;note<7*OCTAVE;note++)
if(period>=npertab[note]) break;
if(note==7*OCTAVE) note=0;
else note++;
}
if(instrument) {
/* if instrument does not exist, note cut */
if((instrument>31)||(!mh->samples[instrument-1].length)) {
UniPTEffect(0xc,0);
if(effect==0xc) effect=effdat=0;
} else {
/* Protracker handling */
if(modtype<=2) {
/* if we had a note, then change instrument... */
if(note)
UniInstrument(instrument-1);
/* ...otherwise, only adjust volume... */
else {
/* ...unless an effect was specified, which forces a new
note to be played */
if(effect||effdat) {
UniInstrument(instrument-1);
note=lastnote;
} else
UniPTEffect(0xc,mh->samples[instrument-1].volume&0x7f);
}
} else {
/* Fasttracker handling */
UniInstrument(instrument-1);
if(!note) note=lastnote;
}
}
}
if(note) {
UniNote(note+2*OCTAVE-1);
lastnote=note;
}
/* Convert pattern jump from Dec to Hex */
if(effect==0xd)
effdat=(((effdat&0xf0)>>4)*10)+(effdat&0xf);
/* Volume slide, up has priority */
if((effect==0xa)&&(effdat&0xf)&&(effdat&0xf0))
effdat&=0xf0;
UniPTEffect(effect,effdat);
Oct 21, 1999
Oct 21, 1999
216
217
}
Dec 27, 1999
Dec 27, 1999
218
static UBYTE* ConvertTrack(MODNOTE* n)
Oct 21, 1999
Oct 21, 1999
219
{
Dec 27, 1999
Dec 27, 1999
220
221
222
223
224
225
226
227
228
int t;
UniReset();
for(t=0;t<64;t++) {
ConvertNote(n);
UniNewline();
n+=of.numchn;
}
return UniDup();
Oct 21, 1999
Oct 21, 1999
229
230
}
Dec 27, 1999
Dec 27, 1999
231
232
/* Loads all patterns of a modfile and converts them into the 3 byte format. */
static BOOL ML_LoadPatterns(void)
Oct 21, 1999
Oct 21, 1999
233
{
Dec 27, 1999
Dec 27, 1999
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
int t,s,tracks = 0;
if(!AllocPatterns()) return 0;
if(!AllocTracks()) return 0;
/* Allocate temporary buffer for loading and converting the patterns */
if(!(patbuf=(MODNOTE*)_mm_calloc(64U*of.numchn,sizeof(MODNOTE)))) return 0;
for(t=0;t<of.numpat;t++) {
/* Load the pattern into the temp buffer and convert it */
for(s=0;s<(64U*of.numchn);s++) {
patbuf[s].a=_mm_read_UBYTE(modreader);
patbuf[s].b=_mm_read_UBYTE(modreader);
patbuf[s].c=_mm_read_UBYTE(modreader);
patbuf[s].d=_mm_read_UBYTE(modreader);
}
for(s=0;s<of.numchn;s++)
if(!(of.tracks[tracks++]=ConvertTrack(patbuf+s))) return 0;
}
return 1;
Oct 21, 1999
Oct 21, 1999
254
255
}
Dec 27, 1999
Dec 27, 1999
256
static BOOL MOD_Load(BOOL curious)
Oct 21, 1999
Oct 21, 1999
257
{
Dec 27, 1999
Dec 27, 1999
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
int t,scan;
SAMPLE *q;
MSAMPINFO *s;
BOOL is_orpheus=0;
/* try to read module header */
_mm_read_string((CHAR*)mh->songname,20,modreader);
mh->songname[20]=0; /* just in case */
for(t=0;t<31;t++) {
s=&mh->samples[t];
_mm_read_string(s->samplename,22,modreader);
s->samplename[22]=0; /* just in case */
s->length =_mm_read_M_UWORD(modreader);
s->finetune =_mm_read_UBYTE(modreader);
s->volume =_mm_read_UBYTE(modreader);
s->reppos =_mm_read_M_UWORD(modreader);
s->replen =_mm_read_M_UWORD(modreader);
}
mh->songlength =_mm_read_UBYTE(modreader);
mh->magic1 =_mm_read_UBYTE(modreader);
_mm_read_UBYTES(mh->positions,128,modreader);
_mm_read_UBYTES(mh->magic2,4,modreader);
if(_mm_eof(modreader)) {
_mm_errno = MMERR_LOADING_HEADER;
return 0;
}
/* set module variables */
of.initspeed = 6;
of.inittempo = 125;
of.numchn = modtypes[modtype].channels;
of.songname = DupStr(mh->songname,21,1);
of.numpos = mh->songlength;
of.reppos = 0;
/* Count the number of patterns */
of.numpat = 0;
for(t=0;t<of.numpos;t++)
if(mh->positions[t]>of.numpat)
of.numpat=mh->positions[t];
/* since some old modules embed extra patterns, we have to check the
whole list to get the samples' file offsets right - however we can find
garbage here, so check carefully */
scan=1;
for(t=of.numpos;t<128;t++)
if(mh->positions[t]>=0x80) scan=0;
if (scan)
for(t=of.numpos;t<128;t++) {
if(mh->positions[t]>of.numpat)
of.numpat=mh->positions[t];
if((curious)&&(mh->positions[t])) of.numpos=t+1;
}
of.numpat++;
of.numtrk = of.numpat*of.numchn;
if(!AllocPositions(of.numpos)) return 0;
for(t=0;t<of.numpos;t++)
of.positions[t]=mh->positions[t];
/* Finally, init the sampleinfo structures */
of.numins=of.numsmp=31;
if(!AllocSamples()) return 0;
s = mh->samples;
q = of.samples;
for(t=0;t<of.numins;t++) {
/* convert the samplename */
q->samplename = DupStr(s->samplename,23,1);
/* init the sampleinfo variables and convert the size pointers */
q->speed = finetune[s->finetune & 0xf];
q->volume = s->volume&0x7f;
q->loopstart = (ULONG)s->reppos << 1;
q->loopend = q->loopstart + ((ULONG)s->replen << 1);
q->length = (ULONG)s->length << 1;
q->flags = SF_SIGNED;
/* Imago Orpheus creates MODs with 16 bit samples, check */
if((modtypes[modtype].name==fasttracker)&&(s->volume&0x80)) {
q->flags|=SF_16BITS;
is_orpheus=1;
}
if(s->replen>1) q->flags |= SF_LOOP;
/* fix replen if repend > length */
if(q->loopend>q->length) q->loopend=q->length;
s++;
q++;
}
if(is_orpheus)
of.modtype=strdup(orpheus);
else
of.modtype=strdup(modtypes[modtype].name);
if(!ML_LoadPatterns()) return 0;
return 1;
Oct 21, 1999
Oct 21, 1999
356
357
}
Dec 27, 1999
Dec 27, 1999
358
static CHAR *MOD_LoadTitle(void)
Oct 21, 1999
Oct 21, 1999
359
{
Dec 27, 1999
Dec 27, 1999
360
CHAR s[21];
Oct 21, 1999
Oct 21, 1999
361
Dec 27, 1999
Dec 27, 1999
362
363
364
_mm_fseek(modreader,0,SEEK_SET);
if(!_mm_read_UBYTES(s,20,modreader)) return NULL;
s[20]=0; /* just in case */
Oct 21, 1999
Oct 21, 1999
365
Dec 27, 1999
Dec 27, 1999
366
367
return(DupStr(s,21,1));
}
Oct 21, 1999
Oct 21, 1999
368
Dec 27, 1999
Dec 27, 1999
369
370
371
372
373
374
375
376
377
378
379
/*========== Loader information */
MLOADER load_mod={
NULL,
"Standard module",
"MOD (31 instrument)",
MOD_Init,
MOD_Test,
MOD_Load,
MOD_Cleanup,
MOD_LoadTitle
Oct 21, 1999
Oct 21, 1999
380
381
};
Dec 27, 1999
Dec 27, 1999
382
/* ex:set ts=4: */