rnd-19990205-1
[rocksndiamonds.git] / src / sound.c
index 8938750c5b6d490b009fa0765d03c7624f0899da..51a75c88dbe5e6a9d7d5f9d1d63e462cd7cc4ff7 100644 (file)
 #include "sound.h"
 #include "misc.h"
 
-#ifdef MSDOS
-extern void sound_handler(struct SoundControl);
-#endif
-
 /*** THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS ***/
 
-#ifndef MSDOS
+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
 };
+
+#ifndef MSDOS
 static int stereo_volume[PSND_MAX_LEFT2RIGHT+1];
 static char premix_first_buffer[SND_BLOCKSIZE];
+#ifdef VOXWARE
 static char premix_left_buffer[SND_BLOCKSIZE];
 static char premix_right_buffer[SND_BLOCKSIZE];
 static int premix_last_buffer[SND_BLOCKSIZE];
+#endif /* VOXWARE */
 static unsigned char playing_buffer[SND_BLOCKSIZE];
-static int playing_sounds = 0;
-#else
-struct SoundControl playlist[MAX_SOUNDS_PLAYING];
-struct SoundControl emptySoundControl;
-int playing_sounds;
+#endif /* MSDOS */
+
+/* forward declaration of internal functions */
+#ifdef VOXWARE
+static void SoundServer_InsertNewSound(struct SoundControl);
+#endif
+#ifndef VOXWARE
+#ifndef MSDOS
+static unsigned char linear_to_ulaw(int);
+static int ulaw_to_linear(unsigned char);
+#endif
+#endif
+#ifdef HPUX_AUDIO
+static void HPUX_Audio_Control();
+#endif
+#ifdef MSDOS
+static void SoundServer_InsertNewSound(struct SoundControl);
+static void SoundServer_StopSound(int);
+static void SoundServer_StopAllSounds();
 #endif
 
 void SoundServer()
@@ -66,14 +80,14 @@ void SoundServer()
   FD_ZERO(&sound_fdset); 
   FD_SET(sound_pipe[0], &sound_fdset);
 
-  for(;;)      /* wait for calls from PlaySound(), StopSound(), ... */
+  while(1)     /* wait for sound playing commands from client */
   {
     FD_SET(sound_pipe[0], &sound_fdset);
     select(sound_pipe[0]+1, &sound_fdset, NULL, NULL, NULL);
     if (!FD_ISSET(sound_pipe[0], &sound_fdset))
       continue;
     if (read(sound_pipe[0], &snd_ctrl, sizeof(snd_ctrl)) != sizeof(snd_ctrl))
-      Error(ERR_EXIT_SOUNDSERVER, "broken pipe - no sounds");
+      Error(ERR_EXIT_SOUND_SERVER, "broken pipe - no sounds");
 
 #ifdef VOXWARE
 
@@ -116,10 +130,14 @@ void SoundServer()
     if (playing_sounds || snd_ctrl.active)
     {
       struct timeval delay = { 0, 0 };
-      char *sample_ptr;
-      long sample_size, max_sample_size;
-      long fragment_size;
-      boolean stereo;
+      byte *sample_ptr;
+      long sample_size;
+      static long max_sample_size = 0;
+      static long fragment_size = 0;
+      /* Even if the stereo flag is used as being boolean, it must be
+        defined as an integer, else 'ioctl()' will fail! */
+      int stereo = TRUE;
+      int sample_rate = 8000;
 
       if (playing_sounds || (sound_device=open(sound_device_name,O_WRONLY))>=0)
       {
@@ -128,12 +146,35 @@ void SoundServer()
          /* 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(sound_device, 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(sound_device, 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(sound_device, 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(sound_device, 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);
        }
 
@@ -249,12 +290,12 @@ void SoundServer()
       }
     }
 
-#else  /* von '#ifdef VOXWARE' */
+#else /* !VOXWARE */
 
     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 */
@@ -270,9 +311,9 @@ void SoundServer()
          FD_SET(sound_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 */
@@ -303,17 +344,70 @@ void SoundServer()
       }
     }
 
-#endif /* von '#ifdef VOXWARE' */
+#endif /* !VOXWARE */
 
   }
-#endif
+#endif /* !MSDOS */
 }
 
-void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
+#ifdef MSDOS
+static void sound_handler(struct SoundControl snd_ctrl)
+{
+  int i;
+
+  if (snd_ctrl.fade_sound)
+  {
+    if (!playing_sounds)
+      return;
+
+    for (i=0; i<MAX_SOUNDS_PLAYING; i++)
+      if ((snd_ctrl.stop_all_sounds || playlist[i].nr == snd_ctrl.nr) &&
+         !playlist[i].fade_sound)
+      {
+       playlist[i].fade_sound = TRUE;
+       if (voice_check(playlist[i].voice))
+         voice_ramp_volume(playlist[i].voice, 1000, 0);
+       playlist[i].loop = PSND_NO_LOOP;
+      }
+  }
+  else if (snd_ctrl.stop_all_sounds)
+  {
+    if (!playing_sounds)
+      return;
+    SoundServer_StopAllSounds();
+  }
+  else if (snd_ctrl.stop_sound)
+  {
+    if (!playing_sounds)
+      return;
+    SoundServer_StopSound(snd_ctrl.nr);
+  }
+
+  for (i=0; i<MAX_SOUNDS_PLAYING; i++)
+  {
+    if (!playlist[i].active || playlist[i].loop)
+      continue;
+
+    playlist[i].playingpos = voice_get_position(playlist[i].voice);
+    playlist[i].volume = voice_get_volume(playlist[i].voice);
+    if (playlist[i].playingpos == -1 || !playlist[i].volume)
+    {
+      deallocate_voice(playlist[i].voice);
+      playlist[i] = emptySoundControl;
+      playing_sounds--;
+    }
+  }
+
+  if (snd_ctrl.active)
+    SoundServer_InsertNewSound(snd_ctrl);
+}
+#endif /* MSDOS */
+
+static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
 {
-  int i,k;
+  int i, k;
 
-  /* wenn voll, ältesten Sound 'rauswerfen */
+  /* if playlist is full, remove oldest sound */
   if (playing_sounds==MAX_SOUNDS_PLAYING)
   {
     int longest=0, longest_nr=0;
@@ -340,14 +434,14 @@ void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
     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<MAX_SOUNDS_PLAYING;i++)
   {
     if (playlist[i].nr == snd_ctrl.nr)
       k++;
   }
 
-  /* falls Sound-Loop: nur neu beginnen, wenn Sound gerade ausklingt */
+  /* restart loop sounds only if they are just fading out */
   if (k>=1 && snd_ctrl.loop)
   {
     for(i=0;i<MAX_SOUNDS_PLAYING;i++)
@@ -366,12 +460,12 @@ void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
     return;
   }
 
-  /* keinen Sound mehr als n mal gleichzeitig spielen (momentan n==2) */
+  /* don't play sound more than n times simultaneously (with n == 2 for now) */
   if (k>=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<MAX_SOUNDS_PLAYING;i++)
     {
       int actual;
@@ -432,7 +526,8 @@ void SoundServer_FadeSound(int nr)
 }
 */
 
-void SoundServer_StopSound(int nr)
+#ifdef MSDOS
+static void SoundServer_StopSound(int nr)
 {
   int i;
 
@@ -456,7 +551,7 @@ void SoundServer_StopSound(int nr)
 #endif
 }
 
-void SoundServer_StopAllSounds()
+static void SoundServer_StopAllSounds()
 {
   int i;
 
@@ -474,22 +569,23 @@ void SoundServer_StopAllSounds()
   close(sound_device);
 #endif
 }
+#endif /* MSDOS */
 
 #ifdef HPUX_AUDIO
-void HPUX_Audio_Control()
+static void HPUX_Audio_Control()
 {
   struct audio_describe ainfo;
   int audio_ctl;
 
   audio_ctl = open("/dev/audioCtl", O_WRONLY | O_NDELAY);
   if (audio_ctl == -1)
-    Error(ERR_EXIT_SOUNDSERVER, "cannot open /dev/audioCtl - no sounds");
+    Error(ERR_EXIT_SOUND_SERVER, "cannot open /dev/audioCtl - no sounds");
 
   if (ioctl(audio_ctl, AUDIO_DESCRIBE, &ainfo) == -1)
-    Error(ERR_EXIT_SOUNDSERVER, "no audio info - no sounds");
+    Error(ERR_EXIT_SOUND_SERVER, "no audio info - no sounds");
 
   if (ioctl(audio_ctl, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_ULAW) == -1)
-    Error(ERR_EXIT_SOUNDSERVER, "ulaw audio not available - no sounds");
+    Error(ERR_EXIT_SOUND_SERVER, "ulaw audio not available - no sounds");
 
   ioctl(audio_ctl, AUDIO_SET_CHANNELS, 1);
   ioctl(audio_ctl, AUDIO_SET_SAMPLE_RATE, 8000);
@@ -498,6 +594,8 @@ void HPUX_Audio_Control()
 }
 #endif /* HPUX_AUDIO */
 
+#ifndef VOXWARE
+#ifndef MSDOS
 /* these two are stolen from "sox"... :) */
 
 /*
@@ -525,7 +623,7 @@ void HPUX_Audio_Control()
 #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
 #define CLIP 32635
 
-unsigned char linear_to_ulaw(int sample)
+static unsigned char linear_to_ulaw(int sample)
 {
   static int exp_lut[256] =
   {
@@ -586,7 +684,7 @@ unsigned char linear_to_ulaw(int sample)
 ** Output: signed 16 bit linear sample
 */
 
-int ulaw_to_linear(unsigned char ulawbyte)
+static int ulaw_to_linear(unsigned char ulawbyte)
 {
   static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
   int sign, exponent, mantissa, sample;
@@ -601,6 +699,8 @@ int ulaw_to_linear(unsigned char ulawbyte)
 
   return(sample);
 }
+#endif /* !MSDOS */
+#endif /* !VOXWARE */
 
 /*** THE STUFF ABOVE IS ONLY USED BY THE SOUND SERVER CHILD PROCESS ***/
 
@@ -608,6 +708,7 @@ int ulaw_to_linear(unsigned char ulawbyte)
 
 /*** THE STUFF BELOW IS ONLY USED BY THE MAIN PROCESS ***/
 
+#ifndef MSDOS
 static unsigned long be2long(unsigned long *be)        /* big-endian -> longword */
 {
   unsigned char *ptr = (unsigned char *)be;
@@ -615,19 +716,104 @@ static unsigned long be2long(unsigned long *be)  /* big-endian -> longword */
   return(ptr[0]<<24 | ptr[1]<<16 | ptr[2]<<8 | ptr[3]);
 }
 
+static unsigned long le2long(unsigned long *be)        /* little-endian -> longword */
+{
+  unsigned char *ptr = (unsigned char *)be;
+
+  return(ptr[3]<<24 | ptr[2]<<16 | ptr[1]<<8 | ptr[0]);
+}
+#endif /* !MSDOS */
+
 boolean LoadSound(struct SoundInfo *snd_info)
 {
+  char filename[256];
+  char *sound_ext = "wav";
+#ifndef MSDOS
+  struct SoundHeader_WAV *sound_header;
   FILE *file;
+  int i;
+#endif
+
+  sprintf(filename, "%s/%s/%s.%s",
+         options.base_directory, SOUNDS_DIRECTORY, snd_info->name, sound_ext);
+
+#ifndef MSDOS
+
+  if ((file = fopen(filename, "r")) == NULL)
+  {
+    Error(ERR_WARN, "cannot open sound file '%s' - no sounds", filename);
+    return(FALSE);
+  }
+
+  if (fseek(file, 0, SEEK_END) < 0)
+  {
+    Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
+    fclose(file);
+    return(FALSE);
+  }
+
+  snd_info->file_len = ftell(file);
+  rewind(file);
+
+  snd_info->file_ptr = checked_malloc(snd_info->file_len);
+
+  if (fread(snd_info->file_ptr, 1, snd_info->file_len, file) !=
+      snd_info->file_len)
+  {
+    Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
+    fclose(file);
+    return(FALSE);
+  }
+
+  fclose(file);
+
+  sound_header = (struct SoundHeader_WAV *)snd_info->file_ptr;
+
+  if (strncmp(sound_header->magic_RIFF, "RIFF", 4) ||
+      snd_info->file_len != le2long(&sound_header->header_size) + 8 ||
+      strncmp(sound_header->magic_WAVE, "WAVE", 4) ||
+      strncmp(sound_header->magic_DATA, "data", 4) ||
+      snd_info->file_len != le2long(&sound_header->data_size) + 44)
+  {
+    Error(ERR_WARN, "'%s' is not a RIFF/WAVE file or broken - no sounds",
+         filename);
+    return(FALSE);
+  }
+
+  snd_info->data_ptr = snd_info->file_ptr + 44;
+  snd_info->data_len = le2long(&sound_header->data_size);
+
+  for (i=0; i<snd_info->data_len; i++)
+    snd_info->data_ptr[i] = snd_info->data_ptr[i]^0x80;
+
+#else /* MSDOS */
+
+  snd_info->sample_ptr = load_sample(filename);
+  if (!snd_info->sample_ptr)
+  {
+    Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
+    return(FALSE);
+  }
+
+#endif /* MSDOS */
+
+  return(TRUE);
+}
+
+boolean LoadSound_8SVX(struct SoundInfo *snd_info)
+{
   char filename[256];
 #ifndef MSDOS
+  struct SoundHeader_8SVX *sound_header;
+  FILE *file;
+  char *ptr;
   char *sound_ext = "8svx";
 #else
   char *sound_ext = "wav";
 #endif
-  struct SoundHeader_8SVX *sound_header;
-  unsigned char *ptr;
 
-  sprintf(filename,"%s/%s.%s",SND_PATH,snd_info->name,sound_ext);
+  sprintf(filename, "%s/%s/%s.%s",
+         options.base_directory, SOUNDS_DIRECTORY, snd_info->name, sound_ext);
 
 #ifndef MSDOS
   if (!(file=fopen(filename,"r")))
@@ -673,9 +859,9 @@ boolean LoadSound(struct SoundInfo *snd_info)
     return(FALSE);
   }
 
-  ptr = snd_info->file_ptr + 12;
+  ptr = (char *)snd_info->file_ptr + 12;
 
-  while(ptr < (unsigned char *)(snd_info->file_ptr + snd_info->file_len))
+  while(ptr < (char *)(snd_info->file_ptr + snd_info->file_len))
   {
     if (!strncmp(ptr,"VHDR",4))
     {
@@ -694,7 +880,7 @@ boolean LoadSound(struct SoundInfo *snd_info)
     }
     else if (!strncmp(ptr,"BODY",4))
     {
-      snd_info->data_ptr = ptr + 8;
+      snd_info->data_ptr = (byte *)ptr + 8;
       snd_info->data_len = be2long((unsigned long *)(ptr + 4));
       return(TRUE);
     }
@@ -707,16 +893,15 @@ boolean LoadSound(struct SoundInfo *snd_info)
   }
 
   return(FALSE);
-#else
+#else /* MSDOS */
   snd_info->sample_ptr = load_sample(filename);
   if(!snd_info->sample_ptr)
   {
     Error(ERR_WARN, "cannot read sound file '%s' - no sounds", filename);
-    fclose(file);
     return(FALSE);
   }
   return(TRUE);
-#endif  // von  #ifndef MSDOS
+#endif /* MSDOS */
 }
 
 void PlaySound(int nr)