]> code.delx.au - gnu-emacs/blobdiff - src/sound.c
(isearch): Use magenta2 as bg for (background light)
[gnu-emacs] / src / sound.c
index fc3adbdba6b3b29c58c9eb83fad19fc4b0b2e593..d9fd55966a5014c4c70dafb6791d87cd17d04fa7 100644 (file)
@@ -1,5 +1,5 @@
 /* sound.c -- sound support.
-   Copyright (C) 1998, 1999 Free Software Foundation.
+   Copyright (C) 1998, 1999, 2001 Free Software Foundation.
 
 This file is part of GNU Emacs.
 
@@ -25,12 +25,19 @@ Boston, MA 02111-1307, USA.  */
 
 #if defined HAVE_SOUND
 
-#include <lisp.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/types.h>
-#include <dispextern.h>
 #include <errno.h>
+#include "lisp.h"
+#include "dispextern.h"
+#include "atimer.h"
+#include <signal.h>
+#include "syssignal.h"
+
+#ifndef MSDOS
+#include <sys/ioctl.h>
+#endif
 
 /* FreeBSD has machine/soundcard.h.  Voxware sound driver docs mention
    sys/soundcard.h.  So, let's try whatever's there.  */
@@ -42,7 +49,6 @@ Boston, MA 02111-1307, USA.  */
 #include <sys/soundcard.h>
 #endif
 #ifdef HAVE_SOUNDCARD_H
-#include <sys/ioctl.h>
 #include <soundcard.h>
 #endif
 
@@ -50,8 +56,6 @@ Boston, MA 02111-1307, USA.  */
 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
 #endif
 
-#define max(X, Y) ((X) > (Y) ? (X) : (Y))
-#define min(X, Y) ((X) < (Y) ? (X) : (Y))
 #define abs(X)    ((X) < 0 ? -(X) : (X))
 
 /* Structure forward declarations.  */
@@ -231,6 +235,7 @@ static void vox_choose_format P_ ((struct sound_device *, struct sound *));
 static void vox_init P_ ((struct sound_device *));
 static void vox_write P_ ((struct sound_device *, char *, int));
 static void sound_perror P_ ((char *));
+static void sound_warning P_ ((char *));
 static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
 static void find_sound_type P_ ((struct sound *));
 static u_int32_t le2hl P_ ((u_int32_t));
@@ -257,7 +262,26 @@ static void
 sound_perror (msg)
      char *msg;
 {
-  error ("%s: %s", msg, strerror (errno));
+  int saved_errno = errno;
+
+  turn_on_atimers (1);
+#ifdef SIGIO
+  sigunblock (sigmask (SIGIO));
+#endif
+  if (saved_errno != 0)
+    error ("%s: %s", msg, strerror (saved_errno));
+  else
+    error ("%s", msg);
+}
+
+
+/* Display a warning message.  */
+
+static void
+sound_warning (msg)
+     char *msg;
+{
+  message (msg);
 }
 
 
@@ -366,24 +390,24 @@ sound_cleanup (arg)
 
 
 DEFUN ("play-sound", Fplay_sound, Splay_sound, 1, 1, 0,
-  "Play sound SOUND.\n\
-SOUND is a list of the form `(sound KEYWORD VALUE...)'.\n\
-The following keywords are recognized:\n\
-\n\
-  :file FILE.- read sound data from FILE.  If FILE isn't an\n\
-absolute file name, it is searched in `data-directory'.\n\
-\n\
-  :data DATA - read sound data from string DATA.\n\
-\n\
-Exactly one of :file or :data must be present.\n\
-\n\
-  :volume VOL - set volume to VOL.  VOL must an integer in the\n\
-range 0..100 or a float in the range 0..1.0.  If not specified,\n\
-don't change the volume setting of the sound device.\n\
-\n\
-  :device DEVICE - play sound on DEVICE.  If not specified,\n\
-a system-dependent default device name is used.")
-  (sound)
+       doc: /* Play sound SOUND.
+SOUND is a list of the form `(sound KEYWORD VALUE...)'.
+The following keywords are recognized:
+
+  :file FILE.- read sound data from FILE.  If FILE isn't an
+absolute file name, it is searched in `data-directory'.
+
+  :data DATA - read sound data from string DATA.
+
+Exactly one of :file or :data must be present.
+
+  :volume VOL - set volume to VOL.  VOL must an integer in the
+range 0..100 or a float in the range 0..1.0.  If not specified,
+don't change the volume setting of the sound device.
+
+  :device DEVICE - play sound on DEVICE.  If not specified,
+a system-dependent default device name is used.  */)
+     (sound)
      Lisp_Object sound;
 {
   Lisp_Object attrs[SOUND_ATTR_SENTINEL];
@@ -411,14 +435,14 @@ a system-dependent default device name is used.")
     {
       /* Open the sound file.  */
       s.fd = openp (Fcons (Vdata_directory, Qnil),
-                    attrs[SOUND_FILE], "", &file, 0);
+                   attrs[SOUND_FILE], Qnil, &file, 0);
       if (s.fd < 0)
-       sound_perror ("Open sound file");
+       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)
-       sound_perror ("Reading sound file header");
+       sound_perror ("Invalid sound file header");
     }
   else
     {
@@ -629,7 +653,7 @@ wav_play (s, sd)
        sd->write (sd, buffer, nbytes);
 
       if (nbytes < 0)
-       sound_perror ("Reading sound file");
+       sound_perror ("Error reading sound file");
     }
 }
 
@@ -719,7 +743,7 @@ au_play (s, sd)
        sd->write (sd, buffer, nbytes);
       
       if (nbytes < 0)
-       sound_perror ("Reading sound file");
+       sound_perror ("Error reading sound file");
     }
 }
 
@@ -760,42 +784,51 @@ static void
 vox_configure (sd)
      struct sound_device *sd;
 {
-  int requested;
+  int val;
   
   xassert (sd->fd >= 0);
 
-  /* Device parameters apparently depend on each other in undocumented
-     ways (not to imply that there is any real documentation).  Be
-     careful when reordering the calls below.  */
-  if (sd->sample_size > 0
-      && ioctl (sd->fd, SNDCTL_DSP_SAMPLESIZE, &sd->sample_size) < 0)
-    sound_perror ("Setting sample size");
-  
-  if (sd->bps > 0
-      && ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->bps) < 0)
-    sound_perror ("Setting speed");
+  /* On GNU/Linux, it seems that the device driver doesn't like to be
+     interrupted by a signal.  Block the ones we know to cause
+     troubles.  */
+  turn_on_atimers (0);
+#ifdef SIGIO
+  sigblock (sigmask (SIGIO));
+#endif
 
-  if (sd->sample_rate > 0
-      && ioctl (sd->fd, SOUND_PCM_WRITE_RATE, &sd->sample_rate) < 0)
-    sound_perror ("Setting sample rate");
+  val = sd->format;
+  if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
+      || val != sd->format)
+    sound_perror ("Could not set sound format");
 
-  requested = sd->format;
-  if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0)
-    sound_perror ("Setting format");
-  else if (requested != sd->format)
-    error ("Setting format");
+  val = sd->channels != 1;
+  if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
+      || val != (sd->channels != 1))
+    sound_perror ("Could not set stereo/mono");
 
-  if (sd->channels > 1
-      && ioctl (sd->fd, SNDCTL_DSP_STEREO, &sd->channels) < 0)
-    sound_perror ("Setting channels");
+  /* I think bps and sampling_rate are the same, but who knows.
+     Check this. and use SND_DSP_SPEED for both.  */
+  if (sd->sample_rate > 0)
+    {
+      val = sd->sample_rate;
+      if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0)
+       sound_perror ("Could not set sound speed");
+      else if (val != sd->sample_rate)
+       sound_warning ("Could not set sample rate");
+    }
 
   if (sd->volume > 0)
     {
       int volume = sd->volume & 0xff;
       volume |= volume << 8;
-      if (ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume) < 0)
-       sound_perror ("Setting volume");
+      /* This may fail if there is no mixer.  Ignore the failure.  */
+      ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
     }
+  
+  turn_on_atimers (1);
+#ifdef SIGIO
+  sigunblock (sigmask (SIGIO));
+#endif
 }
 
 
@@ -807,9 +840,21 @@ vox_close (sd)
 {
   if (sd->fd >= 0)
     {
+      /* On GNU/Linux, it seems that the device driver doesn't like to
+        be interrupted by a signal.  Block the ones we know to cause
+        troubles.  */
+#ifdef SIGIO
+      sigblock (sigmask (SIGIO));
+#endif
+      turn_on_atimers (0);
+      
       /* Flush sound data, and reset the device.  */
       ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
-      ioctl (sd->fd, SNDCTL_DSP_RESET, NULL);
+      
+      turn_on_atimers (1);
+#ifdef SIGIO
+      sigunblock (sigmask (SIGIO));
+#endif
 
       /* Close the device.  */
       emacs_close (sd->fd);
@@ -888,7 +933,7 @@ vox_write (sd, buffer, nbytes)
 {
   int nwritten = emacs_write (sd->fd, buffer, nbytes);
   if (nwritten < 0)
-    sound_perror ("Writing to sound device");
+    sound_perror ("Error writing to sound device");
 }