OSDN Git Service

[portaudio] Import pa_stable_v190700_20210406
[timidity41/timidity41.git] / libunimod / load_mod.c
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 <string.h>
34
35 #include "unimod_priv.h"
36
37 /*========== Module structure */
38
39 typedef struct MSAMPINFO
40   {
41     CHAR samplename[23];        /* 22 in module, 23 in memory */
42     UWORD length;
43     UBYTE finetune;
44     UBYTE volume;
45     UWORD reppos;
46     UWORD replen;
47   }
48 MSAMPINFO;
49
50 typedef struct MODULEHEADER
51   {
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" */
58   }
59 MODULEHEADER;
60
61 typedef struct MODTYPE
62   {
63     CHAR id[5];
64     UBYTE channels;
65     CHAR *name;
66   }
67 MODTYPE;
68
69 typedef struct MODNOTE
70   {
71     UBYTE a, b, c, d;
72   }
73 MODNOTE;
74
75 /*========== Loader variables */
76
77 #define MODULEHEADERSIZE 1084
78
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)";
86
87 #define MODTYPE_COUNT 24
88 static MODTYPE modtypes[MODTYPE_COUNT + 1] =
89 {
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 */
115 };
116
117 static MODULEHEADER *mh = NULL;
118 static MODNOTE *patbuf = NULL;
119 static int modtype = 0;
120
121 /*========== Loader code */
122
123 static BOOL 
124 MOD_Test (void)
125 {
126   UBYTE id[4];
127
128   _mm_fseek (modreader, MODULEHEADERSIZE - 4, SEEK_SET);
129   if (!_mm_read_UBYTES (id, 4, modreader))
130     return 0;
131
132   /* find out which ID string */
133   for (modtype = 0; modtype < MODTYPE_COUNT; modtype++)
134     if (!memcmp (id, modtypes[modtype].id, 4))
135       return 1;
136
137   return 0;
138 }
139
140 static BOOL 
141 MOD_Init (void)
142 {
143   if (!(mh = (MODULEHEADER *) _mm_malloc (sizeof (MODULEHEADER))))
144     return 0;
145   return 1;
146 }
147
148 static void 
149 MOD_Cleanup (void)
150 {
151   _mm_free (mh);
152   _mm_free (patbuf);
153 }
154
155 /*
156    Old (amiga) noteinfo:
157
158    _____byte 1_____   byte2_    _____byte 3_____   byte4_
159    /                \ /      \  /                \ /      \
160    0000          0000-00000000  0000          0000-00000000
161
162    Upper four    12 bits for    Lower four    Effect command.
163    bits of sam-  note period.   bits of sam-
164    ple number.                  ple number.
165
166  */
167
168 static UWORD npertab[7 * OCTAVE] =
169 {
170         /* -> Tuning 0 */
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,
176
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
179 };
180
181
182 static void 
183 ConvertNote (MODNOTE * n)
184 {
185   UBYTE instrument, effect, effdat, note;
186   UWORD period;
187   UBYTE lastnote = 0;
188
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;
192   effect = n->c & 0xf;
193   effdat = n->d;
194
195   /* Convert the period to a note number */
196   note = 0;
197   if (period)
198     {
199       for (note = 0; note < 7 * OCTAVE; note++)
200         if (period >= npertab[note])
201           break;
202       if (note == 7 * OCTAVE)
203         note = 0;
204       else
205         note++;
206     }
207
208   if (instrument)
209     {
210       /* if instrument does not exist, note cut */
211       if ((instrument > 31) || (!mh->samples[instrument - 1].length))
212         {
213           UniPTEffect (0xc, 0);
214           if (effect == 0xc)
215             effect = effdat = 0;
216         }
217       else
218         {
219           /* Protracker handling */
220           if (modtype <= 2)
221             {
222               /* if we had a note, then change instrument... */
223               if (note)
224                 UniInstrument (instrument - 1);
225               /* ...otherwise, only adjust volume... */
226               else
227                 {
228                   /* ...unless an effect was specified, which forces a new
229                      note to be played */
230                   if (effect || effdat)
231                     {
232                       UniInstrument (instrument - 1);
233                       note = lastnote;
234                     }
235                   else
236                     UniPTEffect (0xc, mh->samples[instrument - 1].volume & 0x7f);
237                 }
238             }
239           else
240             {
241               /* Fasttracker handling */
242               UniInstrument (instrument - 1);
243               if (!note)
244                 note = lastnote;
245             }
246         }
247     }
248   if (note)
249     {
250       UniNote (note + 2 * OCTAVE - 1);
251       lastnote = note;
252     }
253
254   /* Convert pattern jump from Dec to Hex */
255   if (effect == 0xd)
256     effdat = (((effdat & 0xf0) >> 4) * 10) + (effdat & 0xf);
257
258   /* Volume slide, up has priority */
259   if ((effect == 0xa) && (effdat & 0xf) && (effdat & 0xf0))
260     effdat &= 0xf0;
261
262   /* Handle ``heavy'' volumes correctly */
263   if ((effect == 0xc) && (effdat > 0x40))
264     effdat = 0x40;
265
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)))
269     effect = 0;
270 #endif
271
272   UniPTEffect (effect, effdat);
273 }
274
275 static UBYTE *
276 ConvertTrack (MODNOTE * n)
277 {
278   int t;
279
280   UniReset ();
281   for (t = 0; t < 64; t++)
282     {
283       ConvertNote (n);
284       UniNewline ();
285       n += of.numchn;
286     }
287   return UniDup ();
288 }
289
290 /* Loads all patterns of a modfile and converts them into the 3 byte format. */
291 static BOOL 
292 ML_LoadPatterns (void)
293 {
294   int t, s, tracks = 0;
295
296   if (!AllocPatterns ())
297     return 0;
298   if (!AllocTracks ())
299     return 0;
300
301   /* Allocate temporary buffer for loading and converting the patterns */
302   if (!(patbuf = (MODNOTE *) _mm_calloc (64U * of.numchn, sizeof (MODNOTE))))
303     return 0;
304
305   for (t = 0; t < of.numpat; t++)
306     {
307       /* Load the pattern into the temp buffer and convert it */
308       for (s = 0; s < (64U * of.numchn); s++)
309         {
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);
314         }
315       for (s = 0; s < of.numchn; s++)
316         if (!(of.tracks[tracks++] = ConvertTrack (patbuf + s)))
317           return 0;
318     }
319   return 1;
320 }
321
322 static BOOL 
323 MOD_Load (BOOL curious)
324 {
325   int t, scan;
326   SAMPLE *q;
327   MSAMPINFO *s;
328   BOOL is_orpheus = 0;
329
330   /* try to read module header */
331   _mm_read_string ((CHAR *) mh->songname, 20, modreader);
332   mh->songname[20] = 0;         /* just in case */
333
334   for (t = 0; t < 31; t++)
335     {
336       s = &mh->samples[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);
344     }
345
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);
350
351   if (_mm_eof (modreader))
352     {
353       _mm_errno = MMERR_LOADING_HEADER;
354       return 0;
355     }
356
357   /* set module variables */
358   of.initspeed = 6;
359   of.inittempo = 125;
360   of.numchn = modtypes[modtype].channels;
361   of.songname = DupStr (mh->songname, 21, 1);
362   of.numpos = mh->songlength;
363   of.reppos = 0;
364
365   /* Count the number of patterns */
366   of.numpat = 0;
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 */
373   scan = 1;
374   for (t = of.numpos; t < 128; t++)
375     if (mh->positions[t] >= 0x80)
376       scan = 0;
377   if (scan)
378     for (t = of.numpos; t < 128; t++)
379       {
380         if (mh->positions[t] > of.numpat)
381           of.numpat = mh->positions[t];
382         if ((curious) && (mh->positions[t]))
383           of.numpos = t + 1;
384       }
385   of.numpat++;
386   of.numtrk = of.numpat * of.numchn;
387
388   if (!AllocPositions (of.numpos))
389     return 0;
390   for (t = 0; t < of.numpos; t++)
391     of.positions[t] = mh->positions[t];
392
393   /* Finally, init the sampleinfo structures  */
394   of.numins = of.numsmp = 31;
395   if (!AllocSamples ())
396     return 0;
397   s = mh->samples;
398   q = of.samples;
399   for (t = 0; t < of.numins; t++)
400     {
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))
412         {
413           q->flags |= SF_16BITS;
414           is_orpheus = 1;
415         }
416
417       if (s->replen > 1)
418         q->flags |= SF_LOOP;
419       /* fix replen if repend > length */
420       if (q->loopend > q->length)
421         q->loopend = q->length;
422
423       s++;
424       q++;
425     }
426
427   if (is_orpheus)
428     of.modtype = strdup (orpheus);
429   else
430     of.modtype = strdup (modtypes[modtype].name);
431
432   if (!ML_LoadPatterns ())
433     return 0;
434   return 1;
435 }
436
437 static CHAR *
438 MOD_LoadTitle (void)
439 {
440   CHAR s[21];
441
442   _mm_fseek (modreader, 0, SEEK_SET);
443   if (!_mm_read_UBYTES (s, 20, modreader))
444     return NULL;
445   s[20] = 0;                    /* just in case */
446
447   return (DupStr (s, 21, 1));
448 }
449
450 /*========== Loader information */
451
452 MLOADER load_mod =
453 {
454   NULL,
455   "Standard module",
456   "MOD (31 instruments)",
457   MOD_Init,
458   MOD_Test,
459   MOD_Load,
460   MOD_Cleanup,
461   MOD_LoadTitle
462 };
463
464 /* ex:set ts=4: */