4
|
1 /*
|
|
2
|
|
3 Name: LOAD_WAV.C
|
|
4
|
|
5 Description:
|
|
6 WAV Streaming Audio Loader / Player
|
|
7
|
|
8 Portability:
|
|
9 All compilers -- All systems (hopefully)
|
|
10
|
|
11 */
|
|
12
|
|
13 #include "mikmod.h"
|
|
14 #include <string.h>
|
|
15
|
|
16
|
|
17 typedef struct
|
|
18 { CHAR rID[4];
|
|
19 ULONG rLen;
|
|
20 CHAR wID[4];
|
|
21 CHAR fID[4];
|
|
22 ULONG fLen;
|
|
23 UWORD wFormatTag;
|
|
24 UWORD nChannels;
|
|
25 ULONG nSamplesPerSec;
|
|
26 ULONG nAvgBytesPerSec;
|
|
27 UWORD nBlockAlign;
|
|
28 UWORD nFormatSpecific;
|
|
29 } WAV;
|
|
30
|
|
31
|
|
32 BOOL WAV_Load(void)
|
|
33 {
|
|
34 SAMPLE *si;
|
|
35 static WAV wh;
|
|
36 static CHAR dID[4];
|
|
37
|
|
38 // read wav header
|
|
39
|
|
40 _mm_read_string(wh.rID,4,stream_fp);
|
|
41 wh.rLen = _mm_read_I_ULONG(stream_fp);
|
|
42 _mm_read_string(wh.wID,4,stream_fp);
|
|
43 _mm_read_string(wh.fID,4,stream_fp);
|
|
44 wh.fLen = _mm_read_I_ULONG(stream_fp);
|
|
45 wh.wFormatTag = _mm_read_I_UWORD(stream_fp);
|
|
46 wh.nChannels = _mm_read_I_UWORD(stream_fp);
|
|
47 wh.nSamplesPerSec = _mm_read_I_ULONG(stream_fp);
|
|
48 wh.nAvgBytesPerSec = _mm_read_I_ULONG(stream_fp);
|
|
49 wh.nBlockAlign = _mm_read_I_UWORD(stream_fp);
|
|
50 wh.nFormatSpecific = _mm_read_I_UWORD(stream_fp);
|
|
51
|
|
52 // check it
|
|
53
|
|
54 if( feof(stream_fp) ||
|
|
55 memcmp(wh.rID,"RIFF",4) ||
|
|
56 memcmp(wh.wID,"WAVE",4) ||
|
|
57 memcmp(wh.fID,"fmt ",4) )
|
|
58 {
|
|
59 _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
|
|
60 return NULL;
|
|
61 }
|
|
62
|
|
63 // skip other crap
|
|
64
|
|
65 _mm_fseek(stream_fp,wh.fLen-16,SEEK_CUR);
|
|
66 _mm_read_string(dID,4,stream_fp);
|
|
67
|
|
68 if( memcmp(dID,"data",4) )
|
|
69 { _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
|
|
70 return NULL;
|
|
71 }
|
|
72
|
|
73 if(wh.nChannels > 1)
|
|
74 { _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
|
|
75 return NULL;
|
|
76 }
|
|
77
|
|
78 // printf("wFormatTag: %x\n",wh.wFormatTag);
|
|
79 // printf("blockalign: %x\n",wh.nBlockAlign);
|
|
80 // prinff("nFormatSpc: %x\n",wh.nFormatSpecific);
|
|
81
|
|
82 if((si=(SAMPLE *)_mm_calloc(1,sizeof(SAMPLE)))==NULL) return NULL;
|
|
83
|
|
84 si->speed = wh.nSamplesPerSec/wh.nChannels;
|
|
85 si->volume = 64;
|
|
86
|
|
87 si->length = _mm_read_I_ULONG(stream_fp);
|
|
88
|
|
89 if(wh.nBlockAlign==2)
|
|
90 { si->flags = SF_16BITS | SF_SIGNED;
|
|
91 si->length>>=1;
|
|
92 }
|
|
93
|
|
94 return 0;
|
|
95 }
|
|
96
|
|
97
|