home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / playercode / sloader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-07  |  11.2 KB  |  512 lines

  1. /*    MikMod sound library
  2.     (c) 1998 Miodrag Vallat and others - see file AUTHORS for complete list
  3.  
  4.     This library is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU Library General Public License as
  6.     published by the Free Software Foundation; either version 2 of
  7.     the License, or (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public
  15.     License along with this library; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*==============================================================================
  20.  
  21.   $Id: sloader.c,v 1.12 1998/12/07 06:00:51 miod Exp $
  22.  
  23.   Routines for loading samples. The sample loader utilizes the routines
  24.   provided by the "registered" sample loader.
  25.  
  26. ==============================================================================*/
  27.  
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31.  
  32. #include <mikmod_internals.h>
  33.  
  34. static    int sl_rlength;
  35. static    SWORD sl_old;
  36. static    SWORD *sl_buffer=NULL;
  37. static    SAMPLOAD *musiclist=NULL,*sndfxlist=NULL;
  38.  
  39. /* size of the loader buffer in words */
  40. #define SLBUFSIZE 2048
  41.  
  42. /* IT-Compressed status structure */
  43. typedef struct ITPACK {
  44.     UWORD bits;    /* current number of bits */
  45.     UWORD bufbits; /* bits in buffer */
  46.     SWORD last;    /* last output */
  47.     UBYTE buf;     /* bit buffer */
  48. } ITPACK;
  49.  
  50. BOOL SL_Init(SAMPLOAD* s)
  51. {
  52.     if(!sl_buffer)
  53.         if(!(sl_buffer=_mm_malloc(SLBUFSIZE*sizeof(SWORD)))) return 0;
  54.  
  55.     sl_rlength = s->length;
  56.     if(s->infmt & SF_16BITS) sl_rlength>>=1;
  57.     sl_old = 0;
  58.  
  59.     return 1;
  60. }
  61.  
  62. void SL_Exit(SAMPLOAD *s)
  63. {
  64.     if(sl_rlength>0) _mm_fseek(s->fp,sl_rlength,SEEK_CUR);
  65.     if(sl_buffer) {
  66.         free(sl_buffer);
  67.         sl_buffer=NULL;
  68.     }
  69. }
  70.  
  71. /* unpack a 8bit IT packed sample */
  72. static BOOL read_itcompr8(ITPACK* status,FILE *fp,SWORD *sl_buffer,UWORD count,UWORD* incnt)
  73. {
  74.     SWORD *dest=sl_buffer,*end=sl_buffer+count;
  75.     UWORD x,y,needbits,havebits,new_count=0;
  76.     UWORD bits = status->bits;
  77.     UWORD bufbits = status->bufbits;
  78.     SBYTE last = status->last;
  79.     UBYTE buf = status->buf;
  80.  
  81.     while (dest<end) {
  82.         needbits=new_count?3:bits;
  83.         x=havebits=0;
  84.         while (needbits) {
  85.             /* feed buffer */
  86.             if (!bufbits) {
  87.                 if((*incnt)--)
  88.                     buf=_mm_read_UBYTE(fp);
  89.                 else
  90.                     buf=0;
  91.                 bufbits=8;
  92.             }
  93.             /* get as many bits as necessary */
  94.             y = needbits<bufbits?needbits:bufbits;
  95.             x|= (buf & ((1<<y)- 1))<<havebits;
  96.             buf>>=y;
  97.             bufbits-=y;
  98.             needbits-=y;
  99.             havebits+=y;
  100.         }
  101.         if (new_count) {
  102.             new_count = 0;
  103.             if (++x >= bits)
  104.                 x++;
  105.             bits = x;
  106.             continue;
  107.         }
  108.         if (bits<7) {
  109.             if (x==(1<<(bits-1))) {
  110.                 new_count = 1;
  111.                 continue;
  112.             }
  113.         }
  114.         else if (bits<9) {
  115.             y = (0xff >> (9-bits)) - 4;
  116.             if ((x>y)&&(x<=y+8)) {
  117.                 if ((x-=y)>=bits)
  118.                     x++;
  119.                 bits = x;
  120.                 continue;
  121.             }
  122.         }
  123.         else if (bits<10) {
  124.             if (x>=0x100) {
  125.                 bits=x-0x100+1;
  126.                 continue;
  127.             }
  128.         } else {
  129.             /* error in compressed data... */
  130.             _mm_errno=MMERR_ITPACK_INVALID_DATA;
  131.             return 0;
  132.         }
  133.  
  134.         if (bits<8) /* extend sign */
  135.             x = ((SBYTE)(x <<(8-bits))) >> (8-bits);
  136.         *(dest++)= (last+=x) << 8; /* convert to 16 bit */
  137.     }
  138.     status->bits = bits;
  139.     status->bufbits = bufbits;
  140.     status->last = last;
  141.     status->buf = buf;
  142.     return dest-sl_buffer;
  143. }
  144.  
  145. /* unpack a 16bit IT packed sample */
  146. static BOOL read_itcompr16(ITPACK *status,FILE *fp,SWORD *sl_buffer,UWORD count,UWORD* incnt)
  147. {
  148.     SWORD *dest=sl_buffer,*end=sl_buffer+count;
  149.     SLONG x,y,needbits,havebits,new_count=0;
  150.     UWORD bits = status->bits;
  151.     UWORD bufbits = status->bufbits;
  152.     SWORD last = status->last;
  153.     UBYTE buf = status->buf;
  154.  
  155.     while (dest<end) {
  156.         needbits=new_count?4:bits;
  157.         x=havebits=0;
  158.         while (needbits) {
  159.             /* feed buffer */
  160.             if (!bufbits) {
  161.                 if((*incnt)--)
  162.                     buf=_mm_read_UBYTE(fp);
  163.                 else
  164.                     buf=0;
  165.                 bufbits=8;
  166.             }
  167.             /* get as many bits as necessary */
  168.             y=needbits<bufbits?needbits:bufbits;
  169.             x|=(buf &((1<<y)-1))<<havebits;
  170.             buf>>=y;
  171.             bufbits-=y;
  172.             needbits-=y;
  173.             havebits+=y;
  174.         }
  175.         if (new_count) {
  176.             new_count = 0;
  177.             if (++x >= bits)
  178.                 x++;
  179.             bits = x;
  180.             continue;
  181.         }
  182.         if (bits<7) {
  183.             if (x==(1<<(bits-1))) {
  184.                 new_count=1;
  185.                 continue;
  186.             }
  187.         }
  188.         else if (bits<17) {
  189.             y=(0xffff>>(17-bits))-8;
  190.             if ((x>y)&&(x<=y+16)) {
  191.                 if ((x-=y)>=bits)
  192.                     x++;
  193.                 bits = x;
  194.                 continue;
  195.             }
  196.         }
  197.         else if (bits<18) {
  198.             if (x>=0x10000) {
  199.                 bits=x-0x10000+1;
  200.                 continue;
  201.             }
  202.         } else {
  203.              /* error in compressed data... */
  204.             _mm_errno=MMERR_ITPACK_INVALID_DATA;
  205.             return 0;
  206.         }
  207.  
  208.         if (bits<16) /* extend sign */
  209.             x = ((SWORD)(x<<(16-bits)))>>(16-bits);
  210.         *(dest++)=(last+=x);
  211.     }
  212.     status->bits = bits;
  213.     status->bufbits = bufbits;
  214.     status->last = last;
  215.     status->buf = buf;
  216.     return dest-sl_buffer;
  217. }
  218.  
  219. static BOOL SL_LoadInternal(void* buffer,UWORD infmt,UWORD outfmt,int scalefactor,int length,FILE* fp,BOOL dither)
  220. {
  221.     SBYTE *bptr = (SBYTE*)buffer;
  222.     SWORD *wptr = (SWORD*)buffer;
  223.     int stodo,t,u;
  224.  
  225.     int result,c_block=0;    /* compression bytes until next block */
  226.     ITPACK status;
  227.     UWORD incnt;
  228.  
  229.     while(length) {
  230.         stodo=(length<SLBUFSIZE)?length:SLBUFSIZE;
  231.  
  232.         if(infmt&SF_ITPACKED) {
  233.             sl_rlength=0;
  234.             if (!c_block) {
  235.                 status.bits = (infmt & SF_16BITS) ? 17 : 9;
  236.                 status.last = status.bufbits = 0;
  237.                 incnt=_mm_read_I_UWORD(fp);
  238.                 c_block = (infmt & SF_16BITS) ? 0x4000 : 0x8000;
  239.                 if(infmt&SF_DELTA) sl_old=0;
  240.             }
  241.             if (infmt & SF_16BITS) {
  242.                 if(!(result=read_itcompr16(&status,fp,sl_buffer,stodo,&incnt)))
  243.                     return 1;
  244.             } else {
  245.                 if(!(result=read_itcompr8(&status,fp,sl_buffer,stodo,&incnt)))
  246.                     return 1;
  247.             }
  248.             if(result!=stodo) {
  249.                 _mm_errno=MMERR_ITPACK_INVALID_DATA;
  250.                 return 1;
  251.             }
  252.             c_block -= stodo;
  253.         } else {
  254.             if(infmt&SF_16BITS) {
  255.                 if(infmt&SF_BIG_ENDIAN)
  256.                     _mm_read_M_SWORDS(sl_buffer,stodo,fp);
  257.                 else
  258.                     _mm_read_I_SWORDS(sl_buffer,stodo,fp);
  259.             } else {
  260.                 SBYTE *src;
  261.                 SWORD *dest;
  262.  
  263.                 fread(sl_buffer,sizeof(SBYTE),stodo,fp);
  264.                 src = (SBYTE*)sl_buffer;
  265.                 dest  = sl_buffer;
  266.                 src += stodo;dest += stodo;
  267.  
  268.                 for(t=0;t<stodo;t++) {
  269.                     src--;dest--;
  270.                     *dest = (*src)<<8;
  271.                 }
  272.             }
  273.             sl_rlength-=stodo;
  274.         }
  275.  
  276.         if(infmt & SF_DELTA)
  277.             for(t=0;t<stodo;t++) {
  278.                 sl_buffer[t] += sl_old;
  279.                 sl_old = sl_buffer[t];
  280.             }
  281.  
  282.         if((infmt^outfmt) & SF_SIGNED) 
  283.             for(t=0;t<stodo;t++)
  284.                 sl_buffer[t]^= 0x8000;
  285.  
  286.         if(scalefactor) {
  287.             int idx = 0;
  288.             SLONG scaleval;
  289.  
  290.             /* Sample Scaling... average values for better results. */
  291.             t= 0;
  292.             while(t<stodo && length) {
  293.                 scaleval = 0;
  294.                 for(u=scalefactor;u && t<stodo;u--,t++)
  295.                     scaleval+=sl_buffer[t];
  296.                 sl_buffer[idx++]=scaleval/(scalefactor-u);
  297.                 length--;
  298.             }
  299.             stodo = idx;
  300.         } else
  301.             length -= stodo;
  302.  
  303.         if (dither) {
  304.             if((infmt & SF_STEREO) && !(outfmt & SF_STEREO)) {
  305.                 /* dither stereo to mono, average together every two samples */
  306.                 SLONG avgval;
  307.                 int idx = 0;
  308.  
  309.                 t=0;
  310.                 while(t<stodo && length) {
  311.                     avgval=sl_buffer[t++];
  312.                     avgval+=sl_buffer[t++];
  313.                     sl_buffer[idx++]=avgval>>1;
  314.                     length-=2;
  315.                 }
  316.                 stodo = idx;
  317.             }
  318.         }
  319.  
  320.         if(outfmt & SF_16BITS) {
  321.             for(t=0;t<stodo;t++)
  322.                 *(wptr++)=sl_buffer[t];
  323.         } else {
  324.             for(t=0;t<stodo;t++)
  325.                 *(bptr++)=sl_buffer[t]>>8;
  326.         }
  327.     }
  328.     return 0;
  329. }
  330.  
  331. BOOL SL_Load(void* buffer,SAMPLOAD *smp,int length)
  332. {
  333.     return SL_LoadInternal(buffer,smp->infmt,smp->outfmt,smp->scalefactor,
  334.                            length,smp->fp,0);
  335. }
  336.  
  337. /* Registers a sample for loading when SL_LoadSamples() is called. */
  338. SAMPLOAD* SL_RegisterSample(SAMPLE* s,int type,FILE* fp)
  339. {
  340.     SAMPLOAD *news,**samplist,*cruise;
  341.  
  342.     if(type==MD_MUSIC) {
  343.         samplist = &musiclist;
  344.         cruise = musiclist;
  345.     } else
  346.       if (type==MD_SNDFX) {
  347.         samplist = &sndfxlist;
  348.         cruise = sndfxlist;
  349.     } else
  350.         return NULL;
  351.     
  352.     /* Allocate and add structure to the END of the list */
  353.     if(!(news=(SAMPLOAD*)_mm_malloc(sizeof(SAMPLOAD)))) return NULL;
  354.  
  355.     if(cruise) {
  356.         while(cruise->next) cruise=cruise->next;
  357.         cruise->next = news;
  358.     } else
  359.         *samplist = news;
  360.  
  361.     news->infmt     = s->flags & SF_FORMATMASK;
  362.     news->outfmt    = news->infmt;
  363.     news->fp        = fp;
  364.     news->sample    = s;
  365.     news->length    = s->length;
  366.     news->loopstart = s->loopstart;
  367.     news->loopend   = s->loopend;
  368.  
  369.     return news;
  370. }
  371.  
  372. static void FreeSampleList(SAMPLOAD* s)
  373. {
  374.     SAMPLOAD *old;
  375.  
  376.     while(s) {
  377.         old = s;
  378.         s = s->next;
  379.         free(old);
  380.     }
  381. }
  382.  
  383. /* Returns the total amount of memory required by the samplelist queue. */
  384. static ULONG SampleTotal(SAMPLOAD* samplist,int type)
  385. {
  386.     int total = 0;
  387.  
  388.     while(samplist) {
  389.         samplist->sample->flags=
  390.           (samplist->sample->flags&~SF_FORMATMASK)|samplist->outfmt;
  391.         total += MD_SampleLength(type,samplist->sample);
  392.         samplist=samplist->next;
  393.     }
  394.  
  395.     return total;
  396. }
  397.  
  398. static ULONG RealSpeed(SAMPLOAD *s)
  399. {
  400.     return(s->sample->speed/(s->scalefactor?s->scalefactor:1));
  401. }    
  402.  
  403. static BOOL DitherSamples(SAMPLOAD* samplist,int type)
  404. {
  405.     SAMPLOAD *c2smp=NULL;
  406.     ULONG maxsize, speed;
  407.     SAMPLOAD *s;
  408.  
  409.     if(!samplist) return 0;
  410.  
  411.     if((maxsize = MD_SampleSpace(type)*1024)) 
  412.         while(SampleTotal(samplist,type)>maxsize) {
  413.             /* First Pass - check for any 16 bit samples */
  414.             s = samplist;
  415.             while(s) {
  416.                 if(s->outfmt & SF_16BITS) {
  417.                     SL_Sample16to8(s);
  418.                     break;
  419.                 }
  420.                 s=s->next;
  421.             }
  422.             /* Second pass (if no 16bits found above) is to take the sample with
  423.                the highest speed and dither it by half. */
  424.             if(!s) {
  425.                 s = samplist;
  426.                 speed = 0;
  427.                 while(s) {
  428.                     if((s->sample->length) && (RealSpeed(s)>speed)) {
  429.                         speed=RealSpeed(s);
  430.                         c2smp=s;
  431.                     }
  432.                     s=s->next;
  433.                 }
  434.                 if (c2smp)
  435.                     SL_HalveSample(c2smp,2);
  436.             }
  437.         }
  438.  
  439.     /* Samples dithered, now load them ! */
  440.     s = samplist;
  441.     while(s) {
  442.         /* sample has to be loaded ? -> increase number of samples, allocate
  443.            memory and load sample. */
  444.         if(s->sample->length) {
  445.             if(s->sample->seekpos)
  446.                 _mm_fseek(s->fp, s->sample->seekpos, SEEK_SET);
  447.  
  448.             /* Call the sample load routine of the driver module. It has to
  449.                return a 'handle' (>=0) that identifies the sample. */
  450.             s->sample->handle = MD_SampleLoad(s, type);
  451.             s->sample->flags  = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;
  452.             if(s->sample->handle<0) {
  453.                 FreeSampleList(samplist);
  454.                 if(_mm_errorhandler) _mm_errorhandler();
  455.                 return 1;
  456.             }
  457.         }
  458.         s = s->next;
  459.     }
  460.  
  461.     FreeSampleList(samplist);
  462.     return 0;
  463. }
  464.  
  465. BOOL SL_LoadSamples(void)
  466. {
  467.     BOOL ok;
  468.  
  469.     _mm_critical = 0;
  470.  
  471.     if((!musiclist)&&(!sndfxlist)) return 0;
  472.     ok=DitherSamples(musiclist,MD_MUSIC)||DitherSamples(sndfxlist,MD_SNDFX);
  473.     musiclist=sndfxlist=NULL;
  474.  
  475.     return ok;
  476. }
  477.  
  478. void SL_Sample16to8(SAMPLOAD* s)
  479. {
  480.     s->outfmt &= ~SF_16BITS;
  481.     s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
  482. }
  483.  
  484. void SL_Sample8to16(SAMPLOAD* s)
  485. {
  486.     s->outfmt |= SF_16BITS;
  487.     s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
  488. }
  489.  
  490. void SL_SampleSigned(SAMPLOAD* s)
  491. {
  492.     s->outfmt |= SF_SIGNED;
  493.     s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
  494. }
  495.  
  496. void SL_SampleUnsigned(SAMPLOAD* s)
  497. {
  498.     s->outfmt &= ~SF_SIGNED;
  499.     s->sample->flags = (s->sample->flags&~SF_FORMATMASK) | s->outfmt;
  500. }
  501.  
  502. void SL_HalveSample(SAMPLOAD* s,int factor)
  503. {
  504.     s->scalefactor=factor>0?factor:2;
  505.  
  506.     s->sample->divfactor = s->scalefactor;
  507.     s->sample->length    = s->length / s->scalefactor;
  508.     s->sample->loopstart = s->loopstart / s->scalefactor;
  509.     s->sample->loopend   = s->loopend / s->scalefactor;
  510. }
  511.  
  512.