1 #if defined(_WIN32) && !defined(__CYGWIN__)
2 # define WIN32_LEAN_AND_MEAN 1
6 # include <sys/ioctl.h>
21 #define SNDFRAGMENT 1024
22 #define MAXSOUNDCOMMANDS 32
23 static int soundcommands
[MAXSOUNDCOMMANDS
];
24 static int soundtake
,soundput
;
25 static int sndplaying
[MIXMAX
],sndposition
[MIXMAX
];
27 static char dirlist
[]="data";
29 #define NUMSOUNDS (sizeof(soundnames)/sizeof(char*))
33 #define SOUND_QUIET -1
35 static char *soundnames
[] =
51 static sample samples
[NUMSOUNDS
];
53 static int soundworking
=0;
55 static int *soundbuffer
;
56 static int soundbufferlen
;
59 static int readsound(int num
)
61 char name
[256],*p1
,*p2
,ch
;
68 while(*p1
&& (ch
=*p1
++)!=',')
70 if(p2
>name
&& p2
[-1]!='/') *p2
++='/';
71 strcpy(p2
,soundnames
[num
]);
80 size
=fseek(file
,0,SEEK_END
);
81 fseek(file
,0,SEEK_SET
);
83 len
=samples
[num
].len
=(size
+fragment
-1)/fragment
;
85 p1
=(void *)(samples
[num
].data
=malloc(len
<<1));
89 i
=fread(p1
,sizeof(char),size
<<1,file
);
96 static void fillaudio(void *udata
, Uint8
*bp
,int len
)
102 Sint16
*buffer
= (void *)bp
;
106 while(soundtake
!=soundput
)
108 com
=soundcommands
[soundtake
];
109 soundtake
=(soundtake
+1)&(MAXSOUNDCOMMANDS
-1);
110 if(com
==SOUND_QUIET
) {memset(sndposition
,0,sizeof(sndposition
));continue;}
113 for(i
=0;i
<MIXMAX
;++i
)
122 memset(soundbuffer
,0,soundbufferlen
);
123 for(i
=0;i
<MIXMAX
;++i
)
125 if(!sndposition
[i
]) continue;
127 if(sndposition
[i
]==samples
[which
].len
)
132 p
=samples
[which
].data
;
134 p
+=len
*(sndposition
[i
]++ -1);
137 while(j
--) *ip
++ += *p
++;
145 t
= (t
>0x7fff) ? 0x7fff : (t
<-0x8000) ? -0x8000 : t
;
153 SDL_AudioSpec wanted
;
156 soundtake
=soundput
=0;
157 memset(sndposition
,0,sizeof(sndposition
));
158 memset(sndplaying
,0,sizeof(sndplaying
));
159 fragment
=SNDFRAGMENT
;
160 soundbufferlen
=fragment
*sizeof(int);
161 soundbuffer
=malloc(soundbufferlen
);
162 if(!soundbuffer
) return -2;
164 memset(&wanted
,0,sizeof(wanted
));
167 wanted
.format
=AUDIO_S16
;
168 wanted
.samples
=fragment
;
169 wanted
.callback
=fillaudio
;
172 if(SDL_OpenAudio(&wanted
,0)<0)
174 fprintf(stderr
,"Couldn't open audio: %s\n",SDL_GetError());
179 for(i
=0;i
<NUMSOUNDS
;++i
)
196 void playsound(int n
)
198 soundcommands
[soundput
]=n
;
199 soundput
=(soundput
+1)&(MAXSOUNDCOMMANDS
-1);