]> code.delx.au - gnu-emacs/blobdiff - src/sound.c
(ses-call-printer-return): Doc fix (Nil -> nil).
[gnu-emacs] / src / sound.c
index 185ba864be7b8827efb07f9cd0f8df7da78f41b3..6f955a7d69141cfe875b1440959caac0b1fb5aae 100644 (file)
@@ -1,5 +1,6 @@
 /* sound.c -- sound support.
-   Copyright (C) 1998, 1999, 2001 Free Software Foundation.
+   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
+                 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -15,8 +16,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* Written by Gerd Moellmann <gerd@gnu.org>.  Tested with Luigi's
    driver on FreeBSD 2.2.7 with a SoundBlaster 16.  */
@@ -33,7 +34,7 @@ Boston, MA 02111-1307, USA.  */
 
   The Windows implementation of play-sound is implemented via the
   Win32 API functions mciSendString, waveOutGetVolume, and
-  waveOutGetVolume which are exported by Winmm.dll.
+  waveOutSetVolume which are exported by Winmm.dll.
 */
 
 #include <config.h>
@@ -72,6 +73,10 @@ Boston, MA 02111-1307, USA.  */
 #ifdef HAVE_SOUNDCARD_H
 #include <soundcard.h>
 #endif
+#ifdef HAVE_ALSA
+#include <asoundlib.h>
+#endif
+
 /* END: Non Windows Includes */
 
 #else /* WINDOWSNT */
@@ -108,7 +113,8 @@ enum sound_attr
   SOUND_ATTR_SENTINEL
 };
 
-static void sound_perror P_ ((char *));
+static void alsa_sound_perror P_ ((char *, int)) NO_RETURN;
+static void sound_perror P_ ((char *)) NO_RETURN;
 static void sound_warning P_ ((char *));
 static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
 
@@ -120,6 +126,9 @@ static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
 #ifndef DEFAULT_SOUND_DEVICE
 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
 #endif
+#ifndef DEFAULT_ALSA_SOUND_DEVICE
+#define DEFAULT_ALSA_SOUND_DEVICE "default"
+#endif
 
 
 /* Structure forward declarations.  */
@@ -226,6 +235,10 @@ struct sound_device
   void (* choose_format) P_ ((struct sound_device *sd,
                              struct sound *s));
 
+  /* Return a preferred data size in bytes to be sent to write (below)
+     each time.  2048 is used if this is NULL.  */
+  int (* period_size) P_ ((struct sound_device *sd));
+
   /* Write NYBTES bytes from BUFFER to device SD.  */
   void (* write) P_ ((struct sound_device *sd, const char *buffer,
                      int nbytes));
@@ -279,7 +292,7 @@ static void vox_open P_ ((struct sound_device *));
 static void vox_configure P_ ((struct sound_device *));
 static void vox_close P_ ((struct sound_device *sd));
 static void vox_choose_format P_ ((struct sound_device *, struct sound *));
-static void vox_init P_ ((struct sound_device *));
+static int vox_init P_ ((struct sound_device *));
 static void vox_write P_ ((struct sound_device *, const char *, int));
 static void find_sound_type P_ ((struct sound *));
 static u_int32_t le2hl P_ ((u_int32_t));
@@ -451,13 +464,12 @@ static Lisp_Object
 sound_cleanup (arg)
      Lisp_Object arg;
 {
-  if (current_sound_device)
-    {
-      if (current_sound_device->close)
-       current_sound_device->close (current_sound_device);
-      if (current_sound->fd > 0)
-       emacs_close (current_sound->fd);
-    }
+  if (current_sound_device->close)
+    current_sound_device->close (current_sound_device);
+  if (current_sound->fd > 0)
+    emacs_close (current_sound->fd);
+  free (current_sound_device);
+  free (current_sound);
 
   return Qnil;
 }
@@ -604,7 +616,7 @@ wav_play (s, sd)
     {
       char *buffer;
       int nbytes;
-      int blksize = 2048;
+      int blksize = sd->period_size ? sd->period_size (sd) : 2048;
 
       buffer = (char *) alloca (blksize);
       lseek (s->fd, sizeof *header, SEEK_SET);
@@ -633,7 +645,8 @@ enum au_encoding
   AU_ENCODING_32,
   AU_ENCODING_IEEE32,
   AU_ENCODING_IEEE64,
-  AU_COMPRESSED = 23
+  AU_COMPRESSED = 23,
+  AU_ENCODING_ALAW_8 = 27
 };
 
 
@@ -689,7 +702,7 @@ au_play (s, sd)
               SBYTES (s->data) - header->data_offset);
   else
     {
-      int blksize = 2048;
+      int blksize = sd->period_size ? sd->period_size (sd) : 2048;
       char *buffer;
       int nbytes;
 
@@ -868,16 +881,33 @@ vox_choose_format (sd, s)
 /* Initialize device SD.  Set up the interface functions in the device
    structure.  */
 
-static void
+static int
 vox_init (sd)
      struct sound_device *sd;
 {
+  char *file;
+  int fd;
+
+  /* Open the sound device.  Default is /dev/dsp.  */
+  if (sd->file)
+    file = sd->file;
+  else
+    file = DEFAULT_SOUND_DEVICE;
+  fd = emacs_open (file, O_WRONLY, 0);
+  if (fd >= 0)
+    emacs_close (fd);
+  else
+    return 0;
+
   sd->fd = -1;
   sd->open = vox_open;
   sd->close = vox_close;
   sd->configure = vox_configure;
   sd->choose_format = vox_choose_format;
   sd->write = vox_write;
+  sd->period_size = NULL;
+
+  return 1;
 }
 
 /* Write NBYTES bytes from BUFFER to device SD.  */
@@ -893,6 +923,370 @@ vox_write (sd, buffer, nbytes)
     sound_perror ("Error writing to sound device");
 }
 
+#ifdef HAVE_ALSA
+/***********************************************************************
+                      ALSA Driver Interface
+ ***********************************************************************/
+
+/* This driver is available on GNU/Linux. */
+
+static void
+alsa_sound_perror (msg, err)
+     char *msg;
+     int err;
+{
+  error ("%s: %s", msg, snd_strerror (err));
+}
+
+struct alsa_params
+{
+  snd_pcm_t *handle;
+  snd_pcm_hw_params_t *hwparams;
+  snd_pcm_sw_params_t *swparams;
+  snd_pcm_uframes_t period_size;
+};
+
+/* Open device SD.  If SD->file is non-null, open that device,
+   otherwise use a default device name.  */
+
+static void
+alsa_open (sd)
+     struct sound_device *sd;
+{
+  char *file;
+  struct alsa_params *p;
+  int err;
+
+  /* Open the sound device.  Default is "default".  */
+  if (sd->file)
+    file = sd->file;
+  else
+    file = DEFAULT_ALSA_SOUND_DEVICE;
+
+  p = xmalloc (sizeof (*p));
+  p->handle = NULL;
+  p->hwparams = NULL;
+  p->swparams = NULL;
+
+  sd->fd = -1;
+  sd->data = p;
+
+
+  err = snd_pcm_open (&p->handle, file, SND_PCM_STREAM_PLAYBACK, 0);
+  if (err < 0)
+    alsa_sound_perror (file, err);
+}
+
+static int
+alsa_period_size (sd)
+       struct sound_device *sd;
+{
+  struct alsa_params *p = (struct alsa_params *) sd->data;
+  return p->period_size;
+}
+
+static void
+alsa_configure (sd)
+     struct sound_device *sd;
+{
+  int val, err, dir;
+  unsigned uval;
+  struct alsa_params *p = (struct alsa_params *) sd->data;
+  snd_pcm_uframes_t buffer_size;
+
+  xassert (p->handle != 0);
+
+  err = snd_pcm_hw_params_malloc (&p->hwparams);
+  if (err < 0)
+    alsa_sound_perror ("Could not allocate hardware parameter structure", err);
+
+  err = snd_pcm_sw_params_malloc (&p->swparams);
+  if (err < 0)
+    alsa_sound_perror ("Could not allocate software parameter structure", err);
+
+  err = snd_pcm_hw_params_any (p->handle, p->hwparams);
+  if (err < 0)
+    alsa_sound_perror ("Could not initialize hardware parameter structure", err);
+
+  err = snd_pcm_hw_params_set_access (p->handle, p->hwparams,
+                                      SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (err < 0)
+    alsa_sound_perror ("Could not set access type", err);
+
+  val = sd->format;
+  err = snd_pcm_hw_params_set_format (p->handle, p->hwparams, val);
+  if (err < 0) 
+    alsa_sound_perror ("Could not set sound format", err);
+
+  uval = sd->sample_rate;
+  err = snd_pcm_hw_params_set_rate_near (p->handle, p->hwparams, &uval, 0);
+  if (err < 0)
+    alsa_sound_perror ("Could not set sample rate", err);
+  
+  val = sd->channels;
+  err = snd_pcm_hw_params_set_channels (p->handle, p->hwparams, val);
+  if (err < 0)
+    alsa_sound_perror ("Could not set channel count", err);
+
+  err = snd_pcm_hw_params (p->handle, p->hwparams);
+  if (err < 0)
+    alsa_sound_perror ("Could not set parameters", err);
+
+
+  err = snd_pcm_hw_params_get_period_size (p->hwparams, &p->period_size, &dir);
+  if (err < 0)
+    alsa_sound_perror ("Unable to get period size for playback", err);
+
+  err = snd_pcm_hw_params_get_buffer_size (p->hwparams, &buffer_size);
+  if (err < 0)
+    alsa_sound_perror("Unable to get buffer size for playback", err);
+
+  err = snd_pcm_sw_params_current (p->handle, p->swparams);
+  if (err < 0)
+    alsa_sound_perror ("Unable to determine current swparams for playback",
+                       err);
+
+  /* Start the transfer when the buffer is almost full */
+  err = snd_pcm_sw_params_set_start_threshold (p->handle, p->swparams,
+                                               (buffer_size / p->period_size)
+                                               * p->period_size);
+  if (err < 0)
+    alsa_sound_perror ("Unable to set start threshold mode for playback", err);
+
+  /* Allow the transfer when at least period_size samples can be processed */
+  err = snd_pcm_sw_params_set_avail_min (p->handle, p->swparams, p->period_size);
+  if (err < 0)
+    alsa_sound_perror ("Unable to set avail min for playback", err);
+
+  /* Align all transfers to 1 period */
+  err = snd_pcm_sw_params_set_xfer_align (p->handle, p->swparams,
+                                          p->period_size);
+  if (err < 0)
+    alsa_sound_perror ("Unable to set transfer align for playback", err);
+
+  err = snd_pcm_sw_params (p->handle, p->swparams);
+  if (err < 0)
+    alsa_sound_perror ("Unable to set sw params for playback\n", err);
+
+  snd_pcm_hw_params_free (p->hwparams);
+  p->hwparams = NULL;
+  snd_pcm_sw_params_free (p->swparams);
+  p->swparams = NULL;
+  
+  err = snd_pcm_prepare (p->handle);
+  if (err < 0)
+    alsa_sound_perror ("Could not prepare audio interface for use", err);
+  
+  if (sd->volume > 0)
+    {
+      int chn;
+      snd_mixer_t *handle;
+      snd_mixer_elem_t *e;
+      char *file = sd->file ? sd->file : DEFAULT_ALSA_SOUND_DEVICE;
+
+      if (snd_mixer_open (&handle, 0) >= 0)
+        {
+          if (snd_mixer_attach (handle, file) >= 0
+              && snd_mixer_load (handle) >= 0
+              && snd_mixer_selem_register (handle, NULL, NULL) >= 0)
+            for (e = snd_mixer_first_elem (handle);
+                 e;
+                 e = snd_mixer_elem_next (e))
+              {
+                if (snd_mixer_selem_has_playback_volume (e))
+                  {
+                    long pmin, pmax;
+                    snd_mixer_selem_get_playback_volume_range (e, &pmin, &pmax);
+                    long vol = pmin + (sd->volume * (pmax - pmin)) / 100;
+                    
+                    for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++)
+                      snd_mixer_selem_set_playback_volume (e, chn, vol);
+                  }
+              }
+          snd_mixer_close(handle);
+        }
+    }
+}
+
+
+/* Close device SD if it is open.  */
+
+static void
+alsa_close (sd)
+     struct sound_device *sd;
+{
+  struct alsa_params *p = (struct alsa_params *) sd->data;
+  if (p)
+    {
+      if (p->hwparams)
+        snd_pcm_hw_params_free (p->hwparams);
+      if (p->swparams)
+        snd_pcm_sw_params_free (p->swparams);
+      if (p->handle)
+        {
+          snd_pcm_drain (p->handle);
+          snd_pcm_close (p->handle);
+        }
+      free (p);
+    }
+}
+
+/* Choose device-dependent format for device SD from sound file S.  */
+
+static void
+alsa_choose_format (sd, s)
+     struct sound_device *sd;
+     struct sound *s;
+{
+  struct alsa_params *p = (struct alsa_params *) sd->data;
+  if (s->type == RIFF)
+    {
+      struct wav_header *h = (struct wav_header *) s->header;
+      if (h->precision == 8)
+       sd->format = SND_PCM_FORMAT_U8;
+      else if (h->precision == 16)
+          sd->format = SND_PCM_FORMAT_S16_LE;
+      else
+       error ("Unsupported WAV file format");
+    }
+  else if (s->type == SUN_AUDIO)
+    {
+      struct au_header *header = (struct au_header *) s->header;
+      switch (header->encoding)
+       {
+       case AU_ENCODING_ULAW_8:
+         sd->format = SND_PCM_FORMAT_MU_LAW;
+          break;
+       case AU_ENCODING_ALAW_8:
+         sd->format = SND_PCM_FORMAT_A_LAW;
+          break;
+       case AU_ENCODING_IEEE32:
+          sd->format = SND_PCM_FORMAT_FLOAT_BE;
+          break;
+       case AU_ENCODING_IEEE64:
+         sd->format = SND_PCM_FORMAT_FLOAT64_BE;
+         break;
+       case AU_ENCODING_8:
+         sd->format = SND_PCM_FORMAT_S8;
+         break;
+       case AU_ENCODING_16:
+         sd->format = SND_PCM_FORMAT_S16_BE;
+         break;
+       case AU_ENCODING_24:
+         sd->format = SND_PCM_FORMAT_S24_BE;
+         break;
+       case AU_ENCODING_32:
+         sd->format = SND_PCM_FORMAT_S32_BE;
+         break;
+
+       default:
+         error ("Unsupported AU file format");
+       }
+    }
+  else
+    abort ();
+}
+
+
+/* Write NBYTES bytes from BUFFER to device SD.  */
+
+static void
+alsa_write (sd, buffer, nbytes)
+     struct sound_device *sd;
+     const char *buffer;
+     int nbytes;
+{
+  struct alsa_params *p = (struct alsa_params *) sd->data;
+
+  /* The the third parameter to snd_pcm_writei is frames, not bytes. */
+  int fact = snd_pcm_format_size (sd->format, 1) * sd->channels;
+  int nwritten = 0;
+  int err;
+
+  while (nwritten < nbytes)
+    {
+      err = snd_pcm_writei (p->handle,
+                            buffer + nwritten,
+                            (nbytes - nwritten)/fact);
+      if (err < 0)
+        {
+          if (err == -EPIPE)
+            {  /* under-run */
+              err = snd_pcm_prepare (p->handle);
+              if (err < 0)
+                alsa_sound_perror ("Can't recover from underrun, prepare failed",
+                                   err);
+            }
+          else if (err == -ESTRPIPE)
+            {
+              while ((err = snd_pcm_resume (p->handle)) == -EAGAIN)
+                sleep(1);      /* wait until the suspend flag is released */
+              if (err < 0)
+                {
+                  err = snd_pcm_prepare (p->handle);
+                  if (err < 0)
+                    alsa_sound_perror ("Can't recover from suspend, "
+                                       "prepare failed",
+                                       err);
+                }
+            }
+          else 
+            alsa_sound_perror ("Error writing to sound device", err);
+          
+        }
+      else
+        nwritten += err * fact;
+    }
+}
+
+static void
+snd_error_quiet (file, line, function, err, fmt)
+     const char *file;
+     int line;
+     const char *function;
+     int err;
+     const char *fmt;
+{
+}
+
+/* Initialize device SD.  Set up the interface functions in the device
+   structure.  */
+
+static int
+alsa_init (sd)
+     struct sound_device *sd;
+{
+  char *file;
+  snd_pcm_t *handle;
+  int err;
+
+  /* Open the sound device.  Default is "default".  */
+  if (sd->file)
+    file = sd->file;
+  else
+    file = DEFAULT_ALSA_SOUND_DEVICE;
+
+  snd_lib_error_set_handler ((snd_lib_error_handler_t) snd_error_quiet);
+  err = snd_pcm_open (&handle, file, SND_PCM_STREAM_PLAYBACK, 0);
+  snd_lib_error_set_handler (NULL);
+  if (err < 0)
+      return 0;
+  snd_pcm_close (handle);
+
+  sd->fd = -1;
+  sd->open = alsa_open;
+  sd->close = alsa_close;
+  sd->configure = alsa_configure;
+  sd->choose_format = alsa_choose_format;
+  sd->write = alsa_write;
+  sd->period_size = alsa_period_size;
+
+  return 1;
+}
+
+#endif /* HAVE_ALSA */
+
+
 /* END: Non Windows functions */
 #else /* WINDOWSNT */
 
@@ -900,79 +1294,74 @@ vox_write (sd, buffer, nbytes)
 
 static int
 do_play_sound (psz_file, ui_volume)
-    const char * psz_file;
-    unsigned long ui_volume;
+     const char *psz_file;
+     unsigned long ui_volume;
 {
-  int i_result=0;
-  MCIERROR mci_error=0;
-  char sz_cmd_buf[520]={0};
-  char sz_ret_buf[520]={0};
-  MMRESULT mm_result=MMSYSERR_NOERROR;
-  unsigned long ui_volume_org=0;
-  BOOL b_reset_volume=FALSE;
+  int i_result = 0;
+  MCIERROR mci_error = 0;
+  char sz_cmd_buf[520] = {0};
+  char sz_ret_buf[520] = {0};
+  MMRESULT mm_result = MMSYSERR_NOERROR;
+  unsigned long ui_volume_org = 0;
+  BOOL b_reset_volume = FALSE;
+
   memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
   memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
-  sprintf (
-    sz_cmd_buf,
-    "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
-    psz_file);
-  mci_error=mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
+  sprintf (sz_cmd_buf,
+           "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
+           psz_file);
+  mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
   if (mci_error != 0)
     {
-      sound_warning (
-        "The open mciSendString command failed to open\n"
-        "the specified sound file");
-      i_result=(int)mci_error;
+      sound_warning ("The open mciSendString command failed to open\n"
+                     "the specified sound file");
+      i_result = (int) mci_error;
       return i_result;
     }
   if ((ui_volume > 0) && (ui_volume != UINT_MAX))
     {
-      mm_result=waveOutGetVolume ((HWAVEOUT)WAVE_MAPPER, &ui_volume_org);
+      mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
       if (mm_result == MMSYSERR_NOERROR)
         {
-          b_reset_volume=TRUE;
-          mm_result=waveOutSetVolume ((HWAVEOUT)WAVE_MAPPER, ui_volume);
+          b_reset_volume = TRUE;
+          mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
           if ( mm_result != MMSYSERR_NOERROR)
             {
-              sound_warning (
-                "waveOutSetVolume failed to set the volume level\n"
-                "of the WAVE_MAPPER device.\n"
-                "As a result, the user selected volume level will\n"
-                "not be used.");
+              sound_warning ("waveOutSetVolume failed to set the volume level\n"
+                             "of the WAVE_MAPPER device.\n"
+                             "As a result, the user selected volume level will\n"
+                             "not be used.");
             }
         }
       else
         {
-          sound_warning (
-            "waveOutGetVolume failed to obtain the original\n"
-            "volume level of the WAVE_MAPPER device.\n"
-            "As a result, the user selected volume level will\n"
-            "not be used.");
+          sound_warning ("waveOutGetVolume failed to obtain the original\n"
+                         "volume level of the WAVE_MAPPER device.\n"
+                         "As a result, the user selected volume level will\n"
+                         "not be used.");
         }
     }
   memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
   memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
   strcpy (sz_cmd_buf, "play GNUEmacs_PlaySound_Device wait");
-  mci_error=mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
+  mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
   if (mci_error != 0)
     {
-      sound_warning (
-        "The play mciSendString command failed to play the\n"
-        "opened sound file.");
-      i_result=(int)mci_error;
+      sound_warning ("The play mciSendString command failed to play the\n"
+                     "opened sound file.");
+      i_result = (int) mci_error;
     }
   memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
   memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
   strcpy (sz_cmd_buf, "close GNUEmacs_PlaySound_Device wait");
-  mci_error=mciSendString ( sz_cmd_buf, sz_ret_buf, 520, NULL);
+  mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
   if (b_reset_volume == TRUE)
     {
-      mm_result=waveOutSetVolume ((HWAVEOUT)WAVE_MAPPER, ui_volume_org);
+      mm_result=waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
       if (mm_result != MMSYSERR_NOERROR)
         {
-          sound_warning (
-            "waveOutSetVolume failed to reset the original volume\n"
-            "level of the WAVE_MAPPER device.");
+          sound_warning ("waveOutSetVolume failed to reset the original volume\n"
+                         "level of the WAVE_MAPPER device.");
         }
     }
   return i_result;
@@ -982,7 +1371,6 @@ do_play_sound (psz_file, ui_volume)
 
 #endif /* WINDOWSNT */
 
-
 DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
        doc: /* Play sound SOUND.
 
@@ -996,16 +1384,14 @@ Internal use only, use `play-sound' instead.\n  */)
 #ifndef WINDOWSNT
   Lisp_Object file;
   struct gcpro gcpro1, gcpro2;
-  struct sound_device sd;
-  struct sound s;
   Lisp_Object args[2];
 #else /* WINDOWSNT */
-  int len=0;
-  Lisp_Object lo_file={0};
-  char * psz_file=NULL;
-  unsigned long ui_volume_tmp=UINT_MAX;
-  unsigned long ui_volume=UINT_MAX;
-  int i_result=0;
+  int len = 0;
+  Lisp_Object lo_file = {0};
+  char * psz_file = NULL;
+  unsigned long ui_volume_tmp = UINT_MAX;
+  unsigned long ui_volume = UINT_MAX;
+  int i_result = 0;
 #endif /* WINDOWSNT */
 
   /* Parse the sound specification.  Give up if it is invalid.  */
@@ -1015,90 +1401,83 @@ Internal use only, use `play-sound' instead.\n  */)
 #ifndef WINDOWSNT
   file = Qnil;
   GCPRO2 (sound, file);
-  bzero (&sd, sizeof sd);
-  bzero (&s, sizeof s);
-  current_sound_device = &sd;
-  current_sound = &s;
+  current_sound_device = (struct sound_device *) xmalloc (sizeof (struct sound_device));
+  bzero (current_sound_device, sizeof (struct sound_device));
+  current_sound = (struct sound *) xmalloc (sizeof (struct sound));
+  bzero (current_sound, sizeof (struct sound));
   record_unwind_protect (sound_cleanup, Qnil);
-  s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
+  current_sound->header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
 
   if (STRINGP (attrs[SOUND_FILE]))
     {
       /* Open the sound file.  */
-      s.fd = openp (Fcons (Vdata_directory, Qnil),
-                   attrs[SOUND_FILE], Qnil, &file, Qnil);
-      if (s.fd < 0)
+      current_sound->fd = openp (Fcons (Vdata_directory, Qnil),
+                                attrs[SOUND_FILE], Qnil, &file, Qnil);
+      if (current_sound->fd < 0)
        sound_perror ("Could not open sound file");
 
       /* Read the first bytes from the file.  */
-      s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
-      if (s.header_size < 0)
+      current_sound->header_size
+       = emacs_read (current_sound->fd, current_sound->header,
+                     MAX_SOUND_HEADER_BYTES);
+      if (current_sound->header_size < 0)
        sound_perror ("Invalid sound file header");
     }
   else
     {
-      s.data = attrs[SOUND_DATA];
-      s.header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (s.data));
-      bcopy (SDATA (s.data), s.header, s.header_size);
+      current_sound->data = attrs[SOUND_DATA];
+      current_sound->header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (current_sound->data));
+      bcopy (SDATA (current_sound->data), current_sound->header, current_sound->header_size);
     }
 
   /* Find out the type of sound.  Give up if we can't tell.  */
-  find_sound_type (&s);
+  find_sound_type (current_sound);
 
   /* Set up a device.  */
   if (STRINGP (attrs[SOUND_DEVICE]))
     {
       int len = SCHARS (attrs[SOUND_DEVICE]);
-      sd.file = (char *) alloca (len + 1);
-      strcpy (sd.file, SDATA (attrs[SOUND_DEVICE]));
+      current_sound_device->file = (char *) alloca (len + 1);
+      strcpy (current_sound_device->file, SDATA (attrs[SOUND_DEVICE]));
     }
 
   if (INTEGERP (attrs[SOUND_VOLUME]))
-    sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
+    current_sound_device->volume = XFASTINT (attrs[SOUND_VOLUME]);
   else if (FLOATP (attrs[SOUND_VOLUME]))
-    sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
+    current_sound_device->volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
 
   args[0] = Qplay_sound_functions;
   args[1] = sound;
   Frun_hook_with_args (2, args);
 
-  /* There is only one type of device we currently support, the VOX
-     sound driver.  Set up the device interface functions for that
-     device.  */
-  vox_init (&sd);
+#ifdef HAVE_ALSA
+  if (!alsa_init (current_sound_device))
+#endif
+    if (!vox_init (current_sound_device))
+      error ("No usable sound device driver found");
 
   /* Open the device.  */
-  sd.open (&sd);
+  current_sound_device->open (current_sound_device);
 
   /* Play the sound.  */
-  s.play (&s, &sd);
-
-  /* Close the input file, if any.  */
-  if (!STRINGP (s.data))
-    {
-      emacs_close (s.fd);
-      s.fd = -1;
-    }
-
-  /* Close the device.  */
-  sd.close (&sd);
+  current_sound->play (current_sound, current_sound_device);
 
   /* Clean up.  */
-  current_sound_device = NULL;
-  current_sound = NULL;
   UNGCPRO;
+
 #else /* WINDOWSNT */
-       lo_file=Fexpand_file_name (attrs[SOUND_FILE], Qnil);
-  len=XSTRING (lo_file)->size;
-  psz_file=(char *)alloca (len+1);
+
+  lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil);
+  len = XSTRING (lo_file)->size;
+  psz_file = (char *) alloca (len + 1);
   strcpy (psz_file, XSTRING (lo_file)->data);
   if (INTEGERP (attrs[SOUND_VOLUME]))
     {
-      ui_volume_tmp=XFASTINT (attrs[SOUND_VOLUME]);
+      ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
     }
   else if (FLOATP (attrs[SOUND_VOLUME]))
     {
-      ui_volume_tmp=(unsigned long)XFLOAT_DATA (attrs[SOUND_VOLUME])*100;
+      ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
     }
   /*
     Based on some experiments I have conducted, a value of 100 or less
@@ -1106,15 +1485,17 @@ Internal use only, use `play-sound' instead.\n  */)
     A value of UINT_MAX indicates that you wish for the sound to played
     at the maximum possible volume.  A value of UINT_MAX/2 plays the
     sound at 50% maximum volume.  Therefore the value passed to do_play_sound
-    (and thus to waveOutSetVolume must be some fraction of UINT_MAX.
+    (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
     The following code adjusts the user specified volume level appropriately.
-   */
+  */
   if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
     {
-      ui_volume=ui_volume_tmp * (UINT_MAX / 100);
+      ui_volume = ui_volume_tmp * (UINT_MAX / 100);
     }
-  i_result=do_play_sound (psz_file, ui_volume);
+  i_result = do_play_sound (psz_file, ui_volume);
+
 #endif /* WINDOWSNT */
+
   unbind_to (count, Qnil);
   return Qnil;
 }
@@ -1145,3 +1526,6 @@ init_sound ()
 }
 
 #endif /* HAVE_SOUND */
+
+/* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
+   (do not change this comment) */