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 Amiga MED module loader
27 ==============================================================================*/
35 #include "unimod_priv.h"
37 /*========== Module information */
39 typedef struct MEDHEADER
43 ULONG pMEDSONG; /* struct MEDSONG *song; */
44 UWORD psecnum; /* for the player routine, MMD2 only */
45 UWORD pseq; /* " " " " */
46 ULONG pMEDBLOCKP; /* struct MMD0Block **blockarr; */
48 ULONG ppMedInstrHdr; /* struct InstrHdr **smplarr; */
50 ULONG pMEDEXP; /* struct MEDEXP *expdata; */
52 UWORD pstate; /* some data for the player routine */
58 UBYTE extra_songs; /* number of songs - 1 */
62 typedef struct MEDSAMPLE
64 UWORD rep, replen; /* offs: 0(s), 2(s) */
65 UBYTE midich; /* offs: 4(s) */
66 UBYTE midipreset; /* offs: 5(s) */
67 UBYTE svol; /* offs: 6(s) */
68 SBYTE strans; /* offs: 7(s) */
72 typedef struct MEDSONG
74 MEDSAMPLE sample[63]; /* 63 * 8 bytes = 504 bytes */
75 UWORD numblocks; /* offs: 504 */
76 UWORD songlen; /* offs: 506 */
77 UBYTE playseq[256]; /* offs: 508 */
78 UWORD deftempo; /* offs: 764 */
79 SBYTE playtransp; /* offs: 766 */
80 UBYTE flags; /* offs: 767 */
81 UBYTE flags2; /* offs: 768 */
82 UBYTE tempo2; /* offs: 769 */
83 UBYTE trkvol[16]; /* offs: 770 */
84 UBYTE mastervol; /* offs: 786 */
85 UBYTE numsamples; /* offs: 787 */
91 ULONG nextmod; /* pointer to next module */
92 ULONG exp_smp; /* pointer to InstrExt array */
95 ULONG annotxt; /* pointer to annotation text */
97 ULONG iinfo; /* pointer to InstrInfo array */
104 ULONG songname; /* pointer to songname */
111 typedef struct MMD0NOTE
117 typedef struct MMD1NOTE
123 typedef struct MEDINSTHEADER
127 /* Followed by actual data */
131 typedef struct MEDINSTEXT
135 UBYTE suppress_midi_off;
140 typedef struct MEDINSTINFO
146 /*========== Loader variables */
148 #define MMD0_string 0x4D4D4430
149 #define MMD1_string 0x4D4D4431
151 static MEDHEADER *mh = NULL;
152 static MEDSONG *ms = NULL;
153 static MEDEXP *me = NULL;
154 static ULONG *ba = NULL;
155 static MMD0NOTE *mmd0pat = NULL;
156 static MMD1NOTE *mmd1pat = NULL;
158 static BOOL decimalvolumes;
159 static BOOL bpmtempos;
161 #define d0note(row,col) mmd0pat[((row)*(UWORD)of.numchn)+(col)]
162 #define d1note(row,col) mmd1pat[((row)*(UWORD)of.numchn)+(col)]
164 static CHAR MED_Version[] = "OctaMED (MMDx)";
166 /*========== Loader code */
173 if (!_mm_read_UBYTES (id, 4, modreader))
175 if ((!memcmp (id, "MMD0", 4)) || (!memcmp (id, "MMD1", 4)))
183 if (!(me = (MEDEXP *) _mm_malloc (sizeof (MEDEXP))))
185 if (!(mh = (MEDHEADER *) _mm_malloc (sizeof (MEDHEADER))))
187 if (!(ms = (MEDSONG *) _mm_malloc (sizeof (MEDSONG))))
204 EffectCvt (UBYTE eff, UBYTE dat)
208 /* 0x0 0x1 0x2 0x3 0x4 PT effects */
209 case 0x5: /* PT vibrato with speed/depth nibbles swapped */
210 UniPTEffect (0x4, (dat >> 4) | ((dat & 0xf) << 4));
212 /* 0x6 0x7 not used */
216 case 0x8: /* midi hold/decay */
223 UniEffect (UNI_S3MEFFECTA, dat);
233 UniPTEffect (0xf, dat);
236 UniEffect (UNI_MEDSPEED, ((UWORD) dat * 125) / (33 * 4));
239 /* 0xa 0xb PT effects */
242 dat = (dat >> 4) * 10 + (dat & 0xf);
243 UniPTEffect (0xc, dat);
245 case 0xd: /* same as PT volslide */
246 UniPTEffect (0xa, dat);
248 case 0xe: /* synth jmp - midi */
253 case 0: /* patternbreak */
254 UniPTEffect (0xd, 0);
256 case 0xf1: /* play note twice */
257 UniWriteByte (UNI_MEDEFFECTF1);
259 case 0xf2: /* delay note */
260 UniWriteByte (UNI_MEDEFFECTF2);
262 case 0xf3: /* play note three times */
263 UniWriteByte (UNI_MEDEFFECTF3);
265 case 0xfe: /* stop playing */
266 UniPTEffect (0xb, of.numpat);
268 case 0xff: /* note cut */
269 UniPTEffect (0xc, 0);
273 UniPTEffect (0xf, dat);
277 UniPTEffect (0xf, (dat < 32) ? 32 : dat);
279 UniEffect (UNI_MEDSPEED, ((UWORD) dat * 125) / 33);
283 default: /* all normal PT effects are handled here */
284 UniPTEffect (eff, dat);
290 MED_Convert1 (int count, int col)
293 UBYTE inst, note, eff, dat;
297 for (t = 0; t < count; t++)
299 n = &d1note (t, col);
307 UniInstrument (inst - 1);
309 UniNote (note + 3 * OCTAVE - 1);
310 EffectCvt (eff, dat);
317 MED_Convert0 (int count, int col)
320 UBYTE a, b, inst, note, eff, dat;
324 for (t = 0; t < count; t++)
326 n = &d0note (t, col);
332 a = ((a & 1) << 1) | (a >> 1);
333 inst = (b >> 4) | (a << 4);
338 UniInstrument (inst - 1);
340 UniNote (note + 3 * OCTAVE - 1);
341 EffectCvt (eff, dat);
348 LoadMMD0Patterns (void)
351 UWORD numtracks, numlines, maxlines = 0, track = 0;
354 /* first, scan patterns to see how many channels are used */
355 for (t = 0; t < of.numpat; t++)
357 _mm_fseek (modreader, ba[t], SEEK_SET);
358 numtracks = _mm_read_UBYTE (modreader);
359 numlines = _mm_read_UBYTE (modreader);
361 if (numtracks > of.numchn)
362 of.numchn = numtracks;
363 if (numlines > maxlines)
367 of.numtrk = of.numpat * of.numchn;
370 if (!AllocPatterns ())
373 if (!(mmd0pat = (MMD0NOTE *) _mm_calloc (of.numchn * (maxlines + 1), sizeof (MMD0NOTE))))
376 /* second read: read and convert patterns */
377 for (t = 0; t < of.numpat; t++)
379 _mm_fseek (modreader, ba[t], SEEK_SET);
380 numtracks = _mm_read_UBYTE (modreader);
381 numlines = _mm_read_UBYTE (modreader);
383 of.pattrows[t] = ++numlines;
384 memset (mmdp = mmd0pat, 0, of.numchn * maxlines * sizeof (MMD0NOTE));
385 for (row = numlines; row; row--)
387 for (col = numtracks; col; col--, mmdp++)
389 mmdp->a = _mm_read_UBYTE (modreader);
390 mmdp->b = _mm_read_UBYTE (modreader);
391 mmdp->c = _mm_read_UBYTE (modreader);
395 for (col = 0; col < of.numchn; col++)
396 of.tracks[track++] = MED_Convert0 (numlines, col);
402 LoadMMD1Patterns (void)
405 UWORD numtracks, numlines, maxlines = 0, track = 0;
408 /* first, scan patterns to see how many channels are used */
409 for (t = 0; t < of.numpat; t++)
411 _mm_fseek (modreader, ba[t], SEEK_SET);
412 numtracks = _mm_read_M_UWORD (modreader);
413 numlines = _mm_read_M_UWORD (modreader);
414 if (numtracks > of.numchn)
415 of.numchn = numtracks;
416 if (numlines > maxlines)
420 of.numtrk = of.numpat * of.numchn;
423 if (!AllocPatterns ())
426 if (!(mmd1pat = (MMD1NOTE *) _mm_calloc (of.numchn * (maxlines + 1), sizeof (MMD1NOTE))))
429 /* second read: really read and convert patterns */
430 for (t = 0; t < of.numpat; t++)
432 _mm_fseek (modreader, ba[t], SEEK_SET);
433 numtracks = _mm_read_M_UWORD (modreader);
434 numlines = _mm_read_M_UWORD (modreader);
436 _mm_fseek (modreader, sizeof (ULONG), SEEK_CUR);
437 of.pattrows[t] = ++numlines;
438 memset (mmdp = mmd1pat, 0, of.numchn * maxlines * sizeof (MMD1NOTE));
440 for (row = numlines; row; row--)
442 for (col = numtracks; col; col--, mmdp++)
444 mmdp->a = _mm_read_UBYTE (modreader);
445 mmdp->b = _mm_read_UBYTE (modreader);
446 mmdp->c = _mm_read_UBYTE (modreader);
447 mmdp->d = _mm_read_UBYTE (modreader);
451 for (col = 0; col < of.numchn; col++)
452 of.tracks[track++] = MED_Convert1 (numlines, col);
458 MED_Load (BOOL curious)
466 /* try to read module header */
467 mh->id = _mm_read_M_ULONG (modreader);
468 mh->modlen = _mm_read_M_ULONG (modreader);
469 mh->pMEDSONG = _mm_read_M_ULONG (modreader);
470 mh->psecnum = _mm_read_M_UWORD (modreader);
471 mh->pseq = _mm_read_M_UWORD (modreader);
472 mh->pMEDBLOCKP = _mm_read_M_ULONG (modreader);
473 mh->reserved1 = _mm_read_M_ULONG (modreader);
474 mh->ppMedInstrHdr = _mm_read_M_ULONG (modreader);
475 mh->reserved2 = _mm_read_M_ULONG (modreader);
476 mh->pMEDEXP = _mm_read_M_ULONG (modreader);
477 mh->reserved3 = _mm_read_M_ULONG (modreader);
478 mh->pstate = _mm_read_M_UWORD (modreader);
479 mh->pblock = _mm_read_M_UWORD (modreader);
480 mh->pline = _mm_read_M_UWORD (modreader);
481 mh->pseqnum = _mm_read_M_UWORD (modreader);
482 mh->actplayline = _mm_read_M_SWORD (modreader);
483 mh->counter = _mm_read_UBYTE (modreader);
484 mh->extra_songs = _mm_read_UBYTE (modreader);
486 /* Seek to MEDSONG struct */
487 _mm_fseek (modreader, mh->pMEDSONG, SEEK_SET);
489 /* Load the MMD0 Song Header */
490 mss = ms->sample; /* load the sample data first */
491 for (t = 63; t; t--, mss++)
493 mss->rep = _mm_read_M_UWORD (modreader);
494 mss->replen = _mm_read_M_UWORD (modreader);
495 mss->midich = _mm_read_UBYTE (modreader);
496 mss->midipreset = _mm_read_UBYTE (modreader);
497 mss->svol = _mm_read_UBYTE (modreader);
498 mss->strans = _mm_read_SBYTE (modreader);
501 ms->numblocks = _mm_read_M_UWORD (modreader);
502 ms->songlen = _mm_read_M_UWORD (modreader);
503 _mm_read_UBYTES (ms->playseq, 256, modreader);
504 ms->deftempo = _mm_read_M_UWORD (modreader);
505 ms->playtransp = _mm_read_SBYTE (modreader);
506 ms->flags = _mm_read_UBYTE (modreader);
507 ms->flags2 = _mm_read_UBYTE (modreader);
508 ms->tempo2 = _mm_read_UBYTE (modreader);
509 _mm_read_UBYTES (ms->trkvol, 16, modreader);
510 ms->mastervol = _mm_read_UBYTE (modreader);
511 ms->numsamples = _mm_read_UBYTE (modreader);
513 /* check for a bad header */
514 if (_mm_eof (modreader))
516 _mm_errno = MMERR_LOADING_HEADER;
520 /* load extension structure */
523 _mm_fseek (modreader, mh->pMEDEXP, SEEK_SET);
524 me->nextmod = _mm_read_M_ULONG (modreader);
525 me->exp_smp = _mm_read_M_ULONG (modreader);
526 me->s_ext_entries = _mm_read_M_UWORD (modreader);
527 me->s_ext_entrsz = _mm_read_M_UWORD (modreader);
528 me->annotxt = _mm_read_M_ULONG (modreader);
529 me->annolen = _mm_read_M_ULONG (modreader);
530 me->iinfo = _mm_read_M_ULONG (modreader);
531 me->i_ext_entries = _mm_read_M_UWORD (modreader);
532 me->i_ext_entrsz = _mm_read_M_UWORD (modreader);
533 me->jumpmask = _mm_read_M_ULONG (modreader);
534 me->rgbtable = _mm_read_M_ULONG (modreader);
535 me->channelsplit = _mm_read_M_ULONG (modreader);
536 me->n_info = _mm_read_M_ULONG (modreader);
537 me->songname = _mm_read_M_ULONG (modreader);
538 me->songnamelen = _mm_read_M_ULONG (modreader);
539 me->dumps = _mm_read_M_ULONG (modreader);
542 /* seek to and read the samplepointer array */
543 _mm_fseek (modreader, mh->ppMedInstrHdr, SEEK_SET);
544 if (!_mm_read_M_ULONGS (sa, ms->numsamples, modreader))
546 _mm_errno = MMERR_LOADING_HEADER;
550 /* alloc and read the blockpointer array */
551 if (!(ba = (ULONG *) _mm_calloc (ms->numblocks, sizeof (ULONG))))
553 _mm_fseek (modreader, mh->pMEDBLOCKP, SEEK_SET);
554 if (!_mm_read_M_ULONGS (ba, ms->numblocks, modreader))
556 _mm_errno = MMERR_LOADING_HEADER;
560 /* copy song positions */
561 if (!AllocPositions (ms->songlen))
563 for (t = 0; t < ms->songlen; t++)
564 of.positions[t] = ms->playseq[t];
566 decimalvolumes = (ms->flags & 0x10) ? 0 : 1;
567 bpmtempos = (ms->flags2 & 0x20) ? 1 : 0;
571 int bpmlen = (ms->flags2 & 0x1f) + 1;
572 of.initspeed = ms->tempo2;
573 of.inittempo = ms->deftempo * bpmlen / 4;
577 /* Let's do some math : compute GCD of BPM beat length and speed */
589 while ((a != b) && (a))
603 of.inittempo = ms->deftempo * bpmlen / (4 * b);
608 of.initspeed = ms->tempo2;
609 of.inittempo = ms->deftempo ? ((UWORD) ms->deftempo * 125) / 33 : 128;
610 if ((ms->deftempo <= 10) && (ms->deftempo))
611 of.inittempo = (of.inittempo * 33) / 6;
612 of.flags |= UF_HIGHBPM;
614 MED_Version[12] = mh->id;
615 of.modtype = strdup (MED_Version);
616 of.numchn = 0; /* will be counted later */
617 of.numpat = ms->numblocks;
618 of.numpos = ms->songlen;
619 of.numins = ms->numsamples;
620 of.numsmp = of.numins;
622 if ((mh->pMEDEXP) && (me->songname) && (me->songnamelen))
626 _mm_fseek (modreader, me->songname, SEEK_SET);
627 name = _mm_malloc (me->songnamelen);
628 _mm_read_UBYTES (name, me->songnamelen, modreader);
629 of.songname = DupStr (name, me->songnamelen, 1);
633 of.songname = DupStr (NULL, 0, 0);
634 if ((mh->pMEDEXP) && (me->annotxt) && (me->annolen))
636 _mm_fseek (modreader, me->annotxt, SEEK_SET);
637 ReadComment (me->annolen);
640 if (!AllocSamples ())
643 for (t = 0; t < of.numins; t++)
645 q->flags = SF_SIGNED;
649 _mm_fseek (modreader, sa[t], SEEK_SET);
650 s.length = _mm_read_M_ULONG (modreader);
651 s.type = _mm_read_M_SWORD (modreader);
656 fputs ("\rNon-sample instruments not supported in MED loader yet\n", stderr);
660 _mm_errno = MMERR_MED_SYNTHSAMPLES;
666 if (_mm_eof (modreader))
668 _mm_errno = MMERR_LOADING_SAMPLEINFO;
672 q->length = s.length;
673 q->seekpos = _mm_ftell (modreader);
674 q->loopstart = ms->sample[t].rep << 1;
675 q->loopend = q->loopstart + (ms->sample[t].replen << 1);
677 if (ms->sample[t].replen > 1)
680 /* don't load sample if length>='MMD0'...
681 such kluges make libmikmod's code unique !!! */
682 if (q->length >= MMD0_string)
688 if ((mh->pMEDEXP) && (me->exp_smp) &&
689 (t < me->s_ext_entries) && (me->s_ext_entrsz >= 4))
693 _mm_fseek (modreader, me->exp_smp + t * me->s_ext_entrsz, SEEK_SET);
694 ie.hold = _mm_read_UBYTE (modreader);
695 ie.decay = _mm_read_UBYTE (modreader);
696 ie.suppress_midi_off = _mm_read_UBYTE (modreader);
697 ie.finetune = _mm_read_SBYTE (modreader);
699 q->speed = finetune[ie.finetune & 0xf];
704 if ((mh->pMEDEXP) && (me->iinfo) &&
705 (t < me->i_ext_entries) && (me->i_ext_entrsz >= 40))
709 _mm_fseek (modreader, me->iinfo + t * me->i_ext_entrsz, SEEK_SET);
710 _mm_read_UBYTES (ii.name, 40, modreader);
711 q->samplename = DupStr ((CHAR *) ii.name, 40, 1);
714 q->samplename = NULL;
719 if (mh->id == MMD0_string)
721 if (!LoadMMD0Patterns ())
723 _mm_errno = MMERR_LOADING_PATTERN;
727 else if (mh->id == MMD1_string)
729 if (!LoadMMD1Patterns ())
731 _mm_errno = MMERR_LOADING_PATTERN;
737 _mm_errno = MMERR_NOT_A_MODULE;
743 /*========== Loader information */