home *** CD-ROM | disk | FTP | other *** search
/ PC Loisirs 18 / cd.iso / sharewar / mikm202 / source / mwav.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  1.7 KB  |  109 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "mtypes.h"
  6. #include "mdriver.h"
  7. #include "mwav.h"
  8.  
  9.  
  10. typedef struct WAV{
  11.     char  rID[4];
  12.     ULONG rLen;
  13.     char  wID[4];
  14.     char  fID[4];
  15.     ULONG fLen;
  16.     UWORD wFormatTag;
  17.     UWORD nChannels;
  18.     ULONG nSamplesPerSec;
  19.     ULONG nAvgBytesPerSec;
  20.     UWORD nBlockAlign;
  21.     UWORD nFormatSpecific;
  22. } WAV;
  23.  
  24.  
  25.  
  26. SAMPLE *MW_LoadWavFP(FILE *fp)
  27. {
  28.     SAMPLE *si;
  29.     WAV wh;
  30.     char dID[4];
  31.  
  32.     fread(&wh,sizeof(WAV),1,fp);
  33.  
  34.     if( memcmp(wh.rID,"RIFF",4) ||
  35.         memcmp(wh.wID,"WAVE",4) ||
  36.         memcmp(wh.fID,"fmt ",4) ){
  37.         myerr="Not a WAV file";
  38.         return NULL;
  39.     }
  40.  
  41.     // skip other crap
  42.  
  43.     fseek(fp,wh.fLen-16,SEEK_CUR);
  44.     fread(dID,4,1,fp);
  45.  
  46.     if( memcmp(dID,"data",4) ){
  47.         myerr="Not a WAV file";
  48.         return NULL;
  49.     }
  50.  
  51.     if(wh.nChannels>1){
  52.         myerr="Only mono WAV's are supported";
  53.         return NULL;
  54.     }
  55.  
  56. //      printf("wFormatTag: %x\n",wh.wFormatTag);
  57. //      printf("blockalign: %x\n",wh.nBlockAlign);
  58. //      prinff("nFormatSpc: %x\n",wh.nFormatSpecific);
  59.  
  60.     if((si=calloc(1,sizeof(SAMPLE)))==NULL){
  61.         myerr="Out of memory";
  62.         return NULL;
  63.     };
  64.  
  65.     si->c2spd=8192;
  66.     si->volume=64;
  67.     fread(&si->length,4,1,fp);
  68.  
  69.     if(wh.nBlockAlign==2){
  70.         si->flags=SF_16BITS|SF_SIGNED;
  71.         si->length>>=1;
  72.     }
  73.  
  74.     si->handle=MD_SampleLoad(fp,si->length,0,si->length,si->flags);
  75.  
  76.     if(si->handle<0){
  77.         free(si);
  78.         return NULL;
  79.     }
  80.  
  81.     return si;
  82. }
  83.  
  84.  
  85. SAMPLE *MW_LoadWavFN(char *filename)
  86. {
  87.     FILE *fp;
  88.     SAMPLE *si;
  89.  
  90.     if((fp=fopen(filename,"rb"))==NULL){
  91.         myerr="Couldn't open wav file";
  92.         return NULL;
  93.     }
  94.  
  95.     si=MW_LoadWavFP(fp);
  96.  
  97.     fclose(fp);
  98.     return si;
  99. }
  100.  
  101.  
  102. void MW_FreeWav(SAMPLE *si)
  103. {
  104.     if(si!=NULL){
  105.         MD_SampleUnLoad(si->handle);
  106.         free(si);
  107.     }
  108. }
  109.