/* 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.
#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. */
#include <sys/soundcard.h>
#endif
#ifdef HAVE_SOUNDCARD_H
-#include <sys/ioctl.h>
#include <soundcard.h>
#endif
#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. */
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));
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);
}
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];
{
/* 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
{
sd->write (sd, buffer, nbytes);
if (nbytes < 0)
- sound_perror ("Reading sound file");
+ sound_perror ("Error reading sound file");
}
}
sd->write (sd, buffer, nbytes);
if (nbytes < 0)
- sound_perror ("Reading sound file");
+ sound_perror ("Error reading sound file");
}
}
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
}
{
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);
{
int nwritten = emacs_write (sd->fd, buffer, nbytes);
if (nwritten < 0)
- sound_perror ("Writing to sound device");
+ sound_perror ("Error writing to sound device");
}