rnd-20020422-1-src
[rocksndiamonds.git] / src / libgame / sound.c
index 088df1276eeb3618188e7365f6620454a6616280..52fdcbfb7357d1d8d5964129fec64a35e13436dc 100644 (file)
@@ -21,6 +21,7 @@
 #include "system.h"
 #include "sound.h"
 #include "misc.h"
+#include "setup.h"
 
 
 static int num_sounds = 0, num_music = 0;
@@ -38,7 +39,7 @@ 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
+  -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, 0,0L,0L,NULL
 };
 
 #if defined(PLATFORM_UNIX)
@@ -135,7 +136,7 @@ static boolean ForkAudioProcess(void)
     return FALSE;
   }
 
-  if (audio.soundserver_pid == 0)      /* we are child */
+  if (audio.soundserver_pid == 0)      /* we are child process */
   {
     SoundServer();
 
@@ -168,7 +169,7 @@ void UnixCloseAudio(void)
   if (audio.device_fd)
     close(audio.device_fd);
 
-  if (audio.soundserver_pid)
+  if (audio.soundserver_pid > 0)       /* we are parent process */
     kill(audio.soundserver_pid, SIGTERM);
 }
 #endif /* PLATFORM_UNIX */
@@ -227,6 +228,22 @@ void SoundServer(void)
        != sizeof(snd_ctrl))
       Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
 
+    if (snd_ctrl.reload_sounds || snd_ctrl.reload_music)
+    {
+      for(i=0;i<MAX_SOUNDS_PLAYING;i++)
+       playlist[i] = emptySoundControl;
+      playing_sounds = 0;
+
+      close(audio.device_fd);
+
+      if (snd_ctrl.reload_sounds)
+       ReloadSounds();
+      else
+       ReloadMusic();
+
+      continue;
+    }
+
 #if defined(AUDIO_STREAMING_DSP)
 
     if (snd_ctrl.fade_sound)
@@ -244,8 +261,8 @@ void SoundServer(void)
        continue;
 
       for(i=0;i<MAX_SOUNDS_PLAYING;i++)
-       playlist[i]=emptySoundControl;
-      playing_sounds=0;
+       playlist[i] = emptySoundControl;
+      playing_sounds = 0;
 
       close(audio.device_fd);
     }
@@ -257,7 +274,7 @@ void SoundServer(void)
       for(i=0;i<MAX_SOUNDS_PLAYING;i++)
        if (playlist[i].nr == snd_ctrl.nr)
        {
-         playlist[i]=emptySoundControl;
+         playlist[i] = emptySoundControl;
          playing_sounds--;
        }
 
@@ -273,7 +290,7 @@ void SoundServer(void)
       static long max_sample_size = 0;
       static long fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
       int sample_rate = DEFAULT_AUDIO_SAMPLE_RATE;
-      boolean stereo = TRUE;
+      static boolean stereo = TRUE;
 
       if (playing_sounds ||
          (audio.device_fd = OpenAudioDevice(audio.device_name)) >= 0)
@@ -910,18 +927,26 @@ static int ulaw_to_linear(unsigned char ulawbyte)
 /* ========================================================================= */
 /* THE STUFF BELOW IS ONLY USED BY THE MAIN PROCESS                          */
 
+void ReloadSounds()
+{
+  printf("reloading sounds ...\n");
+}
+
+void ReloadMusic()
+{
+  printf("reloading music ...\n");
+}
 
 #define CHUNK_ID_LEN            4       /* IFF style chunk id length */
-#define WAV_HEADER_SIZE                20      /* size of WAV file header */
+#define WAV_HEADER_SIZE                16      /* size of WAV file header */
 
-static boolean LoadSoundExt(char *sound_name, boolean is_music)
+static boolean Load_WAV(char *filename)
 {
   struct SampleInfo *snd_info;
-  char filename[256];
 #if !defined(TARGET_SDL) && !defined(PLATFORM_MSDOS)
   byte sound_header_buffer[WAV_HEADER_SIZE];
-  char chunk[CHUNK_ID_LEN + 1];
-  int chunk_size, dummy;
+  char chunk_name[CHUNK_ID_LEN + 1];
+  int chunk_size;
   FILE *file;
   int i;
 #endif
@@ -933,16 +958,14 @@ static boolean LoadSoundExt(char *sound_name, boolean is_music)
   Sound = checked_realloc(Sound, num_sounds * sizeof(struct SampleInfo));
 
   snd_info = &Sound[num_sounds - 1];
-  snd_info->name = sound_name;
-
-  sprintf(filename, "%s/%s/%s", options.ro_base_directory,
-         (is_music ? MUSIC_DIRECTORY : SOUNDS_DIRECTORY), snd_info->name);
+  snd_info->data_len = 0;
+  snd_info->data_ptr = NULL;
 
 #if defined(TARGET_SDL)
 
   if ((snd_info->mix_chunk = Mix_LoadWAV(filename)) == NULL)
   {
-    Error(ERR_WARN, "cannot read sound file '%s' -- no sounds", filename);
+    Error(ERR_WARN, "cannot read sound file '%s'", filename);
     return FALSE;
   }
 
@@ -950,55 +973,74 @@ static boolean LoadSoundExt(char *sound_name, boolean is_music)
 
   if ((file = fopen(filename, MODE_READ)) == NULL)
   {
-    Error(ERR_WARN, "cannot open sound file '%s' -- no sounds", filename);
+    Error(ERR_WARN, "cannot open sound file '%s'", filename);
     return FALSE;
   }
 
-  /* read chunk "RIFF" */
-  getFileChunk(file, chunk, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN);
-  if (strcmp(chunk, "RIFF") != 0)
+  /* read chunk id "RIFF" */
+  getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN);
+  if (strcmp(chunk_name, "RIFF") != 0)
   {
     Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename);
     fclose(file);
     return FALSE;
   }
 
-  /* read chunk "WAVE" */
-  getFileChunk(file, chunk, &dummy, BYTE_ORDER_LITTLE_ENDIAN);
-  if (strcmp(chunk, "WAVE") != 0)
+  /* read "RIFF" type id "WAVE" */
+  getFileChunk(file, chunk_name, NULL, BYTE_ORDER_LITTLE_ENDIAN);
+  if (strcmp(chunk_name, "WAVE") != 0)
   {
-    Error(ERR_WARN, "missing 'WAVE' chunk of sound file '%s'", filename);
+    Error(ERR_WARN, "missing 'WAVE' type ID of sound file '%s'", filename);
     fclose(file);
     return FALSE;
   }
 
-  /* read header information */
-  for (i=0; i<WAV_HEADER_SIZE; i++)
-    sound_header_buffer[i] = fgetc(file);
-
-  /* read chunk "data" */
-  getFileChunk(file, chunk, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN);
-  if (strcmp(chunk, "data") != 0)
+  while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN))
   {
-    Error(ERR_WARN, "missing 'data' chunk of sound file '%s'", filename);
-    fclose(file);
-    return FALSE;
+#if 0
+    printf("DEBUG: file '%s', chunk id '%s', chunk size '%d' [%d]\n",
+          filename, chunk_name, chunk_size, feof(file));
+#endif
+
+    if (strcmp(chunk_name, "fmt ") == 0)
+    {
+      /* read header information */
+      for (i=0; i < MIN(chunk_size, WAV_HEADER_SIZE); i++)
+       sound_header_buffer[i] = fgetc(file);
+
+      if (chunk_size > WAV_HEADER_SIZE)
+       ReadUnusedBytesFromFile(file, chunk_size - WAV_HEADER_SIZE);
+    }
+    else if (strcmp(chunk_name, "data") == 0)
+    {
+      snd_info->data_len = chunk_size;
+      snd_info->data_ptr = checked_malloc(snd_info->data_len);
+
+      /* read sound data */
+      if (fread(snd_info->data_ptr, 1, snd_info->data_len, file) !=
+         snd_info->data_len)
+      {
+       Error(ERR_WARN,"cannot read 'data' chunk of sound file '%s'",filename);
+       fclose(file);
+       return FALSE;
+      }
+
+      /* check for odd number of sample bytes (data chunk is word aligned) */
+      if ((chunk_size % 2) == 1)
+       ReadUnusedBytesFromFile(file, 1);
+    }
+    else       /* unknown chunk -- ignore */
+      ReadUnusedBytesFromFile(file, chunk_size);
   }
 
-  snd_info->data_len = chunk_size;
-  snd_info->data_ptr = checked_malloc(snd_info->data_len);
+  fclose(file);
 
-  /* read sound data */
-  if (fread(snd_info->data_ptr, 1, snd_info->data_len, file) !=
-      snd_info->data_len)
+  if (snd_info->data_ptr == NULL)
   {
-    Error(ERR_WARN, "cannot read sound file '%s' -- no sounds", filename);
-    fclose(file);
+    Error(ERR_WARN, "missing 'data' chunk of sound file '%s'", filename);
     return FALSE;
   }
 
-  fclose(file);
-
   for (i=0; i<snd_info->data_len; i++)
     snd_info->data_ptr[i] = snd_info->data_ptr[i] ^ 0x80;
 
@@ -1007,7 +1049,7 @@ static boolean LoadSoundExt(char *sound_name, boolean is_music)
   snd_info->sample_ptr = load_sample(filename);
   if (!snd_info->sample_ptr)
   {
-    Error(ERR_WARN, "cannot read sound file '%s' -- no sounds", filename);
+    Error(ERR_WARN, "cannot read sound file '%s'", filename);
     return FALSE;
   }
 
@@ -1016,16 +1058,24 @@ static boolean LoadSoundExt(char *sound_name, boolean is_music)
   return TRUE;
 }
 
-boolean LoadSound(char *sound_name)
+boolean LoadCustomSound(char *basename)
 {
-  return LoadSoundExt(sound_name, FALSE);
+  char *filename = getCustomSoundFilename(basename);
+
+  if (filename == NULL)
+  {
+    Error(ERR_WARN, "cannot find sound file '%s' -- no sounds", filename);
+    return FALSE;
+  }
+
+  return Load_WAV(filename);
 }
 
-boolean LoadMod(char *mod_name)
+static boolean Load_MOD(char *mod_name)
 {
 #if defined(TARGET_SDL)
   struct SampleInfo *mod_info;
-  char filename[256];
+  char *filename;
 
   num_mods++;
   Mod = checked_realloc(Mod, num_mods * sizeof(struct SampleInfo));
@@ -1033,26 +1083,28 @@ boolean LoadMod(char *mod_name)
   mod_info = &Mod[num_mods - 1];
   mod_info->name = mod_name;
 
-  sprintf(filename, "%s/%s/%s", options.ro_base_directory,
-         MUSIC_DIRECTORY, mod_info->name);
+  filename = getPath2(options.music_directory, mod_info->name);
 
   if ((mod_info->mix_music = Mix_LoadMUS(filename)) == NULL)
   {
     Error(ERR_WARN, "cannot read music file '%s' -- no music", filename);
+    free(filename);
     return FALSE;
   }
 
+  free(filename);
+
   return TRUE;
 #else
   return FALSE;
 #endif
 }
 
-int LoadMusic(void)
+int LoadCustomMusic(void)
 {
+  char *music_directory = getCustomMusicDirectory();
   DIR *dir;
   struct dirent *dir_entry;
-  char *music_directory = getPath2(options.ro_base_directory, MUSIC_DIRECTORY);
   int num_wav_music = 0;
   int num_mod_music = 0;
 
@@ -1063,38 +1115,26 @@ int LoadMusic(void)
   {
     Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
     audio.music_available = FALSE;
-    free(music_directory);
     return 0;
   }
 
   while ((dir_entry = readdir(dir)) != NULL)   /* loop until last dir entry */
   {
-    char *filename = dir_entry->d_name;
+    char *filename = getPath2(music_directory, dir_entry->d_name);
 
-    if (strlen(filename) > 4 &&
-       strcmp(&filename[strlen(filename) - 4], ".wav") == 0)
-    {
-      if (LoadSoundExt(filename, TRUE))
-       num_wav_music++;
-    }
-    else if (strlen(filename) > 4 &&
-            (strcmp(&filename[strlen(filename) - 4], ".mod") == 0 ||
-             strcmp(&filename[strlen(filename) - 4], ".MOD") == 0 ||
-             strncmp(filename, "mod.", 4) == 0 ||
-             strncmp(filename, "MOD.", 4) == 0))
-    {
-      if (LoadMod(filename))
-       num_mod_music++;
-    }
+    if (FileIsSound(filename) && Load_WAV(filename))
+      num_wav_music++;
+    else if (FileIsMusic(filename) && Load_MOD(filename))
+      num_mod_music++;
+
+    free(filename);
   }
 
   closedir(dir);
 
   if (num_wav_music == 0 && num_mod_music == 0)
     Error(ERR_WARN, "cannot find any valid music files in directory '%s'",
-         music_directory);
-
-  free(music_directory);
+         options.music_directory);
 
   num_music = (num_mod_music > 0 ? num_mod_music : num_wav_music);
 
@@ -1148,7 +1188,9 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop)
 {
   struct SoundControl snd_ctrl = emptySoundControl;
 
-  if (!audio.sound_available || !audio.sound_enabled)
+  if (!audio.sound_available ||
+      !audio.sound_enabled ||
+      audio.sound_deactivated)
     return;
 
   if (volume<PSND_MIN_VOLUME)
@@ -1173,6 +1215,9 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop)
   Mix_Volume(-1, SOUND_MAX_VOLUME);
   Mix_PlayChannel(-1, Sound[nr].mix_chunk, (loop ? -1 : 0));
 #elif defined(PLATFORM_UNIX)
+  if (audio.soundserver_pid == 0)      /* we are child process */
+    return;
+
   if (write(audio.soundserver_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0)
   {
     Error(ERR_WARN, "cannot pipe to child process -- no sounds");
@@ -1278,6 +1323,9 @@ void StopSoundExt(int nr, int method)
 
 #else
 #if !defined(PLATFORM_MSDOS)
+  if (audio.soundserver_pid == 0)      /* we are child process */
+    return;
+
   if (write(audio.soundserver_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0)
   {
     Error(ERR_WARN, "cannot pipe to child process -- no sounds");
@@ -1290,6 +1338,58 @@ void StopSoundExt(int nr, int method)
 #endif
 }
 
+void InitReloadSounds(char *set_name)
+{
+  struct SoundControl snd_ctrl = emptySoundControl;
+
+  if (!audio.sound_available)
+    return;
+
+  snd_ctrl.reload_sounds = TRUE;
+
+#if defined(TARGET_SDL)
+  ReloadSounds();
+#elif defined(PLATFORM_UNIX)
+  if (audio.soundserver_pid == 0)      /* we are child process */
+    return;
+
+  if (write(audio.soundserver_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0)
+  {
+    Error(ERR_WARN, "cannot pipe to child process -- no sounds");
+    audio.sound_available = audio.sound_enabled = FALSE;
+    return;
+  }
+#elif defined(PLATFORM_MSDOS)
+  sound_handler(snd_ctrl);
+#endif
+}
+
+void InitReloadMusic(char *set_name)
+{
+  struct SoundControl snd_ctrl = emptySoundControl;
+
+  if (!audio.sound_available)
+    return;
+
+  snd_ctrl.reload_music = TRUE;
+
+#if defined(TARGET_SDL)
+  ReloadMusic();
+#elif defined(PLATFORM_UNIX)
+  if (audio.soundserver_pid == 0)      /* we are child process */
+    return;
+
+  if (write(audio.soundserver_pipe[1], &snd_ctrl, sizeof(snd_ctrl)) < 0)
+  {
+    Error(ERR_WARN, "cannot pipe to child process -- no sounds");
+    audio.sound_available = audio.sound_enabled = FALSE;
+    return;
+  }
+#elif defined(PLATFORM_MSDOS)
+  sound_handler(snd_ctrl);
+#endif
+}
+
 void FreeSounds(int num_sounds)
 {
   int i;