updated contact info in source file headers
[rocksndiamonds.git] / src / libgame / sound.c
index a4b31c063adbbcabd80ca89f865649fda9926be4..f1c4aaf7fb5e259678d54ac3e4e345edf06e2f69 100644 (file)
@@ -1,15 +1,13 @@
-/***********************************************************
-* Artsoft Retro-Game Library                               *
-*----------------------------------------------------------*
-* (c) 1994-2002 Artsoft Entertainment                      *
-*               Holger Schemel                             *
-*               Detmolder Strasse 189                      *
-*               33604 Bielefeld                            *
-*               Germany                                    *
-*               e-mail: info@artsoft.org                   *
-*----------------------------------------------------------*
-* sound.c                                                  *
-***********************************************************/
+// ============================================================================
+// Artsoft Retro-Game Library
+// ----------------------------------------------------------------------------
+// (c) 1995-2014 by Artsoft Entertainment
+//                         Holger Schemel
+//                 info@artsoft.org
+//                 http://www.artsoft.org/
+// ----------------------------------------------------------------------------
+// sound.c
+// ============================================================================
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -19,6 +17,7 @@
 #include <dirent.h>
 #include <signal.h>
 #include <math.h>
+#include <errno.h>
 
 #include "platform.h"
 
 #define SAME_SOUND_NR(x,y)             ((x).nr == (y).nr)
 #define SAME_SOUND_DATA(x,y)           ((x).data_ptr == (y).data_ptr)
 
+#define SOUND_VOLUME_FROM_PERCENT(v,p) ((p) < 0   ? SOUND_MIN_VOLUME : \
+                                        (p) > 100 ? (v) :              \
+                                        (p) * (v) / 100)
+
+#define SOUND_VOLUME_SIMPLE(v) SOUND_VOLUME_FROM_PERCENT(v, setup.volume_simple)
+#define SOUND_VOLUME_LOOPS(v)  SOUND_VOLUME_FROM_PERCENT(v, setup.volume_loops)
+#define SOUND_VOLUME_MUSIC(v)  SOUND_VOLUME_FROM_PERCENT(v, setup.volume_music)
+
+#define SETUP_SOUND_VOLUME(v,s)                ((s) == SND_CTRL_PLAY_MUSIC ?   \
+                                        SOUND_VOLUME_MUSIC(v) :        \
+                                        (s) == SND_CTRL_PLAY_LOOP ?    \
+                                        SOUND_VOLUME_LOOPS(v) :        \
+                                        SOUND_VOLUME_SIMPLE(v))
+
+
 #if defined(AUDIO_UNIX_NATIVE)
 struct SoundHeader_WAV
 {
   unsigned short compression_code;
   unsigned short num_channels;
-  unsigned long  sample_rate;
-  unsigned long  bytes_per_second;
+  unsigned int   sample_rate;
+  unsigned int   bytes_per_second;
   unsigned short block_align;
   unsigned short bits_per_sample;
 };
@@ -98,7 +112,7 @@ struct SampleInfo
   int type;
   int format;
   void *data_ptr;              /* pointer to first sample (8 or 16 bit) */
-  long data_len;               /* number of samples, NOT number of bytes */
+  int data_len;                        /* number of samples, NOT number of bytes */
   int num_channels;            /* mono: 1 channel, stereo: 2 channels */
 };
 typedef struct SampleInfo SoundInfo;
@@ -114,18 +128,14 @@ struct SoundControl
 
   int state;
 
-  unsigned long playing_starttime;
-  unsigned long playing_pos;
+  unsigned int playing_starttime;
+  unsigned int playing_pos;
 
   int type;
   int format;
   void *data_ptr;              /* pointer to first sample (8 or 16 bit) */
-  long data_len;               /* number of samples, NOT number of bytes */
+  int data_len;                /* number of samples, NOT number of bytes */
   int num_channels;            /* mono: 1 channel, stereo: 2 channels */
-
-#if defined(TARGET_ALLEGRO)
-  int voice;
-#endif
 };
 typedef struct SoundControl SoundControl;
 
@@ -468,14 +478,14 @@ static void WriteReloadInfoToPipe(char *set_identifier, int type)
   SoundControl snd_ctrl;
   TreeInfo *ti = (type == SND_CTRL_RELOAD_SOUNDS ? artwork.snd_current :
                  artwork.mus_current);
-  unsigned long str_size1 = strlen(leveldir_current->fullpath) + 1;
-  unsigned long str_size2 = strlen(leveldir_current->sounds_path) + 1;
-  unsigned long str_size3 = strlen(leveldir_current->music_path) + 1;
-  unsigned long str_size4 = strlen(ti->basepath) + 1;
-  unsigned long str_size5 = strlen(ti->fullpath) + 1;
+  unsigned int str_size1 = strlen(leveldir_current->fullpath) + 1;
+  unsigned int str_size2 = strlen(leveldir_current->sounds_path) + 1;
+  unsigned int str_size3 = strlen(leveldir_current->music_path) + 1;
+  unsigned int str_size4 = strlen(ti->basepath) + 1;
+  unsigned int str_size5 = strlen(ti->fullpath) + 1;
   boolean override_level_artwork = (type == SND_CTRL_RELOAD_SOUNDS ?
-                                   setup.override_level_sounds :
-                                   setup.override_level_music);
+                                   gfx.override_level_sounds :
+                                   gfx.override_level_music);
 
   if (IS_CHILD_PROCESS())
     return;
@@ -483,7 +493,7 @@ static void WriteReloadInfoToPipe(char *set_identifier, int type)
   if (leveldir_current == NULL)                /* should never happen */
     Error(ERR_EXIT, "leveldir_current == NULL");
 
-  memset(&snd_ctrl, 0, sizeof(SoundControl));  /* to make valgrind happy */
+  clear_mem(&snd_ctrl, sizeof(SoundControl));  /* to make valgrind happy */
 
   snd_ctrl.active = FALSE;
   snd_ctrl.state = type;
@@ -500,15 +510,15 @@ static void WriteReloadInfoToPipe(char *set_identifier, int type)
       write(audio.mixer_pipe[1], ti,
            sizeof(TreeInfo)) < 0 ||
       write(audio.mixer_pipe[1], &str_size1,
-           sizeof(unsigned long)) < 0 ||
+           sizeof(unsigned int)) < 0 ||
       write(audio.mixer_pipe[1], &str_size2,
-           sizeof(unsigned long)) < 0 ||
+           sizeof(unsigned int)) < 0 ||
       write(audio.mixer_pipe[1], &str_size3,
-           sizeof(unsigned long)) < 0 ||
+           sizeof(unsigned int)) < 0 ||
       write(audio.mixer_pipe[1], &str_size4,
-           sizeof(unsigned long)) < 0 ||
+           sizeof(unsigned int)) < 0 ||
       write(audio.mixer_pipe[1], &str_size5,
-           sizeof(unsigned long)) < 0 ||
+           sizeof(unsigned int)) < 0 ||
       write(audio.mixer_pipe[1], leveldir_current->fullpath,
            str_size1) < 0 ||
       write(audio.mixer_pipe[1], leveldir_current->sounds_path,
@@ -531,11 +541,11 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl)
   TreeInfo **ti_ptr = ((snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS) ?
                       &artwork.snd_current : &artwork.mus_current);
   TreeInfo *ti = *ti_ptr;
-  unsigned long str_size1, str_size2, str_size3, str_size4, str_size5;
+  unsigned int str_size1, str_size2, str_size3, str_size4, str_size5;
   static char *set_identifier = NULL;
   boolean *override_level_artwork = (snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS ?
-                                    &setup.override_level_sounds :
-                                    &setup.override_level_music);
+                                    &gfx.override_level_sounds :
+                                    &gfx.override_level_music);
 
   checked_free(set_identifier);
 
@@ -562,15 +572,15 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl)
       read(audio.mixer_pipe[0], ti,
           sizeof(TreeInfo)) != sizeof(TreeInfo) ||
       read(audio.mixer_pipe[0], &str_size1,
-          sizeof(unsigned long)) != sizeof(unsigned long) ||
+          sizeof(unsigned int)) != sizeof(unsigned int) ||
       read(audio.mixer_pipe[0], &str_size2,
-          sizeof(unsigned long)) != sizeof(unsigned long) ||
+          sizeof(unsigned int)) != sizeof(unsigned int) ||
       read(audio.mixer_pipe[0], &str_size3,
-          sizeof(unsigned long)) != sizeof(unsigned long) ||
+          sizeof(unsigned int)) != sizeof(unsigned int) ||
       read(audio.mixer_pipe[0], &str_size4,
-          sizeof(unsigned long)) != sizeof(unsigned long) ||
+          sizeof(unsigned int)) != sizeof(unsigned int) ||
       read(audio.mixer_pipe[0], &str_size5,
-          sizeof(unsigned long)) != sizeof(unsigned long))
+          sizeof(unsigned int)) != sizeof(unsigned int))
     Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
 
   leveldir_current->fullpath = checked_calloc(str_size1);
@@ -634,32 +644,15 @@ static boolean Mixer_ChannelExpired(int channel)
     return TRUE;
 
 #if defined(TARGET_SDL)
-
   if (!Mix_Playing(channel))
     return TRUE;
-
-#elif defined(TARGET_ALLEGRO)
-
-  mixer[channel].playing_pos = voice_get_position(mixer[channel].voice);
-  mixer[channel].volume = voice_get_volume(mixer[channel].voice);
-
-  /* sound sample has completed playing or was completely faded out */
-  if (mixer[channel].playing_pos == -1 || mixer[channel].volume == 0)
-    return TRUE;
-
-#endif /* TARGET_ALLEGRO */
+#endif
 
   return FALSE;
 }
 
 static boolean Mixer_AllocateChannel(int channel)
 {
-#if defined(TARGET_ALLEGRO)
-  mixer[channel].voice = allocate_voice((SAMPLE *)mixer[channel].data_ptr);
-  if (mixer[channel].voice < 0)
-    return FALSE;
-#endif
-
   return TRUE;
 }
 
@@ -670,9 +663,6 @@ static void Mixer_SetChannelProperties(int channel)
   Mix_SetPanning(channel,
                 SOUND_VOLUME_LEFT(mixer[channel].stereo_position),
                 SOUND_VOLUME_RIGHT(mixer[channel].stereo_position));
-#elif defined(TARGET_ALLEGRO)
-  voice_set_volume(mixer[channel].voice, mixer[channel].volume);
-  voice_set_pan(mixer[channel].voice, mixer[channel].stereo_position);
 #endif
 }
 
@@ -681,11 +671,6 @@ static void Mixer_StartChannel(int channel)
 #if defined(TARGET_SDL)
   Mix_PlayChannel(channel, mixer[channel].data_ptr,
                  IS_LOOP(mixer[channel]) ? -1 : 0);
-#elif defined(TARGET_ALLEGRO)
-  if (IS_LOOP(mixer[channel]))
-    voice_set_playmode(mixer[channel].voice, PLAYMODE_LOOP);
-
-  voice_start(mixer[channel].voice);       
 #endif
 }
 
@@ -720,7 +705,7 @@ static void Mixer_PlayMusicChannel()
     /* 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);
-    Mix_VolumeMusic(SOUND_MAX_VOLUME);
+    Mix_VolumeMusic(mixer[audio.music_channel].volume);
   }
 #endif
 }
@@ -732,9 +717,6 @@ static void Mixer_StopChannel(int channel)
 
 #if defined(TARGET_SDL)
   Mix_HaltChannel(channel);
-#elif defined(TARGET_ALLEGRO)
-  voice_set_volume(mixer[channel].voice, 0);
-  deallocate_voice(mixer[channel].voice);
 #endif
 
   mixer[channel].active = FALSE;
@@ -759,9 +741,6 @@ static void Mixer_FadeChannel(int channel)
 
 #if defined(TARGET_SDL)
   Mix_FadeOutChannel(channel, SOUND_FADING_INTERVAL);
-#elif defined(TARGET_ALLEGRO)
-  if (voice_check(mixer[channel].voice))
-    voice_ramp_volume(mixer[channel].voice, SOUND_FADING_INTERVAL, 0);
 #endif
 }
 
@@ -785,10 +764,6 @@ static void Mixer_UnFadeChannel(int channel)
 #if defined(TARGET_SDL)
   Mix_ExpireChannel(channel, -1);
   Mix_Volume(channel, mixer[channel].volume);
-#elif defined(TARGET_ALLEGRO)
-  voice_stop_volumeramp(mixer[channel].voice);
-  voice_ramp_volume(mixer[channel].voice, SOUND_FADING_INTERVAL,
-                   mixer[channel].volume);
 #endif
 }
 
@@ -874,7 +849,7 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
   /* don't play sound more than n times simultaneously (with n == 2 for now) */
   if (k >= 2)
   {
-    unsigned long playing_current = Counter();
+    unsigned int playing_current = Counter();
     int longest = 0, longest_nr = audio.first_sound_channel;
 
     /* look for oldest equal sound */
@@ -913,8 +888,8 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
     {
       if (!mixer[i].active)
       {
-       Error(ERR_RETURN, "Mixer_InsertSound: Channel %d inactive", i);
-       Error(ERR_RETURN, "Mixer_InsertSound: This should never happen!");
+       Error(ERR_INFO, "Mixer_InsertSound: Channel %d inactive", i);
+       Error(ERR_INFO, "Mixer_InsertSound: This should never happen!");
 
        mixer_active_channels--;
       }
@@ -925,7 +900,7 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
   if (mixer_active_channels ==
       audio.num_channels - (mixer[audio.music_channel].active ? 0 : 1))
   {
-    unsigned long playing_current = Counter();
+    unsigned int playing_current = Counter();
     int longest = 0, longest_nr = audio.first_sound_channel;
 
 #if 0
@@ -933,8 +908,8 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
     /* print some debugging information about audio channel usage */
     for (i = audio.first_sound_channel; i < audio.num_channels; i++)
     {
-      Error(ERR_RETURN, "Mixer_InsertSound: %d [%d]: %ld (%ld)",
-           i, mixer[i].active, mixer[i].data_len, (long)mixer[i].data_ptr);
+      Error(ERR_INFO, "Mixer_InsertSound: %d [%d]: %d (%d)",
+           i, mixer[i].active, mixer[i].data_len, (int)mixer[i].data_ptr);
     }
 #endif
 #endif
@@ -1120,7 +1095,7 @@ static void CopySampleToMixingBuffer(SoundControl *snd_ctrl,
 static void Mixer_Main_DSP()
 {
   static short premix_first_buffer[DEFAULT_AUDIO_FRAGMENT_SIZE];
-  static long premix_last_buffer[DEFAULT_AUDIO_FRAGMENT_SIZE];
+  static int premix_last_buffer[DEFAULT_AUDIO_FRAGMENT_SIZE];
   static byte playing_buffer[DEFAULT_AUDIO_FRAGMENT_SIZE];
   boolean stereo;
   int fragment_size;
@@ -1147,12 +1122,12 @@ static void Mixer_Main_DSP()
   max_sample_size = fragment_size / (num_output_channels * sample_bytes);
 
   /* first clear the last premixing buffer */
-  memset(premix_last_buffer, 0,
-        max_sample_size * num_output_channels * sizeof(long));
+  clear_mem(premix_last_buffer,
+           max_sample_size * num_output_channels * sizeof(int));
 
   for (i = 0; i < audio.num_channels; i++)
   {
-    void *sample_ptr;
+    // void *sample_ptr;
     int sample_len;
     int sample_pos;
     int sample_size;
@@ -1167,7 +1142,7 @@ static void Mixer_Main_DSP()
     }
 
     /* pointer, lenght and actual playing position of sound sample */
-    sample_ptr = mixer[i].data_ptr;
+    // sample_ptr = mixer[i].data_ptr;
     sample_len = mixer[i].data_len;
     sample_pos = mixer[i].playing_pos;
     sample_size = MIN(max_sample_size, sample_len - sample_pos);
@@ -1203,7 +1178,7 @@ static void Mixer_Main_DSP()
     if (mixer[i].volume != SOUND_MAX_VOLUME)
       for (j = 0; j < sample_size * num_output_channels; j++)
        premix_first_buffer[j] =
-         mixer[i].volume * (long)premix_first_buffer[j] / SOUND_MAX_VOLUME;
+         mixer[i].volume * (int)premix_first_buffer[j] / SOUND_MAX_VOLUME;
 
     /* adjust left and right channel volume due to stereo sound position */
     if (stereo)
@@ -1265,7 +1240,8 @@ static void Mixer_Main_DSP()
   }
 
   /* finally play the sound fragment */
-  write(audio.device_fd, playing_buffer, fragment_size);
+  if (write(audio.device_fd, playing_buffer, fragment_size) == -1)
+    Error(ERR_WARN, "write() failed; %s", strerror(errno));
 
   if (!mixer_active_channels)
     CloseAudioDevice(&audio.device_fd);
@@ -1302,7 +1278,7 @@ static int Mixer_Main_SimpleAudio(SoundControl snd_ctrl)
   if (mixer[i].volume != SOUND_MAX_VOLUME)
     for (j = 0; j < sample_size; j++)
       premix_first_buffer[j] =
-       mixer[i].volume * (long)premix_first_buffer[j] / SOUND_MAX_VOLUME;
+       mixer[i].volume * (int)premix_first_buffer[j] / SOUND_MAX_VOLUME;
 
   /* might be needed for u-law /dev/audio */
   for (j = 0; j < sample_size; j++)
@@ -1546,19 +1522,10 @@ static void *Load_WAV(char *filename)
 
   snd_info->data_len = ((Mix_Chunk *)snd_info->data_ptr)->alen;
 
-#elif defined(TARGET_ALLEGRO)
-
-  if ((snd_info->data_ptr = load_sample(filename)) == NULL)
-  {
-    Error(ERR_WARN, "cannot read sound file '%s'", filename);
-    free(snd_info);
-    return NULL;
-  }
-
-  snd_info->data_len = ((SAMPLE *)snd_info->data_ptr)->len;
-
 #else /* AUDIO_UNIX_NATIVE */
 
+  clear_mem(&header, sizeof(struct SoundHeader_WAV));  /* to make gcc happy */
+
   if ((file = fopen(filename, MODE_READ)) == NULL)
   {
     Error(ERR_WARN, "cannot open sound file '%s'", filename);
@@ -1568,7 +1535,7 @@ static void *Load_WAV(char *filename)
 
   /* read chunk id "RIFF" */
   getFileChunkLE(file, chunk_name, &chunk_size);
-  if (strcmp(chunk_name, "RIFF") != 0)
+  if (!strEqual(chunk_name, "RIFF"))
   {
     Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename);
     fclose(file);
@@ -1578,7 +1545,7 @@ static void *Load_WAV(char *filename)
 
   /* read "RIFF" type id "WAVE" */
   getFileChunkLE(file, chunk_name, NULL);
-  if (strcmp(chunk_name, "WAVE") != 0)
+  if (!strEqual(chunk_name, "WAVE"))
   {
     Error(ERR_WARN, "missing 'WAVE' type ID of sound file '%s'", filename);
     fclose(file);
@@ -1588,7 +1555,7 @@ static void *Load_WAV(char *filename)
 
   while (getFileChunkLE(file, chunk_name, &chunk_size))
   {
-    if (strcmp(chunk_name, "fmt ") == 0)
+    if (strEqual(chunk_name, "fmt "))
     {
       if (chunk_size < WAV_HEADER_SIZE)
       {
@@ -1646,13 +1613,13 @@ static void *Load_WAV(char *filename)
       printf("WAV file: '%s'\n", filename);
       printf("  Compression code: %d'\n", header.compression_code);
       printf("  Number of channels: %d'\n", header.num_channels);
-      printf("  Sample rate: %ld'\n", header.sample_rate);
-      printf("  Average bytes per second: %ld'\n", header.bytes_per_second);
+      printf("  Sample rate: %d'\n", header.sample_rate);
+      printf("  Average bytes per second: %d'\n", header.bytes_per_second);
       printf("  Block align: %d'\n", header.block_align);
       printf("  Significant bits per sample: %d'\n", header.bits_per_sample);
 #endif
     }
-    else if (strcmp(chunk_name, "data") == 0)
+    else if (strEqual(chunk_name, "data"))
     {
       data_byte_len = chunk_size;
 
@@ -1754,14 +1721,108 @@ static void *Load_MOD(char *filename)
 
 static void *Load_WAV_or_MOD(char *filename)
 {
+#if 1
+  if (FileIsMusic(filename))
+    return Load_MOD(filename);
+  else if (FileIsSound(filename))
+    return Load_WAV(filename);
+  else
+    return NULL;
+#else
   if (FileIsSound(filename))
     return Load_WAV(filename);
   else if (FileIsMusic(filename))
     return Load_MOD(filename);
   else
     return NULL;
+#endif
+}
+
+#if 1
+
+void LoadCustomMusic_NoConf(void)
+{
+  static boolean draw_init_text = TRUE;                /* only draw at startup */
+  static char *last_music_directory = NULL;
+  char *music_directory = getCustomMusicDirectory();
+  Directory *dir;
+  DirectoryEntry *dir_entry;
+  int num_music = getMusicListSize();
+
+  if (!audio.sound_available)
+    return;
+
+  if (last_music_directory != NULL &&
+      strEqual(last_music_directory, music_directory))
+    return;    /* old and new music directory are the same */
+
+  if (last_music_directory != NULL)
+    free(last_music_directory);
+  last_music_directory = getStringCopy(music_directory);
+
+  FreeAllMusic_NoConf();
+
+  if ((dir = openDirectory(music_directory)) == NULL)
+  {
+    Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
+
+    audio.music_available = FALSE;
+
+    return;
+  }
+
+  if (draw_init_text)
+    DrawInitText("Loading music", 120, FC_GREEN);
+
+  while ((dir_entry = readDirectory(dir)) != NULL)     /* loop all entries */
+  {
+    char *basename = dir_entry->basename;
+    char *filename = NULL;
+    MusicInfo *mus_info = NULL;
+    boolean music_already_used = FALSE;
+    int i;
+
+    /* skip all music files that are configured in music config file */
+    for (i = 0; i < num_music; i++)
+    {
+      struct FileInfo *music = getMusicListEntry(i);
+
+      if (strEqual(basename, music->filename))
+      {
+       music_already_used = TRUE;
+       break;
+      }
+    }
+
+    if (music_already_used)
+      continue;
+
+    if (draw_init_text)
+      DrawInitText(basename, 150, FC_YELLOW);
+
+    filename = getPath2(music_directory, basename);
+
+    if (FileIsMusic(basename))
+      mus_info = Load_WAV_or_MOD(filename);
+
+    free(filename);
+
+    if (mus_info)
+    {
+      num_music_noconf++;
+      Music_NoConf = checked_realloc(Music_NoConf,
+                                    num_music_noconf * sizeof(MusicInfo *));
+      Music_NoConf[num_music_noconf - 1] = mus_info;
+    }
+  }
+
+  closeDirectory(dir);
+
+  draw_init_text = FALSE;
 }
 
+#else
+
 void LoadCustomMusic_NoConf(void)
 {
   static boolean draw_init_text = TRUE;                /* only draw at startup */
@@ -1775,7 +1836,7 @@ void LoadCustomMusic_NoConf(void)
     return;
 
   if (last_music_directory != NULL &&
-      strcmp(last_music_directory, music_directory) == 0)
+      strEqual(last_music_directory, music_directory))
     return;    /* old and new music directory are the same */
 
   if (last_music_directory != NULL)
@@ -1787,12 +1848,14 @@ void LoadCustomMusic_NoConf(void)
   if ((dir = opendir(music_directory)) == NULL)
   {
     Error(ERR_WARN, "cannot read music directory '%s'", music_directory);
+
     audio.music_available = FALSE;
+
     return;
   }
 
   if (draw_init_text)
-    DrawInitText("Loading music:", 120, FC_GREEN);
+    DrawInitText("Loading music", 120, FC_GREEN);
 
   while ((dir_entry = readdir(dir)) != NULL)   /* loop until last dir entry */
   {
@@ -1807,7 +1870,7 @@ void LoadCustomMusic_NoConf(void)
     {
       struct FileInfo *music = getMusicListEntry(i);
 
-      if (strcmp(basename, music->filename) == 0)
+      if (strEqual(basename, music->filename))
       {
        music_already_used = TRUE;
        break;
@@ -1841,6 +1904,8 @@ void LoadCustomMusic_NoConf(void)
   draw_init_text = FALSE;
 }
 
+#endif
+
 int getSoundListSize()
 {
   return (sound_info->num_file_list_entries +
@@ -2073,21 +2138,33 @@ void PlayMusic(int nr)
 
 void PlaySound(int nr)
 {
+  if (!setup.sound_simple)
+    return;
+
   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_SOUND);
 }
 
 void PlaySoundStereo(int nr, int stereo_position)
 {
+  if (!setup.sound_simple)
+    return;
+
   PlaySoundExt(nr, SOUND_MAX_VOLUME, stereo_position, SND_CTRL_PLAY_SOUND);
 }
 
 void PlaySoundLoop(int nr)
 {
+  if (!setup.sound_loops)
+    return;
+
   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_LOOP);
 }
 
 void PlaySoundMusic(int nr)
 {
+  if (!setup.sound_music)
+    return;
+
   PlaySoundExt(nr, SOUND_MAX_VOLUME, SOUND_MIDDLE, SND_CTRL_PLAY_MUSIC);
 }
 
@@ -2100,6 +2177,8 @@ void PlaySoundExt(int nr, int volume, int stereo_position, int state)
       audio.sound_deactivated)
     return;
 
+  volume = SETUP_SOUND_VOLUME(volume, state);
+
   if (volume < SOUND_MIN_VOLUME)
     volume = SOUND_MIN_VOLUME;
   else if (volume > SOUND_MAX_VOLUME)
@@ -2110,7 +2189,7 @@ void PlaySoundExt(int nr, int volume, int stereo_position, int state)
   else if (stereo_position > SOUND_MAX_RIGHT)
     stereo_position = SOUND_MAX_RIGHT;
 
-  memset(&snd_ctrl, 0, sizeof(SoundControl));  /* to make valgrind happy */
+  clear_mem(&snd_ctrl, sizeof(SoundControl));  /* to make valgrind happy */
 
   snd_ctrl.active = TRUE;
   snd_ctrl.nr = nr;
@@ -2171,7 +2250,7 @@ void StopSoundExt(int nr, int state)
   if (!audio.sound_available)
     return;
 
-  memset(&snd_ctrl, 0, sizeof(SoundControl));  /* to make valgrind happy */
+  clear_mem(&snd_ctrl, sizeof(SoundControl));  /* to make valgrind happy */
 
   snd_ctrl.active = FALSE;
   snd_ctrl.nr = nr;
@@ -2240,8 +2319,6 @@ void FreeSound(void *ptr)
   {
 #if defined(TARGET_SDL)
     Mix_FreeChunk(sound->data_ptr);
-#elif defined(TARGET_ALLEGRO)
-    destroy_sample(sound->data_ptr);
 #else /* AUDIO_UNIX_NATIVE */
     free(sound->data_ptr);
 #endif
@@ -2266,8 +2343,6 @@ void FreeMusic(void *ptr)
       Mix_FreeMusic(music->data_ptr);
     else
       Mix_FreeChunk(music->data_ptr);
-#elif defined(TARGET_ALLEGRO)
-    destroy_sample(music->data_ptr);
 #else /* AUDIO_UNIX_NATIVE */
     free(music->data_ptr);
 #endif