X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fsound.c;h=91da5d7db44c1cc85f0b78df9c4f5bf1bdbb2ca3;hb=228a5718ede5ee00512ed2e333b3cee47baa124c;hp=dc14ad55349e9792a17d121d38b14e48d735402f;hpb=c3d03cdffce070695ba0520d00667b42b8460087;p=rocksndiamonds.git diff --git a/src/sound.c b/src/sound.c index dc14ad55..91da5d7d 100644 --- a/src/sound.c +++ b/src/sound.c @@ -1,70 +1,153 @@ /*********************************************************** * Rocks'n'Diamonds -- McDuffin Strikes Back! * *----------------------------------------------------------* -* ©1995 Artsoft Development * -* Holger Schemel * -* 33659 Bielefeld-Senne * -* Telefon: (0521) 493245 * -* eMail: aeglos@valinor.owl.de * -* aeglos@uni-paderborn.de * -* q99492@pbhrzx.uni-paderborn.de * +* (c) 1995-98 Artsoft Entertainment * +* Holger Schemel * +* Oststrasse 11a * +* 33604 Bielefeld * +* phone: ++49 +521 290471 * +* email: aeglos@valinor.owl.de * *----------------------------------------------------------* * sound.c * ***********************************************************/ #include "sound.h" +#include "misc.h" /*** THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS ***/ +static int playing_sounds = 0; static struct SoundControl playlist[MAX_SOUNDS_PLAYING]; static struct SoundControl emptySoundControl = { -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE, 0,0L,0L,NULL }; + +#if defined(PLATFORM_UNIX) static int stereo_volume[PSND_MAX_LEFT2RIGHT+1]; static char premix_first_buffer[SND_BLOCKSIZE]; +#if defined(AUDIO_STREAMING_DSP) static char premix_left_buffer[SND_BLOCKSIZE]; static char premix_right_buffer[SND_BLOCKSIZE]; static int premix_last_buffer[SND_BLOCKSIZE]; +#endif static unsigned char playing_buffer[SND_BLOCKSIZE]; -static int playing_sounds = 0; +#endif + +/* forward declaration of internal functions */ +#if defined(AUDIO_STREAMING_DSP) +static void SoundServer_InsertNewSound(struct SoundControl); +#elif defined(PLATFORM_UNIX) +static unsigned char linear_to_ulaw(int); +static int ulaw_to_linear(unsigned char); +#endif + +#if defined(PLATFORM_HPUX) +static void HPUX_Audio_Control(); +#endif + +#if defined(PLATFORM_MSDOS) +static void SoundServer_InsertNewSound(struct SoundControl); +static void SoundServer_StopSound(int); +static void SoundServer_StopAllSounds(); +#endif + +#if defined(PLATFORM_UNIX) +int OpenAudioDevice(char *audio_device_name) +{ + int audio_fd; + + /* check if desired audio device is accessible */ + if (access(sound_device_name, W_OK) != 0) + return -1; + + /* try to open audio device in non-blocking mode */ + if ((audio_fd = open(audio_device_name, O_WRONLY | O_NONBLOCK)) < 0) + return audio_fd; + + /* re-open audio device in blocking mode */ + close(audio_fd); + audio_fd = open(audio_device_name, O_WRONLY); + + return audio_fd; +} + +void UnixOpenAudio(struct AudioSystemInfo *audio) +{ + static char *audio_device_name[] = + { + DEVICENAME_DSP, + DEVICENAME_AUDIO + }; + int audio_fd; + int i; + + /* look for available audio devices, starting with preferred ones */ + for (i=0; i= 0) + break; + + if (audio_fd < 0) + { + Error(ERR_WARN, "cannot open audio device - no sound"); + return; + } + + close(audio_fd); + + audio->sound_available = TRUE; + +#if defined(AUDIO_STREAMING_DSP) + audio->loops_available = TRUE; +#endif +} + +void UnixCloseAudio(struct AudioSystemInfo *audio) +{ + if (audio->device_fd) + close(audio->device_fd); +} + +#endif /* PLATFORM_UNIX */ void SoundServer() { + int i; +#if defined(PLATFORM_UNIX) struct SoundControl snd_ctrl; fd_set sound_fdset; - int i; - close(sound_pipe[1]); /* no writing into pipe needed */ + close(audio.soundserver_pipe[1]); /* no writing into pipe needed */ +#endif for(i=0;i=0) + if (playing_sounds || + (audio.device_fd = OpenAudioDevice(sound_device_name)) >= 0) { if (!playing_sounds) /* we just opened the audio device */ { /* 2 buffers / 512 bytes, giving 1/16 second resolution */ /* (with stereo the effective buffer size will shrink to 256) */ fragment_size = 0x00020009; - ioctl(sound_device, SNDCTL_DSP_SETFRAGMENT, &fragment_size); + + if (ioctl(audio.device_fd,SNDCTL_DSP_SETFRAGMENT,&fragment_size) < 0) + Error(ERR_EXIT_SOUND_SERVER, + "cannot set fragment size of /dev/dsp - no sounds"); + /* try if we can use stereo sound */ - stereo = TRUE; - ioctl(sound_device, SNDCTL_DSP_STEREO, &stereo); + if (ioctl(audio.device_fd, SNDCTL_DSP_STEREO, &stereo) < 0) + { +#ifdef DEBUG + static boolean reported = FALSE; + + if (!reported) + { + Error(ERR_RETURN, "cannot get stereo sound on /dev/dsp"); + reported = TRUE; + } +#endif + stereo = FALSE; + } + + if (ioctl(audio.device_fd, SNDCTL_DSP_SPEED, &sample_rate) < 0) + Error(ERR_EXIT_SOUND_SERVER, + "cannot set sample rate of /dev/dsp - no sounds"); + /* get the real fragmentation size; this should return 512 */ - ioctl(sound_device, SNDCTL_DSP_GETBLKSIZE, &fragment_size); + if (ioctl(audio.device_fd, SNDCTL_DSP_GETBLKSIZE,&fragment_size) < 0) + Error(ERR_EXIT_SOUND_SERVER, + "cannot get fragment size of /dev/dsp - no sounds"); + max_sample_size = fragment_size / (stereo ? 2 : 1); } @@ -130,9 +245,10 @@ void SoundServer() SoundServer_InsertNewSound(snd_ctrl); while(playing_sounds && - select(sound_pipe[0]+1,&sound_fdset,NULL,NULL,&delay)<1) + select(audio.soundserver_pipe[0] + 1, + &sound_fdset, NULL, NULL, &delay) < 1) { - FD_SET(sound_pipe[0], &sound_fdset); + FD_SET(audio.soundserver_pipe[0], &sound_fdset); /* first clear the last premixing buffer */ memset(premix_last_buffer,0,fragment_size*sizeof(int)); @@ -229,39 +345,40 @@ void SoundServer() } /* finally play the sound fragment */ - write(sound_device,playing_buffer,fragment_size); + write(audio.device_fd, playing_buffer,fragment_size); } /* if no sounds playing, free device for other sound programs */ if (!playing_sounds) - close(sound_device); + close(audio.device_fd); } } -#else /* von '#ifdef VOXWARE' */ +#else /* !AUDIO_STREAMING_DSP */ if (snd_ctrl.active && !snd_ctrl.loop) { struct timeval delay = { 0, 0 }; - char *sample_ptr; + byte *sample_ptr; long sample_size, max_sample_size = SND_BLOCKSIZE; long sample_rate = 8000; /* standard "/dev/audio" sampling rate */ int wait_percent = 90; /* wait 90% of the real playing time */ int i; - if ((sound_device=open(sound_device_name,O_WRONLY))>=0) + if ((audio.device_fd = OpenAudioDevice(sound_device_name)) >= 0) { playing_sounds = 1; while(playing_sounds && - select(sound_pipe[0]+1,&sound_fdset,NULL,NULL,&delay)<1) + select(audio.soundserver_pipe[0] + 1, + &sound_fdset, NULL, NULL, &delay) < 1) { - FD_SET(sound_pipe[0], &sound_fdset); + FD_SET(audio.soundserver_pipe[0], &sound_fdset); /* get pointer and size of the actual sound sample */ - sample_ptr = snd_ctrl.data_ptr+snd_ctrl.playingpos; + sample_ptr = snd_ctrl.data_ptr + snd_ctrl.playingpos; sample_size = - MIN(max_sample_size,snd_ctrl.data_len-snd_ctrl.playingpos); + MIN(max_sample_size, snd_ctrl.data_len - snd_ctrl.playingpos); snd_ctrl.playingpos += sample_size; /* fill the first mixing buffer with original sample */ @@ -283,33 +400,93 @@ void SoundServer() playing_sounds = 0; /* finally play the sound fragment */ - write(sound_device,playing_buffer,sample_size); + write(audio.device_fd,playing_buffer,sample_size); delay.tv_sec = 0; delay.tv_usec = ((sample_size*10*wait_percent)/(sample_rate))*1000; } - close(sound_device); + close(audio.device_fd); } } -#endif /* von '#ifdef VOXWARE' */ +#endif /* !AUDIO_STREAMING_DSP */ } + +#endif /* PLATFORM_UNIX */ + } -void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) +#if defined(PLATFORM_MSDOS) +static void sound_handler(struct SoundControl snd_ctrl) { - int i,k; + int i; + + if (snd_ctrl.fade_sound) + { + if (!playing_sounds) + return; + + for (i=0; ilongest) { @@ -317,18 +494,22 @@ void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) longest_nr=i; } } +#if defined(PLATFORM_MSDOS) + voice_set_volume(playlist[longest_nr].voice, 0); + deallocate_voice(playlist[longest_nr].voice); +#endif playlist[longest_nr] = emptySoundControl; playing_sounds--; } - /* nachsehen, ob (und ggf. wie oft) Sound bereits gespielt wird */ + /* check if sound is already being played (and how often) */ for(k=0,i=0;i=1 && snd_ctrl.loop) { for(i=0;i=2) { int longest=0, longest_nr=0; - /* den bereits am längsten gespielten (gleichen) Sound suchen */ + /* look for oldest equal sound */ for(i=0;i=longest) { longest=actual; longest_nr=i; } } + +#if defined(PLATFORM_MSDOS) + voice_set_volume(playlist[longest_nr].voice, 0); + deallocate_voice(playlist[longest_nr].voice); +#endif playlist[longest_nr] = emptySoundControl; playing_sounds--; } @@ -373,10 +568,20 @@ void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) { playlist[i] = snd_ctrl; playing_sounds++; + +#if defined(PLATFORM_MSDOS) + playlist[i].voice = allocate_voice(Sound[snd_ctrl.nr].sample_ptr); + if(snd_ctrl.loop) + voice_set_playmode(playlist[i].voice, PLAYMODE_LOOP); + voice_set_volume(playlist[i].voice, snd_ctrl.volume); + voice_set_pan(playlist[i].voice, snd_ctrl.stereo); + voice_start(playlist[i].voice); +#endif break; } } } +#endif /* !PLATFORM_WIN32 */ /* void SoundServer_FadeSound(int nr) @@ -392,7 +597,9 @@ void SoundServer_FadeSound(int nr) } */ -void SoundServer_StopSound(int nr) +#if !defined(PLATFORM_WIN32) +#if defined(PLATFORM_MSDOS) +static void SoundServer_StopSound(int nr) { int i; @@ -402,56 +609,65 @@ void SoundServer_StopSound(int nr) for(i=0;iname, sound_ext); - sprintf(filename,"%s/%s.%s",SND_PATH,snd_info->name,sound_ext); +#if defined(TARGET_SDL) - if (!(file=fopen(filename,"r"))) + snd_info->mix_chunk = Mix_LoadWAV(filename); + if (snd_info->mix_chunk == NULL) { - fprintf(stderr,"%s: cannot open sound file '%s' - no sounds\n", - progname,filename); - return(FALSE); + Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename); + return FALSE; } - if (fseek(file,0,SEEK_END)<0) +#else /* !TARGET_SDL */ + +#if !defined(PLATFORM_MSDOS) + + if ((file = fopen(filename, "r")) == NULL) { - fprintf(stderr,"%s: cannot read sound file '%s' - no sounds\n", - progname,filename); - fclose(file); - return(FALSE); + Error(ERR_WARN, "cannot open sound file '%s' - no sounds", filename); + return FALSE; } - snd_info->file_len = ftell(file); - rewind(file); - - if (!(snd_info->file_ptr=malloc(snd_info->file_len))) + /* read chunk "RIFF" */ + getFileChunk(file, chunk, &chunk_length, BYTE_ORDER_LITTLE_ENDIAN); + if (strcmp(chunk, "RIFF") != 0) { - fprintf(stderr,"%s: out of memory (this shouldn't happen :) - no sounds\n", - progname); + Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename); fclose(file); - return(FALSE); + return FALSE; } - if (fread(snd_info->file_ptr,1,snd_info->file_len,file)!=snd_info->file_len) + /* read chunk "WAVE" */ + getFileChunk(file, chunk, &dummy, BYTE_ORDER_LITTLE_ENDIAN); + if (strcmp(chunk, "WAVE") != 0) { - fprintf(stderr,"%s: cannot read sound file '%s' - no sounds\n", - progname,filename); + Error(ERR_WARN, "missing 'WAVE' chunk of sound file '%s'", filename); fclose(file); - return(FALSE); + return FALSE; } - fclose(file); + /* read header information */ + for (i=0; ifile_ptr; + /* read chunk "data" */ + getFileChunk(file, chunk, &chunk_length, BYTE_ORDER_LITTLE_ENDIAN); + if (strcmp(chunk, "data") != 0) + { + Error(ERR_WARN, "missing 'data' chunk of sound file '%s'", filename); + fclose(file); + return FALSE; + } + + snd_info->data_len = chunk_length; + snd_info->data_ptr = checked_malloc(snd_info->data_len); - if (strncmp(snd_hdr->magic_FORM,"FORM",4) || - snd_info->file_len!=be2long(&snd_hdr->chunk_size)+8 || - strncmp(snd_hdr->magic_8SVX,"8SVX",4)) + /* read sound data */ + if (fread(snd_info->data_ptr, 1, snd_info->data_len, file) != + snd_info->data_len) { - fprintf(stderr,"%s: '%s' is not an IFF/8SVX file or broken- no sounds\n", - progname,filename); - return(FALSE); + Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename); + fclose(file); + return FALSE; } - ptr = (unsigned char *)snd_info->file_ptr; + fclose(file); + + for (i=0; idata_len; i++) + snd_info->data_ptr[i] = snd_info->data_ptr[i] ^ 0x80; + +#else /* PLATFORM_MSDOS */ - while(ptr<(unsigned char *)snd_info->file_ptr+snd_info->file_len) + snd_info->sample_ptr = load_sample(filename); + if (!snd_info->sample_ptr) { - if (!strncmp(ptr,"VHDR",4)) - { - ptr+=be2long((unsigned long *)(ptr+4)); - } - if (!strncmp(ptr,"ANNO",4)) - { - ptr+=be2long((unsigned long *)(ptr+4)); - } - if (!strncmp(ptr,"CHAN",4)) - { - ptr+=be2long((unsigned long *)(ptr+4)); - } - if (!strncmp(ptr,"BODY",4)) - { - snd_info->data_ptr = ptr+8; - snd_info->data_len = be2long((unsigned long *)(ptr+4)); - return(TRUE); - } - ptr++; + Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename); + return FALSE; } - return(FALSE); +#endif /* PLATFORM_MSDOS */ +#endif /* !TARGET_SDL */ + + return TRUE; } void PlaySound(int nr) @@ -663,11 +898,11 @@ void PlaySoundLoop(int nr) PlaySoundExt(nr, PSND_MAX_VOLUME, PSND_MIDDLE, PSND_LOOP); } -void PlaySoundExt(int nr, int volume, int stereo, BOOL loop) +void PlaySoundExt(int nr, int volume, int stereo, boolean loop) { struct SoundControl snd_ctrl = emptySoundControl; - if (sound_status==SOUND_OFF || !sound_on) + if (!audio.sound_available || !setup.sound) return; if (volume