rnd-20020505-1-src
[rocksndiamonds.git] / src / libgame / sound.c
index 5d0817a33e0ce9558520a9623fd0ae0e4569ce52..b71ed27fe3d7ef4076494e33f9d641e6cb9c62ec 100644 (file)
@@ -651,6 +651,7 @@ static void sound_handler(struct SoundControl snd_ctrl)
 #if !defined(PLATFORM_WIN32)
 static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
 {
+  SoundInfo *snd_info;
   int i, k;
 
   if (snd_ctrl.music)
@@ -658,6 +659,15 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
   else if (snd_ctrl.nr >= num_sounds)
     return;
 
+  snd_info = (snd_ctrl.music ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]);
+  if (snd_info == NULL)
+  {
+#if 1
+    printf("sound/music %d undefined\n", snd_ctrl.nr);
+#endif
+    return;
+  }
+
   /* if playlist is full, remove oldest sound */
   if (playing_sounds == MAX_SOUNDS_PLAYING)
   {
@@ -752,9 +762,6 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
     if (!playlist[i].active ||
        (snd_ctrl.music && i == audio.music_channel))
     {
-      SoundInfo *snd_info =
-       (snd_ctrl.music ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]);
-
       snd_ctrl.data_ptr = snd_info->data_ptr;
       snd_ctrl.data_len = snd_info->data_len;
       snd_ctrl.format   = snd_info->format;
@@ -1123,7 +1130,7 @@ static SoundInfo *Load_WAV(char *filename)
   if (!audio.sound_available)
     return NULL;
 
-#if 1
+#if 0
   printf("loading WAV file '%s'\n", filename);
 #endif
 
@@ -1230,50 +1237,87 @@ static SoundInfo *Load_WAV(char *filename)
   return snd_info;
 }
 
-static void LoadCustomSound(SoundInfo **snd_info, char *basename)
+static void deleteSoundEntry(SoundInfo **snd_info)
 {
-  char *filename = getCustomSoundFilename(basename);
-
-  if (filename == NULL)                /* (should never happen) */
-  {
-    Error(ERR_WARN, "cannot find sound file '%s'", basename);
-    return;
-  }
-
   if (*snd_info)
   {
-    char *filename_old = (*snd_info)->source_filename;
-
-    if (strcmp(filename, filename_old) == 0)
-    {
-      /* The old and new sound are the same (have the same filename and path).
-        This usually means that this sound does not exist in this sound set
-        and a fallback to the existing sound is done. */
+    char *filename = (*snd_info)->source_filename;
 
 #if 1
-      printf("[sound '%s' already exists]\n", filename);
+    printf("[decrementing reference counter of sound '%s']\n", filename);
 #endif
 
-      return;
-    }
-
     if (--(*snd_info)->num_references <= 0)
     {
 #if 1
-      printf("[deleting sound '%s']\n", filename_old);
+      printf("[deleting sound '%s']\n", filename);
 #endif
 
       /*
       FreeSound(*snd_info);
       */
-      deleteNodeFromList(&SoundFileList, filename_old, FreeSound);
+      deleteNodeFromList(&SoundFileList, filename, FreeSound);
     }
+
+    *snd_info = NULL;
+  }
+}
+
+static void replaceSoundEntry(SoundInfo **snd_info, char *filename)
+{
+  ListNode *node;
+
+  /* check if the old and the new sound file are the same */
+  if (*snd_info && strcmp((*snd_info)->source_filename, filename) == 0)
+  {
+    /* The old and new sound are the same (have the same filename and path).
+       This usually means that this sound does not exist in this sound set
+       and a fallback to the existing sound is done. */
+
+#if 0
+    printf("[sound '%s' already exists (same list entry)]\n", filename);
+#endif
+
+    return;
   }
 
-  *snd_info = Load_WAV(filename);
-  (*snd_info)->num_references = 1;
+  /* delete existing sound file entry */
+  deleteSoundEntry(snd_info);
 
-  addNodeToList(&SoundFileList, (*snd_info)->source_filename, *snd_info);
+  /* check if the new sound file already exists in the list of sounds */
+  if ((node = getNodeFromKey(SoundFileList, filename)) != NULL)
+  {
+#if 0
+      printf("[sound '%s' already exists (other list entry)]\n", filename);
+#endif
+
+      *snd_info = (SoundInfo *)node->content;
+      (*snd_info)->num_references++;
+  }
+  else if ((*snd_info = Load_WAV(filename)) != NULL)   /* load new sound */
+  {
+    (*snd_info)->num_references = 1;
+    addNodeToList(&SoundFileList, (*snd_info)->source_filename, *snd_info);
+  }
+}
+
+static void LoadCustomSound(SoundInfo **snd_info, char *basename)
+{
+  char *filename = getCustomSoundFilename(basename);
+
+  if (strcmp(basename, SND_FILE_UNDEFINED) == 0)
+  {
+    deleteSoundEntry(snd_info);
+    return;
+  }
+
+  if (filename == NULL)
+  {
+    Error(ERR_WARN, "cannot find sound file '%s'", basename);
+    return;
+  }
+
+  replaceSoundEntry(snd_info, filename);
 }
 
 void InitSoundList(struct SoundEffectInfo *sounds_list, int num_list_entries)
@@ -1290,13 +1334,17 @@ void LoadSoundToList(char *basename, int list_pos)
   if (Sound == NULL || list_pos >= num_sounds)
     return;
 
+#if 0
   printf("loading sound '%s' ...  [%d]\n",
         basename, getNumNodes(SoundFileList));
+#endif
 
   LoadCustomSound(&Sound[list_pos], basename);
 
+#if 0
   printf("loading sound '%s' done [%d]\n",
         basename, getNumNodes(SoundFileList));
+#endif
 }
 
 static MusicInfo *Load_MOD(char *filename)
@@ -1636,7 +1684,8 @@ void dumpList(ListNode *node_first)
 
   while (node)
   {
-    printf("['%s']\n", node->key);
+    printf("['%s' (%d)]\n", node->key,
+          ((SoundInfo *)node->content)->num_references);
     node = node->next;
   }
 
@@ -1681,7 +1730,7 @@ static void ReloadCustomSounds()
 {
   int i;
 
-#if 1
+#if 0
   printf("DEBUG: reloading sounds '%s' ...\n", artwork.sounds_set_current);
 #endif
 
@@ -1698,12 +1747,15 @@ static void ReloadCustomSounds()
   /*
   printf("list size == %d\n", getNumNodes(SoundFileList));
   */
+
+#if 0
   dumpList(SoundFileList);
+#endif
 }
 
 static void ReloadCustomMusic()
 {
-#if 1
+#if 0
   printf("DEBUG: reloading music '%s' ...\n", artwork.music_set_current);
 #endif