3 * @brief The implementation of the gljewel sound module.
5 * Copyright 2001, 2008 David Ashley <dashxdr@gmail.com>
6 * Copyright 2008 Stephen M. Webb <stephen.webb@bregmasoft.ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of Version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #if defined(_WIN32) && !defined(__CYGWIN__)
23 # define WIN32_LEAN_AND_MEAN 1
27 # include <sys/ioctl.h>
28 # include <sys/time.h>
42 #define SNDFRAGMENT 1024
43 #define MAXSOUNDCOMMANDS 32
44 static int soundcommands
[MAXSOUNDCOMMANDS
];
45 static int soundtake
,soundput
;
46 static int sndplaying
[MIXMAX
],sndposition
[MIXMAX
];
48 static char dirlist
[]="data,"DATA_DIR
;
50 #define NUMSOUNDS (sizeof(soundnames)/sizeof(char*))
54 #define SOUND_QUIET -1
56 static char *soundnames
[] =
72 static sample samples
[NUMSOUNDS
];
74 static int soundworking
=0;
76 static int *soundbuffer
;
77 static int soundbufferlen
;
80 static int readsound(int num
)
82 char name
[256],*p1
,*p2
,ch
;
89 while(*p1
&& (ch
=*p1
++)!=',')
91 if(p2
>name
&& p2
[-1]!='/') *p2
++='/';
92 strcpy(p2
,soundnames
[num
]);
93 file
=fopen(name
,"rb");
101 size
=fseek(file
,0,SEEK_END
);
107 fseek(file
,0,SEEK_SET
);
109 len
=samples
[num
].len
=(size
+fragment
-1)/fragment
;
111 p1
=(void *)(samples
[num
].data
=malloc(len
<<1));
115 i
=fread(p1
,sizeof(char),size
<<1,file
);
122 static void fillaudio(void *udata
, Uint8
*bp
,int len
)
128 Sint16
*buffer
= (void *)bp
;
132 while(soundtake
!=soundput
)
134 com
=soundcommands
[soundtake
];
135 soundtake
=(soundtake
+1)&(MAXSOUNDCOMMANDS
-1);
136 if(com
==SOUND_QUIET
) {memset(sndposition
,0,sizeof(sndposition
));continue;}
139 for(i
=0;i
<MIXMAX
;++i
)
148 memset(soundbuffer
,0,soundbufferlen
);
149 for(i
=0;i
<MIXMAX
;++i
)
151 if(!sndposition
[i
]) continue;
153 if(sndposition
[i
]==samples
[which
].len
)
158 p
=samples
[which
].data
;
160 p
+=len
*(sndposition
[i
]++ -1);
163 while(j
--) *ip
++ += *p
++;
171 t
= (t
>0x7fff) ? 0x7fff : (t
<-0x8000) ? -0x8000 : t
;
179 SDL_AudioSpec wanted
;
182 soundtake
=soundput
=0;
183 memset(sndposition
,0,sizeof(sndposition
));
184 memset(sndplaying
,0,sizeof(sndplaying
));
185 fragment
=SNDFRAGMENT
;
186 soundbufferlen
=fragment
*sizeof(int);
187 soundbuffer
=malloc(soundbufferlen
);
188 if(!soundbuffer
) return -2;
190 memset(&wanted
,0,sizeof(wanted
));
193 wanted
.format
=AUDIO_S16
;
194 wanted
.samples
=fragment
;
195 wanted
.callback
=fillaudio
;
198 if(SDL_OpenAudio(&wanted
,0)<0)
200 fprintf(stderr
,"Couldn't open audio: %s\n",SDL_GetError());
205 for(i
=0;i
<NUMSOUNDS
;++i
)
222 void playsound(int n
)
224 soundcommands
[soundput
]=n
;
225 soundput
=(soundput
+1)&(MAXSOUNDCOMMANDS
-1);