2 "$Id: alsa9.c 6716 2007-06-27 18:57:31Z akirschbaum $";
42 #include <sys/types.h>
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
60 #ifdef HAVE_SYS_SELECT_H
61 #include <sys/select.h>
76 #include <alsa/asoundlib.h>
77 #include <alsa/pcm_plugin.h>
80 #define ALSA_PCM_NEW_HW_PARAMS_API
81 #define AUDIODEV "default:0,0"
85 #define ALSA9_ERROR(str,err) { \
86 fprintf(stderr,"ALSA9 Error: %s %s\n",str,snd_strerror(err)); }
88 #define CONFIG_FILE "/.crossfire/sndconfig"
91 #define SOUND_DECREASE 0.1
118 snd_pcm_sw_params_t *sw_params;
121 printf(
"cfsndserv compiled for ALSA9 sound system\n");
133 snd_pcm_hw_params_alloca(&
params);
141 if ((
err = snd_pcm_hw_params_set_access(
handle,
params,SND_PCM_ACCESS_RW_INTERLEAVED)) <0) {
149 format =
settings.
sign?SND_PCM_FORMAT_S8:SND_PCM_FORMAT_U8;
151 format = SND_PCM_FORMAT_U16;
169 if (r == 0) r = 41100;
176 printf(
"init_audio(): sample_size = %d\n",
sample_size);
193 if ((
err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
194 fprintf (stderr,
"cannot allocate software parameters structure (%s)\n",
198 if ((
err = snd_pcm_sw_params_current (
handle, sw_params)) < 0) {
199 fprintf (stderr,
"cannot initialize software parameters structure (%s)\n",
203 if ((
err = snd_pcm_sw_params_set_avail_min (
handle, sw_params, 4096)) < 0) {
204 fprintf (stderr,
"cannot set minimum available count (%s)\n",
208 if ((
err = snd_pcm_sw_params_set_start_threshold (
handle, sw_params, 0U)) < 0) {
209 fprintf (stderr,
"cannot set start mode (%s)\n",
213 if ((
err = snd_pcm_sw_params (
handle, sw_params)) < 0) {
214 fprintf (stderr,
"cannot set software parameters (%s)\n",
232 }
else if (e == -EPIPE) {
235 ALSA9_ERROR(
"alsa_recover(): Unable to recover from underrun. ",
err);
238 }
else if (e == -ESTRPIPE) {
239 while ((
err = snd_pcm_resume(
handle)) == -EAGAIN) sleep(1);
243 ALSA9_ERROR(
"alsa_recover(): Unable to recover from suspend. ",
err);
265 #ifdef SOUND_DEBUG_WRITES
266 printf(
"audio play - writing starting at %d, %d bytes, off=%d\n",
300 unsigned left_ratio,right_ratio;
315 fprintf(stderr,
"Invalid sound number: %d\n", soundnum);
328 fprintf(stderr,
"Unknown soundtype: %d\n", soundtype);
333 fprintf(stderr,
"Sound %d (type %d) is not defined\n", soundnum, soundtype);
345 fprintf(stderr,
"Loading file: %s\n",si->
filename);
351 si->
size=sbuf.st_size;
352 if (si->
size <=0 )
return;
354 fprintf(stderr,
"Sound %s too long (%i > %i)\n",si->
filename,si->
size,
358 si->
data=(
unsigned char *)malloc(si->
size);
371 fprintf(stderr,
"Playing sound %i (%s), volume %i, x,y=%d,%d, dist=%f\n",soundnum,si->
symbolic,si->
volume,x,y, dist);
378 diff=(1.0-fabs((
double)x/dist));
382 printf(
"diff: %f\n",diff);
385 if (x<0) right_ratio*=diff;
386 else left_ratio*=diff;
390 fprintf(stderr,
"Ratio: %i, %i\n",left_ratio,right_ratio);
396 for(i=0;i<si->
size;i++){
397 int dat=si->
data[i]-0x80;
409 #ifdef WORDS_BIGENDIAN
443 fprintf(stderr,
"Added %d bytes, last buffer=%d, lastpos=%d\n",
461 int main(
int argc,
char *argv[])
465 int inbuf_pos=0, sfd, frames, maxframes;
467 struct timeval timeout;
481 maxframes = snd_pcm_avail_update (
handle);
485 timeout.tv_usec=10000;
488 sfd = select(FD_SETSIZE,&inset,NULL,NULL,&timeout);
495 frames = snd_pcm_avail_update (
handle);
497 while (((
settings.
buflen+frames)> maxframes) || (frames == -EPIPE)) {
502 if (frames == -EPIPE) snd_pcm_prepare(
handle);
504 #ifdef SOUND_DEBUG_WRITES
505 printf(
"play_sound(): wrote %d\n",wrote);
521 frames = snd_pcm_avail_update (
handle);
526 if (FD_ISSET(infd,&inset)){
527 int err=read(infd,inbuf+inbuf_pos,1);
528 if (err<1 && errno!=EINTR){
529 if (err<0) perror(
"read");
532 if (inbuf[inbuf_pos]==
'\n'){
533 inbuf[inbuf_pos++]=0;
534 SoundCmd((
unsigned char *)inbuf,inbuf_pos);
539 if (inbuf_pos>=1024){
540 fprintf(stderr,
"Input buffer overflow!\n");
int audio_play(int buffer, int off)
Sound_Info spell_sounds[MAX_SOUNDS]
snd_pcm_hw_params_t * params
Sound_Info normal_sounds[MAX_SOUNDS]
void alsa_audio_close(void)
#define ALSA9_ERROR(str, err)
int main(int argc, char *argv[])
void play_sound(int soundnum, int soundtype, int x, int y)
static char * rcsid_cfsndserv_c
struct sound_settings settings
void SoundCmd(unsigned char *data, int len)
static snd_pcm_uframes_t chunk_size