Bump version numbers for 3.13
[maemo-rb.git] / apps / plugins / mikmod / sloader.c
blob8c1070cba13bced6e436040fd2d7245edfc62dcf
1 /* MikMod sound library
2 (c) 1998, 1999, 2000, 2001 Miodrag Vallat and others - see file AUTHORS
3 for complete list.
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
18 02111-1307, USA.
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 ==============================================================================*/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #include "mikmod_internals.h"
40 static int sl_rlength;
41 static SWORD sl_old;
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 */
54 } ITPACK;
56 int SL_Init(SAMPLOAD* s)
58 if(!sl_buffer)
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;
63 sl_old = 0;
65 return 1;
68 void SL_Exit(SAMPLOAD *s)
70 if(sl_rlength>0) _mm_fseek(s->reader,sl_rlength,SEEK_CUR);
71 if(sl_buffer) {
72 MikMod_free(sl_buffer);
73 sl_buffer=NULL;
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;
87 while (dest<end) {
88 needbits=new_count?3:bits;
89 x=havebits=0;
90 while (needbits) {
91 /* feed buffer */
92 if (!bufbits) {
93 if((*incnt)--)
94 buf=_mm_read_UBYTE(reader);
95 else
96 buf=0;
97 bufbits=8;
99 /* get as many bits as necessary */
100 y = needbits<bufbits?needbits:bufbits;
101 x|= (buf & ((1<<y)- 1))<<havebits;
102 buf>>=y;
103 bufbits-=y;
104 needbits-=y;
105 havebits+=y;
107 if (new_count) {
108 new_count = 0;
109 if (++x >= bits)
110 x++;
111 bits = x;
112 continue;
114 if (bits<7) {
115 if (x==(1<<(bits-1))) {
116 new_count = 1;
117 continue;
120 else if (bits<9) {
121 y = (0xff >> (9-bits)) - 4;
122 if ((x>y)&&(x<=y+8)) {
123 if ((x-=y)>=bits)
124 x++;
125 bits = x;
126 continue;
129 else if (bits<10) {
130 if (x>=0x100) {
131 bits=x-0x100+1;
132 continue;
134 } else {
135 /* error in compressed data... */
136 _mm_errno=MMERR_ITPACK_INVALID_DATA;
137 return 0;
140 if (bits<8) /* extend sign */
141 x = ((SBYTE)(x <<(8-bits))) >> (8-bits);
142 *(dest++)= (last+=x) << 8; /* convert to 16 bit */
144 status->bits = bits;
145 status->bufbits = bufbits;
146 status->last = last;
147 status->buf = buf;
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;
161 while (dest<end) {
162 needbits=new_count?4:bits;
163 x=havebits=0;
164 while (needbits) {
165 /* feed buffer */
166 if (!bufbits) {
167 if((*incnt)--)
168 buf=_mm_read_UBYTE(reader);
169 else
170 buf=0;
171 bufbits=8;
173 /* get as many bits as necessary */
174 y=needbits<bufbits?needbits:bufbits;
175 x|=(buf &((1<<y)-1))<<havebits;
176 buf>>=y;
177 bufbits-=(UWORD)y;
178 needbits-=(UWORD)y;
179 havebits+=(UWORD)y;
181 if (new_count) {
182 new_count = 0;
183 if (++x >= bits)
184 x++;
185 bits = (UWORD)x;
186 continue;
188 if (bits<7) {
189 if (x==(1<<(bits-1))) {
190 new_count=1;
191 continue;
194 else if (bits<17) {
195 y=(0xffff>>(17-bits))-8;
196 if ((x>y)&&(x<=y+16)) {
197 if ((x-=y)>=bits)
198 x++;
199 bits = (UWORD)x;
200 continue;
203 else if (bits<18) {
204 if (x>=0x10000) {
205 bits=(UWORD)(x-0x10000+1);
206 continue;
208 } else {
209 /* error in compressed data... */
210 _mm_errno=MMERR_ITPACK_INVALID_DATA;
211 return 0;
214 if (bits<16) /* extend sign */
215 x = ((SWORD)(x<<(16-bits)))>>(16-bits);
216 *(dest++)=(last+=x);
218 status->bits = bits;
219 status->bufbits = bufbits;
220 status->last = last;
221 status->buf = buf;
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;
229 int stodo,t,u;
231 int result,c_block=0; /* compression bytes until next block */
232 ITPACK status;
233 UWORD incnt = 0;
235 memset(&status, 0, sizeof(status)); /* initialize status */
238 while(length) {
239 stodo=(length<SLBUFSIZE)?length:SLBUFSIZE;
241 if(infmt&SF_ITPACKED) {
242 sl_rlength=0;
243 if (!c_block) {
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)))
252 return 1;
253 } else {
254 if(!(result=read_itcompr8(&status,reader,sl_buffer,stodo,&incnt)))
255 return 1;
257 if(result!=stodo) {
258 _mm_errno=MMERR_ITPACK_INVALID_DATA;
259 return 1;
261 c_block -= stodo;
262 } else {
263 if(infmt&SF_16BITS) {
264 if(infmt&SF_BIG_ENDIAN)
265 _mm_read_M_SWORDS(sl_buffer,stodo,reader);
266 else
267 _mm_read_I_SWORDS(sl_buffer,stodo,reader);
268 } else {
269 SBYTE *src;
270 SWORD *dest;
272 reader->Read(reader,sl_buffer,sizeof(SBYTE)*stodo);
273 src = (SBYTE*)sl_buffer;
274 dest = sl_buffer;
275 src += stodo;dest += stodo;
277 for(t=0;t<stodo;t++) {
278 src--;dest--;
279 *dest = (*src)<<8;
282 sl_rlength-=stodo;
285 if(infmt & SF_DELTA)
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)
292 for(t=0;t<stodo;t++)
293 sl_buffer[t]^= 0x8000;
295 if(scalefactor) {
296 int idx = 0;
297 SLONG scaleval;
299 /* Sample Scaling... average values for better results. */
300 t= 0;
301 while(t<stodo && length) {
302 scaleval = 0;
303 for(u=scalefactor;u && t<stodo;u--,t++)
304 scaleval+=sl_buffer[t];
305 sl_buffer[idx++]=(UWORD)(scaleval/(scalefactor-u));
306 length--;
308 stodo = idx;
309 } else
310 length -= stodo;
312 if (dither) {
313 if((infmt & SF_STEREO) && !(outfmt & SF_STEREO)) {
314 /* dither stereo to mono, average together every two samples */
315 SLONG avgval;
316 int idx = 0;
318 t=0;
319 while(t<stodo && length) {
320 avgval=sl_buffer[t++];
321 avgval+=sl_buffer[t++];
322 sl_buffer[idx++]=(SWORD)(avgval>>1);
323 length-=2;
325 stodo = idx;
329 if(outfmt & SF_16BITS) {
330 for(t=0;t<stodo;t++)
331 *(wptr++)=sl_buffer[t];
332 } else {
333 for(t=0;t<stodo;t++)
334 *(bptr++)=sl_buffer[t]>>8;
337 return 0;
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;
351 if(type==MD_MUSIC) {
352 samplist = &musiclist;
353 cruise = musiclist;
354 } else
355 if (type==MD_SNDFX) {
356 samplist = &sndfxlist;
357 cruise = sndfxlist;
358 } else
359 return NULL;
361 /* Allocate and add structure to the END of the list */
362 if(!(news=(SAMPLOAD*)MikMod_malloc(sizeof(SAMPLOAD)))) return NULL;
364 if(cruise) {
365 while(cruise->next) cruise=cruise->next;
366 cruise->next = news;
367 } else
368 *samplist = news;
370 news->infmt = s->flags & SF_FORMATMASK;
371 news->outfmt = news->infmt;
372 news->reader = reader;
373 news->sample = s;
374 news->length = s->length;
375 news->loopstart = s->loopstart;
376 news->loopend = s->loopend;
378 return news;
381 static void FreeSampleList(SAMPLOAD* s)
383 SAMPLOAD *old;
385 while(s) {
386 old = s;
387 s = s->next;
388 MikMod_free(old);
392 /* Returns the total amount of memory required by the samplelist queue. */
393 static ULONG SampleTotal(SAMPLOAD* samplist,int type)
395 int total = 0;
397 while(samplist) {
398 samplist->sample->flags=
399 (samplist->sample->flags&~SF_FORMATMASK)|samplist->outfmt;
400 total += MD_SampleLength(type,samplist->sample);
401 samplist=samplist->next;
404 return total;
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;
416 SAMPLOAD *s;
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 */
423 s = samplist;
424 while(s) {
425 if(s->outfmt & SF_16BITS) {
426 SL_Sample16to8(s);
427 break;
429 s=s->next;
431 /* Second pass (if no 16bits found above) is to take the sample with
432 the highest speed and dither it by half. */
433 if(!s) {
434 s = samplist;
435 speed = 0;
436 while(s) {
437 if((s->sample->length) && (RealSpeed(s)>speed)) {
438 speed=RealSpeed(s);
439 c2smp=s;
441 s=s->next;
443 if (c2smp)
444 SL_HalveSample(c2smp,2);
448 /* Samples dithered, now load them ! */
449 s = samplist;
450 while(s) {
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();
464 return 1;
467 s = s->next;
470 FreeSampleList(samplist);
471 return 0;
474 int SL_LoadSamples(void)
476 int ok;
478 _mm_critical = 0;
480 if((!musiclist)&&(!sndfxlist)) return 0;
481 ok=DitherSamples(musiclist,MD_MUSIC)||DitherSamples(sndfxlist,MD_SNDFX);
482 musiclist=sndfxlist=NULL;
484 return ok;
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;
522 /* ex:set ts=4: */