#include #include #include #include #include #include #include "sound.h" static char dirlist[]=DATADIR; static int readsound(int num); #define NUMSOUNDS ((int)(sizeof(soundnames)/sizeof(char*))) #define MIXMAX 16 #define SOUND_QUIET -1 static const char *soundnames[] = { "bomb1.raw", "power1.raw", "death.raw", "drop.raw", "bomb2.raw", "power2.raw", }; typedef struct sample { char *data; int len; } sample; #define SNDFRAGMENT 1024 static sample samples[NUMSOUNDS]; static int soundworking=0; static int fragment; // static int soundwrite,soundread; static int *soundbuffer; static int soundbufferlen; static unsigned char sndclip[8192]; #define MAXSOUNDCOMMANDS 32 static char soundcommands[MAXSOUNDCOMMANDS]; static int soundtake,soundput; static int sndplaying[MIXMAX],sndposition[MIXMAX]; static void fillaudio(void *udata,Uint8 *buffer,int len) { char com,*p; int i,j,*ip; int which; while(soundtake!=soundput) { com=soundcommands[soundtake]; soundtake=(soundtake+1)&(MAXSOUNDCOMMANDS-1); if(com==SOUND_QUIET) {memset(sndposition,0,sizeof(sndposition));continue;} if(com>1; wanted.callback=fillaudio; wanted.userdata=0; if(SDL_OpenAudio(&wanted,0)<0) { fprintf(stderr,"Couldn't open audio: %s\n",SDL_GetError()); return -1; } soundworking=1; for(i=0;i<8192;i++) { j=i-4096; sndclip[i]=j > 127 ? 255 : (j<-128 ? 0 : j+128); } for(i=0;iname && p2[-1]!='/') *p2++='/'; strcpy(p2,soundnames[num]); file=open(name,O_RDONLY); if(file>=0) break; if(!*p1) { samples[num].len=-1; return 0; } } size=lseek(file,0,SEEK_END); lseek(file,0,SEEK_SET); len=samples[num].len=(size+fragment-1)/fragment; len*=fragment; p1=samples[num].data=malloc(len); if(p1) { i=read(file,p1,size); if(len-size) memset(p1+size,0,len-size); while(size--) *p1++ ^= 0x80; } else samples[num].data=0; close(file); return 0; } void playsound(int n) { soundcommands[soundput]=n; soundput=(soundput+1)&(MAXSOUNDCOMMANDS-1); }