/* * A replacement of strdup(), since it's not defined at some * unix variants. */ extern int init_audio(); #define MAX_SOUNDS 1024 char *buffers=NULL; void play_sound(int soundnum, int soundtype, int x, int y); char *strdup_local(char *str) { char *c=(char *)malloc(sizeof(char)*strlen(str)+1); strcpy(c,str); return c; } typedef struct Sound_Info { char *filename; char *symbolic; unsigned char volume; int size; unsigned char *data; } Sound_Info; Sound_Info normal_sounds[MAX_SOUNDS], spell_sounds[MAX_SOUNDS], default_normal, default_spell; /* parses a line from the sound file. This is a little uglier because * we store some static values in the function so we know what we are doing - * however, it is somewhat necessary so that we can use this same function * to parse both files and the compiled in data. * * Note that this function will modify the data in line. lineno is just * for error tracking purposes. */ static void parse_sound_line(char *line, int lineno) { static int readtype=0, lastnum=0; int newnum, len; char *cp,*volume,*symbolic,*cp1,filename[512]; if (line[0]=='#' || line[0]=='\n') return; if (!strcmp(line,"Standard Sounds:\n")) { lastnum=0; readtype=1; return; } if (!strcmp(line,"Spell Sounds:\n")) { lastnum=0; readtype=2; return; } if (!readtype) { #ifdef SOUND_DEBUG fprintf(stderr,"Got input without finding section header yet:\n%d:%s\n", lineno, line); #endif return; } if (line[strlen(line)-1]=='\n') line[strlen(line)-1]='\0'; len=strcspn(line, " \t"); line[len]='\0'; cp = line+len+1; /* Skip all whitespace for the next field */ while (*cp!='\0' && (*cp==' ' || *cp=='\t')) cp++; volume=cp; /* No symbolic name or number - that is ok */ cp1=cp; if (!(cp=strchr(cp1,' ')) && !(cp=strchr(cp1,'\t'))) { newnum=lastnum+1; symbolic=NULL; } else { /* We think we have a symbolic name */ /* Don't need to nulterm the volume, since we atoi it anyways */ while (*cp!='\0' && (*cp==' ' || *cp=='\t')) cp++; symbolic=cp; /* Some symbolc names are double quote protected. If, do some * special processing. We strip off the quotes. */ if (*symbolic=='"') { symbolic++; for (cp=symbolic; *cp!='\0' && *cp!='"'; cp++) ; *cp='\0'; cp++; } /* Lets try to find the sound number now */ cp1 = cp; if (!(cp=strchr(cp1,' ')) && !(cp=strchr(cp1,'\t'))) newnum=lastnum+1; else { *cp++='\0'; while (*cp!='\0' && (*cp==' ' || *cp=='\t')) cp++; if (isdigit(*cp)) newnum=atoi(cp); else newnum=lastnum+1; } } if (newnum < 0 || newnum>MAX_SOUNDS) { fprintf(stderr,"Invalid sound number %d, line %d, buf %s\n", newnum, lineno, line); return; } /* Compatibility processing for older files - if the file ends in * .au, convert to .raw. A bit of a hack, but probably better than * trying to play an au file. */ strcpy(filename, line); cp = filename + strlen(filename)-3; if (!strcmp(cp, ".au")) strcpy(cp, ".raw"); if (symbolic && !strcmp(symbolic,"DEFAULT")) { if (readtype==1) { default_normal.filename=strdup_local(filename); default_normal.volume=atoi(volume); } else if (readtype==2) { default_spell.filename=strdup_local(filename); default_spell.volume=atoi(volume); } return; } else { if (readtype==1) { normal_sounds[newnum].filename = strdup_local(filename); normal_sounds[newnum].volume = atoi(volume); if (symbolic) normal_sounds[newnum].symbolic=strdup_local(symbolic); else normal_sounds[newnum].symbolic=NULL; } else if (readtype==2) { spell_sounds[newnum].filename = strdup_local(filename); spell_sounds[newnum].volume = atoi(volume); if (symbolic) spell_sounds[newnum].symbolic=strdup_local(symbolic); else spell_sounds[newnum].symbolic=NULL; } lastnum=newnum; } } /* init_sounds open the audio device, and reads any configuration files * that need to be. It returns 0 on success. On failure, the calling * function will likely disable sound support/requests from the server. */ int init_sounds() { int i; FILE *fp; char path[256], buf[512]; #ifdef SOUND_DEBUG fprintf( stderr,"Settings: bits: %i, ",settings.bit8?8:16); fprintf( stderr,"%s, ",settings.sign?"signed":"unsigned"); fprintf( stderr,"%s, ",settings.stereo?"stereo":"mono"); fprintf( stderr,"frequency: %i, ",settings.frequency); fprintf( stderr,"device: %s\n",settings.audiodev); #endif buffers = (char *)malloc( settings.buffers * settings.buflen ); if ( !buffers ) return -1; sounds_in_buffer = (int *)calloc( settings.buffers,sizeof(int) ); if ( !sounds_in_buffer ) return -1; if (init_audio()) return -1; memset(buffers,zerolevel,settings.buflen*settings.buffers); #ifdef SOUND_DEBUG fprintf( stderr,"bits: %i, ",settings.bit8?8:16); fprintf( stderr,"%s, ",sign?"signed":"unsigned"); fprintf( stderr,"%s, ",stereo?"stereo":"mono"); fprintf( stderr,"freq: %i, ",frequency); fprintf( stderr,"smpl_size: %i, ",sample_size); fprintf( stderr,"0level: %i\n",zerolevel); #endif for (i=0; i