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 15 instrument MOD loader
26 Also supports Ultimate Sound Tracker (old M15 format)
28 ==============================================================================*/
36 #include "unimod_priv.h"
38 /*========== Module Structure */
40 typedef struct MSAMPINFO
42 CHAR samplename[23]; /* 22 in module, 23 in memory */
51 typedef struct MODULEHEADER
53 CHAR songname[21]; /* the songname.., 20 in module, 21 in memory */
54 MSAMPINFO samples[15]; /* all sampleinfo */
55 UBYTE songlength; /* number of patterns used */
56 UBYTE magic1; /* should be 127 */
57 UBYTE positions[128]; /* which pattern to play at pos */
61 typedef struct MODNOTE
67 /*========== Loader variables */
69 static MODULEHEADER *mh = NULL;
70 static MODNOTE *patbuf = NULL;
71 static BOOL ust_loader = 0; /* if TRUE, load as an ust module. */
73 /* known file formats which can confuse the loader */
75 static char *signatures[REJECT] =
77 "CAKEWALK", /* cakewalk midi files */
78 "SZDD" /* Microsoft compressed files */
80 static int siglen[REJECT] =
83 /*========== Loader code */
86 LoadModuleHeader (MODULEHEADER * mh)
90 _mm_read_string (mh->songname, 20, modreader);
91 mh->songname[20] = 0; /* just in case */
93 /* sanity check : title should contain printable characters and a bunch
95 for (t = 0; t < 20; t++)
96 if ((mh->songname[t]) && (mh->songname[t] < 32))
98 for (t = 0; (mh->songname[t]) && (t < 20); t++);
104 for (t = 0; t < 15; t++)
106 MSAMPINFO *s = &mh->samples[t];
108 _mm_read_string (s->samplename, 22, modreader);
109 s->samplename[22] = 0; /* just in case */
110 s->length = _mm_read_M_UWORD (modreader);
111 s->finetune = _mm_read_UBYTE (modreader);
112 s->volume = _mm_read_UBYTE (modreader);
113 s->reppos = _mm_read_M_UWORD (modreader);
114 s->replen = _mm_read_M_UWORD (modreader);
116 /* sanity check : sample title should contain printable characters and
117 a bunch of null chars */
118 for (u = 0; u < 20; u++)
119 if ((s->samplename[u]) && (s->samplename[u] < /*32 */ 14))
121 for (u = 0; (s->samplename[u]) && (u < 20); u++);
124 if (s->samplename[u])
127 /* sanity check : finetune values */
128 if (s->finetune >> 4)
132 mh->songlength = _mm_read_UBYTE (modreader);
133 mh->magic1 = _mm_read_UBYTE (modreader); /* should be 127 */
135 /* sanity check : no more than 128 positions, restart position in range */
136 if ((!mh->songlength) || (mh->songlength > 128))
138 /* values encountered so far are 0x6a and 0x78 */
139 if (((mh->magic1 & 0xf8) != 0x78) && (mh->magic1 != 0x6a) && (mh->magic1 > mh->songlength))
142 _mm_read_UBYTES (mh->positions, 128, modreader);
144 /* sanity check : pattern range is 0..63 */
145 for (t = 0; t < 128; t++)
146 if (mh->positions[t] > 63)
149 return (!_mm_eof (modreader));
152 /* Checks the patterns in the modfile for UST / 15-inst indications.
153 For example, if an effect 3xx is found, it is assumed that the song
154 is 15-inst. If a 1xx effect has dat greater than 0x20, it is UST.
156 Returns: 0 indecisive; 1 = UST; 2 = 15-inst */
158 CheckPatternType (int numpat)
163 for (t = 0; t < numpat * (64U * 4); t++)
165 /* Load the pattern into the temp buffer and scan it */
166 _mm_read_UBYTE (modreader);
167 _mm_read_UBYTE (modreader);
168 eff = _mm_read_UBYTE (modreader);
169 dat = _mm_read_UBYTE (modreader);
201 if (!LoadModuleHeader (&mh))
204 /* reject other file types */
205 for (t = 0; t < REJECT; t++)
206 if (!memcmp (mh.songname, signatures[t], siglen[t]))
211 if ((!mh.songlength) || (mh.songlength > mh.magic1))
214 for (t = 0; t < 15; t++)
216 /* all finetunes should be zero */
217 if (mh.samples[t].finetune)
220 /* all volumes should be <= 64 */
221 if (mh.samples[t].volume > 64)
224 /* all instrument names should begin with s, st-, or a number */
225 if (mh.samples[t].samplename[0] == 's')
227 if ((memcmp (mh.samples[t].samplename, "st-", 3)) &&
228 (memcmp (mh.samples[t].samplename, "ST-", 3)) &&
229 (*mh.samples[t].samplename))
232 else if ((mh.samples[t].samplename[0] < '0') ||
233 (mh.samples[t].samplename[0] > '9'))
236 if (mh.samples[t].length > 4999 || mh.samples[t].reppos > 9999)
239 if (mh.samples[t].length > 32768)
243 /* if loop information is incorrect as words, but correct as bytes,
244 this is likely to be an ust-style module */
245 if((mh.samples[t].reppos + mh.samples[t].replen > mh.samples[t].length) &&
246 (mh.samples[t].reppos + mh.samples[t].replen < (mh.samples[t].length << 1)))
253 for (numpat = 0, t = 0; t < mh.songlength; t++)
254 if (mh.positions[t] > numpat)
255 numpat = mh.positions[t];
257 switch (CheckPatternType (numpat))
259 case 0: /* indecisive, so check more clues... */
274 if (!(mh = (MODULEHEADER *) _mm_malloc (sizeof (MODULEHEADER))))
287 Old (amiga) noteinfo:
289 _____byte 1_____ byte2_ _____byte 3_____ byte4_
291 0000 0000-00000000 0000 0000-00000000
293 Upper four 12 bits for Lower four Effect command.
294 bits of sam- note period. bits of sam-
295 ple number. ple number.
298 static UWORD npertab[7 * OCTAVE] =
301 1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906,
302 856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
303 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
304 214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
305 107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
307 53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28,
308 27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14
313 M15_ConvertNote (MODNOTE * n)
315 UBYTE instrument, effect, effdat, note;
319 /* decode the 4 bytes that make up a single note */
320 instrument = n->c >> 4;
321 period = (((UWORD) n->a & 0xf) << 8) + n->b;
325 /* Convert the period to a note number */
329 for (note = 0; note < 7 * OCTAVE; note++)
330 if (period >= npertab[note])
332 if (note == 7 * OCTAVE)
340 /* if instrument does not exist, note cut */
341 if ((instrument > 15) || (!mh->samples[instrument - 1].length))
343 UniPTEffect (0xc, 0);
349 /* if we had a note, then change instrument... */
351 UniInstrument (instrument - 1);
352 /* ...otherwise, only adjust volume... */
355 /* ...unless an effect was specified, which forces a new note
357 if (effect || effdat)
359 UniInstrument (instrument - 1);
363 UniPTEffect (0xc, mh->samples[instrument - 1].volume & 0x7f);
369 UniNote (note + 2 * OCTAVE - 1);
373 /* Handle ``heavy'' volumes correctly */
374 if ((effect == 0xc) && (effdat > 0x40))
377 /* Convert pattern jump from Dec to Hex */
379 effdat = (((effdat & 0xf0) >> 4) * 10) + (effdat & 0xf);
381 /* Volume slide, up has priority */
382 if ((effect == 0xa) && (effdat & 0xf) && (effdat & 0xf0))
393 UniPTEffect (0, effdat);
397 UniPTEffect (1, effdat & 0xf);
399 UniPTEffect (2, effdat >> 2);
402 UniPTEffect (effect, effdat);
407 /* Ignore 100, 200 and 300 (there is no porta memory in mod files) */
408 if ((!effdat) && ((effect == 1)||(effect == 2)||(effect == 3)))
411 UniPTEffect (effect, effdat);
416 M15_ConvertTrack (MODNOTE * n)
421 for (t = 0; t < 64; t++)
430 /* Loads all patterns of a modfile and converts them into the 3 byte format. */
432 M15_LoadPatterns (void)
434 int t, s, tracks = 0;
436 if (!AllocPatterns ())
441 /* Allocate temporary buffer for loading and converting the patterns */
442 if (!(patbuf = (MODNOTE *) _mm_calloc (64U * 4, sizeof (MODNOTE))))
445 for (t = 0; t < of.numpat; t++)
447 /* Load the pattern into the temp buffer and convert it */
448 for (s = 0; s < (64U * 4); s++)
450 patbuf[s].a = _mm_read_UBYTE (modreader);
451 patbuf[s].b = _mm_read_UBYTE (modreader);
452 patbuf[s].c = _mm_read_UBYTE (modreader);
453 patbuf[s].d = _mm_read_UBYTE (modreader);
456 for (s = 0; s < 4; s++)
457 if (!(of.tracks[tracks++] = M15_ConvertTrack (patbuf + s)))
464 M15_Load (BOOL curious)
470 /* try to read module header */
471 if (!LoadModuleHeader (mh))
473 _mm_errno = MMERR_LOADING_HEADER;
478 of.modtype = strdup ("Ultimate Soundtracker");
480 of.modtype = strdup ("Soundtracker");
482 /* set module variables */
486 of.songname = DupStr (mh->songname, 21, 1);
487 of.numpos = mh->songlength;
490 /* Count the number of patterns */
492 for (t = 0; t < of.numpos; t++)
493 if (mh->positions[t] > of.numpat)
494 of.numpat = mh->positions[t];
495 /* since some old modules embed extra patterns, we have to check the
496 whole list to get the samples' file offsets right - however we can find
497 garbage here, so check carefully */
499 for (t = of.numpos; t < 128; t++)
500 if (mh->positions[t] >= 0x80)
503 for (t = of.numpos; t < 128; t++)
505 if (mh->positions[t] > of.numpat)
506 of.numpat = mh->positions[t];
507 if ((curious) && (mh->positions[t]))
511 of.numtrk = of.numpat * of.numchn;
513 if (!AllocPositions (of.numpos))
515 for (t = 0; t < of.numpos; t++)
516 of.positions[t] = mh->positions[t];
518 /* Finally, init the sampleinfo structures */
519 of.numins = of.numsmp = 15;
520 if (!AllocSamples ())
526 for (t = 0; t < of.numins; t++)
528 /* convert the samplename */
529 q->samplename = DupStr (s->samplename, 23, 1);
531 /* init the sampleinfo variables and convert the size pointers */
532 q->speed = finetune[s->finetune & 0xf];
533 q->volume = s->volume;
535 q->loopstart = s->reppos;
537 q->loopstart = s->reppos << 1;
538 q->loopend = q->loopstart + (s->replen << 1);
539 q->length = s->length << 1;
541 q->flags = SF_SIGNED;
543 q->flags |= SF_UST_LOOP;
547 /* fix replen if repend>length */
548 if (q->loopend > q->length)
549 q->loopend = q->length;
555 if (!M15_LoadPatterns ())
567 _mm_fseek (modreader, 0, SEEK_SET);
568 if (!_mm_read_UBYTES (s, 20, modreader))
570 s[20] = 0; /* just in case */
571 return (DupStr (s, 21, 1));
574 /*========== Loader information */
579 "15-instrument module",
580 "MOD (15 instruments)",