added boundary checks for sound/music ID for some sound and music functions
[rocksndiamonds.git] / src / libgame / sound.c
index 4831248c86d2b9abc1ef56cf9ad0cd885ebc06c3..8a5ab84eb71b153d780973be1bed2fe3149cef6c 100644 (file)
@@ -116,6 +116,8 @@ static MusicInfo **Music_NoConf = NULL;
 static int num_music_noconf = 0;
 static int stereo_volume[SOUND_MAX_LEFT2RIGHT + 1];
 
+static char *currently_playing_music_filename = NULL;
+
 
 /* ========================================================================= */
 /* THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS            */
@@ -219,10 +221,20 @@ static void Mixer_PlayMusicChannel()
 
   if (mixer[audio.music_channel].type != MUS_TYPE_WAV)
   {
-    /* Mix_VolumeMusic() must be called _after_ Mix_PlayMusic() --
-       this looks like a bug in the SDL_mixer library */
-    Mix_PlayMusic(mixer[audio.music_channel].data_ptr, -1);
+    // use short fade-in to prevent "plop" sound for certain music files
+    // (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
   }
 }
 
@@ -242,6 +254,8 @@ static void Mixer_StopMusicChannel()
   Mixer_StopChannel(audio.music_channel);
 
   Mix_HaltMusic();
+
+  setString(&currently_playing_music_filename, NULL);
 }
 
 static void Mixer_FadeChannel(int channel)
@@ -259,6 +273,18 @@ static void Mixer_FadeMusicChannel()
   Mixer_FadeChannel(audio.music_channel);
 
   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);
 }
 
 static void Mixer_UnFadeChannel(int channel)
@@ -322,6 +348,9 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
     mixer[audio.music_channel] = snd_ctrl;
     Mixer_PlayMusicChannel();
 
+    setString(&currently_playing_music_filename,
+             getBaseNamePtr(snd_info->source_filename));
+
     return;
   }
 
@@ -658,44 +687,75 @@ 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 < 0 || pos >= num_music)     /* invalid music */
+    return NULL;
+
   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;
+}
+
 int getSoundListPropertyMappingSize()
 {
   return sound_info->num_property_mapping_entries;