1 /* MikMod sound library
2 (c) 1998, 1999, 2000, 2001 Miodrag Vallat and others - see file AUTHORS
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 Routines for loading samples. The sample loader utilizes the routines
26 provided by the "registered" sample loader.
28 ==============================================================================*/
38 #include "mikmod_internals.h"
40 static int sl_rlength;
42 static SWORD *sl_buffer=NULL;
43 static SAMPLOAD *musiclist=NULL,*sndfxlist=NULL;
45 /* size of the loader buffer in words */
46 #define SLBUFSIZE 2048
48 /* IT-Compressed status structure */
49 typedef struct ITPACK {
50 UWORD bits; /* current number of bits */
51 UWORD bufbits; /* bits in buffer */
52 SWORD last; /* last output */
53 UBYTE buf; /* bit buffer */
56 BOOL SL_Init(SAMPLOAD* s)
59 if(!(sl_buffer=(SWORD*)MikMod_malloc(SLBUFSIZE*sizeof(SWORD)))) return 0;
61 sl_rlength = s->length;
62 if(s->infmt & SF_16BITS) sl_rlength>>=1;
68 void SL_Exit(SAMPLOAD *s)
70 if(sl_rlength>0) _mm_fseek(s->reader,sl_rlength,SEEK_CUR);
72 MikMod_free(sl_buffer);
76 /* unpack a 8bit IT packed sample */
77 static int read_itcompr8(ITPACK* status,MREADER *reader,SWORD *out,UWORD count,UWORD* incnt)
79 SWORD *dest=out,*end=out+count;
80 UWORD x,y,needbits,havebits,new_count=0;
81 UWORD bits = status->bits;
82 UWORD bufbits = status->bufbits;
83 SBYTE last = status->last;
84 UBYTE buf = status->buf;
87 needbits=new_count?3:bits;
93 buf=_mm_read_UBYTE(reader);
98 /* get as many bits as necessary */
99 y = needbits<bufbits?needbits:bufbits;
100 x|= (buf & ((1<<y)- 1))<<havebits;
114 if (x==(1<<(bits-1))) {
120 y = (0xff >> (9-bits)) - 4;
121 if ((x>y)&&(x<=y+8)) {
134 /* error in compressed data... */
135 _mm_errno=MMERR_ITPACK_INVALID_DATA;
139 if (bits<8) /* extend sign */
140 x = ((SBYTE)(x <<(8-bits))) >> (8-bits);
141 *(dest++)= (last+=x) << 8; /* convert to 16 bit */
144 status->bufbits = bufbits;
150 /* unpack a 16bit IT packed sample */
151 static int read_itcompr16(ITPACK *status,MREADER *reader,SWORD *out,UWORD count,UWORD* incnt)
153 SWORD *dest=out,*end=out+count;
154 SLONG x,y,needbits,havebits,new_count=0;
155 UWORD bits = status->bits;
156 UWORD bufbits = status->bufbits;
157 SWORD last = status->last;
158 UBYTE buf = status->buf;
161 needbits=new_count?4:bits;
167 buf=_mm_read_UBYTE(reader);
172 /* get as many bits as necessary */
173 y=needbits<bufbits?needbits:bufbits;
174 x|=(buf &((1<<y)-1))<<havebits;
188 if (x==(1<<(bits-1))) {
194 y=(0xffff>>(17-bits))-8;
195 if ((x>y)&&(x<=y+16)) {
204 bits=(UWORD)(x-0x10000+1);
208 /* error in compressed data... */
209 _mm_errno=MMERR_ITPACK_INVALID_DATA;
213 if (bits<16) /* extend sign */
214 x = ((SWORD)(x<<(16-bits)))>>(16-bits);
218 status->bufbits = bufbits;
224 static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor,ULONG length,MREADER* reader,BOOL dither)
226 SBYTE *bptr = (SBYTE*)buffer;
227 SWORD *wptr = (SWORD*)buffer;
230 int result,c_block=0; /* compression bytes until next block */
240 stodo=(length<SLBUFSIZE)?length:SLBUFSIZE;
242 if(infmt&SF_ITPACKED) {
245 status.bits = (infmt & SF_16BITS) ? 17 : 9;
246 status.last = status.bufbits = 0;
247 incnt=_mm_read_I_UWORD(reader);
248 c_block = (infmt & SF_16BITS) ? 0x4000 : 0x8000;
249 if(infmt&SF_DELTA) sl_old=0;
251 if (infmt & SF_16BITS) {
252 if(!(result=read_itcompr16(&status,reader,sl_buffer,stodo,&incnt)))
255 if(!(result=read_itcompr8(&status,reader,sl_buffer,stodo,&incnt)))
259 _mm_errno=MMERR_ITPACK_INVALID_DATA;
264 if(infmt&SF_16BITS) {
265 if(infmt&SF_BIG_ENDIAN)
266 _mm_read_M_SWORDS(sl_buffer,stodo,reader);
268 _mm_read_I_SWORDS(sl_buffer,stodo,reader);
273 reader->Read(reader,sl_buffer,sizeof(SBYTE)*stodo);
274 src = (SBYTE*)sl_buffer;
276 src += stodo;dest += stodo;
278 for(t=0;t<stodo;t++) {
287 for(t=0;t<stodo;t++) {
288 sl_buffer[t] += sl_old;
289 sl_old = sl_buffer[t];
292 if((infmt^outfmt) & SF_SIGNED)
294 sl_buffer[t]^= 0x8000;
300 /* Sample Scaling... average values for better results. */
302 while(t<stodo && length) {
304 for(u=scalefactor;u && t<stodo;u--,t++)
305 scaleval+=sl_buffer[t];
306 sl_buffer[idx++]=(UWORD)(scaleval/(scalefactor-u));
314 if((infmt & SF_STEREO) && !(outfmt & SF_STEREO)) {
315 /* dither stereo to mono, average together every two samples */
320 while(t<stodo && length) {
321 avgval=sl_buffer[t++];
322 avgval+=sl_buffer[t++];
323 sl_buffer[idx++]=(SWORD)(avgval>>1);
330 if(outfmt & SF_16BITS) {
332 *(wptr++)=sl_buffer[t];
335 *(bptr++)=sl_buffer[t]>>8;
341 int SL_Load(void* buffer,SAMPLOAD *smp,ULONG length)
343 return SL_LoadInternal(buffer,smp->infmt,smp->outfmt,smp->scalefactor,
344 length,smp->reader,0);
347 /* Registers a sample for loading when SL_LoadSamples() is called. */
348 SAMPLOAD* SL_RegisterSample(SAMPLE* s,int type,MREADER* reader)
350 SAMPLOAD *news,**samplist,*cruise;
353 samplist = &musiclist;
356 if (type==MD_SNDFX) {
357 samplist = &sndfxlist;
362 /* Allocate and add structure to the END of the list */
363 if(!(news=(SAMPLOAD*)MikMod_malloc(sizeof(SAMPLOAD)))) return NULL;
366 while(cruise->next) cruise=cruise->next;
371 news->infmt = s->flags & SF_FORMATMASK;
372 news->outfmt = news->infmt;
373 news->reader = reader;
375 news->length = s->length;
376 news->loopstart = s->loopstart;
377 news->loopend = s->loopend;
382 static void FreeSampleList(SAMPLOAD* s)
393 /* Returns the total amount of memory required by the samplelist queue. */
394 static ULONG SampleTotal(SAMPLOAD* samplist,int type)
399 samplist->sample->flags=
400 (samplist->sample->flags&~SF_FORMATMASK)|samplist->outfmt;
401 total += MD_SampleLength(type,samplist->sample);
402 samplist=samplist->next;
408 static ULONG RealSpeed(SAMPLOAD *s)
410 return(s->sample->speed/(s->scalefactor?s->scalefactor:1));
413 static int DitherSamples(SAMPLOAD* samplist,int type)
415 SAMPLOAD *c2smp=NULL;
416 ULONG maxsize, speed;
419 if(!samplist) return 0;
421 if((maxsize=MD_SampleSpace(type)*1024) != 0)
422 while(SampleTotal(samplist,type)>maxsize) {
423 /* First Pass - check for any 16 bit samples */
426 if(s->outfmt & SF_16BITS) {
432 /* Second pass (if no 16bits found above) is to take the sample with
433 the highest speed and dither it by half. */
438 if((s->sample->length) && (RealSpeed(s)>speed)) {
445 SL_HalveSample(c2smp,2);
449 /* Samples dithered, now load them ! */
452 /* sample has to be loaded ? -> increase number of samples, allocate
453 memory and load sample. */
454 if(s->sample->length) {
455 if(s->sample->seekpos)
456 _mm_fseek(s->reader, s->sample->seekpos, SEEK_SET);
458 /* Call the sample load routine of the driver module. It has to
459 return a 'handle' (>=0) that identifies the sample. */
460 s->sample->handle = MD_SampleLoad(s, type);
461 s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
462 if(s->sample->handle<0) {
463 FreeSampleList(samplist);
464 if(_mm_errorhandler) _mm_errorhandler();
471 FreeSampleList(samplist);
475 int SL_LoadSamples(void)
481 if((!musiclist)&&(!sndfxlist)) return 0;
482 rc=DitherSamples(musiclist,MD_MUSIC)||DitherSamples(sndfxlist,MD_SNDFX);
483 musiclist=sndfxlist=NULL;
488 void SL_Sample16to8(SAMPLOAD* s)
490 s->outfmt &= ~SF_16BITS;
491 s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
494 void SL_Sample8to16(SAMPLOAD* s)
496 s->outfmt |= SF_16BITS;
497 s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
500 void SL_SampleSigned(SAMPLOAD* s)
502 s->outfmt |= SF_SIGNED;
503 s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
506 void SL_SampleUnsigned(SAMPLOAD* s)
508 s->outfmt &= ~SF_SIGNED;
509 s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
512 void SL_HalveSample(SAMPLOAD* s,int factor)
514 s->scalefactor=factor>0?factor:2;
516 s->sample->divfactor = s->scalefactor;
517 s->sample->length = s->length / s->scalefactor;
518 s->sample->loopstart = s->loopstart / s->scalefactor;
519 s->sample->loopend = s->loopend / s->scalefactor;