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