moved handling game music with negative ID from mixer to helper function
[rocksndiamonds.git] / src / libgame / sound.c
index cb788e7e573769a7efeec3e8ff1fbeaec7692fc3..15354b68575dbc6b95b97563b996905ed17f24cb 100644 (file)
@@ -225,6 +225,16 @@ static void Mixer_PlayMusicChannel()
     // (this may happen when switching on music while playing the game)
     Mix_VolumeMusic(mixer[audio.music_channel].volume);
     Mix_FadeInMusic(mixer[audio.music_channel].data_ptr, -1, 100);
+
+#if defined(PLATFORM_WIN32)
+    // playing MIDI music is broken since Windows Vista, as it sets the volume
+    // for MIDI music also for all other sounds and music, which cannot be set
+    // back to normal unless playing MIDI music again with that desired volume
+    // (more details: https://www.artsoft.org/forum/viewtopic.php?f=7&t=2253)
+    // => workaround: always play MIDI music with maximum volume
+    if (Mix_GetMusicType(NULL) == MUS_MID)
+      Mix_VolumeMusic(SOUND_MAX_VOLUME);
+#endif
   }
 }
 
@@ -264,6 +274,16 @@ static void Mixer_FadeMusicChannel()
 
   Mix_FadeOutMusic(SOUND_FADING_INTERVAL);
 
+#if defined(PLATFORM_WIN32)
+  // playing MIDI music is broken since Windows Vista, as it sets the volume
+  // for MIDI music also for all other sounds and music, which cannot be set
+  // back to normal unless playing MIDI music again with that desired volume
+  // (more details: https://www.artsoft.org/forum/viewtopic.php?f=7&t=2253)
+  // => workaround: never fade MIDI music to lower volume, but just stop it
+  if (Mix_GetMusicType(NULL) == MUS_MID)
+    Mixer_StopMusicChannel();
+#endif
+
   setString(&currently_playing_music_filename, NULL);
 }
 
@@ -283,32 +303,11 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
 {
   SoundInfo *snd_info;
   int i, k;
-  int num_sounds = getSoundListSize();
-  int num_music  = getMusicListSize();
 
   if (IS_MUSIC(snd_ctrl))
-  {
-    if (snd_ctrl.nr >= num_music)      /* invalid music */
-      return;
-
-    if (snd_ctrl.nr < 0)               /* undefined music */
-    {
-      if (num_music_noconf == 0)       /* no fallback music available */
-       return;
-
-      snd_ctrl.nr = UNMAP_NOCONF_MUSIC(snd_ctrl.nr) % num_music_noconf;
-      snd_info = Music_NoConf[snd_ctrl.nr];
-    }
-    else
-      snd_info = getMusicInfoEntryFromMusicID(snd_ctrl.nr);
-  }
+    snd_info = getMusicInfoEntryFromMusicID(snd_ctrl.nr);
   else
-  {
-    if (snd_ctrl.nr < 0 || snd_ctrl.nr >= num_sounds)
-      return;
-
     snd_info = getSoundInfoEntryFromSoundID(snd_ctrl.nr);
-  }
 
   if (snd_info == NULL)
     return;
@@ -667,44 +666,80 @@ int getMusicListSize()
 
 struct FileInfo *getSoundListEntry(int pos)
 {
+  int num_sounds = getSoundListSize();
   int num_list_entries = sound_info->num_file_list_entries;
   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
 
+  if (pos < 0 || pos >= num_sounds)    /* invalid sound */
+    return NULL;
+
   return (pos < num_list_entries ? &sound_info->file_list[list_pos] :
          &sound_info->dynamic_file_list[list_pos]);
 }
 
 struct FileInfo *getMusicListEntry(int pos)
 {
+  int num_music = getMusicListSize();
   int num_list_entries = music_info->num_file_list_entries;
   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
 
+  if (pos < 0 || pos >= num_music)     /* invalid music */
+    return NULL;
+
   return (pos < num_list_entries ? &music_info->file_list[list_pos] :
          &music_info->dynamic_file_list[list_pos]);
 }
 
 static SoundInfo *getSoundInfoEntryFromSoundID(int pos)
 {
+  int num_sounds = getSoundListSize();
   int num_list_entries = sound_info->num_file_list_entries;
   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
   SoundInfo **snd_info =
     (SoundInfo **)(pos < num_list_entries ? sound_info->artwork_list :
                   sound_info->dynamic_artwork_list);
 
+  if (pos < 0 || pos >= num_sounds)    /* invalid sound */
+    return NULL;
+
   return snd_info[list_pos];
 }
 
 static MusicInfo *getMusicInfoEntryFromMusicID(int pos)
 {
+  int num_music = getMusicListSize();
   int num_list_entries = music_info->num_file_list_entries;
   int list_pos = (pos < num_list_entries ? pos : pos - num_list_entries);
   MusicInfo **mus_info =
     (MusicInfo **)(pos < num_list_entries ? music_info->artwork_list :
                   music_info->dynamic_artwork_list);
 
+  if (pos >= num_music)                        /* invalid music */
+    return NULL;
+
+  if (pos < 0)                         /* undefined music */
+  {
+    if (num_music_noconf == 0)         /* no fallback music available */
+      return NULL;
+
+    pos = UNMAP_NOCONF_MUSIC(pos) % num_music_noconf;
+
+    return Music_NoConf[pos];
+  }
+
   return mus_info[list_pos];
 }
 
+char *getMusicInfoEntryFilename(int pos)
+{
+  MusicInfo *mus_info = getMusicInfoEntryFromMusicID(pos);
+
+  if (mus_info == NULL)
+    return NULL;
+
+  return getBaseNamePtr(mus_info->source_filename);
+}
+
 char *getCurrentlyPlayingMusicFilename()
 {
   return currently_playing_music_filename;