rocksndiamonds-3.3.1.0
[rocksndiamonds.git] / src / game_em / sound.c
index b0bac6a3995ba3ba8cce1697092554adde3e4462..f03abf1e014abbb3c48d95f06b0117924c001396 100644 (file)
@@ -3,45 +3,61 @@
  * handle sounds in emerald mine
  */
 
-#include "../libgame/platform.h"
+#include "main_em.h"
 
 
-#if defined(TARGET_X11)
+#if defined(AUDIO_UNIX_NATIVE)
 
 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
 
 #ifdef PLATFORM_LINUX
+#include <sys/ioctl.h>
 #include <sys/soundcard.h>
 #endif
 
 #ifdef PLATFORM_BSD
+#include <ioctl.h>
 #include <soundcard.h>
 #endif
 
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include "global.h"
-#include "sample.h"
-
 static char audioname[] = "/dev/audio";
 
 static const int sound_priority[SAMPLE_MAX] =
 {
-  SAMPLE_exit, SAMPLE_die, SAMPLE_time, SAMPLE_boom, SAMPLE_tick,
-  SAMPLE_collect, SAMPLE_roll, SAMPLE_push, SAMPLE_dynamite, SAMPLE_press,
-  SAMPLE_door, SAMPLE_dirt, SAMPLE_blank, SAMPLE_android, SAMPLE_ball,
-  SAMPLE_grow, SAMPLE_squash, SAMPLE_crack, SAMPLE_slurp, SAMPLE_drip,
-  SAMPLE_wonder, SAMPLE_wheel, SAMPLE_stone, SAMPLE_spring, SAMPLE_diamond,
-  SAMPLE_nut, SAMPLE_bug, SAMPLE_tank, SAMPLE_eater, SAMPLE_alien,
+  SAMPLE_exit_open,
+  SAMPLE_exit_leave,
+  SAMPLE_die,
+  SAMPLE_time,
+  SAMPLE_boom,
+  SAMPLE_tick,
+  SAMPLE_collect,
+  SAMPLE_roll,
+  SAMPLE_push,
+  SAMPLE_dynamite,
+  SAMPLE_press,
+  SAMPLE_door,
+  SAMPLE_dirt,
+  SAMPLE_blank,
+  SAMPLE_android_clone,
+  SAMPLE_android_move,
+  SAMPLE_ball,
+  SAMPLE_grow,
+  SAMPLE_squash,
+  SAMPLE_wonderfall,
+  SAMPLE_crack,
+  SAMPLE_slurp,
+  SAMPLE_drip,
+  SAMPLE_wonder,
+  SAMPLE_wheel,
+  SAMPLE_stone,
+  SAMPLE_spring,
+  SAMPLE_diamond,
+  SAMPLE_nut,
+  SAMPLE_bug,
+  SAMPLE_tank,
+  SAMPLE_eater,
+  SAMPLE_eater_eat,
+  SAMPLE_alien,
   SAMPLE_acid
 };
 
@@ -49,13 +65,13 @@ int sound_thread(void)
 {
   int audio_fd; /* file descriptor of /dev/audio or -1 if not open */
   int audio_format;
-  int sample_rate;
+  // int sample_rate;
   int fragment_size;
   unsigned char *audio_buffer; /* actual buffer pumped to /dev/audio */
   short *mix_buffer;
 
   char sound_play[SAMPLE_MAX]; /* if set, we should be playing these sounds */
-  long sound_pos[SAMPLE_MAX]; /* position in the sound */
+  int sound_pos[SAMPLE_MAX]; /* position in the sound */
   int mix_play[MIXER_MAX]; /* which sounds we have chosen to mix (calculated each time) */
   int mix_count;
   int i;
@@ -64,13 +80,13 @@ int sound_thread(void)
 
   audio_fd = -1;
   audio_format = AUDIO_ULAW; /* defaults for non-OSS /dev/audio */
-  sample_rate = 8000;
+  // sample_rate = 8000;
   fragment_size = 256;
   audio_buffer = 0;
   mix_buffer = 0;
   mix_count = 0;
 
-  memset(sound_play, 0, sizeof(sound_play)); /* not playing any sounds */
+  clear_mem(sound_play, sizeof(sound_play)); /* not playing any sounds */
 
   for (;;)
   {
@@ -78,7 +94,9 @@ int sound_thread(void)
     {
 
       /* pick sounds to play, if any */
-      if (sound_play[SAMPLE_exit] || sound_play[SAMPLE_die])
+      if (sound_play[SAMPLE_exit_open] ||
+         sound_play[SAMPLE_exit_leave] ||
+         sound_play[SAMPLE_die])
        sound_play[SAMPLE_boom] = 0; /* no explosions if player goes home */
 
       mix_count = 0;
@@ -107,8 +125,8 @@ int sound_thread(void)
 
        if (i == -1)
        {
-         fprintf(stderr, "%s: %s: %s\n", progname, "select failed",
-                 strerror(errno));
+         Error(ERR_WARN, "select() failed in sound thread");
+
          goto fail;
        }
 
@@ -121,20 +139,22 @@ int sound_thread(void)
 
       if (i == -1)
       {
-       fprintf(stderr, "%s: %s: %s\n", progname, "read failed",
-               strerror(errno));
+       Error(ERR_WARN, "read() failed in sound thread");
+
        goto fail;
       }
 
       if (i == 0)
       {
-       fprintf(stderr, "%s: %s: %s\n", progname, "read sound", "Broken pipe");
+       Error(ERR_WARN, "reading sound failed in sound thread");
+
        goto fail;
       }
 
       if (i != sizeof(play))
       {
-       fprintf(stderr, "%s: %s\n", progname, "bad message length");
+       Error(ERR_WARN, "bad message length in sound thread");
+
        goto fail;
       }
 
@@ -161,15 +181,15 @@ int sound_thread(void)
 
       if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &i) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s (%d): %s\n", progname, audioname,
-               "unable to set fragment size", 512, strerror(errno));
+       Error(ERR_WARN, "unable to set fragment size in sound thread");
+
        goto reset;
       }
 
       if (ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &i) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, audioname,
-               "unable to query audio format", strerror(errno));
+       Error(ERR_WARN, "unable to query audio format in sound thread");
+
        goto reset;
       }
 
@@ -179,8 +199,8 @@ int sound_thread(void)
       i = audio_format;
       if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &i) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s (%d): %s\n", progname, audioname,
-               "unable to set audio format", audio_format, strerror(errno));
+       Error(ERR_WARN, "unable to set audio format in sound thread");
+
        goto reset;
       }
 
@@ -194,39 +214,39 @@ int sound_thread(void)
       }
       else
       {
-       fprintf(stderr, "%s: \"%s\": %s (%d)\n", progname, audioname,
-               "audio format required by device not supported", i);
+       Error(ERR_WARN, "audio format required by device not supported");
+
        goto reset;
       }
 
       i = 1;
       if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &i) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, audioname,
-               "unable to set channels to mono", strerror(errno));
+       Error(ERR_WARN, "unable to set channels to mono in sound thread");
+
        goto reset;
       }
 
       if (i != 1)
       {
-       fprintf(stderr, "%s: \"%s\": %s (%d)\n", progname, audioname,
-               "channels required by device not supported", i);
+       Error(ERR_WARN, "channels required by device not supported");
+
        goto reset;
       }
 
       i = 8000;
       if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &i) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, audioname,
-               "unable to set sampling rate", strerror(errno));
+       Error(ERR_WARN, "unable to set sampling rate in sound thread");
+
        goto reset;
       }
 
-      sample_rate = i;
+      // sample_rate = i;
       if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &i) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, audioname,
-               "unable to get block size", strerror(errno));
+       Error(ERR_WARN, "unable to get block size in sound thread");
+
        goto reset;
       }
 
@@ -235,8 +255,8 @@ int sound_thread(void)
 #else
       if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) == -1)
       {
-       fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, audioname,
-               "unable to make audio non blocking", strerror(errno));
+       Error(ERR_WARN, "unable to make audio non blocking in sound thread");
+
        goto reset;
       }
 
@@ -245,18 +265,16 @@ int sound_thread(void)
       audio_buffer = malloc(fragment_size * sizeof(*audio_buffer));
       if (audio_buffer == 0)
       {
-       fprintf(stderr, "%s: %s (%d): %s\n", progname,
-               "unable to malloc audio buffer",
-               fragment_size * sizeof(*audio_buffer), strerror(errno));
+       Error(ERR_WARN, "unable to malloc audio buffer in sound thread");
+
        goto fail;
       }
 
       mix_buffer = malloc(fragment_size * sizeof(*mix_buffer));
       if (mix_buffer == 0)
       {
-       fprintf(stderr, "%s: %s (%d): %s\n", progname,
-               "unable to malloc mixing buffer",
-               fragment_size * sizeof(*mix_buffer), strerror(errno));
+       Error(ERR_WARN, "unable to malloc mixing buffer in sound thread");
+
        goto fail;
       }
     }
@@ -276,14 +294,14 @@ int sound_thread(void)
     if (mix_count && audio_fd != -1)
     {
       /* prepare mix buffer */
-      memset(mix_buffer, 0, fragment_size * sizeof(*mix_buffer));
+      clear_mem(mix_buffer, fragment_size * sizeof(*mix_buffer));
 
       for (i = 0; i < mix_count; i++)
       {
        register short *mix_ptr = mix_buffer;
        register short *sound_ptr =
          sound_data[mix_play[i]] + sound_pos[mix_play[i]];
-       register long count =
+       register int count =
          sound_length[mix_play[i]] - sound_pos[mix_play[i]];
 
        if (count > fragment_size)
@@ -326,14 +344,15 @@ int sound_thread(void)
       i = write(audio_fd, audio_buffer, fragment_size);
       if (i == -1)
       {
-       fprintf(stderr, "%s: %s: %s\n", progname, "write error",
-               strerror(errno));
+       Error(ERR_WARN, "cannot write to audio device in sound thread");
+
        goto reset;
       }
 
       if (i != fragment_size)
       {
-       fprintf(stderr, "%s: %s\n", progname, "bad write length");
+       Error(ERR_WARN, "bad write length to audio device in sound thread");
+
        goto reset;
       }
     }
@@ -347,6 +366,7 @@ int sound_thread(void)
     free(audio_buffer);
   if (mix_buffer)
     free(mix_buffer);
+
   goto loop; /* back to top */
 
  fail:
@@ -360,22 +380,22 @@ int sound_thread(void)
   return(0);
 }
 
-int read_sample(char *name, short **data, long *length)
+int read_sample(char *name, short **data, int *length)
 {
   int result;
   FILE *file = 0;
   short *dataptr = 0;
-  long datalength;
+  int datalength;
 
   int i, actual, ch;
   unsigned char buffer[24];
-  unsigned long temp;
+  unsigned int temp;
 
   file = fopen(name, "rb");
   if (file == 0)
   {
-    fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, name, "open error",
-           strerror(errno));
+    Error(ERR_WARN, "cannot open file '%s' in sound thread", name);
+
     result = 1;
     goto fail;
   }
@@ -383,15 +403,16 @@ int read_sample(char *name, short **data, long *length)
   actual = fread(buffer, 1, 24, file);
   if (actual == -1)
   {
-    fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, name, "read error",
-           strerror(errno));
+    Error(ERR_WARN, "cannot read file '%s' in sound thread", name);
+
     result = 1;
     goto fail;
   }
 
   if (actual < 24)
   {
-    fprintf(stderr, "%s: \"%s\": %s\n", progname, name, "premature eof");
+    Error(ERR_WARN, "premature eof of file '%s' in sound thread", name);
+
     result = 1;
     goto fail;
   }
@@ -400,8 +421,8 @@ int read_sample(char *name, short **data, long *length)
   temp = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
   if (temp != 0x2e736e64)
   {
-    fprintf(stderr, "%s: \"%s\": %s\n", progname, name,
-           "unrecognized file format");
+    Error(ERR_WARN, "unrecognized format of file '%s' in sound thread", name);
+
     result = 1;
     goto fail;
   }
@@ -410,7 +431,8 @@ int read_sample(char *name, short **data, long *length)
   temp = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
   if (temp < 24)
   {
-    fprintf(stderr, "%s: \"%s\": %s\n", progname, name, "bad header length");
+    Error(ERR_WARN, "bad header length of file '%s' in sound thread", name);
+
     result = 1;
     goto fail;
   }
@@ -432,7 +454,7 @@ int read_sample(char *name, short **data, long *length)
   temp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
   if (temp != 1)
   {
-    fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
+    fprintf(stderr, "%s: \"%s\": %s (%d != 1)\n", progname, name,
            "bad encoding type", temp);
     result = 1;
     goto fail;
@@ -442,7 +464,7 @@ int read_sample(char *name, short **data, long *length)
   temp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
   if (temp != 8000)
   {
-    fprintf(stderr, "%s: \"%s\": %s (%ld != 8000)\n", progname, name,
+    fprintf(stderr, "%s: \"%s\": %s (%d != 8000)\n", progname, name,
            "bad sample rate", temp);
     result = 1;
     goto fail;
@@ -452,7 +474,7 @@ int read_sample(char *name, short **data, long *length)
   temp = buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
   if (temp != 1)
   {
-    fprintf(stderr, "%s: \"%s\": %s (%ld != 1)\n", progname, name,
+    fprintf(stderr, "%s: \"%s\": %s (%d != 1)\n", progname, name,
            "unsupported channels", temp);
     result = 1;
     goto fail;
@@ -461,9 +483,9 @@ int read_sample(char *name, short **data, long *length)
   dataptr = malloc(datalength * sizeof(*dataptr));
   if (dataptr == 0)
   {
-    fprintf(stderr, "%s: \"%s\": %s (%ld): %s\n", progname, name,
-           "unable to malloc buffer", datalength * sizeof(*dataptr),
-           strerror(errno));
+    Error(ERR_WARN, "unable to malloc buffer for file '%s' in sound thread",
+         name);
+
     result = 1;
     goto fail;
   }
@@ -492,4 +514,4 @@ int read_sample(char *name, short **data, long *length)
 
 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
 
-#endif
+#endif /* AUDIO_UNIX_NATIVE */