rnd-20020510-3-src
authorHolger Schemel <info@artsoft.org>
Fri, 10 May 2002 13:20:03 +0000 (15:20 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 30 Aug 2014 08:37:28 +0000 (10:37 +0200)
src/libgame/sound.c
src/libgame/sound.h

index 20991570de7e00005ccdf1f900790e70f4429145..c876945f8ea45374b15367616433530435bf27d4 100644 (file)
@@ -49,24 +49,9 @@ static int num_sounds = 0, num_music = 0;
 /* ========================================================================= */
 /* THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS            */
 
 /* ========================================================================= */
 /* THE STUFF BELOW IS ONLY USED BY THE SOUND SERVER CHILD PROCESS            */
 
-static struct AudioFormatInfo afmt =
-{
-  TRUE, 0, DEFAULT_AUDIO_SAMPLE_RATE, DEFAULT_AUDIO_FRAGMENT_SIZE
-};
-
-static int playing_sounds = 0;
+static struct AudioFormatInfo afmt;
 static struct SoundControl playlist[NUM_MIXER_CHANNELS];
 static struct SoundControl playlist[NUM_MIXER_CHANNELS];
-
-#if 0
-static struct SoundControl emptySoundControl =
-{
-#if 1
-  FALSE, -1, 0, 0, FALSE, 0, 0, 0, NULL
-#else
-  -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, 0,0, 0,NULL
-#endif
-};
-#endif
+static int playing_sounds = 0;
 
 /* forward declaration of internal functions */
 static void SoundServer_InsertNewSound(struct SoundControl);
 
 /* forward declaration of internal functions */
 static void SoundServer_InsertNewSound(struct SoundControl);
@@ -311,6 +296,10 @@ static void PlaylistRemoveSound(int pos)
   if (!playing_sounds || !playlist[pos].active)
     return;
 
   if (!playing_sounds || !playlist[pos].active)
     return;
 
+#if 0
+  printf("REMOVING PLAYLIST SOUND %d\n", pos);
+#endif
+
   playlist[pos].active = FALSE;
   playing_sounds--;
 }
   playlist[pos].active = FALSE;
   playing_sounds--;
 }
@@ -360,7 +349,7 @@ static void PlaylistInsertSound(struct SoundControl snd_ctrl)
   /* if playlist is full, remove oldest sound */
   if (playing_sounds == audio.num_channels)
   {
   /* if playlist is full, remove oldest sound */
   if (playing_sounds == audio.num_channels)
   {
-    int longest = 0, longest_nr = 0;
+    int longest = 0, longest_nr = audio.first_sound_channel;
 
     for (i=audio.first_sound_channel; i<audio.num_channels; i++)
     {
 
     for (i=audio.first_sound_channel; i<audio.num_channels; i++)
     {
@@ -411,7 +400,7 @@ static void PlaylistInsertSound(struct SoundControl snd_ctrl)
   /* don't play sound more than n times simultaneously (with n == 2 for now) */
   if (k >= 2)
   {
   /* don't play sound more than n times simultaneously (with n == 2 for now) */
   if (k >= 2)
   {
-    int longest = 0, longest_nr = 0;
+    int longest = 0, longest_nr = audio.first_sound_channel;
 
     /* look for oldest equal sound */
     for(i=audio.first_sound_channel; i<audio.num_channels; i++)
 
     /* look for oldest equal sound */
     for(i=audio.first_sound_channel; i<audio.num_channels; i++)
@@ -458,6 +447,9 @@ static void PlaylistInsertSound(struct SoundControl snd_ctrl)
       snd_ctrl.data_len = snd_info->data_len;
       snd_ctrl.format   = snd_info->format;
 
       snd_ctrl.data_len = snd_info->data_len;
       snd_ctrl.format   = snd_info->format;
 
+      snd_ctrl.playingpos = 0;
+      snd_ctrl.playingtime = 0;
+
 #if 1
       if (snd_info->data_len == 0)
       {
 #if 1
       if (snd_info->data_len == 0)
       {
@@ -592,10 +584,10 @@ static void SoundServer_Mixer()
   static short premix_right_buffer[SND_BLOCKSIZE];
   static long premix_last_buffer[SND_BLOCKSIZE];
   static byte playing_buffer[SND_BLOCKSIZE];
   static short premix_right_buffer[SND_BLOCKSIZE];
   static long premix_last_buffer[SND_BLOCKSIZE];
   static byte playing_buffer[SND_BLOCKSIZE];
+  boolean stereo;
+  int fragment_size;
+  int sample_bytes;
   int max_sample_size;
   int max_sample_size;
-  int fragment_size = afmt.fragment_size;
-  int sample_bytes = (afmt.format & AUDIO_FORMAT_U8 ? 1 : 2);
-  boolean stereo = afmt.stereo;
   int i, j;
 
   if (!stereo_volume_calculated)
   int i, j;
 
   if (!stereo_volume_calculated)
@@ -618,6 +610,9 @@ static void SoundServer_Mixer()
     InitAudioDevice(&afmt);
   }
 
     InitAudioDevice(&afmt);
   }
 
+  stereo = afmt.stereo;
+  fragment_size = afmt.fragment_size;
+  sample_bytes = (afmt.format & AUDIO_FORMAT_U8 ? 1 : 2);
   max_sample_size = fragment_size / ((stereo ? 2 : 1) * sample_bytes);
 
   /* first clear the last premixing buffer */
   max_sample_size = fragment_size / ((stereo ? 2 : 1) * sample_bytes);
 
   /* first clear the last premixing buffer */
@@ -948,188 +943,7 @@ static void sound_handler_SDL(struct SoundControl snd_ctrl)
 #if !defined(PLATFORM_WIN32)
 static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
 {
 #if !defined(PLATFORM_WIN32)
 static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl)
 {
-  SoundInfo *snd_info;
-  int i, k;
-
-#if 0
-  printf("NEW SOUND %d HAS ARRIVED [%d]\n", snd_ctrl.nr, num_sounds);
-#endif
-
-  if (IS_MUSIC(snd_ctrl))
-    snd_ctrl.nr = snd_ctrl.nr % num_music;
-  else if (snd_ctrl.nr >= num_sounds)
-    return;
-
-  snd_info = (IS_MUSIC(snd_ctrl) ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]);
-  if (snd_info == NULL)
-  {
-#if 0
-    printf("sound/music %d undefined\n", snd_ctrl.nr);
-#endif
-    return;
-  }
-
-#if 0
-  printf("-> %d\n", playing_sounds);
-#endif
-
-  if (playing_sounds == audio.num_channels)
-  {
-    for (i=0; i<audio.num_channels; i++)
-    {
-      if (playlist[i].data_ptr == NULL)
-      {
-#if 1
-       printf("THIS SHOULD NEVER HAPPEN! [%d]\n", i);
-#endif
-
-       PlaylistRemoveSound(i);
-      }
-    }
-  }
-
-  /* if playlist is full, remove oldest sound */
-  if (playing_sounds == audio.num_channels)
-  {
-    int longest = 0, longest_nr = 0;
-
-    for (i=audio.first_sound_channel; i<audio.num_channels; i++)
-    {
-#if !defined(PLATFORM_MSDOS)
-      int actual = 100 * playlist[i].playingpos / playlist[i].data_len;
-#else
-      int actual = playlist[i].playingpos;
-#endif
-
-      if (!IS_LOOP(playlist[i]) && actual > longest)
-      {
-       longest = actual;
-       longest_nr = i;
-      }
-    }
-#if defined(PLATFORM_MSDOS)
-    voice_set_volume(playlist[longest_nr].voice, 0);
-    deallocate_voice(playlist[longest_nr].voice);
-#endif
-    PlaylistRemoveSound(longest_nr);
-  }
-
-  /* check if sound is already being played (and how often) */
-  for (k=0, i=audio.first_sound_channel; i<audio.num_channels; i++)
-    if (playlist[i].nr == snd_ctrl.nr)
-      k++;
-
-  /* restart loop sounds only if they are just fading out */
-  if (k >= 1 && IS_LOOP(snd_ctrl))
-  {
-    for(i=audio.first_sound_channel; i<audio.num_channels; i++)
-    {
-      if (playlist[i].nr == snd_ctrl.nr && IS_FADING(playlist[i]))
-      {
-       playlist[i].state &= ~SND_CTRL_FADE;
-       playlist[i].volume = PSND_MAX_VOLUME;
-#if defined(PLATFORM_MSDOS)
-        playlist[i].state |= SND_CTRL_LOOP;
-        voice_stop_volumeramp(playlist[i].voice);
-        voice_ramp_volume(playlist[i].voice, playlist[i].volume, 1000);
-#endif
-      }
-    }
-
-    return;
-  }
-
-  /* don't play sound more than n times simultaneously (with n == 2 for now) */
-  if (k >= 2)
-  {
-    int longest = 0, longest_nr = 0;
-
-    /* look for oldest equal sound */
-    for(i=audio.first_sound_channel; i<audio.num_channels; i++)
-    {
-      int actual;
-
-      if (!playlist[i].active || playlist[i].nr != snd_ctrl.nr)
-       continue;
-
-#if !defined(PLATFORM_MSDOS)
-      actual = 100 * playlist[i].playingpos / playlist[i].data_len;
-#else
-      actual = playlist[i].playingpos;
-#endif
-      if (actual >= longest)
-      {
-       longest = actual;
-       longest_nr = i;
-      }
-    }
-
-#if defined(PLATFORM_MSDOS)
-    voice_set_volume(playlist[longest_nr].voice, 0);
-    deallocate_voice(playlist[longest_nr].voice);
-#endif
-    PlaylistRemoveSound(longest_nr);
-  }
-
-  /* add new sound to playlist */
-  for(i=0; i<audio.num_channels; i++)
-  {
-#if 0
-    printf("CHECKING CHANNEL %d FOR SOUND %d ...\n", i, snd_ctrl.nr);
-#endif
-
-    /*
-    if (!playlist[i].active ||
-       (IS_MUSIC(snd_ctrl) && i == audio.music_channel))
-    */
-    if ((i == audio.music_channel && IS_MUSIC(snd_ctrl)) ||
-       (i != audio.music_channel && !playlist[i].active))
-    {
-      snd_ctrl.data_ptr = snd_info->data_ptr;
-      snd_ctrl.data_len = snd_info->data_len;
-      snd_ctrl.format   = snd_info->format;
-
-      snd_ctrl.playingpos = 0;
-      snd_ctrl.playingtime = 0;
-
-#if 1
-      if (snd_info->data_len == 0)
-      {
-       printf("THIS SHOULD NEVER HAPPEN! [snd_info->data_len == 0]\n");
-      }
-#endif
-
-#if 1
-      if (IS_MUSIC(snd_ctrl) && i == audio.music_channel && playlist[i].active)
-      {
-       printf("THIS SHOULD NEVER HAPPEN! [adding music twice]\n");
-
-#if 1
-       PlaylistRemoveSound(i);
-#endif
-      }
-#endif
-
-      playlist[i] = snd_ctrl;
-      playing_sounds++;
-
-#if 0
-      printf("NEW SOUND %d ADDED TO PLAYLIST\n", snd_ctrl.nr);
-#endif
-
-#if defined(PLATFORM_MSDOS)
-      playlist[i].voice = allocate_voice((SAMPLE *)playlist[i].data_ptr);
-
-      if (snd_ctrl.loop)
-        voice_set_playmode(playlist[i].voice, PLAYMODE_LOOP);
-
-      voice_set_volume(playlist[i].voice, snd_ctrl.volume);
-      voice_set_pan(playlist[i].voice, snd_ctrl.stereo);
-      voice_start(playlist[i].voice);       
-#endif
-      break;
-    }
-  }
+  PlaylistInsertSound(snd_ctrl);
 }
 #endif /* !PLATFORM_WIN32 */
 
 }
 #endif /* !PLATFORM_WIN32 */
 
@@ -1338,6 +1152,11 @@ static void InitAudioDevice_HPUX(struct AudioFormatInfo *afmt)
 #if defined(PLATFORM_UNIX)
 static void InitAudioDevice(struct AudioFormatInfo *afmt)
 {
 #if defined(PLATFORM_UNIX)
 static void InitAudioDevice(struct AudioFormatInfo *afmt)
 {
+  afmt->stereo = TRUE;
+  afmt->format = AUDIO_FORMAT_UNKNOWN;
+  afmt->sample_rate = DEFAULT_AUDIO_SAMPLE_RATE;
+  afmt->fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE;
+
 #if defined(AUDIO_LINUX_IOCTL)
   InitAudioDevice_Linux(afmt);
 #elif defined(PLATFORM_NETBSD)
 #if defined(AUDIO_LINUX_IOCTL)
   InitAudioDevice_Linux(afmt);
 #elif defined(PLATFORM_NETBSD)
index 945b88f813ec0b88bc48d6f7b732a08af6a353a9..5ad49a929e247fad2ceced08ce79ba47a0f25c21 100644 (file)
@@ -55,6 +55,7 @@
 #define AUDIO_NUM_CHANNELS_MONO                        1
 #define AUDIO_NUM_CHANNELS_STEREO              2
 
 #define AUDIO_NUM_CHANNELS_MONO                        1
 #define AUDIO_NUM_CHANNELS_STEREO              2
 
+#define AUDIO_FORMAT_UNKNOWN                   (0)
 #define AUDIO_FORMAT_U8                                (1 << 0)
 #define AUDIO_FORMAT_S16                       (1 << 1)
 #define AUDIO_FORMAT_LE                                (1 << 2)
 #define AUDIO_FORMAT_U8                                (1 << 0)
 #define AUDIO_FORMAT_S16                       (1 << 1)
 #define AUDIO_FORMAT_LE                                (1 << 2)