annotate playercode/mwav.c @ 8:b30908f9d9f9

Initial entry of mikmod into the CVS tree.
author darius
date Fri, 23 Jan 1998 16:05:10 +0000
parents d14fd386d182
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
1 /*
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
2
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
3 Name: MWAV.C
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
4
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
5 Description:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
6 WAV sample loader
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
7 Stereo .WAV files are not yet supported as samples.
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
8
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
9 Portability:
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
10 All compilers -- All systems (hopefully)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
11
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
12 */
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
13
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
14 #include <string.h>
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
15 #include "mikmod.h"
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
16
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
17
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
18 typedef struct WAV
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
19 { CHAR rID[4];
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
20 ULONG rLen;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
21 CHAR wID[4];
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
22 CHAR fID[4];
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
23 ULONG fLen;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
24 UWORD wFormatTag;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
25 UWORD nChannels;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
26 ULONG nSamplesPerSec;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
27 ULONG nAvgBytesPerSec;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
28 UWORD nBlockAlign;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
29 UWORD nFormatSpecific;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
30 } WAV;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
31
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
32
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
33 SAMPLE *WAV_LoadFP(FILE *fp)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
34 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
35 SAMPLE *si;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
36 static WAV wh;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
37 static CHAR dID[4];
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
38
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
39 // read wav header
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
40
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
41 _mm_read_string(wh.rID,4,fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
42 wh.rLen = _mm_read_I_ULONG(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
43 _mm_read_string(wh.wID,4,fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
44
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
45 while(1)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
46 { _mm_read_string(wh.fID,4,fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
47 wh.fLen = _mm_read_I_ULONG(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
48 if(memcmp(wh.fID,"fmt ",4) == 0) break;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
49 _mm_fseek(fp,wh.fLen,SEEK_CUR);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
50 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
51
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
52 if( feof(fp) ||
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
53 memcmp(wh.rID,"RIFF",4) ||
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
54 memcmp(wh.wID,"WAVE",4))
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
55 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
56 _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
57 return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
58 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
59
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
60 wh.wFormatTag = _mm_read_I_UWORD(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
61 wh.nChannels = _mm_read_I_UWORD(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
62 wh.nSamplesPerSec = _mm_read_I_ULONG(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
63 wh.nAvgBytesPerSec = _mm_read_I_ULONG(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
64 wh.nBlockAlign = _mm_read_I_UWORD(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
65 wh.nFormatSpecific = _mm_read_I_UWORD(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
66
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
67 // check it
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
68
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
69 if(feof(fp))
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
70 { _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
71 return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
72 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
73
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
74 // skip other crap
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
75
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
76 _mm_fseek(fp,wh.fLen-16,SEEK_CUR);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
77 _mm_read_string(dID,4,fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
78
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
79 if(memcmp(dID,"data",4))
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
80 { _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
81 return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
82 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
83
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
84 if(wh.nChannels > 1)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
85 { _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
86 return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
87 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
88
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
89 // printf("wFormatTag: %x\n",wh.wFormatTag);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
90 // printf("blockalign: %x\n",wh.nBlockAlign);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
91 // prinff("nFormatSpc: %x\n",wh.nFormatSpecific);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
92
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
93 if((si=(SAMPLE *)_mm_calloc(1,sizeof(SAMPLE)))==NULL) return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
94
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
95 si->speed = wh.nSamplesPerSec;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
96 si->volume = 64;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
97 si->length = _mm_read_I_ULONG(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
98
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
99 if(wh.nBlockAlign == 2)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
100 { si->flags = SF_16BITS | SF_SIGNED;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
101 si->length >>= 1;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
102 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
103
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
104 SL_RegisterSample(si,MD_SNDFX,fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
105
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
106 return si;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
107 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
108
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
109
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
110 SAMPLE *WAV_LoadFN(CHAR *filename)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
111 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
112 FILE *fp;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
113 SAMPLE *si;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
114
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
115 if(!(md_mode & DMODE_SOFT_SNDFX)) return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
116 if((fp=_mm_fopen(filename,"rb"))==NULL) return NULL;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
117
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
118 si = WAV_LoadFP(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
119 SL_LoadSamples();
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
120 fclose(fp);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
121
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
122 return si;
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
123 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
124
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
125
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
126 void WAV_Free(SAMPLE *si)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
127 {
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
128 if(si!=NULL)
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
129 { MD_SampleUnLoad(si->handle);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
130 free(si);
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
131 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
132 }
d14fd386d182 Initial entry of mikmod into the CVS tree.
darius
parents:
diff changeset
133