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
/*==============================================================================
23
$Id: sloader.c,v 1.3 1999/12/28 18:51:11 hercules Exp $
25
Routines for loading samples. The sample loader utilizes the routines
26
provided by the "registered" sample loader.
28
==============================================================================*/
34
#include <mikmod_internals.h>
36
static int sl_rlength;
38
static SWORD *sl_buffer=NULL;
39
static SAMPLOAD *musiclist=NULL,*sndfxlist=NULL;
41
/* size of the loader buffer in words */
42
#define SLBUFSIZE 2048
44
/* IT-Compressed status structure */
45
typedef struct ITPACK {
46
UWORD bits; /* current number of bits */
47
UWORD bufbits; /* bits in buffer */
48
SWORD last; /* last output */
49
UBYTE buf; /* bit buffer */
52
BOOL SL_Init(SAMPLOAD* s)
55
if(!(sl_buffer=_mm_malloc(SLBUFSIZE*sizeof(SWORD)))) return 0;
57
sl_rlength = s->length;
58
if(s->infmt & SF_16BITS) sl_rlength>>=1;
64
void SL_Exit(SAMPLOAD *s)
66
if(sl_rlength>0) _mm_fseek(s->reader,sl_rlength,SEEK_CUR);
73
/* unpack a 8bit IT packed sample */
74
static BOOL read_itcompr8(ITPACK* status,MREADER *reader,SWORD *sl_buffer,UWORD count,UWORD* incnt)
76
SWORD *dest=sl_buffer,*end=sl_buffer+count;
77
UWORD x,y,needbits,havebits,new_count=0;
78
UWORD bits = status->bits;
79
UWORD bufbits = status->bufbits;
80
SBYTE last = status->last;
81
UBYTE buf = status->buf;
84
needbits=new_count?3:bits;
90
buf=_mm_read_UBYTE(reader);
95
/* get as many bits as necessary */
96
y = needbits<bufbits?needbits:bufbits;
97
x|= (buf & ((1<<y)- 1))<<havebits;
111
if (x==(1<<(bits-1))) {
117
y = (0xff >> (9-bits)) - 4;
118
if ((x>y)&&(x<=y+8)) {
131
/* error in compressed data... */
132
_mm_errno=MMERR_ITPACK_INVALID_DATA;
136
if (bits<8) /* extend sign */
137
x = ((SBYTE)(x <<(8-bits))) >> (8-bits);
138
*(dest++)= (last+=x) << 8; /* convert to 16 bit */
141
status->bufbits = bufbits;
144
return dest-sl_buffer;
147
/* unpack a 16bit IT packed sample */
148
static BOOL read_itcompr16(ITPACK *status,MREADER *reader,SWORD *sl_buffer,UWORD count,UWORD* incnt)
150
SWORD *dest=sl_buffer,*end=sl_buffer+count;
151
SLONG x,y,needbits,havebits,new_count=0;
152
UWORD bits = status->bits;
153
UWORD bufbits = status->bufbits;
154
SWORD last = status->last;
155
UBYTE buf = status->buf;
158
needbits=new_count?4:bits;
164
buf=_mm_read_UBYTE(reader);
169
/* get as many bits as necessary */
170
y=needbits<bufbits?needbits:bufbits;
171
x|=(buf &((1<<y)-1))<<havebits;
185
if (x==(1<<(bits-1))) {
191
y=(0xffff>>(17-bits))-8;
192
if ((x>y)&&(x<=y+16)) {
205
/* error in compressed data... */
206
_mm_errno=MMERR_ITPACK_INVALID_DATA;
210
if (bits<16) /* extend sign */
211
x = ((SWORD)(x<<(16-bits)))>>(16-bits);
215
status->bufbits = bufbits;
218
return dest-sl_buffer;
221
static BOOL SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor,ULONG length,MREADER* reader,BOOL dither)
223
SBYTE *bptr = (SBYTE*)buffer;
224
SWORD *wptr = (SWORD*)buffer;
227
int result,c_block=0; /* compression bytes until next block */
232
stodo=(length<SLBUFSIZE)?length:SLBUFSIZE;
234
if(infmt&SF_ITPACKED) {
237
status.bits = (infmt & SF_16BITS) ? 17 : 9;
238
status.last = status.bufbits = 0;
239
incnt=_mm_read_I_UWORD(reader);
240
c_block = (infmt & SF_16BITS) ? 0x4000 : 0x8000;
241
if(infmt&SF_DELTA) sl_old=0;
243
if (infmt & SF_16BITS) {
244
if(!(result=read_itcompr16(&status,reader,sl_buffer,stodo,&incnt)))
247
if(!(result=read_itcompr8(&status,reader,sl_buffer,stodo,&incnt)))
251
_mm_errno=MMERR_ITPACK_INVALID_DATA;
256
if(infmt&SF_16BITS) {
257
if(infmt&SF_BIG_ENDIAN)
258
_mm_read_M_SWORDS(sl_buffer,stodo,reader);
260
_mm_read_I_SWORDS(sl_buffer,stodo,reader);
265
reader->Read(reader,sl_buffer,sizeof(SBYTE)*stodo);
266
src = (SBYTE*)sl_buffer;
268
src += stodo;dest += stodo;
270
for(t=0;t<stodo;t++) {
279
for(t=0;t<stodo;t++) {
280
sl_buffer[t] += sl_old;
281
sl_old = sl_buffer[t];
284
if((infmt^outfmt) & SF_SIGNED)
286
sl_buffer[t]^= 0x8000;
292
/* Sample Scaling... average values for better results. */
294
while(t<stodo && length) {
296
for(u=scalefactor;u && t<stodo;u--,t++)
297
scaleval+=sl_buffer[t];
298
sl_buffer[idx++]=scaleval/(scalefactor-u);
306
if((infmt & SF_STEREO) && !(outfmt & SF_STEREO)) {
307
/* dither stereo to mono, average together every two samples */
312
while(t<stodo && length) {
313
avgval=sl_buffer[t++];
314
avgval+=sl_buffer[t++];
315
sl_buffer[idx++]=avgval>>1;
322
if(outfmt & SF_16BITS) {
324
*(wptr++)=sl_buffer[t];
327
*(bptr++)=sl_buffer[t]>>8;
333
BOOL SL_Load(void* buffer,SAMPLOAD *smp,ULONG length)
335
return SL_LoadInternal(buffer,smp->infmt,smp->outfmt,smp->scalefactor,
336
length,smp->reader,0);
339
/* Registers a sample for loading when SL_LoadSamples() is called. */
340
SAMPLOAD* SL_RegisterSample(SAMPLE* s,int type,MREADER* reader)
342
SAMPLOAD *news,**samplist,*cruise;
345
samplist = &musiclist;
348
if (type==MD_SNDFX) {
349
samplist = &sndfxlist;
354
/* Allocate and add structure to the END of the list */
355
if(!(news=(SAMPLOAD*)_mm_malloc(sizeof(SAMPLOAD)))) return NULL;
358
while(cruise->next) cruise=cruise->next;
363
news->infmt = s->flags & SF_FORMATMASK;
364
news->outfmt = news->infmt;
365
news->reader = reader;
367
news->length = s->length;
368
news->loopstart = s->loopstart;
369
news->loopend = s->loopend;
374
static void FreeSampleList(SAMPLOAD* s)
385
/* Returns the total amount of memory required by the samplelist queue. */
386
static ULONG SampleTotal(SAMPLOAD* samplist,int type)
391
samplist->sample->flags=
392
(samplist->sample->flags&~SF_FORMATMASK)|samplist->outfmt;
393
total += MD_SampleLength(type,samplist->sample);
394
samplist=samplist->next;
400
static ULONG RealSpeed(SAMPLOAD *s)
402
return(s->sample->speed/(s->scalefactor?s->scalefactor:1));
405
static BOOL DitherSamples(SAMPLOAD* samplist,int type)
407
SAMPLOAD *c2smp=NULL;
408
ULONG maxsize, speed;
411
if(!samplist) return 0;
413
if((maxsize=MD_SampleSpace(type)*1024))
414
while(SampleTotal(samplist,type)>maxsize) {
415
/* First Pass - check for any 16 bit samples */
418
if(s->outfmt & SF_16BITS) {
424
/* Second pass (if no 16bits found above) is to take the sample with
425
the highest speed and dither it by half. */
430
if((s->sample->length) && (RealSpeed(s)>speed)) {
437
SL_HalveSample(c2smp,2);
441
/* Samples dithered, now load them ! */
444
/* sample has to be loaded ? -> increase number of samples, allocate
445
memory and load sample. */
446
if(s->sample->length) {
447
if(s->sample->seekpos)
448
_mm_fseek(s->reader, s->sample->seekpos, SEEK_SET);
450
/* Call the sample load routine of the driver module. It has to
451
return a 'handle' (>=0) that identifies the sample. */
452
s->sample->handle = MD_SampleLoad(s, type);
453
s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
454
if(s->sample->handle<0) {
455
FreeSampleList(samplist);
456
if(_mm_errorhandler) _mm_errorhandler();
463
FreeSampleList(samplist);
467
BOOL SL_LoadSamples(void)
473
if((!musiclist)&&(!sndfxlist)) return 0;
474
ok=DitherSamples(musiclist,MD_MUSIC)||DitherSamples(sndfxlist,MD_SNDFX);
475
musiclist=sndfxlist=NULL;
480
void SL_Sample16to8(SAMPLOAD* s)
482
s->outfmt &= ~SF_16BITS;
483
s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
486
void SL_Sample8to16(SAMPLOAD* s)
488
s->outfmt |= SF_16BITS;
489
s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
492
void SL_SampleSigned(SAMPLOAD* s)
494
s->outfmt |= SF_SIGNED;
495
s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
498
void SL_SampleUnsigned(SAMPLOAD* s)
500
s->outfmt &= ~SF_SIGNED;
501
s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
504
void SL_HalveSample(SAMPLOAD* s,int factor)
506
s->scalefactor=factor>0?factor:2;
508
s->sample->divfactor = s->scalefactor;
509
s->sample->length = s->length / s->scalefactor;
510
s->sample->loopstart = s->loopstart / s->scalefactor;
511
s->sample->loopend = s->loopend / s->scalefactor;