summaryrefslogtreecommitdiff
path: root/apps/plugins/mikmod/sloader.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/mikmod/sloader.c')
-rw-r--r--apps/plugins/mikmod/sloader.c60
1 files changed, 34 insertions, 26 deletions
diff --git a/apps/plugins/mikmod/sloader.c b/apps/plugins/mikmod/sloader.c
index 8c1070cba1..af7f623799 100644
--- a/apps/plugins/mikmod/sloader.c
+++ b/apps/plugins/mikmod/sloader.c
@@ -6,12 +6,12 @@
6 it under the terms of the GNU Library General Public License as 6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of 7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version. 8 the License, or (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details. 13 GNU Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public 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 16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
@@ -20,7 +20,7 @@
20 20
21/*============================================================================== 21/*==============================================================================
22 22
23 $Id: sloader.c,v 1.3 2007/12/06 17:46:08 denis111 Exp $ 23 $Id$
24 24
25 Routines for loading samples. The sample loader utilizes the routines 25 Routines for loading samples. The sample loader utilizes the routines
26 provided by the "registered" sample loader. 26 provided by the "registered" sample loader.
@@ -56,7 +56,7 @@ typedef struct ITPACK {
56int SL_Init(SAMPLOAD* s) 56int SL_Init(SAMPLOAD* s)
57{ 57{
58 if(!sl_buffer) 58 if(!sl_buffer)
59 if(!(sl_buffer=MikMod_malloc(SLBUFSIZE*sizeof(SWORD)))) return 0; 59 if(!(sl_buffer=(SWORD*)MikMod_malloc(SLBUFSIZE*sizeof(SWORD)))) return 0;
60 60
61 sl_rlength = s->length; 61 sl_rlength = s->length;
62 if(s->infmt & SF_16BITS) sl_rlength>>=1; 62 if(s->infmt & SF_16BITS) sl_rlength>>=1;
@@ -68,16 +68,15 @@ int SL_Init(SAMPLOAD* s)
68void SL_Exit(SAMPLOAD *s) 68void SL_Exit(SAMPLOAD *s)
69{ 69{
70 if(sl_rlength>0) _mm_fseek(s->reader,sl_rlength,SEEK_CUR); 70 if(sl_rlength>0) _mm_fseek(s->reader,sl_rlength,SEEK_CUR);
71 if(sl_buffer) { 71
72 MikMod_free(sl_buffer); 72 MikMod_free(sl_buffer);
73 sl_buffer=NULL; 73 sl_buffer=NULL;
74 }
75} 74}
76 75
77/* unpack a 8bit IT packed sample */ 76/* unpack a 8bit IT packed sample */
78static int read_itcompr8(ITPACK* status,MREADER *reader,SWORD *sl_buffer,UWORD count,UWORD* incnt) 77static int read_itcompr8(ITPACK* status,MREADER *reader,SWORD *out,UWORD count,UWORD* incnt)
79{ 78{
80 SWORD *dest=sl_buffer,*end=sl_buffer+count; 79 SWORD *dest=out,*end=out+count;
81 UWORD x,y,needbits,havebits,new_count=0; 80 UWORD x,y,needbits,havebits,new_count=0;
82 UWORD bits = status->bits; 81 UWORD bits = status->bits;
83 UWORD bufbits = status->bufbits; 82 UWORD bufbits = status->bufbits;
@@ -145,13 +144,13 @@ static int read_itcompr8(ITPACK* status,MREADER *reader,SWORD *sl_buffer,UWORD c
145 status->bufbits = bufbits; 144 status->bufbits = bufbits;
146 status->last = last; 145 status->last = last;
147 status->buf = buf; 146 status->buf = buf;
148 return (dest-sl_buffer); 147 return (dest-out);
149} 148}
150 149
151/* unpack a 16bit IT packed sample */ 150/* unpack a 16bit IT packed sample */
152static int read_itcompr16(ITPACK *status,MREADER *reader,SWORD *sl_buffer,UWORD count,UWORD* incnt) 151static int read_itcompr16(ITPACK *status,MREADER *reader,SWORD *out,UWORD count,UWORD* incnt)
153{ 152{
154 SWORD *dest=sl_buffer,*end=sl_buffer+count; 153 SWORD *dest=out,*end=out+count;
155 SLONG x,y,needbits,havebits,new_count=0; 154 SLONG x,y,needbits,havebits,new_count=0;
156 UWORD bits = status->bits; 155 UWORD bits = status->bits;
157 UWORD bufbits = status->bufbits; 156 UWORD bufbits = status->bufbits;
@@ -219,7 +218,7 @@ static int read_itcompr16(ITPACK *status,MREADER *reader,SWORD *sl_buffer,UWORD
219 status->bufbits = bufbits; 218 status->bufbits = bufbits;
220 status->last = last; 219 status->last = last;
221 status->buf = buf; 220 status->buf = buf;
222 return (dest-sl_buffer); 221 return (dest-out);
223} 222}
224 223
225static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor,ULONG length,MREADER* reader,int dither) 224static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor,ULONG length,MREADER* reader,int dither)
@@ -231,9 +230,11 @@ static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor
231 int result,c_block=0; /* compression bytes until next block */ 230 int result,c_block=0; /* compression bytes until next block */
232 ITPACK status; 231 ITPACK status;
233 UWORD incnt = 0; 232 UWORD incnt = 0;
234 233
235 memset(&status, 0, sizeof(status)); /* initialize status */ 234 status.buf = 0;
236 235 status.last = 0;
236 status.bufbits = 0;
237 status.bits = 0;
237 238
238 while(length) { 239 while(length) {
239 stodo=(length<SLBUFSIZE)?length:SLBUFSIZE; 240 stodo=(length<SLBUFSIZE)?length:SLBUFSIZE;
@@ -261,6 +262,10 @@ static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor
261 c_block -= stodo; 262 c_block -= stodo;
262 } else { 263 } else {
263 if(infmt&SF_16BITS) { 264 if(infmt&SF_16BITS) {
265 if(_mm_eof(reader)) {
266 _mm_errno=MMERR_NOT_A_STREAM;/* better error? */
267 return 1;
268 }
264 if(infmt&SF_BIG_ENDIAN) 269 if(infmt&SF_BIG_ENDIAN)
265 _mm_read_M_SWORDS(sl_buffer,stodo,reader); 270 _mm_read_M_SWORDS(sl_buffer,stodo,reader);
266 else 271 else
@@ -269,6 +274,10 @@ static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor
269 SBYTE *src; 274 SBYTE *src;
270 SWORD *dest; 275 SWORD *dest;
271 276
277 if(_mm_eof(reader)) {
278 _mm_errno=MMERR_NOT_A_STREAM;/* better error? */
279 return 1;
280 }
272 reader->Read(reader,sl_buffer,sizeof(SBYTE)*stodo); 281 reader->Read(reader,sl_buffer,sizeof(SBYTE)*stodo);
273 src = (SBYTE*)sl_buffer; 282 src = (SBYTE*)sl_buffer;
274 dest = sl_buffer; 283 dest = sl_buffer;
@@ -288,7 +297,7 @@ static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor
288 sl_old = sl_buffer[t]; 297 sl_old = sl_buffer[t];
289 } 298 }
290 299
291 if((infmt^outfmt) & SF_SIGNED) 300 if((infmt^outfmt) & SF_SIGNED)
292 for(t=0;t<stodo;t++) 301 for(t=0;t<stodo;t++)
293 sl_buffer[t]^= 0x8000; 302 sl_buffer[t]^= 0x8000;
294 303
@@ -340,7 +349,7 @@ static int SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor
340int SL_Load(void* buffer,SAMPLOAD *smp,ULONG length) 349int SL_Load(void* buffer,SAMPLOAD *smp,ULONG length)
341{ 350{
342 return SL_LoadInternal(buffer,smp->infmt,smp->outfmt,smp->scalefactor, 351 return SL_LoadInternal(buffer,smp->infmt,smp->outfmt,smp->scalefactor,
343 length,smp->reader,0); 352 length,smp->reader,0);
344} 353}
345 354
346/* Registers a sample for loading when SL_LoadSamples() is called. */ 355/* Registers a sample for loading when SL_LoadSamples() is called. */
@@ -357,7 +366,7 @@ SAMPLOAD* SL_RegisterSample(SAMPLE* s,int type,MREADER* reader)
357 cruise = sndfxlist; 366 cruise = sndfxlist;
358 } else 367 } else
359 return NULL; 368 return NULL;
360 369
361 /* Allocate and add structure to the END of the list */ 370 /* Allocate and add structure to the END of the list */
362 if(!(news=(SAMPLOAD*)MikMod_malloc(sizeof(SAMPLOAD)))) return NULL; 371 if(!(news=(SAMPLOAD*)MikMod_malloc(sizeof(SAMPLOAD)))) return NULL;
363 372
@@ -407,7 +416,7 @@ static ULONG SampleTotal(SAMPLOAD* samplist,int type)
407static ULONG RealSpeed(SAMPLOAD *s) 416static ULONG RealSpeed(SAMPLOAD *s)
408{ 417{
409 return(s->sample->speed/(s->scalefactor?s->scalefactor:1)); 418 return(s->sample->speed/(s->scalefactor?s->scalefactor:1));
410} 419}
411 420
412static int DitherSamples(SAMPLOAD* samplist,int type) 421static int DitherSamples(SAMPLOAD* samplist,int type)
413{ 422{
@@ -417,7 +426,7 @@ static int DitherSamples(SAMPLOAD* samplist,int type)
417 426
418 if(!samplist) return 0; 427 if(!samplist) return 0;
419 428
420 if((maxsize=MD_SampleSpace(type)*1024)) 429 if((maxsize=MD_SampleSpace(type)*1024) != 0)
421 while(SampleTotal(samplist,type)>maxsize) { 430 while(SampleTotal(samplist,type)>maxsize) {
422 /* First Pass - check for any 16 bit samples */ 431 /* First Pass - check for any 16 bit samples */
423 s = samplist; 432 s = samplist;
@@ -473,15 +482,15 @@ static int DitherSamples(SAMPLOAD* samplist,int type)
473 482
474int SL_LoadSamples(void) 483int SL_LoadSamples(void)
475{ 484{
476 int ok; 485 int rc;
477 486
478 _mm_critical = 0; 487 _mm_critical = 0;
479 488
480 if((!musiclist)&&(!sndfxlist)) return 0; 489 if((!musiclist)&&(!sndfxlist)) return 0;
481 ok=DitherSamples(musiclist,MD_MUSIC)||DitherSamples(sndfxlist,MD_SNDFX); 490 rc=DitherSamples(musiclist,MD_MUSIC)||DitherSamples(sndfxlist,MD_SNDFX);
482 musiclist=sndfxlist=NULL; 491 musiclist=sndfxlist=NULL;
483 492
484 return ok; 493 return rc;
485} 494}
486 495
487void SL_Sample16to8(SAMPLOAD* s) 496void SL_Sample16to8(SAMPLOAD* s)
@@ -518,5 +527,4 @@ void SL_HalveSample(SAMPLOAD* s,int factor)
518 s->sample->loopend = s->loopend / s->scalefactor; 527 s->sample->loopend = s->loopend / s->scalefactor;
519} 528}
520 529
521
522/* ex:set ts=4: */ 530/* ex:set ts=4: */