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 These routines are used to access the available module loaders
27 ==============================================================================*/
37 #include "unimod_priv.h"
46 static MLOADER *firstloader = NULL;
50 8363, 8413, 8463, 8529, 8581, 8651, 8723, 8757,
51 7895, 7941, 7985, 8046, 8107, 8169, 8232, 8280
54 /* This is a handle of sorts attached to any sample registered with
56 typedef struct SAMPLOAD
58 struct SAMPLOAD *next;
60 ULONG length; /* length of sample (in samples!) */
61 ULONG loopstart; /* repeat position (relative to start, in samples) */
62 ULONG loopend; /* repeat end */
70 static int sl_rlength;
72 static SWORD *sl_buffer = NULL;
73 static SAMPLOAD *musiclist = NULL;
75 /* size of the loader buffer in words */
76 #define SLBUFSIZE 2048
78 /* max # of KB to be devoted to samples */
79 /* #define MAX_SAMPLESPACE 1024 */
81 /* IT-Compressed status structure */
84 UWORD bits; /* current number of bits */
85 UWORD bufbits; /* bits in buffer */
86 SWORD last; /* last output */
87 UBYTE buf; /* bit buffer */
91 #ifdef MAX_SAMPLESPACE
92 static void SL_HalveSample (SAMPLOAD *);
94 static void SL_Sample8to16 (SAMPLOAD *);
95 static void SL_Sample16to8 (SAMPLOAD *);
96 static void SL_SampleSigned (SAMPLOAD *);
97 static void SL_SampleUnsigned (SAMPLOAD *);
98 static SAMPLOAD *SL_RegisterSample (SAMPLE *, URL);
99 static SWORD *SL_Load (SAMPLOAD *);
100 static BOOL SL_Init (SAMPLOAD *);
101 static void SL_Exit (SAMPLOAD *);
104 SL_Init (SAMPLOAD * s)
107 if (!(sl_buffer = _mm_malloc (SLBUFSIZE * sizeof (SWORD))))
110 sl_rlength = s->length;
111 if (s->infmt & SF_16BITS)
119 SL_Exit (SAMPLOAD * s)
122 _mm_fseek (s->reader, sl_rlength, SEEK_CUR);
130 /* unpack a 8bit IT packed sample */
132 read_itcompr8 (ITPACK * status, URL reader, SWORD * sl_buffer, UWORD count, UWORD * incnt)
134 SWORD *dest = sl_buffer, *end = sl_buffer + count;
135 UWORD x, y, needbits, havebits, new_count = 0;
136 UWORD bits = status->bits;
137 UWORD bufbits = status->bufbits;
138 SBYTE last = status->last;
139 UBYTE buf = status->buf;
143 needbits = new_count ? 3 : bits;
151 buf = _mm_read_UBYTE (reader);
156 /* get as many bits as necessary */
157 y = needbits < bufbits ? needbits : bufbits;
158 x |= (buf & ((1 << y) - 1)) << havebits;
174 if (x == (1 << (bits - 1)))
182 y = (0xff >> (9 - bits)) - 4;
183 if ((x > y) && (x <= y + 8))
185 if ((x -= y) >= bits)
195 bits = x - 0x100 + 1;
201 /* error in compressed data... */
202 _mm_errno = MMERR_ITPACK_INVALID_DATA;
206 if (bits < 8) /* extend sign */
207 x = ((SBYTE) (x << (8 - bits))) >> (8 - bits);
208 *(dest++) = (last += x) << 8; /* convert to 16 bit */
211 status->bufbits = bufbits;
214 return dest - sl_buffer;
217 /* unpack a 16bit IT packed sample */
219 read_itcompr16 (ITPACK * status, URL reader, SWORD * sl_buffer, UWORD count, UWORD * incnt)
221 SWORD *dest = sl_buffer, *end = sl_buffer + count;
222 SLONG x, y, needbits, havebits, new_count = 0;
223 UWORD bits = status->bits;
224 UWORD bufbits = status->bufbits;
225 SWORD last = status->last;
226 UBYTE buf = status->buf;
230 needbits = new_count ? 4 : bits;
238 buf = _mm_read_UBYTE (reader);
243 /* get as many bits as necessary */
244 y = needbits < bufbits ? needbits : bufbits;
245 x |= (buf & ((1 << y) - 1)) << havebits;
261 if (x == (1 << (bits - 1)))
269 y = (0xffff >> (17 - bits)) - 8;
270 if ((x > y) && (x <= y + 16))
272 if ((x -= y) >= bits)
282 bits = x - 0x10000 + 1;
288 /* error in compressed data... */
289 _mm_errno = MMERR_ITPACK_INVALID_DATA;
293 if (bits < 16) /* extend sign */
294 x = ((SWORD) (x << (16 - bits))) >> (16 - bits);
295 *(dest++) = (last += x);
298 status->bufbits = bufbits;
301 return dest - sl_buffer;
305 SL_LoadInternal (void *buffer, UWORD infmt, UWORD outfmt, int scalefactor, ULONG length, URL reader)
307 SBYTE *bptr = (SBYTE *) buffer;
308 SWORD *wptr = (SWORD *) buffer;
311 int result, c_block = 0; /* compression bytes until next block */
312 ITPACK status = { 0,0,0,0 };
317 stodo = (length < SLBUFSIZE) ? length : SLBUFSIZE;
319 if (infmt & SF_ITPACKED)
324 status.bits = (infmt & SF_16BITS) ? 17 : 9;
325 status.last = status.bufbits = 0;
326 incnt = _mm_read_I_UWORD (reader);
327 c_block = (infmt & SF_16BITS) ? 0x4000 : 0x8000;
328 if (infmt & SF_DELTA)
331 if (infmt & SF_16BITS)
333 if (!(result = read_itcompr16 (&status, reader, sl_buffer, stodo, &incnt)))
338 if (!(result = read_itcompr8 (&status, reader, sl_buffer, stodo, &incnt)))
343 _mm_errno = MMERR_ITPACK_INVALID_DATA;
350 if (infmt & SF_16BITS)
352 if (infmt & SF_BIG_ENDIAN)
353 _mm_read_M_SWORDS (sl_buffer, stodo, reader);
355 _mm_read_I_SWORDS (sl_buffer, stodo, reader);
359 /* Always convert to 16 bits for internal use */
363 _mm_read_UBYTES (sl_buffer, stodo, reader);
364 src = (SBYTE *) sl_buffer;
369 for (t = 0; t < stodo; t++)
379 if (infmt & SF_DELTA)
380 for (t = 0; t < stodo; t++)
382 sl_buffer[t] += sl_old;
383 sl_old = sl_buffer[t];
386 if ((infmt ^ outfmt) & SF_SIGNED)
387 for (t = 0; t < stodo; t++)
388 sl_buffer[t] ^= 0x8000;
391 if ((infmt & SF_STEREO) && !(outfmt & SF_STEREO))
393 /* dither stereo to mono, average together every two samples */
398 while (t < stodo && length)
400 avgval = sl_buffer[t++];
401 avgval += sl_buffer[t++];
402 sl_buffer[idx++] = avgval >> 1;
407 else if (scalefactor)
412 /* Sample Scaling... average values for better results. */
414 while (t < stodo && length)
417 for (u = scalefactor; u && t < stodo; u--, t++)
418 scaleval += sl_buffer[t];
419 sl_buffer[idx++] = scaleval / (scalefactor - u);
427 if (outfmt & SF_16BITS)
429 for (t = 0; t < stodo; t++)
430 *(wptr++) = sl_buffer[t];
434 for (t = 0; t < stodo; t++)
435 *(bptr++) = sl_buffer[t] >> 8;
442 SL_Load (struct SAMPLOAD *sload)
444 SAMPLE *s = sload->sample;
446 ULONG t, length, loopstart, loopend;
449 loopstart = s->loopstart;
450 loopend = s->loopend;
452 if (!(data = (SWORD *) _mm_malloc ((length + 20) << 1)))
454 _mm_errno = MMERR_SAMPLE_TOO_BIG;
458 /* read sample into buffer */
459 if (SL_LoadInternal (data, sload->infmt, sload->outfmt,
460 sload->scalefactor, length, sload->reader))
464 if (s->flags & SF_LOOP)
466 if (s->flags & SF_BIDI)
467 for (t = 0; t < 16; t++)
468 data[loopend + t] = data[(loopend - t) - 1];
470 for (t = 0; t < 16; t++)
471 data[loopend + t] = data[t + loopstart];
474 for (t = 0; t < 16; t++)
475 data[t + length] = 0;
480 /* Registers a sample for loading when SL_LoadSamples() is called. */
482 SL_RegisterSample (SAMPLE * s, URL reader)
484 SAMPLOAD *news, *cruise;
488 /* Allocate and add structure to the END of the list */
489 if (!(news = (SAMPLOAD *) _mm_malloc (sizeof (SAMPLOAD))))
495 cruise = cruise->next;
501 news->infmt = s->flags & SF_FORMATMASK;
502 news->outfmt = news->infmt;
503 news->reader = reader;
505 news->length = s->length;
506 news->loopstart = s->loopstart;
507 news->loopend = s->loopend;
511 news->outfmt &= ~SF_STEREO;
516 SL_SampleUnsigned (news);
517 SL_Sample16to8 (news);
521 SL_SampleSigned (news);
522 SL_Sample8to16 (news);
530 FreeSampleList (void)
532 SAMPLOAD *old, *s = musiclist;
543 /* Returns the total amount of memory required by the musiclist queue. */
544 #ifdef MAX_SAMPLESPACE
549 SAMPLOAD *samplist = musiclist;
554 s = samplist->sample;
555 s->flags = (s->flags & ~SF_FORMATMASK) | samplist->outfmt;
557 total += (s->length * ((s->flags & SF_16BITS) ? 2 : 1)) + 16;
559 samplist = samplist->next;
566 RealSpeed (SAMPLOAD * s)
568 return (s->sample->speed / (s->scalefactor ? s->scalefactor : 1));
573 SL_LoadSamples (void)
580 #ifdef MAX_SAMPLESPACE
581 while (SampleTotal () > (MAX_SAMPLESPACE * 1024))
583 /* First Pass - check for any 16 bit samples */
587 if (s->outfmt & SF_16BITS)
594 /* Second pass (if no 16bits found above) is to take the sample with
595 the highest speed and dither it by half. */
598 SAMPLOAD *c2smp = NULL;
599 ULONG maxsize, speed;
605 if ((s->sample->length) && (RealSpeed (s) > speed))
607 speed = RealSpeed (s);
613 SL_HalveSample (c2smp);
618 /* Samples dithered, now load them ! */
622 /* sample has to be loaded ? -> increase number of samples, allocate
623 memory and load sample. */
624 if (s->sample->length)
626 if (s->sample->seekpos)
627 _mm_fseek (s->reader, s->sample->seekpos, SEEK_SET);
629 /* Call the sample load routine of the driver module. It has to
630 return a pointer to sample dat). */
633 s->sample->data = SL_Load (s);
636 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
637 if (s->sample->data == NULL)
651 SL_Sample16to8 (SAMPLOAD * s)
653 s->outfmt &= ~SF_16BITS;
654 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
658 SL_Sample8to16 (SAMPLOAD * s)
660 s->outfmt |= SF_16BITS;
661 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
665 SL_SampleSigned (SAMPLOAD * s)
667 s->outfmt |= SF_SIGNED;
668 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
672 SL_SampleUnsigned (SAMPLOAD * s)
674 s->outfmt &= ~SF_SIGNED;
675 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
678 #ifdef MAX_SAMPLESPACE
680 SL_HalveSample (SAMPLOAD * s)
682 s->scalefactor = 2; /* this is a divisor */
683 s->sample->divfactor = 1; /* this is a shift count */
684 s->sample->length = s->length / s->scalefactor;
685 s->sample->loopstart = s->loopstart / s->scalefactor;
686 s->sample->loopend = s->loopend / s->scalefactor;
698 /* compute size of buffer */
699 for (l = firstloader; l; l = l->next)
700 len += 1 + (l->next ? 1 : 0) + strlen (l->version);
703 if ((list = _mm_malloc (len * sizeof (CHAR))))
706 /* list all registered module loders */
707 for (l = firstloader; l; l = l->next)
708 sprintf (list, (l->next) ? "%s%s\n" : "%s%s", list, l->version);
714 ReadComment (UWORD len)
720 if (!(of.comment = (CHAR *) _mm_malloc (len + 1)))
722 _mm_read_UBYTES (of.comment, len, modreader);
724 /* translate IT linefeeds */
725 for (i = 0; i < len; i++)
726 if (of.comment[i] == '\r')
727 of.comment[i] = '\n';
729 of.comment[len] = 0; /* just in case */
740 ReadLinedComment (UWORD lines, UWORD linelen)
742 CHAR *tempcomment, *line, *storage;
743 UWORD total = 0, t, len = lines * linelen;
748 if (!(tempcomment = (CHAR *) _mm_malloc (len + 1)))
750 if (!(storage = (CHAR *) _mm_malloc (linelen + 1)))
755 _mm_read_UBYTES (tempcomment, len, modreader);
757 /* compute message length */
758 for (line = tempcomment, total = t = 0; t < lines; t++, line += linelen)
760 for (i = linelen; (i >= 0) && (line[i] == ' '); i--)
762 for (i = 0; i < linelen; i++)
770 if (!(of.comment = (CHAR *) _mm_malloc (total + 1)))
777 /* convert message */
778 for (line = tempcomment, t = 0; t < lines; t++, line += linelen)
780 for (i = 0; i < linelen; i++)
781 if (!(storage[i] = line[i]))
783 storage[i] = 0; /* if (i==linelen) */
784 strcat (of.comment, storage);
785 strcat (of.comment, "\r");
795 AllocPositions (int total)
799 _mm_errno = MMERR_NOT_A_MODULE;
802 if (!(of.positions = _mm_calloc (total, sizeof (UWORD))))
810 int s, t, tracks = 0;
812 if ((!of.numpat) || (!of.numchn))
814 _mm_errno = MMERR_NOT_A_MODULE;
817 /* Allocate track sequencing array */
818 if (!(of.patterns = (UWORD *) _mm_calloc ((ULONG) (of.numpat + 1) * of.numchn, sizeof (UWORD))))
820 if (!(of.pattrows = (UWORD *) _mm_calloc (of.numpat + 1, sizeof (UWORD))))
823 for (t = 0; t <= of.numpat; t++)
826 for (s = 0; s < of.numchn; s++)
827 of.patterns[(t * of.numchn) + s] = tracks++;
838 _mm_errno = MMERR_NOT_A_MODULE;
841 if (!(of.tracks = (UBYTE **) _mm_calloc (of.numtrk, sizeof (UBYTE *))))
847 AllocInstruments (void)
853 _mm_errno = MMERR_NOT_A_MODULE;
856 if (!(of.instruments = (INSTRUMENT *) _mm_calloc (of.numins, sizeof (INSTRUMENT))))
859 for (t = 0; t < of.numins; t++)
861 for (n = 0; n < INSTNOTES; n++)
863 /* Init note / sample lookup table */
864 of.instruments[t].samplenote[n] = n;
865 of.instruments[t].samplenumber[n] = t;
867 of.instruments[t].globvol = 64;
879 _mm_errno = MMERR_NOT_A_MODULE;
882 if (!(of.samples = (SAMPLE *) _mm_calloc (of.numsmp, sizeof (SAMPLE))))
885 for (u = 0; u < of.numsmp; u++)
887 of.samples[u].panning = 128; /* center */
888 of.samples[u].data = NULL;
889 of.samples[u].globvol = 64;
890 of.samples[u].volume = 64;
896 ML_LoadSamples (void)
901 for (u = of.numsmp, s = of.samples; u; u--, s++)
903 SL_RegisterSample (s, modreader);
908 /* Creates a CSTR out of a character buffer of 'len' bytes, but strips any
909 terminating non-printing characters like 0, spaces etc. */
911 DupStr (CHAR * s, UWORD len, BOOL strict)
916 /* Scan for last printing char in buffer [includes high ascii up to 254] */
919 if (s[len - 1] > 0x20)
924 /* Scan forward for possible NULL character */
927 for (t = 0; t < len; t++)
934 /* When the buffer wasn't completely empty, allocate a cstring and copy the
935 buffer into that string, except for any control-chars */
936 if ((d = (CHAR *) _mm_malloc (sizeof (CHAR) * (len + 1))))
938 for (t = 0; t < len; t++)
939 d[t] = (s[t] < 32) ? '.' : s[t];
946 ML_XFreeSample (SAMPLE * s)
951 free (s->samplename);
955 ML_XFreeInstrument (INSTRUMENT * i)
962 ML_Free (MODULE * mf)
977 free (mf->positions);
985 for (t = 0; t < mf->numtrk; t++)
987 free (mf->tracks[t]);
992 for (t = 0; t < mf->numins; t++)
993 ML_XFreeInstrument (&mf->instruments[t]);
994 free (mf->instruments);
998 for (t = 0; t < mf->numsmp; t++)
999 if (mf->samples[t].length)
1000 ML_XFreeSample (&mf->samples[t]);
1003 memset (mf, 0, sizeof (MODULE));
1009 ML_AllocUniMod (void)
1011 return (_mm_malloc (sizeof (MODULE)));
1015 ML_LoadTitle (URL reader)
1022 /* Try to find a loader that recognizes the module */
1023 for (l = firstloader; l; l = l->next)
1025 _mm_rewind (modreader);
1032 _mm_errno = MMERR_NOT_A_MODULE;
1036 return l->LoadTitle ();
1039 /* Check if it is a module given a reader */
1041 ML_Test (URL reader)
1048 /* Try to find a loader that recognizes the module */
1049 for (l = firstloader; l; l = l->next)
1051 _mm_rewind (modreader);
1058 /* Loads a module given a reader */
1060 ML_Load (URL reader, int maxchan, BOOL curious)
1070 /* Try to find a loader that recognizes the module */
1071 for (l = firstloader; l; l = l->next)
1073 _mm_rewind (modreader);
1080 _mm_errno = MMERR_NOT_A_MODULE;
1081 _mm_rewind (modreader);
1085 /* init unitrk routines */
1088 _mm_rewind (modreader);
1092 /* load the song using the song's loader variable */
1093 memset (&of, 0, sizeof (MODULE));
1094 of.initvolume = 128;
1096 /* init panning array */
1097 for (t = 0; t < 64; t++)
1098 of.panning[t] = ((t + 1) & 2) ? 255 : 0;
1099 for (t = 0; t < 64; t++)
1102 /* init module loader and load the header / patterns */
1105 _mm_rewind (modreader);
1106 ok = l->Load (curious);
1111 /* free loader and unitrk allocations */
1118 _mm_rewind (modreader);
1122 if (!ML_LoadSamples ())
1125 _mm_rewind (modreader);
1129 if (!(mf = ML_AllocUniMod ()))
1135 /* Copy the static MODULE contents into the dynamic MODULE struct. */
1136 memcpy (mf, &of, sizeof (MODULE));
1140 if (!(mf->flags & UF_NNA) && (mf->numchn < maxchan))
1141 maxchan = mf->numchn;
1142 else if ((mf->numvoices) && (mf->numvoices < maxchan))
1143 maxchan = mf->numvoices;
1145 if (maxchan < mf->numchn)
1146 mf->flags |= UF_NNA;
1148 if (SL_LoadSamples ())
1158 ML_RegisterAllLoaders (void)
1160 MLOADER *last = NULL;
1165 #define LOADER(fmt) { \
1166 extern MLOADER fmt; \
1168 firstloader = &fmt; \
1170 last->next = &fmt; \
1175 /* Most likely first */
1181 /* Then the others in alphabetic order */