42 #include "sdl_mixer.h"
43 #pragma comment( lib, "sdl.lib" )
44 #pragma comment( lib, "sdl_mixer.lib" )
47 #define MAX_SOUNDS 1024
48 #define SOUND_NORMAL 0
67 #define SOUND_DECREASE 0.1
73 static int readtype=0, lastnum=0;
75 char *cp,*volume,*symbolic,*cp1,filename[512];
77 if (line[0]==
'#' || line[0]==
'\n')
return;
79 if ( !strcmp(line,
"Standard Sounds:\n")) {
84 if ( !strcmp(line,
"Spell Sounds:\n")) {
90 fprintf(stderr,
"Got input without finding section header yet:\n%d:%s\n",
95 if (line[strlen(line)-1]==
'\n') {
96 line[strlen(line)-1]=
'\0';
99 len = strcspn(line,
" \t");
104 while(*cp !=
'\0' && (*cp==
' ' || *cp==
'\t')) {
112 if (!(cp=strchr(cp1,
' ')) && !(cp=strchr(cp1,
'\t'))) {
118 while(*cp !=
'\0' && (*cp==
' ' || *cp==
'\t')) {
125 if ( *symbolic==
'"') {
127 for (cp=symbolic; *cp !=
'\0' && *cp !=
'"'; cp++ );
133 if ( !(cp=strchr(cp1,
' ')) && !(cp=strchr(cp1,
'\t'))) {
137 while(*cp!=
'\0' && (*cp==
' ' || *cp==
'\t')) {
148 fprintf(stderr,
"Invalid sound number %d, line %d, buf %s\n",
149 newnum, lineno, line );
153 strcpy(filename, line);
155 if ( symbolic && !strcmp(symbolic,
"DEFAULT")) {
157 default_normal.
filename=strdup(filename);
158 default_normal.
volume = atoi(volume);
159 }
else if (readtype==2) {
160 default_spell.
filename = strdup(filename);
161 default_spell.
volume = atoi(volume);
166 normal_sounds[newnum].
filename = strdup(filename);
167 normal_sounds[newnum].
volume = atoi(volume);
169 normal_sounds[newnum].
symbolic = strdup(symbolic);
171 normal_sounds[newnum].
symbolic = NULL;
173 }
else if (readtype==2) {
174 spell_sounds[newnum].
filename = strdup(filename);
175 spell_sounds[newnum].
volume = atoi(volume);
177 spell_sounds[newnum].
symbolic = strdup(symbolic);
179 spell_sounds[newnum].
symbolic = NULL;
186 void load_sounds_file( )
190 char path[256], buf[512];
195 normal_sounds[i].
size = -1;
196 spell_sounds[i].
size = -1;
201 sprintf(path,
"%s/sounds", getenv(
"HOME"));
203 if ( !(fp=fopen(path,
"r"))) {
204 fprintf(stderr,
"Unable to open %s - will use built in defaults\n", path);
205 for (; i<
sizeof(
def_sounds)/
sizeof(
char*); i++ ) {
210 while(fgets(buf,511,fp)!=NULL) {
218 if ( !normal_sounds[i].filename ) {
222 if ( !spell_sounds[i].filename ) {
226 normal_sounds[i].
data = NULL;
227 spell_sounds[i].
volume = 0;
234 #include <SDL_Mixer.h>
247 }
settings={0,1,0,11025,100,1024,4};
251 Mix_Music *music = NULL;
259 fprintf( stderr,
"Settings: bits: %i, ",
settings.
bit8?8:16);
260 fprintf( stderr,
"%s, ",
settings.
sign?
"signed":
"unsigned");
266 if( SDL_WasInit(SDL_INIT_AUDIO) == 0) {
267 int audio_rate = 22050;
268 uint16 audio_format = AUDIO_S16;
269 int audio_channels = 2;
270 int audio_buffers = 4096;
272 if( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
274 fprintf( stderr,
"Could not initialize SDL audio: %s\n", SDL_GetError());
279 if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
280 fprintf(stderr,
"SDL_MIXER: Unable to open audio: %s\n", SDL_GetError());
285 numtimesopened = Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
287 sprintf(format_str,
"unknown");
288 switch(audio_format) {
289 case AUDIO_U8: sprintf(format_str,
"U8");
break;
290 case AUDIO_S8: sprintf(format_str,
"S8");
break;
291 case AUDIO_U16LSB: sprintf(format_str,
"U16LSB");
break;
292 case AUDIO_S16LSB: sprintf(format_str,
"S16LSB");
break;
293 case AUDIO_U16MSB: sprintf(format_str,
"U16MSB");
break;
294 case AUDIO_S16MSB: sprintf(format_str,
"S16MSB");
break;
296 fprintf(stderr,
"SDL_MIXER: Using SDL Mixer for audio [ %d kHz | %d channels | Audio Format %s ].\n",
297 audio_rate, audio_channels, format_str );
305 if ( music == NULL ) {
306 music = Mix_LoadMUS(
"sfx/backg1.wav");
308 Mix_PlayMusic(music,-1);
311 Mix_PlayMusic(music,-1);
322 if ( !sounds_in_buffer ) {
336 fprintf( stderr,
"bits: %i, ",
bit8?8:16);
337 fprintf( stderr,
"%s, ",
sign?
"signed":
"unsigned");
338 fprintf( stderr,
"%s, ",stereo?
"stereo":
"mono");
341 fprintf( stderr,
"0level: %i\n",
zerolevel);
345 PlaySound(NULL,NULL,SND_ASYNC);
366 if ( soundnum>=MAX_SOUNDS || soundnum<0 ) {
367 fprintf(stderr,
"Invalid sound number: %d\n", soundnum );
371 si = &normal_sounds[soundnum];
373 si = &spell_sounds[soundnum];
375 fprintf(stderr,
"Unknown soundtype: %d\n", soundtype);
382 fprintf(stderr,
"SDL_MIXER: Couldn't load %s: %s\n", si->
filename, SDL_GetError());
391 for ( index=0; index<MIX_CHANNELS; index++ ) {
392 if ( !Mix_Playing(index) ) {
397 dist = sqrt(x*x+y*y);
400 angle = ( y < 0 ) ? 0 : 180;
404 angle = ( asin( (
double )x / (
double )dist ) ) * 180. / 3.14159;
409 if ( Mix_Playing(playchannel) ) {
410 Mix_HaltChannel(playchannel);
414 Mix_SetPosition( playchannel, angle, dist );
416 Mix_PlayChannel(playchannel,si->
data,0);
463 void SoundCmd(
unsigned char *data,
int len)
469 fprintf(stderr,
"Got invalid length on sound command: %d\n", len);
478 fprintf(stderr,
"Playing sound %d (type %d), offset %d, %x\n",
491 LOG(
LOG_INFO,
"init_sounds",
"using regular Windows PlaySound");
492 PlaySound(NULL,NULL,SND_ASYNC);
497 void SoundCmd(
unsigned char *data,
int len)
503 LOG(
LOG_WARNING,
"SoundCmd(win)",
"Got invalid length on sound command: %d\n", len);
510 si = &normal_sounds[ num ];
512 si = &spell_sounds[ num ];
528 if ( ( stat( si->
filename, &sbuf ) == -1 ) || ( ( fsound = fopen( si->
filename,
"rb" ) ) == NULL ) )
538 si->
size=sbuf.st_size;
540 fread( si->
data, si->
size, 1, fsound );
544 PlaySound( si->
data, NULL, SND_ASYNC | SND_MEMORY | SND_NOWAIT);
555 void gettimeofday(
struct timeval *tv,
void* unused)
557 memset(tv, 0,
sizeof(
struct timeval));
572 int strncasecmp(
const char *s1,
const char *s2,
int n)
574 register char c1, c2;
576 while (*s1 && *s2 && n) {
587 return (
int) (*s1 - *s2);
590 int strcasecmp(
const char *s1,
const char*s2)
592 register char c1, c2;
602 if (*s1==
'\0' && *s2==
'\0')
604 return (
int) (*s1 - *s2);
Sound_Info spell_sounds[MAX_SOUNDS]
short GetShort_String(const unsigned char *data)
static void play_sound(int soundnum, int soundtype, int x, int y)
Sound_Info normal_sounds[MAX_SOUNDS]
void LOG(LogLevel level, const char *origin, const char *format,...)
static void parse_sound_line(char *line, int lineno)
sint16 use_config[CONFIG_NUMS]
struct Sound_Info Sound_Info
const char *const def_sounds[]
Sound_Info default_normal
struct sound_settings settings
void SoundCmd(unsigned char *data, int len)