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 These routines are used to access the available module loaders
26
27 ==============================================================================*/
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #ifdef HAVE_MEMORY_H
34 #include <memory.h>
35 #endif
36
37 #include "unimod_priv.h"
38
39 #include <string.h>
40
41 URL modreader;
42 MODULE of;
43 BOOL ML_8bitsamples;
44 BOOL ML_monosamples;
45
46 static MLOADER *firstloader = NULL;
47
48 UWORD finetune[16] =
49 {
50 8363, 8413, 8463, 8529, 8581, 8651, 8723, 8757,
51 7895, 7941, 7985, 8046, 8107, 8169, 8232, 8280
52 };
53
54 /* This is a handle of sorts attached to any sample registered with
55 SL_RegisterSample. */
56 typedef struct SAMPLOAD
57 {
58 struct SAMPLOAD *next;
59
60 ULONG length; /* length of sample (in samples!) */
61 ULONG loopstart; /* repeat position (relative to start, in samples) */
62 ULONG loopend; /* repeat end */
63 UWORD infmt, outfmt;
64 int scalefactor;
65 SAMPLE *sample;
66 URL reader;
67 }
68 SAMPLOAD;
69
70 static int sl_rlength;
71 static SWORD sl_old;
72 static SWORD *sl_buffer = NULL;
73 static SAMPLOAD *musiclist = NULL;
74
75 /* size of the loader buffer in words */
76 #define SLBUFSIZE 2048
77
78 /* max # of KB to be devoted to samples */
79 /* #define MAX_SAMPLESPACE 1024 */
80
81 /* IT-Compressed status structure */
82 typedef struct ITPACK
83 {
84 UWORD bits; /* current number of bits */
85 UWORD bufbits; /* bits in buffer */
86 SWORD last; /* last output */
87 UBYTE buf; /* bit buffer */
88 }
89 ITPACK;
90
91 #ifdef MAX_SAMPLESPACE
92 static void SL_HalveSample (SAMPLOAD *);
93 #endif
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 *);
102
103 BOOL
SL_Init(SAMPLOAD * s)104 SL_Init (SAMPLOAD * s)
105 {
106 if (!sl_buffer)
107 if (!(sl_buffer = _mm_malloc (SLBUFSIZE * sizeof (SWORD))))
108 return 0;
109
110 sl_rlength = s->length;
111 if (s->infmt & SF_16BITS)
112 sl_rlength >>= 1;
113 sl_old = 0;
114
115 return 1;
116 }
117
118 void
SL_Exit(SAMPLOAD * s)119 SL_Exit (SAMPLOAD * s)
120 {
121 if (sl_rlength > 0)
122 _mm_fseek (s->reader, sl_rlength, SEEK_CUR);
123 if (sl_buffer)
124 {
125 free (sl_buffer);
126 sl_buffer = NULL;
127 }
128 }
129
130 /* unpack a 8bit IT packed sample */
131 static BOOL
read_itcompr8(ITPACK * status,URL reader,SWORD * sl_buffer,UWORD count,UWORD * incnt)132 read_itcompr8 (ITPACK * status, URL reader, SWORD * sl_buffer, UWORD count, UWORD * incnt)
133 {
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;
140
141 while (dest < end)
142 {
143 needbits = new_count ? 3 : bits;
144 x = havebits = 0;
145 while (needbits)
146 {
147 /* feed buffer */
148 if (!bufbits)
149 {
150 if ((*incnt)--)
151 buf = _mm_read_UBYTE (reader);
152 else
153 buf = 0;
154 bufbits = 8;
155 }
156 /* get as many bits as necessary */
157 y = needbits < bufbits ? needbits : bufbits;
158 x |= (buf & ((1 << y) - 1)) << havebits;
159 buf >>= y;
160 bufbits -= y;
161 needbits -= y;
162 havebits += y;
163 }
164 if (new_count)
165 {
166 new_count = 0;
167 if (++x >= bits)
168 x++;
169 bits = x;
170 continue;
171 }
172 if (bits < 7)
173 {
174 if (x == (1 << (bits - 1)))
175 {
176 new_count = 1;
177 continue;
178 }
179 }
180 else if (bits < 9)
181 {
182 y = (0xff >> (9 - bits)) - 4;
183 if ((x > y) && (x <= y + 8))
184 {
185 if ((x -= y) >= bits)
186 x++;
187 bits = x;
188 continue;
189 }
190 }
191 else if (bits < 10)
192 {
193 if (x >= 0x100)
194 {
195 bits = x - 0x100 + 1;
196 continue;
197 }
198 }
199 else
200 {
201 /* error in compressed data... */
202 _mm_errno = MMERR_ITPACK_INVALID_DATA;
203 return 0;
204 }
205
206 if (bits < 8) /* extend sign */
207 x = ((SBYTE) (x << (8 - bits))) >> (8 - bits);
208 *(dest++) = (last += x) << 8; /* convert to 16 bit */
209 }
210 status->bits = bits;
211 status->bufbits = bufbits;
212 status->last = last;
213 status->buf = buf;
214 return dest - sl_buffer;
215 }
216
217 /* unpack a 16bit IT packed sample */
218 static BOOL
read_itcompr16(ITPACK * status,URL reader,SWORD * sl_buffer,UWORD count,UWORD * incnt)219 read_itcompr16 (ITPACK * status, URL reader, SWORD * sl_buffer, UWORD count, UWORD * incnt)
220 {
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;
227
228 while (dest < end)
229 {
230 needbits = new_count ? 4 : bits;
231 x = havebits = 0;
232 while (needbits)
233 {
234 /* feed buffer */
235 if (!bufbits)
236 {
237 if ((*incnt)--)
238 buf = _mm_read_UBYTE (reader);
239 else
240 buf = 0;
241 bufbits = 8;
242 }
243 /* get as many bits as necessary */
244 y = needbits < bufbits ? needbits : bufbits;
245 x |= (buf & ((1 << y) - 1)) << havebits;
246 buf >>= y;
247 bufbits -= y;
248 needbits -= y;
249 havebits += y;
250 }
251 if (new_count)
252 {
253 new_count = 0;
254 if (++x >= bits)
255 x++;
256 bits = x;
257 continue;
258 }
259 if (bits < 7)
260 {
261 if (x == (1 << (bits - 1)))
262 {
263 new_count = 1;
264 continue;
265 }
266 }
267 else if (bits < 17)
268 {
269 y = (0xffff >> (17 - bits)) - 8;
270 if ((x > y) && (x <= y + 16))
271 {
272 if ((x -= y) >= bits)
273 x++;
274 bits = x;
275 continue;
276 }
277 }
278 else if (bits < 18)
279 {
280 if (x >= 0x10000)
281 {
282 bits = x - 0x10000 + 1;
283 continue;
284 }
285 }
286 else
287 {
288 /* error in compressed data... */
289 _mm_errno = MMERR_ITPACK_INVALID_DATA;
290 return 0;
291 }
292
293 if (bits < 16) /* extend sign */
294 x = ((SWORD) (x << (16 - bits))) >> (16 - bits);
295 *(dest++) = (last += x);
296 }
297 status->bits = bits;
298 status->bufbits = bufbits;
299 status->last = last;
300 status->buf = buf;
301 return dest - sl_buffer;
302 }
303
304 static BOOL
SL_LoadInternal(void * buffer,UWORD infmt,UWORD outfmt,int scalefactor,ULONG length,URL reader)305 SL_LoadInternal (void *buffer, UWORD infmt, UWORD outfmt, int scalefactor, ULONG length, URL reader)
306 {
307 SBYTE *bptr = (SBYTE *) buffer;
308 SWORD *wptr = (SWORD *) buffer;
309 int stodo, t, u;
310
311 int result, c_block = 0; /* compression bytes until next block */
312 ITPACK status = { 0,0,0,0 };
313 UWORD incnt = 0;
314
315 while (length)
316 {
317 stodo = (length < SLBUFSIZE) ? length : SLBUFSIZE;
318
319 if (infmt & SF_ITPACKED)
320 {
321 sl_rlength = 0;
322 if (!c_block)
323 {
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)
329 sl_old = 0;
330 }
331 if (infmt & SF_16BITS)
332 {
333 if (!(result = read_itcompr16 (&status, reader, sl_buffer, stodo, &incnt)))
334 return 1;
335 }
336 else
337 {
338 if (!(result = read_itcompr8 (&status, reader, sl_buffer, stodo, &incnt)))
339 return 1;
340 }
341 if (result != stodo)
342 {
343 _mm_errno = MMERR_ITPACK_INVALID_DATA;
344 return 1;
345 }
346 c_block -= stodo;
347 }
348 else
349 {
350 if (infmt & SF_16BITS)
351 {
352 if (infmt & SF_BIG_ENDIAN)
353 _mm_read_M_SWORDS (sl_buffer, stodo, reader);
354 else
355 _mm_read_I_SWORDS (sl_buffer, stodo, reader);
356 }
357 else
358 {
359 /* Always convert to 16 bits for internal use */
360 SBYTE *src;
361 SWORD *dest;
362
363 _mm_read_UBYTES (sl_buffer, stodo, reader);
364 src = (SBYTE *) sl_buffer;
365 dest = sl_buffer;
366 src += stodo;
367 dest += stodo;
368
369 for (t = 0; t < stodo; t++)
370 {
371 src--;
372 dest--;
373 *dest = (*src) << 8;
374 }
375 }
376 sl_rlength -= stodo;
377 }
378
379 if (infmt & SF_DELTA)
380 for (t = 0; t < stodo; t++)
381 {
382 sl_buffer[t] += sl_old;
383 sl_old = sl_buffer[t];
384 }
385
386 if ((infmt ^ outfmt) & SF_SIGNED)
387 for (t = 0; t < stodo; t++)
388 sl_buffer[t] ^= 0x8000;
389
390 /* Dithering... */
391 if ((infmt & SF_STEREO) && !(outfmt & SF_STEREO))
392 {
393 /* dither stereo to mono, average together every two samples */
394 SLONG avgval;
395 int idx = 0;
396
397 t = 0;
398 while (t < stodo && length)
399 {
400 avgval = sl_buffer[t++];
401 avgval += sl_buffer[t++];
402 sl_buffer[idx++] = avgval >> 1;
403 length -= 2;
404 }
405 stodo = idx;
406 }
407 else if (scalefactor)
408 {
409 int idx = 0;
410 SLONG scaleval;
411
412 /* Sample Scaling... average values for better results. */
413 t = 0;
414 while (t < stodo && length)
415 {
416 scaleval = 0;
417 for (u = scalefactor; u && t < stodo; u--, t++)
418 scaleval += sl_buffer[t];
419 sl_buffer[idx++] = scaleval / (scalefactor - u);
420 length--;
421 }
422 stodo = idx;
423 }
424 else
425 length -= stodo;
426
427 if (outfmt & SF_16BITS)
428 {
429 for (t = 0; t < stodo; t++)
430 *(wptr++) = sl_buffer[t];
431 }
432 else
433 {
434 for (t = 0; t < stodo; t++)
435 *(bptr++) = sl_buffer[t] >> 8;
436 }
437 }
438 return 0;
439 }
440
441 static SWORD *
SL_Load(struct SAMPLOAD * sload)442 SL_Load (struct SAMPLOAD *sload)
443 {
444 SAMPLE *s = sload->sample;
445 SWORD *data;
446 ULONG t, length, loopstart, loopend;
447
448 length = s->length;
449 loopstart = s->loopstart;
450 loopend = s->loopend;
451
452 if (!(data = (SWORD *) _mm_malloc ((length + 20) << 1)))
453 {
454 _mm_errno = MMERR_SAMPLE_TOO_BIG;
455 return NULL;
456 }
457
458 /* read sample into buffer */
459 if (SL_LoadInternal (data, sload->infmt, sload->outfmt,
460 sload->scalefactor, length, sload->reader))
461 return NULL;
462
463 /* Unclick sample */
464 if (s->flags & SF_LOOP)
465 {
466 if (s->flags & SF_BIDI)
467 for (t = 0; t < 16; t++)
468 data[loopend + t] = data[(loopend - t) - 1];
469 else
470 for (t = 0; t < 16; t++)
471 data[loopend + t] = data[t + loopstart];
472 }
473 else
474 for (t = 0; t < 16; t++)
475 data[t + length] = 0;
476
477 return data;
478 }
479
480 /* Registers a sample for loading when SL_LoadSamples() is called. */
481 SAMPLOAD *
SL_RegisterSample(SAMPLE * s,URL reader)482 SL_RegisterSample (SAMPLE * s, URL reader)
483 {
484 SAMPLOAD *news, *cruise;
485
486 cruise = musiclist;
487
488 /* Allocate and add structure to the END of the list */
489 if (!(news = (SAMPLOAD *) _mm_malloc (sizeof (SAMPLOAD))))
490 return NULL;
491
492 if (cruise)
493 {
494 while (cruise->next)
495 cruise = cruise->next;
496 cruise->next = news;
497 }
498 else
499 musiclist = news;
500
501 news->infmt = s->flags & SF_FORMATMASK;
502 news->outfmt = news->infmt;
503 news->reader = reader;
504 news->sample = s;
505 news->length = s->length;
506 news->loopstart = s->loopstart;
507 news->loopend = s->loopend;
508
509 if (ML_monosamples)
510 {
511 news->outfmt &= ~SF_STEREO;
512 }
513
514 if (ML_8bitsamples)
515 {
516 SL_SampleUnsigned (news);
517 SL_Sample16to8 (news);
518 }
519 else
520 {
521 SL_SampleSigned (news);
522 SL_Sample8to16 (news);
523 }
524
525
526 return news;
527 }
528
529 static void
FreeSampleList(void)530 FreeSampleList (void)
531 {
532 SAMPLOAD *old, *s = musiclist;
533
534 while (s)
535 {
536 old = s;
537 s = s->next;
538 free (old);
539 }
540 musiclist = NULL;
541 }
542
543 /* Returns the total amount of memory required by the musiclist queue. */
544 #ifdef MAX_SAMPLESPACE
545 static ULONG
SampleTotal(void)546 SampleTotal (void)
547 {
548 int total = 0;
549 SAMPLOAD *samplist = musiclist;
550 SAMPLE *s;
551
552 while (samplist)
553 {
554 s = samplist->sample;
555 s->flags = (s->flags & ~SF_FORMATMASK) | samplist->outfmt;
556
557 total += (s->length * ((s->flags & SF_16BITS) ? 2 : 1)) + 16;
558
559 samplist = samplist->next;
560 }
561
562 return total;
563 }
564
565 static ULONG
RealSpeed(SAMPLOAD * s)566 RealSpeed (SAMPLOAD * s)
567 {
568 return (s->sample->speed / (s->scalefactor ? s->scalefactor : 1));
569 }
570 #endif
571
572 BOOL
SL_LoadSamples(void)573 SL_LoadSamples (void)
574 {
575 SAMPLOAD *s;
576
577 if (!musiclist)
578 return 0;
579
580 #ifdef MAX_SAMPLESPACE
581 while (SampleTotal () > (MAX_SAMPLESPACE * 1024))
582 {
583 /* First Pass - check for any 16 bit samples */
584 s = musiclist;
585 while (s)
586 {
587 if (s->outfmt & SF_16BITS)
588 {
589 SL_Sample16to8 (s);
590 break;
591 }
592 s = s->next;
593 }
594 /* Second pass (if no 16bits found above) is to take the sample with
595 the highest speed and dither it by half. */
596 if (!s)
597 {
598 SAMPLOAD *c2smp = NULL;
599 ULONG maxsize, speed;
600
601 s = musiclist;
602 speed = 0;
603 while (s)
604 {
605 if ((s->sample->length) && (RealSpeed (s) > speed))
606 {
607 speed = RealSpeed (s);
608 c2smp = s;
609 }
610 s = s->next;
611 }
612 if (c2smp)
613 SL_HalveSample (c2smp);
614 }
615 }
616 #endif
617
618 /* Samples dithered, now load them ! */
619 s = musiclist;
620 while (s)
621 {
622 /* sample has to be loaded ? -> increase number of samples, allocate
623 memory and load sample. */
624 if (s->sample->length)
625 {
626 if (s->sample->seekpos)
627 _mm_fseek (s->reader, s->sample->seekpos, SEEK_SET);
628
629 /* Call the sample load routine of the driver module. It has to
630 return a pointer to sample dat). */
631 if (SL_Init (s))
632 {
633 s->sample->data = SL_Load (s);
634 SL_Exit (s);
635 }
636 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
637 if (s->sample->data == NULL)
638 {
639 FreeSampleList ();
640 return 1;
641 }
642 }
643 s = s->next;
644 }
645
646 FreeSampleList ();
647 return 0;
648 }
649
650 void
SL_Sample16to8(SAMPLOAD * s)651 SL_Sample16to8 (SAMPLOAD * s)
652 {
653 s->outfmt &= ~SF_16BITS;
654 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
655 }
656
657 void
SL_Sample8to16(SAMPLOAD * s)658 SL_Sample8to16 (SAMPLOAD * s)
659 {
660 s->outfmt |= SF_16BITS;
661 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
662 }
663
664 void
SL_SampleSigned(SAMPLOAD * s)665 SL_SampleSigned (SAMPLOAD * s)
666 {
667 s->outfmt |= SF_SIGNED;
668 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
669 }
670
671 void
SL_SampleUnsigned(SAMPLOAD * s)672 SL_SampleUnsigned (SAMPLOAD * s)
673 {
674 s->outfmt &= ~SF_SIGNED;
675 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
676 }
677
678 #ifdef MAX_SAMPLESPACE
679 void
SL_HalveSample(SAMPLOAD * s)680 SL_HalveSample (SAMPLOAD * s)
681 {
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;
687 }
688 #endif
689
690
691 CHAR *
ML_InfoLoader(void)692 ML_InfoLoader (void)
693 {
694 int len = 0;
695 MLOADER *l;
696 CHAR *list = NULL;
697
698 /* compute size of buffer */
699 for (l = firstloader; l; l = l->next)
700 len += 1 + (l->next ? 1 : 0) + strlen (l->version);
701
702 if (len)
703 if ((list = _mm_malloc (len * sizeof (CHAR))))
704 {
705 list[0] = 0;
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);
709 }
710 return list;
711 }
712
713 BOOL
ReadComment(UWORD len)714 ReadComment (UWORD len)
715 {
716 if (len)
717 {
718 int i;
719
720 if (!(of.comment = (CHAR *) _mm_malloc (len + 1)))
721 return 0;
722 _mm_read_UBYTES (of.comment, len, modreader);
723
724 /* translate IT linefeeds */
725 for (i = 0; i < len; i++)
726 if (of.comment[i] == '\r')
727 of.comment[i] = '\n';
728
729 of.comment[len] = 0; /* just in case */
730 }
731 if (!of.comment[0])
732 {
733 free (of.comment);
734 of.comment = NULL;
735 }
736 return 1;
737 }
738
739 BOOL
ReadLinedComment(UWORD lines,UWORD linelen)740 ReadLinedComment (UWORD lines, UWORD linelen)
741 {
742 CHAR *tempcomment, *line, *storage;
743 UWORD total = 0, t, len = lines * linelen;
744 int i;
745
746 if (lines)
747 {
748 if (!(tempcomment = (CHAR *) _mm_malloc (len + 1)))
749 return 0;
750 if (!(storage = (CHAR *) _mm_malloc (linelen + 1)))
751 {
752 free (tempcomment);
753 return 0;
754 }
755 _mm_read_UBYTES (tempcomment, len, modreader);
756
757 /* compute message length */
758 for (line = tempcomment, total = t = 0; t < lines; t++, line += linelen)
759 {
760 for (i = linelen; (i >= 0) && (line[i] == ' '); i--)
761 line[i] = 0;
762 for (i = 0; i < linelen; i++)
763 if (!line[i])
764 break;
765 total += 1 + i;
766 }
767
768 if (total > lines)
769 {
770 if (!(of.comment = (CHAR *) _mm_malloc (total + 1)))
771 {
772 free (storage);
773 free (tempcomment);
774 return 0;
775 }
776
777 /* convert message */
778 for (line = tempcomment, t = 0; t < lines; t++, line += linelen)
779 {
780 for (i = 0; i < linelen; i++)
781 if (!(storage[i] = line[i]))
782 break;
783 storage[i] = 0; /* if (i==linelen) */
784 strcat (of.comment, storage);
785 strcat (of.comment, "\r");
786 }
787 free (storage);
788 free (tempcomment);
789 }
790 }
791 return 1;
792 }
793
794 BOOL
AllocPositions(int total)795 AllocPositions (int total)
796 {
797 if (!total)
798 {
799 _mm_errno = MMERR_NOT_A_MODULE;
800 return 0;
801 }
802 if (!(of.positions = _mm_calloc (total, sizeof (UWORD))))
803 return 0;
804 return 1;
805 }
806
807 BOOL
AllocPatterns(void)808 AllocPatterns (void)
809 {
810 int s, t, tracks = 0;
811
812 if ((!of.numpat) || (!of.numchn))
813 {
814 _mm_errno = MMERR_NOT_A_MODULE;
815 return 0;
816 }
817 /* Allocate track sequencing array */
818 if (!(of.patterns = (UWORD *) _mm_calloc ((ULONG) (of.numpat + 1) * of.numchn, sizeof (UWORD))))
819 return 0;
820 if (!(of.pattrows = (UWORD *) _mm_calloc (of.numpat + 1, sizeof (UWORD))))
821 return 0;
822
823 for (t = 0; t <= of.numpat; t++)
824 {
825 of.pattrows[t] = 64;
826 for (s = 0; s < of.numchn; s++)
827 of.patterns[(t * of.numchn) + s] = tracks++;
828 }
829
830 return 1;
831 }
832
833 BOOL
AllocTracks(void)834 AllocTracks (void)
835 {
836 if (!of.numtrk)
837 {
838 _mm_errno = MMERR_NOT_A_MODULE;
839 return 0;
840 }
841 if (!(of.tracks = (UBYTE **) _mm_calloc (of.numtrk, sizeof (UBYTE *))))
842 return 0;
843 return 1;
844 }
845
846 BOOL
AllocInstruments(void)847 AllocInstruments (void)
848 {
849 int t, n;
850
851 if (!of.numins)
852 {
853 _mm_errno = MMERR_NOT_A_MODULE;
854 return 0;
855 }
856 if (!(of.instruments = (INSTRUMENT *) _mm_calloc (of.numins, sizeof (INSTRUMENT))))
857 return 0;
858
859 for (t = 0; t < of.numins; t++)
860 {
861 for (n = 0; n < INSTNOTES; n++)
862 {
863 /* Init note / sample lookup table */
864 of.instruments[t].samplenote[n] = n;
865 of.instruments[t].samplenumber[n] = t;
866 }
867 of.instruments[t].globvol = 64;
868 }
869 return 1;
870 }
871
872 BOOL
AllocSamples(void)873 AllocSamples (void)
874 {
875 UWORD u;
876
877 if (!of.numsmp)
878 {
879 _mm_errno = MMERR_NOT_A_MODULE;
880 return 0;
881 }
882 if (!(of.samples = (SAMPLE *) _mm_calloc (of.numsmp, sizeof (SAMPLE))))
883 return 0;
884
885 for (u = 0; u < of.numsmp; u++)
886 {
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;
891 }
892 return 1;
893 }
894
895 static BOOL
ML_LoadSamples(void)896 ML_LoadSamples (void)
897 {
898 SAMPLE *s;
899 int u;
900
901 for (u = of.numsmp, s = of.samples; u; u--, s++)
902 if (s->length)
903 SL_RegisterSample (s, modreader);
904
905 return 1;
906 }
907
908 /* Creates a CSTR out of a character buffer of 'len' bytes, but strips any
909 terminating non-printing characters like 0, spaces etc. */
910 CHAR *
DupStr(CHAR * s,UWORD len,BOOL strict)911 DupStr (CHAR * s, UWORD len, BOOL strict)
912 {
913 UWORD t;
914 CHAR *d = NULL;
915
916 /* Scan for last printing char in buffer [includes high ascii up to 254] */
917 while (len)
918 {
919 if (s[len - 1] > 0x20)
920 break;
921 len--;
922 }
923
924 /* Scan forward for possible NULL character */
925 if (strict)
926 {
927 for (t = 0; t < len; t++)
928 if (!s[t])
929 break;
930 if (t < len)
931 len = t;
932 }
933
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))))
937 {
938 for (t = 0; t < len; t++)
939 d[t] = (s[t] < 32) ? '.' : s[t];
940 d[len] = 0;
941 }
942 return d;
943 }
944
945 static void
ML_XFreeSample(SAMPLE * s)946 ML_XFreeSample (SAMPLE * s)
947 {
948 if (s->data)
949 free (s->data);
950 if (s->samplename)
951 free (s->samplename);
952 }
953
954 static void
ML_XFreeInstrument(INSTRUMENT * i)955 ML_XFreeInstrument (INSTRUMENT * i)
956 {
957 if (i->insname)
958 free (i->insname);
959 }
960
961 void
ML_Free(MODULE * mf)962 ML_Free (MODULE * mf)
963 {
964 UWORD t;
965
966 if (!mf)
967 return;
968
969 if (mf->songname)
970 free (mf->songname);
971 if (mf->comment)
972 free (mf->comment);
973
974 if (mf->modtype)
975 free (mf->modtype);
976 if (mf->positions)
977 free (mf->positions);
978 if (mf->patterns)
979 free (mf->patterns);
980 if (mf->pattrows)
981 free (mf->pattrows);
982
983 if (mf->tracks)
984 {
985 for (t = 0; t < mf->numtrk; t++)
986 if (mf->tracks[t])
987 free (mf->tracks[t]);
988 free (mf->tracks);
989 }
990 if (mf->instruments)
991 {
992 for (t = 0; t < mf->numins; t++)
993 ML_XFreeInstrument (&mf->instruments[t]);
994 free (mf->instruments);
995 }
996 if (mf->samples)
997 {
998 for (t = 0; t < mf->numsmp; t++)
999 if (mf->samples[t].length)
1000 ML_XFreeSample (&mf->samples[t]);
1001 free (mf->samples);
1002 }
1003 memset (mf, 0, sizeof (MODULE));
1004 if (mf != &of)
1005 free (mf);
1006 }
1007
1008 static MODULE *
ML_AllocUniMod(void)1009 ML_AllocUniMod (void)
1010 {
1011 return (_mm_malloc (sizeof (MODULE)));
1012 }
1013
1014 CHAR *
ML_LoadTitle(URL reader)1015 ML_LoadTitle (URL reader)
1016 {
1017 MLOADER *l;
1018
1019 modreader = reader;
1020 _mm_errno = 0;
1021
1022 /* Try to find a loader that recognizes the module */
1023 for (l = firstloader; l; l = l->next)
1024 {
1025 _mm_rewind (modreader);
1026 if (l->Test ())
1027 break;
1028 }
1029
1030 if (!l)
1031 {
1032 _mm_errno = MMERR_NOT_A_MODULE;
1033 return NULL;
1034 }
1035
1036 return l->LoadTitle ();
1037 }
1038
1039 /* Check if it is a module given a reader */
1040 BOOL
ML_Test(URL reader)1041 ML_Test (URL reader)
1042 {
1043 MLOADER *l;
1044
1045 modreader = reader;
1046 _mm_errno = 0;
1047
1048 /* Try to find a loader that recognizes the module */
1049 for (l = firstloader; l; l = l->next)
1050 {
1051 _mm_rewind (modreader);
1052 if (l->Test ())
1053 return 1;
1054 }
1055 return 0;
1056 }
1057
1058 /* Loads a module given a reader */
1059 MODULE *
ML_Load(URL reader,int maxchan,BOOL curious)1060 ML_Load (URL reader, int maxchan, BOOL curious)
1061 {
1062 int t;
1063 MLOADER *l;
1064 BOOL ok;
1065 MODULE *mf;
1066
1067 modreader = reader;
1068 _mm_errno = 0;
1069
1070 /* Try to find a loader that recognizes the module */
1071 for (l = firstloader; l; l = l->next)
1072 {
1073 _mm_rewind (modreader);
1074 if (l->Test ())
1075 break;
1076 }
1077
1078 if (!l)
1079 {
1080 _mm_errno = MMERR_NOT_A_MODULE;
1081 _mm_rewind (modreader);
1082 return NULL;
1083 }
1084
1085 /* init unitrk routines */
1086 if (!UniInit ())
1087 {
1088 _mm_rewind (modreader);
1089 return NULL;
1090 }
1091
1092 /* load the song using the song's loader variable */
1093 memset (&of, 0, sizeof (MODULE));
1094 of.initvolume = 128;
1095
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++)
1100 of.chanvol[t] = 64;
1101
1102 /* init module loader and load the header / patterns */
1103 if (l->Init ())
1104 {
1105 _mm_rewind (modreader);
1106 ok = l->Load (curious);
1107 }
1108 else
1109 ok = 0;
1110
1111 /* free loader and unitrk allocations */
1112 l->Cleanup ();
1113 UniCleanup ();
1114
1115 if (!ok)
1116 {
1117 ML_Free (&of);
1118 _mm_rewind (modreader);
1119 return NULL;
1120 }
1121
1122 if (!ML_LoadSamples ())
1123 {
1124 ML_Free (&of);
1125 _mm_rewind (modreader);
1126 return NULL;
1127 }
1128
1129 if (!(mf = ML_AllocUniMod ()))
1130 {
1131 ML_Free (&of);
1132 return NULL;
1133 }
1134
1135 /* Copy the static MODULE contents into the dynamic MODULE struct. */
1136 memcpy (mf, &of, sizeof (MODULE));
1137
1138 if (maxchan > 0)
1139 {
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;
1144
1145 if (maxchan < mf->numchn)
1146 mf->flags |= UF_NNA;
1147 }
1148 if (SL_LoadSamples ())
1149 {
1150 ML_Free (mf);
1151 return NULL;
1152 }
1153 return mf;
1154 }
1155
1156
1157 void
ML_RegisterAllLoaders(void)1158 ML_RegisterAllLoaders (void)
1159 {
1160 MLOADER *last = NULL;
1161
1162 if (firstloader)
1163 return;
1164
1165 #define LOADER(fmt) { \
1166 extern MLOADER fmt; \
1167 if (!last) \
1168 firstloader = &fmt; \
1169 else \
1170 last->next = &fmt; \
1171 last = &fmt; \
1172 }
1173
1174
1175 /* Most likely first */
1176 LOADER (load_xm);
1177 LOADER (load_s3m);
1178 LOADER (load_mod);
1179 LOADER (load_it);
1180
1181 /* Then the others in alphabetic order */
1182 LOADER (load_669);
1183 LOADER (load_amf);
1184 LOADER (load_dsm);
1185 LOADER (load_far);
1186 LOADER (load_gdm);
1187 LOADER (load_imf);
1188 LOADER (load_med);
1189 LOADER (load_mtm);
1190 LOADER (load_okt);
1191 LOADER (load_stm);
1192 LOADER (load_stx);
1193 LOADER (load_ult);
1194 LOADER (load_uni);
1195
1196 /* must be last! */
1197 LOADER (load_m15);
1198 }
1199 /* ex:set ts=4: */
1200