reorganized the source code
[dosdemo] / libs / oldmik / src / mwav.c
1 /*\r
2 \r
3 Name:\r
4 MWAV.C\r
5 \r
6 Description:\r
7 WAV sample loader\r
8 \r
9 Portability:\r
10 \r
11 MSDOS:  BC(y)   Watcom(y)       DJGPP(?)\r
12 Win95:  BC(n)\r
13 Linux:  n\r
14 \r
15 (y) - yes\r
16 (n) - no (not possible or not useful)\r
17 (?) - may be possible, but not tested\r
18 \r
19 */\r
20 #include <stdio.h>\r
21 #include <stdlib.h>\r
22 #include <string.h>\r
23 \r
24 #include "mikmod.h"\r
25 \r
26 \r
27 typedef struct WAV{\r
28         char  rID[4];\r
29         ULONG rLen;\r
30         char  wID[4];\r
31         char  fID[4];\r
32         ULONG fLen;\r
33         UWORD wFormatTag;\r
34         UWORD nChannels;\r
35         ULONG nSamplesPerSec;\r
36         ULONG nAvgBytesPerSec;\r
37         UWORD nBlockAlign;\r
38         UWORD nFormatSpecific;\r
39 } WAV;\r
40 \r
41 \r
42 \r
43 SAMPLE *MW_LoadWavFP(FILE *fp)\r
44 {\r
45         SAMPLE *si;\r
46         static WAV wh;\r
47         static char dID[4];\r
48 \r
49         _mm_rewind(fp);\r
50 \r
51         /* read wav header */\r
52 \r
53         _mm_read_str(wh.rID,4,fp);\r
54         wh.rLen=_mm_read_I_ULONG(fp);\r
55         _mm_read_str(wh.wID,4,fp);\r
56         _mm_read_str(wh.fID,4,fp);\r
57         wh.fLen=_mm_read_I_ULONG(fp);\r
58         wh.wFormatTag=_mm_read_I_UWORD(fp);\r
59         wh.nChannels=_mm_read_I_UWORD(fp);\r
60         wh.nSamplesPerSec=_mm_read_I_ULONG(fp);\r
61         wh.nAvgBytesPerSec=_mm_read_I_ULONG(fp);\r
62         wh.nBlockAlign=_mm_read_I_UWORD(fp);\r
63         wh.nFormatSpecific=_mm_read_I_UWORD(fp);\r
64 \r
65         /* check it */\r
66 \r
67         if( feof(fp) ||\r
68                 memcmp(wh.rID,"RIFF",4) ||\r
69                 memcmp(wh.wID,"WAVE",4) ||\r
70                 memcmp(wh.fID,"fmt ",4) ){\r
71                 myerr="Not a WAV file";\r
72                 return NULL;\r
73         }\r
74 \r
75         /* skip other crap */\r
76 \r
77         _mm_fseek(fp,wh.fLen-16,SEEK_CUR);\r
78         _mm_read_str(dID,4,fp);\r
79 \r
80         if( memcmp(dID,"data",4) ){\r
81                 myerr="Not a WAV file";\r
82                 return NULL;\r
83         }\r
84 \r
85         if(wh.nChannels>1){\r
86                 myerr="Only mono WAV's are supported";\r
87                 return NULL;\r
88         }\r
89 \r
90 /*  printf("wFormatTag: %x\n",wh.wFormatTag); */\r
91 /*  printf("blockalign: %x\n",wh.nBlockAlign); */\r
92 /*  prinff("nFormatSpc: %x\n",wh.nFormatSpecific); */\r
93 \r
94         if((si=(SAMPLE *)calloc(1,sizeof(SAMPLE)))==NULL){\r
95                 myerr="Out of memory";\r
96                 return NULL;\r
97         };\r
98 \r
99         si->c2spd=8192;\r
100         si->volume=64;\r
101 \r
102         si->length=_mm_read_I_ULONG(fp);\r
103 \r
104         if(wh.nBlockAlign==2){\r
105                 si->flags=SF_16BITS|SF_SIGNED;\r
106                 si->length>>=1;\r
107         }\r
108 \r
109         si->handle=MD_SampleLoad(fp,si->length,0,si->length,si->flags);\r
110 \r
111         if(si->handle<0){\r
112                 free(si);\r
113                 return NULL;\r
114         }\r
115 \r
116         return si;\r
117 }\r
118 \r
119 \r
120 SAMPLE *MW_LoadWavFN(char *filename)\r
121 {\r
122         FILE *fp;\r
123         SAMPLE *si;\r
124 \r
125         if((fp=fopen(filename,"rb"))==NULL){\r
126                 myerr="Couldn't open wav file";\r
127                 return NULL;\r
128         }\r
129 \r
130         si=MW_LoadWavFP(fp);\r
131 \r
132         fclose(fp);\r
133         return si;\r
134 }\r
135 \r
136 \r
137 void MW_FreeWav(SAMPLE *si)\r
138 {\r
139         if(si!=NULL){\r
140                 MD_SampleUnLoad(si->handle);\r
141                 free(si);\r
142         }\r
143 }\r