initial import
[dosrtxon] / libs / mikmod / loaders / load_mod.c
1 /*      MikMod sound library
2         (c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file
3         AUTHORS for complete list.
4
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.
9
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.
14
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
18         02111-1307, USA.
19 */
20
21 /*==============================================================================
22
23   $Id$
24
25   Generic MOD loader (Protracker, StarTracker, FastTracker, etc)
26
27 ==============================================================================*/
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include <stdio.h>
38 #ifdef HAVE_MEMORY_H
39 #include <memory.h>
40 #endif
41 #include <string.h>
42
43 #include "mikmod_internals.h"
44 #include "mikmod_ctype.h"
45
46 #ifdef SUNOS
47 extern int fprintf(FILE *, const char *, ...);
48 #endif
49
50 /*========== Module structure */
51
52 typedef struct MSAMPINFO {
53         CHAR samplename[23];            /* 22 in module, 23 in memory */
54         UWORD length;
55         UBYTE finetune;
56         UBYTE volume;
57         UWORD reppos;
58         UWORD replen;
59 } MSAMPINFO;
60
61 typedef struct MODULEHEADER {
62         CHAR songname[21];                      /* the songname.. 20 in module, 21 in memory */
63         MSAMPINFO samples[31];          /* all sampleinfo */
64         UBYTE songlength;                       /* number of patterns used */
65         UBYTE magic1;                           /* should be 127 */
66         UBYTE positions[128];           /* which pattern to play at pos */
67         UBYTE magic2[4];                        /* string "M.K." or "FLT4" or "FLT8" */
68 } MODULEHEADER;
69
70 typedef struct MODTYPE {
71         CHAR id[5];
72         UBYTE channels;
73         CHAR *name;
74 } MODTYPE;
75
76 typedef struct MODNOTE {
77         UBYTE a, b, c, d;
78 } MODNOTE;
79
80 /*========== Loader variables */
81
82 #define MODULEHEADERSIZE 0x438
83
84 static CHAR protracker[] = "Protracker";
85 static CHAR startrekker[] = "Startrekker";
86 static CHAR fasttracker[] = "Fasttracker";
87 static CHAR oktalyser[] = "Oktalyser";
88 static CHAR oktalyzer[] = "Oktalyzer";
89 static CHAR taketracker[] = "TakeTracker";
90 static CHAR orpheus[] = "Imago Orpheus (MOD format)";
91
92 static MODULEHEADER *mh = NULL;
93 static MODNOTE *patbuf = NULL;
94 static int modtype, trekker;
95
96 /*========== Loader code */
97
98 /* given the module ID, determine the number of channels and the tracker
99    description ; also alters modtype */
100 static BOOL MOD_CheckType(UBYTE *id, UBYTE *numchn, CHAR **descr)
101 {
102         modtype = trekker = 0;
103
104         /* Protracker and variants */
105         if ((!memcmp(id, "M.K.", 4)) || (!memcmp(id, "M!K!", 4))) {
106                 *descr = protracker;
107                 modtype = 0;
108                 *numchn = 4;
109                 return 1;
110         }
111
112         /* Star Tracker */
113         if (((!memcmp(id, "FLT", 3)) || (!memcmp(id, "EXO", 3))) &&
114                 (mik_isdigit(id[3]))) {
115                 *descr = startrekker;
116                 modtype = trekker = 1;
117                 *numchn = id[3] - '0';
118                 if (*numchn == 4 || *numchn == 8)
119                         return 1;
120 #ifdef MIKMOD_DEBUG
121                 else
122                         fprintf(stderr, "\rUnknown FLT%d module type\n", *numchn);
123 #endif
124                 return 0;
125         }
126
127         /* Oktalyzer (Amiga) */
128         if (!memcmp(id, "OKTA", 4)) {
129                 *descr = oktalyzer;
130                 modtype = 1;
131                 *numchn = 8;
132                 return 1;
133         }
134
135         /* Oktalyser (Atari) */
136         if (!memcmp(id, "CD81", 4)) {
137                 *descr = oktalyser;
138                 modtype = 1;
139                 *numchn = 8;
140                 return 1;
141         }
142
143         /* Fasttracker */
144         if ((!memcmp(id + 1, "CHN", 3)) && (mik_isdigit(id[0]))) {
145                 *descr = fasttracker;
146                 modtype = 1;
147                 *numchn = id[0] - '0';
148                 return 1;
149         }
150         /* Fasttracker or Taketracker */
151         if (((!memcmp(id + 2, "CH", 2)) || (!memcmp(id + 2, "CN", 2)))
152                 && (mik_isdigit(id[0])) && (mik_isdigit(id[1]))) {
153                 if (id[3] == 'H') {
154                         *descr = fasttracker;
155                         modtype = 2;            /* this can also be Imago Orpheus */
156                 } else {
157                         *descr = taketracker;
158                         modtype = 1;
159                 }
160                 *numchn = (id[0] - '0') * 10 + (id[1] - '0');
161                 return 1;
162         }
163
164         return 0;
165 }
166
167 static BOOL MOD_Test(void)
168 {
169         UBYTE id[4], numchn;
170         CHAR *descr;
171
172         _mm_fseek(modreader, MODULEHEADERSIZE, SEEK_SET);
173         if (!_mm_read_UBYTES(id, 4, modreader))
174                 return 0;
175
176         if (MOD_CheckType(id, &numchn, &descr))
177                 return 1;
178
179         return 0;
180 }
181
182 static BOOL MOD_Init(void)
183 {
184         if (!(mh = (MODULEHEADER *)MikMod_malloc(sizeof(MODULEHEADER))))
185                 return 0;
186         return 1;
187 }
188
189 static void MOD_Cleanup(void)
190 {
191         MikMod_free(mh);
192         MikMod_free(patbuf);
193         mh=NULL;
194         patbuf=NULL;
195 }
196
197 /*
198 Old (amiga) noteinfo:
199
200 _____byte 1_____   byte2_    _____byte 3_____   byte4_
201 /                \ /      \  /                \ /      \
202 0000          0000-00000000  0000          0000-00000000
203
204 Upper four    12 bits for    Lower four    Effect command.
205 bits of sam-  note period.   bits of sam-
206 ple number.                  ple number.
207
208 */
209
210 static UBYTE ConvertNote(MODNOTE *n, UBYTE lasteffect)
211 {
212         UBYTE instrument, effect, effdat, note;
213         UWORD period;
214         UBYTE lastnote = 0;
215
216         /* extract the various information from the 4 bytes that make up a note */
217         instrument = (n->a & 0x10) | (n->c >> 4);
218         period = (((UWORD)n->a & 0xf) << 8) + n->b;
219         effect = n->c & 0xf;
220         effdat = n->d;
221
222         /* Convert the period to a note number */
223         note = 0;
224         if (period) {
225                 for (note = 0; note < 7 * OCTAVE; note++)
226                         if (period >= npertab[note])
227                                 break;
228                 if (note == 7 * OCTAVE)
229                         note = 0;
230                 else
231                         note++;
232         }
233
234         if (instrument) {
235                 /* if instrument does not exist, note cut */
236                 if ((instrument > 31) || (!mh->samples[instrument - 1].length)) {
237                         UniPTEffect(0xc, 0);
238                         if (effect == 0xc)
239                                 effect = effdat = 0;
240                 } else {
241                         /* Protracker handling */
242                         if (!modtype) {
243                                 /* if we had a note, then change instrument... */
244                                 if (note)
245                                         UniInstrument(instrument - 1);
246                                 /* ...otherwise, only adjust volume... */
247                                 else {
248                                         /* ...unless an effect was specified, which forces a new
249                                            note to be played */
250                                         if (effect || effdat) {
251                                                 UniInstrument(instrument - 1);
252                                                 note = lastnote;
253                                         } else
254                                                 UniPTEffect(0xc,
255                                                                         mh->samples[instrument -
256                                                                                                 1].volume & 0x7f);
257                                 }
258                         } else {
259                                 /* Fasttracker handling */
260                                 UniInstrument(instrument - 1);
261                                 if (!note)
262                                         note = lastnote;
263                         }
264                 }
265         }
266         if (note) {
267                 UniNote(note + 2 * OCTAVE - 1);
268                 lastnote = note;
269         }
270
271         /* Convert pattern jump from Dec to Hex */
272         if (effect == 0xd)
273                 effdat = (((effdat & 0xf0) >> 4) * 10) + (effdat & 0xf);
274
275         /* Volume slide, up has priority */
276         if ((effect == 0xa) && (effdat & 0xf) && (effdat & 0xf0))
277                 effdat &= 0xf0;
278
279         /* Handle ``heavy'' volumes correctly */
280         if ((effect == 0xc) && (effdat > 0x40))
281                 effdat = 0x40;
282
283         /* An isolated 100, 200 or 300 effect should be ignored (no
284            "standalone" porta memory in mod files). However, a sequence such
285            as 1XX, 100, 100, 100 is fine. */
286         if ((!effdat) && ((effect == 1)||(effect == 2)||(effect ==3)) &&
287                 (lasteffect < 0x10) && (effect != lasteffect))
288                 effect = 0;
289
290         UniPTEffect(effect, effdat);
291         if (effect == 8)
292                 of.flags |= UF_PANNING;
293
294         return effect;
295 }
296
297 static UBYTE *ConvertTrack(MODNOTE *n, int numchn)
298 {
299         int t;
300         UBYTE lasteffect = 0x10;        /* non existant effect */
301
302         UniReset();
303         for (t = 0; t < 64; t++) {
304                 lasteffect = ConvertNote(n,lasteffect);
305                 UniNewline();
306                 n += numchn;
307         }
308         return UniDup();
309 }
310
311 /* Loads all patterns of a modfile and converts them into the 3 byte format. */
312 static BOOL ML_LoadPatterns(void)
313 {
314         int t, s, tracks = 0;
315
316         if (!AllocPatterns())
317                 return 0;
318         if (!AllocTracks())
319                 return 0;
320
321         /* Allocate temporary buffer for loading and converting the patterns */
322         if (!(patbuf = (MODNOTE *)MikMod_calloc(64U * of.numchn, sizeof(MODNOTE))))
323                 return 0;
324
325         if (trekker && of.numchn == 8) {
326                 /* Startrekker module dual pattern */
327                 for (t = 0; t < of.numpat; t++) {
328                         for (s = 0; s < (64U * 4); s++) {
329                                 patbuf[s].a = _mm_read_UBYTE(modreader);
330                                 patbuf[s].b = _mm_read_UBYTE(modreader);
331                                 patbuf[s].c = _mm_read_UBYTE(modreader);
332                                 patbuf[s].d = _mm_read_UBYTE(modreader);
333                         }
334                         for (s = 0; s < 4; s++)
335                                 if (!(of.tracks[tracks++] = ConvertTrack(patbuf + s, 4)))
336                                         return 0;
337                         for (s = 0; s < (64U * 4); s++) {
338                                 patbuf[s].a = _mm_read_UBYTE(modreader);
339                                 patbuf[s].b = _mm_read_UBYTE(modreader);
340                                 patbuf[s].c = _mm_read_UBYTE(modreader);
341                                 patbuf[s].d = _mm_read_UBYTE(modreader);
342                         }
343                         for (s = 0; s < 4; s++)
344                                 if (!(of.tracks[tracks++] = ConvertTrack(patbuf + s, 4)))
345                                         return 0;
346                 }
347         } else {
348                 /* Generic module pattern */
349                 for (t = 0; t < of.numpat; t++) {
350                         /* Load the pattern into the temp buffer and convert it */
351                         for (s = 0; s < (int)(64U * of.numchn); s++) {
352                                 patbuf[s].a = _mm_read_UBYTE(modreader);
353                                 patbuf[s].b = _mm_read_UBYTE(modreader);
354                                 patbuf[s].c = _mm_read_UBYTE(modreader);
355                                 patbuf[s].d = _mm_read_UBYTE(modreader);
356                         }
357                         for (s = 0; s < of.numchn; s++)
358                                 if (!(of.tracks[tracks++] = ConvertTrack(patbuf + s, of.numchn)))
359                                         return 0;
360                 }
361         }
362         return 1;
363 }
364
365 static BOOL MOD_Load(BOOL curious)
366 {
367         int t, scan;
368         SAMPLE *q;
369         MSAMPINFO *s;
370         CHAR *descr;
371
372         /* try to read module header */
373         _mm_read_string((CHAR *)mh->songname, 20, modreader);
374         mh->songname[20] = 0;           /* just in case */
375
376         for (t = 0; t < 31; t++) {
377                 s = &mh->samples[t];
378                 _mm_read_string(s->samplename, 22, modreader);
379                 s->samplename[22] = 0;  /* just in case */
380                 s->length = _mm_read_M_UWORD(modreader);
381                 s->finetune = _mm_read_UBYTE(modreader);
382                 s->volume = _mm_read_UBYTE(modreader);
383                 s->reppos = _mm_read_M_UWORD(modreader);
384                 s->replen = _mm_read_M_UWORD(modreader);
385         }
386
387         mh->songlength = _mm_read_UBYTE(modreader);
388
389         /* this fixes mods which declare more than 128 positions.
390          * eg: beatwave.mod */
391         if (mh->songlength > 128) { mh->songlength = 128; }
392
393         mh->magic1 = _mm_read_UBYTE(modreader);
394         _mm_read_UBYTES(mh->positions, 128, modreader);
395         _mm_read_UBYTES(mh->magic2, 4, modreader);
396
397         if (_mm_eof(modreader)) {
398                 _mm_errno = MMERR_LOADING_HEADER;
399                 return 0;
400         }
401
402         /* set module variables */
403         of.initspeed = 6;
404         of.inittempo = 125;
405         if (!(MOD_CheckType(mh->magic2, &of.numchn, &descr))) {
406                 _mm_errno = MMERR_NOT_A_MODULE;
407                 return 0;
408         }
409         if (trekker && of.numchn == 8)
410                 for (t = 0; t < 128; t++)
411                         /* if module pretends to be FLT8, yet the order table
412                            contains odd numbers, chances are it's a lying FLT4... */
413                         if (mh->positions[t] & 1) {
414                                 of.numchn = 4;
415                                 break;
416                         }
417         if (trekker && of.numchn == 8)
418                 for (t = 0; t < 128; t++)
419                         mh->positions[t] >>= 1;
420
421         of.songname = DupStr(mh->songname, 21, 1);
422         of.numpos = mh->songlength;
423         of.reppos = 0;
424
425         /* Count the number of patterns */
426         of.numpat = 0;
427         for (t = 0; t < of.numpos; t++)
428                 if (mh->positions[t] > of.numpat)
429                         of.numpat = mh->positions[t];
430
431         /* since some old modules embed extra patterns, we have to check the
432            whole list to get the samples' file offsets right - however we can find
433            garbage here, so check carefully */
434         scan = 1;
435         for (t = of.numpos; t < 128; t++)
436                 if (mh->positions[t] >= 0x80)
437                         scan = 0;
438         if (scan)
439                 for (t = of.numpos; t < 128; t++) {
440                         if (mh->positions[t] > of.numpat)
441                                 of.numpat = mh->positions[t];
442                         if ((curious) && (mh->positions[t]))
443                                 of.numpos = t + 1;
444                 }
445         of.numpat++;
446         of.numtrk = of.numpat * of.numchn;
447
448         if (!AllocPositions(of.numpos))
449                 return 0;
450         for (t = 0; t < of.numpos; t++)
451                 of.positions[t] = mh->positions[t];
452
453         /* Finally, init the sampleinfo structures  */
454         of.numins = of.numsmp = 31;
455         if (!AllocSamples())
456                 return 0;
457         s = mh->samples;
458         q = of.samples;
459         for (t = 0; t < of.numins; t++) {
460                 /* convert the samplename */
461                 q->samplename = DupStr(s->samplename, 23, 1);
462                 /* init the sampleinfo variables and convert the size pointers */
463                 q->speed = finetune[s->finetune & 0xf];
464                 q->volume = s->volume & 0x7f;
465                 q->loopstart = (ULONG)s->reppos << 1;
466                 q->loopend = q->loopstart + ((ULONG)s->replen << 1);
467                 q->length = (ULONG)s->length << 1;
468                 q->flags = SF_SIGNED;
469                 /* Imago Orpheus creates MODs with 16 bit samples, check */
470                 if ((modtype == 2) && (s->volume & 0x80)) {
471                         q->flags |= SF_16BITS;
472                         descr = orpheus;
473                 }
474                 if (s->replen > 2)
475                         q->flags |= SF_LOOP;
476
477                 s++;
478                 q++;
479         }
480
481         of.modtype = MikMod_strdup(descr);
482
483         if (!ML_LoadPatterns())
484                 return 0;
485
486         return 1;
487 }
488
489 static CHAR *MOD_LoadTitle(void)
490 {
491         CHAR s[21];
492
493         _mm_fseek(modreader, 0, SEEK_SET);
494         if (!_mm_read_UBYTES(s, 20, modreader))
495                 return NULL;
496         s[20] = 0;                                      /* just in case */
497
498         return (DupStr(s, 21, 1));
499 }
500
501 /*========== Loader information */
502
503 MIKMODAPI MLOADER load_mod = {
504         NULL,
505         "Standard module",
506         "MOD (31 instruments)",
507         MOD_Init,
508         MOD_Test,
509         MOD_Load,
510         MOD_Cleanup,
511         MOD_LoadTitle
512 };
513
514 /* ex:set ts=4: */