1 /* MikMod sound library
2 (c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 /*==============================================================================
25 Generic MOD loader (Protracker, StarTracker, FastTracker, etc)
27 ==============================================================================*/
35 #include "unimod_priv.h"
37 /*========== Module structure */
39 typedef struct MSAMPINFO
41 CHAR samplename[23]; /* 22 in module, 23 in memory */
50 typedef struct MODULEHEADER
52 CHAR songname[21]; /* the songname.. 20 in module, 21 in memory */
53 MSAMPINFO samples[31]; /* all sampleinfo */
54 UBYTE songlength; /* number of patterns used */
55 UBYTE magic1; /* should be 127 */
56 UBYTE positions[128]; /* which pattern to play at pos */
57 UBYTE magic2[4]; /* string "M.K." or "FLT4" or "FLT8" */
61 typedef struct MODTYPE
69 typedef struct MODNOTE
75 /*========== Loader variables */
77 #define MODULEHEADERSIZE 1084
79 static CHAR protracker[] = "Protracker";
80 static CHAR startracker[] = "Startracker";
81 static CHAR fasttracker[] = "Fasttracker";
82 static CHAR ins15tracker[] = "15-instrument";
83 static CHAR oktalyzer[] = "Oktalyzer";
84 static CHAR taketracker[] = "TakeTracker";
85 static CHAR orpheus[] = "Imago Orpheus (MOD format)";
87 #define MODTYPE_COUNT 24
88 static MODTYPE modtypes[MODTYPE_COUNT + 1] =
90 {"M.K.", 4, protracker}, /* protracker 4 channel */
91 {"M!K!", 4, protracker}, /* protracker 4 channel */
92 {"FLT4", 4, startracker}, /* startracker 4 channel */
93 {"2CHN", 2, fasttracker}, /* fasttracker 2 channel */
94 {"4CHN", 4, fasttracker}, /* fasttracker 4 channel */
95 {"6CHN", 6, fasttracker}, /* fasttracker 6 channel */
96 {"8CHN", 8, fasttracker}, /* fasttracker 8 channel */
97 {"10CH", 10, fasttracker}, /* fasttracker 10 channel */
98 {"12CH", 12, fasttracker}, /* fasttracker 12 channel */
99 {"14CH", 14, fasttracker}, /* fasttracker 14 channel */
100 {"15CH", 15, fasttracker}, /* fasttracker 15 channel */
101 {"16CH", 16, fasttracker}, /* fasttracker 16 channel */
102 {"18CH", 18, fasttracker}, /* fasttracker 18 channel */
103 {"20CH", 20, fasttracker}, /* fasttracker 20 channel */
104 {"22CH", 22, fasttracker}, /* fasttracker 22 channel */
105 {"24CH", 24, fasttracker}, /* fasttracker 24 channel */
106 {"26CH", 26, fasttracker}, /* fasttracker 26 channel */
107 {"28CH", 28, fasttracker}, /* fasttracker 28 channel */
108 {"30CH", 30, fasttracker}, /* fasttracker 30 channel */
109 {"32CH", 32, fasttracker}, /* fasttracker 32 channel */
110 {"CD81", 8, oktalyzer}, /* atari oktalyzer 8 channel */
111 {"OKTA", 8, oktalyzer}, /* atari oktalyzer 8 channel */
112 {"16CN", 16, taketracker}, /* taketracker 16 channel */
113 {"32CN", 32, taketracker}, /* taketracker 32 channel */
114 {" ", 4, ins15tracker} /* 15-instrument 4 channel */
117 static MODULEHEADER *mh = NULL;
118 static MODNOTE *patbuf = NULL;
119 static int modtype = 0;
121 /*========== Loader code */
128 _mm_fseek (modreader, MODULEHEADERSIZE - 4, SEEK_SET);
129 if (!_mm_read_UBYTES (id, 4, modreader))
132 /* find out which ID string */
133 for (modtype = 0; modtype < MODTYPE_COUNT; modtype++)
134 if (!memcmp (id, modtypes[modtype].id, 4))
143 if (!(mh = (MODULEHEADER *) _mm_malloc (sizeof (MODULEHEADER))))
156 Old (amiga) noteinfo:
158 _____byte 1_____ byte2_ _____byte 3_____ byte4_
160 0000 0000-00000000 0000 0000-00000000
162 Upper four 12 bits for Lower four Effect command.
163 bits of sam- note period. bits of sam-
164 ple number. ple number.
168 static UWORD npertab[7 * OCTAVE] =
171 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906,
172 856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
173 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
174 214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
175 107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
177 53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28,
178 27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14
183 ConvertNote (MODNOTE * n)
185 UBYTE instrument, effect, effdat, note;
189 /* extract the various information from the 4 bytes that make up a note */
190 instrument = (n->a & 0x10) | (n->c >> 4);
191 period = (((UWORD) n->a & 0xf) << 8) + n->b;
195 /* Convert the period to a note number */
199 for (note = 0; note < 7 * OCTAVE; note++)
200 if (period >= npertab[note])
202 if (note == 7 * OCTAVE)
210 /* if instrument does not exist, note cut */
211 if ((instrument > 31) || (!mh->samples[instrument - 1].length))
213 UniPTEffect (0xc, 0);
219 /* Protracker handling */
222 /* if we had a note, then change instrument... */
224 UniInstrument (instrument - 1);
225 /* ...otherwise, only adjust volume... */
228 /* ...unless an effect was specified, which forces a new
230 if (effect || effdat)
232 UniInstrument (instrument - 1);
236 UniPTEffect (0xc, mh->samples[instrument - 1].volume & 0x7f);
241 /* Fasttracker handling */
242 UniInstrument (instrument - 1);
250 UniNote (note + 2 * OCTAVE - 1);
254 /* Convert pattern jump from Dec to Hex */
256 effdat = (((effdat & 0xf0) >> 4) * 10) + (effdat & 0xf);
258 /* Volume slide, up has priority */
259 if ((effect == 0xa) && (effdat & 0xf) && (effdat & 0xf0))
262 /* Handle ``heavy'' volumes correctly */
263 if ((effect == 0xc) && (effdat > 0x40))
266 /* Ignore 100, 200 and 300 (there is no porta memory in mod files) */
267 #if 0 /* space_debris.mod uses 300 and porta memory!! */
268 if ((!effdat) && ((effect == 1)||(effect == 2)||(effect ==3)))
272 UniPTEffect (effect, effdat);
276 ConvertTrack (MODNOTE * n)
281 for (t = 0; t < 64; t++)
290 /* Loads all patterns of a modfile and converts them into the 3 byte format. */
292 ML_LoadPatterns (void)
294 int t, s, tracks = 0;
296 if (!AllocPatterns ())
301 /* Allocate temporary buffer for loading and converting the patterns */
302 if (!(patbuf = (MODNOTE *) _mm_calloc (64U * of.numchn, sizeof (MODNOTE))))
305 for (t = 0; t < of.numpat; t++)
307 /* Load the pattern into the temp buffer and convert it */
308 for (s = 0; s < (64U * of.numchn); s++)
310 patbuf[s].a = _mm_read_UBYTE (modreader);
311 patbuf[s].b = _mm_read_UBYTE (modreader);
312 patbuf[s].c = _mm_read_UBYTE (modreader);
313 patbuf[s].d = _mm_read_UBYTE (modreader);
315 for (s = 0; s < of.numchn; s++)
316 if (!(of.tracks[tracks++] = ConvertTrack (patbuf + s)))
323 MOD_Load (BOOL curious)
330 /* try to read module header */
331 _mm_read_string ((CHAR *) mh->songname, 20, modreader);
332 mh->songname[20] = 0; /* just in case */
334 for (t = 0; t < 31; t++)
337 _mm_read_string (s->samplename, 22, modreader);
338 s->samplename[22] = 0; /* just in case */
339 s->length = _mm_read_M_UWORD (modreader);
340 s->finetune = _mm_read_UBYTE (modreader);
341 s->volume = _mm_read_UBYTE (modreader);
342 s->reppos = _mm_read_M_UWORD (modreader);
343 s->replen = _mm_read_M_UWORD (modreader);
346 mh->songlength = _mm_read_UBYTE (modreader);
347 mh->magic1 = _mm_read_UBYTE (modreader);
348 _mm_read_UBYTES (mh->positions, 128, modreader);
349 _mm_read_UBYTES (mh->magic2, 4, modreader);
351 if (_mm_eof (modreader))
353 _mm_errno = MMERR_LOADING_HEADER;
357 /* set module variables */
360 of.numchn = modtypes[modtype].channels;
361 of.songname = DupStr (mh->songname, 21, 1);
362 of.numpos = mh->songlength;
365 /* Count the number of patterns */
367 for (t = 0; t < of.numpos; t++)
368 if (mh->positions[t] > of.numpat)
369 of.numpat = mh->positions[t];
370 /* since some old modules embed extra patterns, we have to check the
371 whole list to get the samples' file offsets right - however we can find
372 garbage here, so check carefully */
374 for (t = of.numpos; t < 128; t++)
375 if (mh->positions[t] >= 0x80)
378 for (t = of.numpos; t < 128; t++)
380 if (mh->positions[t] > of.numpat)
381 of.numpat = mh->positions[t];
382 if ((curious) && (mh->positions[t]))
386 of.numtrk = of.numpat * of.numchn;
388 if (!AllocPositions (of.numpos))
390 for (t = 0; t < of.numpos; t++)
391 of.positions[t] = mh->positions[t];
393 /* Finally, init the sampleinfo structures */
394 of.numins = of.numsmp = 31;
395 if (!AllocSamples ())
399 for (t = 0; t < of.numins; t++)
401 /* convert the samplename */
402 q->samplename = DupStr (s->samplename, 23, 1);
403 /* init the sampleinfo variables and convert the size pointers */
404 q->speed = finetune[s->finetune & 0xf];
405 q->volume = s->volume & 0x7f;
406 q->loopstart = (ULONG) s->reppos << 1;
407 q->loopend = q->loopstart + ((ULONG) s->replen << 1);
408 q->length = (ULONG) s->length << 1;
409 q->flags = SF_SIGNED;
410 /* Imago Orpheus creates MODs with 16 bit samples, check */
411 if ((modtypes[modtype].name == fasttracker) && (s->volume & 0x80))
413 q->flags |= SF_16BITS;
419 /* fix replen if repend > length */
420 if (q->loopend > q->length)
421 q->loopend = q->length;
428 of.modtype = strdup (orpheus);
430 of.modtype = strdup (modtypes[modtype].name);
432 if (!ML_LoadPatterns ())
442 _mm_fseek (modreader, 0, SEEK_SET);
443 if (!_mm_read_UBYTES (s, 20, modreader))
445 s[20] = 0; /* just in case */
447 return (DupStr (s, 21, 1));
450 /*========== Loader information */
456 "MOD (31 instruments)",