rnd-20020824-1-src
[rocksndiamonds.git] / src / libgame / sound.c
index cd34b284473e3126a0191fc3074f7005793fd0e0..e98c42a2d2056b2977958837e1acf1cb957dcc2a 100644 (file)
@@ -66,6 +66,7 @@
 #define MUS_TYPE_MOD                   2
 
 #define DEVICENAME_DSP                 "/dev/dsp"
+#define DEVICENAME_SOUND_DSP           "/dev/sound/dsp"
 #define DEVICENAME_AUDIO               "/dev/audio"
 #define DEVICENAME_AUDIOCTL            "/dev/audioCtl"
 
@@ -234,6 +235,7 @@ static boolean TestAudioDevices(void)
   static char *audio_device_name[] =
   {
     DEVICENAME_DSP,
+    DEVICENAME_SOUND_DSP,
     DEVICENAME_AUDIO
   };
   int audio_device_fd = -1;
@@ -350,7 +352,7 @@ static void InitAudioDevice_Linux(struct AudioFormatInfo *afmt)
 
   if (ioctl(audio.device_fd, SNDCTL_DSP_SETFRAGMENT, &fragment_spec) < 0)
     Error(ERR_EXIT_SOUND_SERVER,
-         "cannot set fragment size of /dev/dsp -- no sounds");
+         "cannot set fragment size of audio device -- no sounds");
 
   i = 0;
   afmt->format = 0;
@@ -366,7 +368,7 @@ static void InitAudioDevice_Linux(struct AudioFormatInfo *afmt)
 
   if (afmt->format == 0)       /* no supported audio format found */
     Error(ERR_EXIT_SOUND_SERVER,
-         "cannot set audio format of /dev/dsp -- no sounds");
+         "cannot set audio format of audio device -- no sounds");
 
   /* try if we can use stereo sound */
   afmt->stereo = TRUE;
@@ -375,15 +377,15 @@ static void InitAudioDevice_Linux(struct AudioFormatInfo *afmt)
 
   if (ioctl(audio.device_fd, SNDCTL_DSP_SPEED, &afmt->sample_rate) < 0)
     Error(ERR_EXIT_SOUND_SERVER,
-         "cannot set sample rate of /dev/dsp -- no sounds");
+         "cannot set sample rate of audio device -- no sounds");
 
   /* get the real fragmentation size; this should return 512 */
   if (ioctl(audio.device_fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size_query) < 0)
     Error(ERR_EXIT_SOUND_SERVER,
-         "cannot get fragment size of /dev/dsp -- no sounds");
+         "cannot get fragment size of audio device -- no sounds");
   if (fragment_size_query != afmt->fragment_size)
     Error(ERR_EXIT_SOUND_SERVER,
-         "cannot set fragment size of /dev/dsp -- no sounds");
+         "cannot set fragment size of audio device -- no sounds");
 }
 #endif /* AUDIO_LINUX_IOCTL */
 
@@ -397,8 +399,8 @@ static void InitAudioDevice_NetBSD(struct AudioFormatInfo *afmt)
   a_info.play.encoding = AUDIO_ENCODING_LINEAR8;
   a_info.play.precision = 8;
   a_info.play.channels = 2;
-  a_info.play.sample_rate = sample_rate;
-  a_info.blocksize = fragment_size;
+  a_info.play.sample_rate = afmt->sample_rate;
+  a_info.blocksize = afmt->fragment_size;
 
   afmt->format = AUDIO_FORMAT_U8;
   afmt->stereo = TRUE;
@@ -412,7 +414,7 @@ static void InitAudioDevice_NetBSD(struct AudioFormatInfo *afmt)
 
     if (ioctl(audio.device_fd, AUDIO_SETINFO, &a_info) < 0)
       Error(ERR_EXIT_SOUND_SERVER,
-           "cannot set sample rate of /dev/audio -- no sounds");
+           "cannot set sample rate of audio device -- no sounds");
   }
 }
 #endif /* PLATFORM_NETBSD */
@@ -425,7 +427,7 @@ static void InitAudioDevice_HPUX(struct AudioFormatInfo *afmt)
 
   audio_ctl = open("/dev/audioCtl", O_WRONLY | O_NDELAY);
   if (audio_ctl == -1)
-    Error(ERR_EXIT_SOUND_SERVER, "cannot open /dev/audioCtl -- no sounds");
+    Error(ERR_EXIT_SOUND_SERVER, "cannot open audio device -- no sounds");
 
   if (ioctl(audio_ctl, AUDIO_DESCRIBE, &ainfo) == -1)
     Error(ERR_EXIT_SOUND_SERVER, "no audio info -- no sounds");
@@ -490,7 +492,7 @@ static void ReadSoundControlFromMainProcess(SoundControl *snd_ctrl)
     Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
 }
 
-static void WriteReloadInfoToPipe(char *set_name, int type)
+static void WriteReloadInfoToPipe(char *set_identifier, int type)
 {
   SoundControl snd_ctrl;
   TreeInfo *ti = (type == SND_CTRL_RELOAD_SOUNDS ? artwork.snd_current :
@@ -510,11 +512,11 @@ static void WriteReloadInfoToPipe(char *set_name, int type)
 
   snd_ctrl.active = FALSE;
   snd_ctrl.state = type;
-  snd_ctrl.data_len = strlen(set_name) + 1;
+  snd_ctrl.data_len = strlen(set_identifier) + 1;
 
   if (write(audio.mixer_pipe[1], &snd_ctrl,
            sizeof(snd_ctrl)) < 0 ||
-      write(audio.mixer_pipe[1], set_name,
+      write(audio.mixer_pipe[1], set_identifier,
            snd_ctrl.data_len) < 0 ||
       write(audio.mixer_pipe[1], &override_level_artwork,
            sizeof(boolean)) < 0 ||
@@ -547,15 +549,15 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl)
                       &artwork.snd_current : &artwork.mus_current);
   TreeInfo *ti = *ti_ptr;
   unsigned long str_size1, str_size2, str_size3;
-  static char *set_name = NULL;
+  static char *set_identifier = NULL;
   boolean *override_level_artwork = (snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS ?
                                     &setup.override_level_sounds :
                                     &setup.override_level_music);
 
-  if (set_name)
-    free(set_name);
+  if (set_identifier)
+    free(set_identifier);
 
-  set_name = checked_malloc(snd_ctrl->data_len);
+  set_identifier = checked_malloc(snd_ctrl->data_len);
 
   if (leveldir_current == NULL)
     leveldir_current = checked_calloc(sizeof(TreeInfo));
@@ -568,7 +570,7 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl)
   if (ti->fullpath != NULL)
     free(ti->fullpath);
 
-  if (read(audio.mixer_pipe[0], set_name,
+  if (read(audio.mixer_pipe[0], set_identifier,
           snd_ctrl->data_len) != snd_ctrl->data_len ||
       read(audio.mixer_pipe[0], override_level_artwork,
           sizeof(boolean)) != sizeof(boolean) ||
@@ -597,9 +599,9 @@ static void ReadReloadInfoFromPipe(SoundControl *snd_ctrl)
     Error(ERR_EXIT_SOUND_SERVER, "broken pipe -- no sounds");
 
   if (snd_ctrl->state & SND_CTRL_RELOAD_SOUNDS)
-    artwork.sounds_set_current_name = set_name;
+    artwork.snd_current_identifier = set_identifier;
   else
-    artwork.music_set_current_name = set_name;
+    artwork.mus_current_identifier = set_identifier;
 }
 
 #endif /* AUDIO_UNIX_NATIVE */
@@ -803,11 +805,8 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
   int i, k;
 
 #if 0
-  printf("NEW SOUND %d HAS ARRIVED [%d]\n", snd_ctrl.nr, num_sounds);
-#endif
-
-#if 0
-  printf("%d ACTIVE CHANNELS\n", mixer_active_channels);
+  printf("NEW SOUND %d ARRIVED [%d] [%d ACTIVE CHANNELS]\n",
+        snd_ctrl.nr, num_sounds, mixer_active_channels);
 #endif
 
   if (IS_MUSIC(snd_ctrl))
@@ -833,10 +832,7 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
   /* play music samples on a dedicated music channel */
   if (IS_MUSIC(snd_ctrl))
   {
-#if 0
-    printf("PLAY MUSIC WITH VOLUME/STEREO %d/%d\n",
-          snd_ctrl.volume, snd_ctrl.stereo_position);
-#endif
+    Mixer_StopMusicChannel();
 
     mixer[audio.music_channel] = snd_ctrl;
     Mixer_PlayMusicChannel();
@@ -921,12 +917,36 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
      of the channel's sound sample when compiling with the SDL mixer
      library, we use the current playing time (in milliseconds) instead. */
 
+#if DEBUG
+  /* channel allocation sanity check -- should not be needed */
+  if (mixer_active_channels ==
+      audio.num_channels - (mixer[audio.music_channel].active ? 0 : 1))
+  {
+    for (i=audio.first_sound_channel; i<audio.num_channels; i++)
+    {
+      if (!mixer[i].active)
+      {
+       Error(ERR_RETURN, "Mixer_InsertSound: Channel %d inactive", i);
+       Error(ERR_RETURN, "Mixer_InsertSound: This should never happen!");
+
+       mixer_active_channels--;
+      }
+    }
+  }
+#endif
+
   if (mixer_active_channels ==
       audio.num_channels - (mixer[audio.music_channel].active ? 0 : 1))
   {
     unsigned long playing_current = Counter();
     int longest = 0, longest_nr = audio.first_sound_channel;
 
+    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);
+    }
+
     for (i=audio.first_sound_channel; i<audio.num_channels; i++)
     {
       int playing_time = playing_current - mixer[i].playing_starttime;
@@ -943,42 +963,24 @@ static void Mixer_InsertSound(SoundControl snd_ctrl)
   }
 
   /* add the new sound to the mixer */
-  for(i=0; i<audio.num_channels; i++)
+  for(i=audio.first_sound_channel; i<audio.num_channels; i++)
   {
 #if 0
     printf("CHECKING CHANNEL %d FOR SOUND %d ...\n", i, snd_ctrl.nr);
 #endif
 
-    /*
-    if (!mixer[i].active ||
-       (IS_MUSIC(snd_ctrl) && i == audio.music_channel))
-    */
-    if ((i == audio.music_channel && IS_MUSIC(snd_ctrl)) ||
-       (i != audio.music_channel && !mixer[i].active))
+    if (!mixer[i].active)
     {
 #if 0
       printf("ADDING NEW SOUND %d TO MIXER\n", snd_ctrl.nr);
 #endif
 
-#if 1
 #if defined(AUDIO_UNIX_NATIVE)
       if (snd_info->data_len == 0)
       {
        printf("THIS SHOULD NEVER HAPPEN! [snd_info->data_len == 0]\n");
       }
 #endif
-#endif
-
-#if 1
-      if (IS_MUSIC(snd_ctrl) && i == audio.music_channel && mixer[i].active)
-      {
-       printf("THIS SHOULD NEVER HAPPEN! [adding music twice]\n");
-
-#if 1
-       Mixer_StopChannel(i);
-#endif
-      }
-#endif
 
       mixer[i] = snd_ctrl;
       Mixer_PlayChannel(i);
@@ -2181,25 +2183,25 @@ static void ReloadCustomMusic()
   LoadCustomMusic();
 }
 
-void InitReloadSounds(char *set_name)
+void InitReloadSounds(char *set_identifier)
 {
   if (!audio.sound_available)
     return;
 
 #if defined(AUDIO_UNIX_NATIVE)
-  WriteReloadInfoToPipe(set_name, SND_CTRL_RELOAD_SOUNDS);
+  WriteReloadInfoToPipe(set_identifier, SND_CTRL_RELOAD_SOUNDS);
 #else
   ReloadCustomSounds();
 #endif
 }
 
-void InitReloadMusic(char *set_name)
+void InitReloadMusic(char *set_identifier)
 {
   if (!audio.music_available)
     return;
 
 #if defined(AUDIO_UNIX_NATIVE)
-  WriteReloadInfoToPipe(set_name, SND_CTRL_RELOAD_MUSIC);
+  WriteReloadInfoToPipe(set_identifier, SND_CTRL_RELOAD_MUSIC);
 #else
   ReloadCustomMusic();
 #endif