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 /*==============================================================================
23 $Id: sloader.c,v 1.3 2007/12/06 17:46:08 denis111 Exp $
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 int SL_Init(SAMPLOAD
* s
)
59 if(!(sl_buffer
=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
);
77 /* unpack a 8bit IT packed sample */
78 static int read_itcompr8(ITPACK
* status
,MREADER
*reader
,SWORD
*sl_buffer
,UWORD count
,UWORD
* incnt
)
80 SWORD
*dest
=sl_buffer
,*end
=sl_buffer
+count
;
81 UWORD x
,y
,needbits
,havebits
,new_count
=0;
82 UWORD bits
= status
->bits
;
83 UWORD bufbits
= status
->bufbits
;
84 SBYTE last
= status
->last
;
85 UBYTE buf
= status
->buf
;
88 needbits
=new_count
?3:bits
;
94 buf
=_mm_read_UBYTE(reader
);
99 /* get as many bits as necessary */
100 y
= needbits
<bufbits
?needbits
:bufbits
;
101 x
|= (buf
& ((1<<y
)- 1))<<havebits
;
115 if (x
==(1<<(bits
-1))) {
121 y
= (0xff >> (9-bits
)) - 4;
122 if ((x
>y
)&&(x
<=y
+8)) {
135 /* error in compressed data... */
136 _mm_errno
=MMERR_ITPACK_INVALID_DATA
;
140 if (bits
<8) /* extend sign */
141 x
= ((SBYTE
)(x
<<(8-bits
))) >> (8-bits
);
142 *(dest
++)= (last
+=x
) << 8; /* convert to 16 bit */
145 status
->bufbits
= bufbits
;
148 return (dest
-sl_buffer
);
151 /* unpack a 16bit IT packed sample */
152 static int read_itcompr16(ITPACK
*status
,MREADER
*reader
,SWORD
*sl_buffer
,UWORD count
,UWORD
* incnt
)
154 SWORD
*dest
=sl_buffer
,*end
=sl_buffer
+count
;
155 SLONG x
,y
,needbits
,havebits
,new_count
=0;
156 UWORD bits
= status
->bits
;
157 UWORD bufbits
= status
->bufbits
;
158 SWORD last
= status
->last
;
159 UBYTE buf
= status
->buf
;
162 needbits
=new_count
?4:bits
;
168 buf
=_mm_read_UBYTE(reader
);
173 /* get as many bits as necessary */
174 y
=needbits
<bufbits
?needbits
:bufbits
;
175 x
|=(buf
&((1<<y
)-1))<<havebits
;
189 if (x
==(1<<(bits
-1))) {
195 y
=(0xffff>>(17-bits
))-8;
196 if ((x
>y
)&&(x
<=y
+16)) {
205 bits
=(UWORD
)(x
-0x10000+1);
209 /* error in compressed data... */
210 _mm_errno
=MMERR_ITPACK_INVALID_DATA
;
214 if (bits
<16) /* extend sign */
215 x
= ((SWORD
)(x
<<(16-bits
)))>>(16-bits
);
219 status
->bufbits
= bufbits
;
222 return (dest
-sl_buffer
);
225 static int SL_LoadInternal(void* buffer
,UWORD infmt
,UWORD outfmt
,int scalefactor
,ULONG length
,MREADER
* reader
,int dither
)
227 SBYTE
*bptr
= (SBYTE
*)buffer
;
228 SWORD
*wptr
= (SWORD
*)buffer
;
231 int result
,c_block
=0; /* compression bytes until next block */
235 memset(&status
, 0, sizeof(status
)); /* initialize status */
239 stodo
=(length
<SLBUFSIZE
)?length
:SLBUFSIZE
;
241 if(infmt
&SF_ITPACKED
) {
244 status
.bits
= (infmt
& SF_16BITS
) ? 17 : 9;
245 status
.last
= status
.bufbits
= 0;
246 incnt
=_mm_read_I_UWORD(reader
);
247 c_block
= (infmt
& SF_16BITS
) ? 0x4000 : 0x8000;
248 if(infmt
&SF_DELTA
) sl_old
=0;
250 if (infmt
& SF_16BITS
) {
251 if(!(result
=read_itcompr16(&status
,reader
,sl_buffer
,stodo
,&incnt
)))
254 if(!(result
=read_itcompr8(&status
,reader
,sl_buffer
,stodo
,&incnt
)))
258 _mm_errno
=MMERR_ITPACK_INVALID_DATA
;
263 if(infmt
&SF_16BITS
) {
264 if(infmt
&SF_BIG_ENDIAN
)
265 _mm_read_M_SWORDS(sl_buffer
,stodo
,reader
);
267 _mm_read_I_SWORDS(sl_buffer
,stodo
,reader
);
272 reader
->Read(reader
,sl_buffer
,sizeof(SBYTE
)*stodo
);
273 src
= (SBYTE
*)sl_buffer
;
275 src
+= stodo
;dest
+= stodo
;
277 for(t
=0;t
<stodo
;t
++) {
286 for(t
=0;t
<stodo
;t
++) {
287 sl_buffer
[t
] += sl_old
;
288 sl_old
= sl_buffer
[t
];
291 if((infmt
^outfmt
) & SF_SIGNED
)
293 sl_buffer
[t
]^= 0x8000;
299 /* Sample Scaling... average values for better results. */
301 while(t
<stodo
&& length
) {
303 for(u
=scalefactor
;u
&& t
<stodo
;u
--,t
++)
304 scaleval
+=sl_buffer
[t
];
305 sl_buffer
[idx
++]=(UWORD
)(scaleval
/(scalefactor
-u
));
313 if((infmt
& SF_STEREO
) && !(outfmt
& SF_STEREO
)) {
314 /* dither stereo to mono, average together every two samples */
319 while(t
<stodo
&& length
) {
320 avgval
=sl_buffer
[t
++];
321 avgval
+=sl_buffer
[t
++];
322 sl_buffer
[idx
++]=(SWORD
)(avgval
>>1);
329 if(outfmt
& SF_16BITS
) {
331 *(wptr
++)=sl_buffer
[t
];
334 *(bptr
++)=sl_buffer
[t
]>>8;
340 int SL_Load(void* buffer
,SAMPLOAD
*smp
,ULONG length
)
342 return SL_LoadInternal(buffer
,smp
->infmt
,smp
->outfmt
,smp
->scalefactor
,
343 length
,smp
->reader
,0);
346 /* Registers a sample for loading when SL_LoadSamples() is called. */
347 SAMPLOAD
* SL_RegisterSample(SAMPLE
* s
,int type
,MREADER
* reader
)
349 SAMPLOAD
*news
,**samplist
,*cruise
;
352 samplist
= &musiclist
;
355 if (type
==MD_SNDFX
) {
356 samplist
= &sndfxlist
;
361 /* Allocate and add structure to the END of the list */
362 if(!(news
=(SAMPLOAD
*)MikMod_malloc(sizeof(SAMPLOAD
)))) return NULL
;
365 while(cruise
->next
) cruise
=cruise
->next
;
370 news
->infmt
= s
->flags
& SF_FORMATMASK
;
371 news
->outfmt
= news
->infmt
;
372 news
->reader
= reader
;
374 news
->length
= s
->length
;
375 news
->loopstart
= s
->loopstart
;
376 news
->loopend
= s
->loopend
;
381 static void FreeSampleList(SAMPLOAD
* s
)
392 /* Returns the total amount of memory required by the samplelist queue. */
393 static ULONG
SampleTotal(SAMPLOAD
* samplist
,int type
)
398 samplist
->sample
->flags
=
399 (samplist
->sample
->flags
&~SF_FORMATMASK
)|samplist
->outfmt
;
400 total
+= MD_SampleLength(type
,samplist
->sample
);
401 samplist
=samplist
->next
;
407 static ULONG
RealSpeed(SAMPLOAD
*s
)
409 return(s
->sample
->speed
/(s
->scalefactor
?s
->scalefactor
:1));
412 static int DitherSamples(SAMPLOAD
* samplist
,int type
)
414 SAMPLOAD
*c2smp
=NULL
;
415 ULONG maxsize
, speed
;
418 if(!samplist
) return 0;
420 if((maxsize
=MD_SampleSpace(type
)*1024))
421 while(SampleTotal(samplist
,type
)>maxsize
) {
422 /* First Pass - check for any 16 bit samples */
425 if(s
->outfmt
& SF_16BITS
) {
431 /* Second pass (if no 16bits found above) is to take the sample with
432 the highest speed and dither it by half. */
437 if((s
->sample
->length
) && (RealSpeed(s
)>speed
)) {
444 SL_HalveSample(c2smp
,2);
448 /* Samples dithered, now load them ! */
451 /* sample has to be loaded ? -> increase number of samples, allocate
452 memory and load sample. */
453 if(s
->sample
->length
) {
454 if(s
->sample
->seekpos
)
455 _mm_fseek(s
->reader
, s
->sample
->seekpos
, SEEK_SET
);
457 /* Call the sample load routine of the driver module. It has to
458 return a 'handle' (>=0) that identifies the sample. */
459 s
->sample
->handle
= MD_SampleLoad(s
, type
);
460 s
->sample
->flags
= (s
->sample
->flags
& ~SF_FORMATMASK
) | s
->outfmt
;
461 if(s
->sample
->handle
<0) {
462 FreeSampleList(samplist
);
463 if(_mm_errorhandler
) _mm_errorhandler();
470 FreeSampleList(samplist
);
474 int SL_LoadSamples(void)
480 if((!musiclist
)&&(!sndfxlist
)) return 0;
481 ok
=DitherSamples(musiclist
,MD_MUSIC
)||DitherSamples(sndfxlist
,MD_SNDFX
);
482 musiclist
=sndfxlist
=NULL
;
487 void SL_Sample16to8(SAMPLOAD
* s
)
489 s
->outfmt
&= ~SF_16BITS
;
490 s
->sample
->flags
= (s
->sample
->flags
&~SF_FORMATMASK
) | s
->outfmt
;
493 void SL_Sample8to16(SAMPLOAD
* s
)
495 s
->outfmt
|= SF_16BITS
;
496 s
->sample
->flags
= (s
->sample
->flags
&~SF_FORMATMASK
) | s
->outfmt
;
499 void SL_SampleSigned(SAMPLOAD
* s
)
501 s
->outfmt
|= SF_SIGNED
;
502 s
->sample
->flags
= (s
->sample
->flags
&~SF_FORMATMASK
) | s
->outfmt
;
505 void SL_SampleUnsigned(SAMPLOAD
* s
)
507 s
->outfmt
&= ~SF_SIGNED
;
508 s
->sample
->flags
= (s
->sample
->flags
&~SF_FORMATMASK
) | s
->outfmt
;
511 void SL_HalveSample(SAMPLOAD
* s
,int factor
)
513 s
->scalefactor
=factor
>0?factor
:2;
515 s
->sample
->divfactor
= s
->scalefactor
;
516 s
->sample
->length
= s
->length
/ s
->scalefactor
;
517 s
->sample
->loopstart
= s
->loopstart
/ s
->scalefactor
;
518 s
->sample
->loopend
= s
->loopend
/ s
->scalefactor
;