#include <config.h>
#if defined HAVE_SOUND
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include "lisp.h"
#include "dispextern.h"
#include "atimer.h"
#include <signal.h>
#include "syssignal.h"
#ifndef WINDOWSNT
#ifndef MSDOS
#include <sys/ioctl.h>
#endif
#ifdef HAVE_MACHINE_SOUNDCARD_H
#include <machine/soundcard.h>
#endif
#ifdef HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#endif
#ifdef HAVE_SOUNDCARD_H
#include <soundcard.h>
#endif
#ifdef HAVE_ALSA
#ifdef ALSA_SUBDIR_INCLUDE
#include <alsa/asoundlib.h>
#else
#include <asoundlib.h>
#endif
#endif
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <windows.h>
#include <mmsystem.h>
#endif
#define abs(X) ((X) < 0 ? -(X) : (X))
extern Lisp_Object QCfile, QCdata;
Lisp_Object QCvolume, QCdevice;
Lisp_Object Qsound;
Lisp_Object Qplay_sound_functions;
enum sound_attr
{
SOUND_FILE,
SOUND_DATA,
SOUND_DEVICE,
SOUND_VOLUME,
SOUND_ATTR_SENTINEL
};
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 *));
#ifndef WINDOWSNT
#ifndef DEFAULT_SOUND_DEVICE
#define DEFAULT_SOUND_DEVICE "/dev/dsp"
#endif
#ifndef DEFAULT_ALSA_SOUND_DEVICE
#define DEFAULT_ALSA_SOUND_DEVICE "default"
#endif
struct sound;
struct sound_device;
struct wav_header
{
u_int32_t magic;
u_int32_t length;
u_int32_t chunk_type;
u_int32_t chunk_format;
u_int32_t chunk_length;
u_int16_t format;
u_int16_t channels;
u_int32_t sample_rate;
u_int32_t bytes_per_second;
u_int16_t sample_size;
u_int16_t precision;
u_int32_t chunk_data;
u_int32_t data_length;
};
struct au_header
{
u_int32_t magic_number;
u_int32_t data_offset;
u_int32_t data_size;
u_int32_t encoding;
u_int32_t sample_rate;
u_int32_t channels;
};
#define MAX_SOUND_HEADER_BYTES \
max (sizeof (struct wav_header), sizeof (struct au_header))
struct sound_device
{
char *file;
int fd;
int format;
int volume;
int sample_size;
int sample_rate;
int bps;
int channels;
void (* open) P_ ((struct sound_device *sd));
void (* close) P_ ((struct sound_device *sd));
void (* configure) P_ ((struct sound_device *device));
void (* choose_format) P_ ((struct sound_device *sd,
struct sound *s));
int (* period_size) P_ ((struct sound_device *sd));
void (* write) P_ ((struct sound_device *sd, const char *buffer,
int nbytes));
void *data;
};
enum sound_type
{
RIFF,
SUN_AUDIO
};
struct sound
{
enum sound_type type;
int fd;
char *header;
int header_size;
Lisp_Object data;
void (* play) P_ ((struct sound *s, struct sound_device *sd));
};
struct sound_device *current_sound_device;
struct sound *current_sound;
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 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));
static u_int16_t le2hs P_ ((u_int16_t));
static u_int32_t be2hl P_ ((u_int32_t));
static int wav_init P_ ((struct sound *));
static void wav_play P_ ((struct sound *, struct sound_device *));
static int au_init P_ ((struct sound *));
static void au_play P_ ((struct sound *, struct sound_device *));
#if 0
static u_int16_t be2hs P_ ((u_int16_t));
#endif
#else
static int do_play_sound P_ ((const char *, unsigned long));
#endif
static void
sound_perror (msg)
char *msg;
{
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);
}
static void
sound_warning (msg)
char *msg;
{
message (msg);
}
static int
parse_sound (sound, attrs)
Lisp_Object sound;
Lisp_Object *attrs;
{
if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
return 0;
sound = XCDR (sound);
attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
#ifndef WINDOWSNT
if (!STRINGP (attrs[SOUND_FILE])
&& !STRINGP (attrs[SOUND_DATA]))
return 0;
#else
if (!STRINGP (attrs[SOUND_FILE]))
{
return 0;
}
#endif
if (!NILP (attrs[SOUND_VOLUME]))
{
if (INTEGERP (attrs[SOUND_VOLUME]))
{
if (XINT (attrs[SOUND_VOLUME]) < 0
|| XINT (attrs[SOUND_VOLUME]) > 100)
return 0;
}
else if (FLOATP (attrs[SOUND_VOLUME]))
{
if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
|| XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
return 0;
}
else
return 0;
}
#ifndef WINDOWSNT
if (!NILP (attrs[SOUND_DEVICE])
&& !STRINGP (attrs[SOUND_DEVICE]))
return 0;
#endif
return 1;
}
#ifndef WINDOWSNT
static void
find_sound_type (s)
struct sound *s;
{
if (!wav_init (s) && !au_init (s))
error ("Unknown sound format");
}
static Lisp_Object
sound_cleanup (arg)
Lisp_Object arg;
{
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;
}
static u_int32_t
le2hl (value)
u_int32_t value;
{
#ifdef WORDS_BIG_ENDIAN
unsigned char *p = (unsigned char *) &value;
value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
#endif
return value;
}
static u_int16_t
le2hs (value)
u_int16_t value;
{
#ifdef WORDS_BIG_ENDIAN
unsigned char *p = (unsigned char *) &value;
value = p[0] + (p[1] << 8);
#endif
return value;
}
static u_int32_t
be2hl (value)
u_int32_t value;
{
#ifndef WORDS_BIG_ENDIAN
unsigned char *p = (unsigned char *) &value;
value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
#endif
return value;
}
#if 0
static u_int16_t
be2hs (value)
u_int16_t value;
{
#ifndef WORDS_BIG_ENDIAN
unsigned char *p = (unsigned char *) &value;
value = p[1] + (p[0] << 8);
#endif
return value;
}
#endif
static int
wav_init (s)
struct sound *s;
{
struct wav_header *header = (struct wav_header *) s->header;
if (s->header_size < sizeof *header
|| bcmp (s->header, "RIFF", 4) != 0)
return 0;
header->magic = le2hl (header->magic);
header->length = le2hl (header->length);
header->chunk_type = le2hl (header->chunk_type);
header->chunk_format = le2hl (header->chunk_format);
header->chunk_length = le2hl (header->chunk_length);
header->format = le2hs (header->format);
header->channels = le2hs (header->channels);
header->sample_rate = le2hl (header->sample_rate);
header->bytes_per_second = le2hl (header->bytes_per_second);
header->sample_size = le2hs (header->sample_size);
header->precision = le2hs (header->precision);
header->chunk_data = le2hl (header->chunk_data);
header->data_length = le2hl (header->data_length);
s->type = RIFF;
s->play = wav_play;
return 1;
}
static void
wav_play (s, sd)
struct sound *s;
struct sound_device *sd;
{
struct wav_header *header = (struct wav_header *) s->header;
sd->choose_format (sd, s);
sd->sample_size = header->sample_size;
sd->sample_rate = header->sample_rate;
sd->bps = header->bytes_per_second;
sd->channels = header->channels;
sd->configure (sd);
if (STRINGP (s->data))
sd->write (sd, SDATA (s->data) + sizeof *header,
SBYTES (s->data) - sizeof *header);
else
{
char *buffer;
int nbytes;
int blksize = sd->period_size ? sd->period_size (sd) : 2048;
int data_left = header->data_length;
buffer = (char *) alloca (blksize);
lseek (s->fd, sizeof *header, SEEK_SET);
while (data_left > 0
&& (nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
{
if (data_left < nbytes) nbytes = data_left;
data_left -= nbytes;
sd->write (sd, buffer, nbytes);
}
if (nbytes < 0)
sound_perror ("Error reading sound file");
}
}
enum au_encoding
{
AU_ENCODING_ULAW_8 = 1,
AU_ENCODING_8,
AU_ENCODING_16,
AU_ENCODING_24,
AU_ENCODING_32,
AU_ENCODING_IEEE32,
AU_ENCODING_IEEE64,
AU_COMPRESSED = 23,
AU_ENCODING_ALAW_8 = 27
};
static int
au_init (s)
struct sound *s;
{
struct au_header *header = (struct au_header *) s->header;
if (s->header_size < sizeof *header
|| bcmp (s->header, ".snd", 4) != 0)
return 0;
header->magic_number = be2hl (header->magic_number);
header->data_offset = be2hl (header->data_offset);
header->data_size = be2hl (header->data_size);
header->encoding = be2hl (header->encoding);
header->sample_rate = be2hl (header->sample_rate);
header->channels = be2hl (header->channels);
s->type = SUN_AUDIO;
s->play = au_play;
return 1;
}
static void
au_play (s, sd)
struct sound *s;
struct sound_device *sd;
{
struct au_header *header = (struct au_header *) s->header;
sd->sample_size = 0;
sd->sample_rate = header->sample_rate;
sd->bps = 0;
sd->channels = header->channels;
sd->choose_format (sd, s);
sd->configure (sd);
if (STRINGP (s->data))
sd->write (sd, SDATA (s->data) + header->data_offset,
SBYTES (s->data) - header->data_offset);
else
{
int blksize = sd->period_size ? sd->period_size (sd) : 2048;
char *buffer;
int nbytes;
lseek (s->fd, header->data_offset, SEEK_SET);
buffer = (char *) alloca (blksize);
while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
sd->write (sd, buffer, nbytes);
if (nbytes < 0)
sound_perror ("Error reading sound file");
}
}
static void
vox_open (sd)
struct sound_device *sd;
{
char *file;
if (sd->file)
file = sd->file;
else
file = DEFAULT_SOUND_DEVICE;
sd->fd = emacs_open (file, O_WRONLY, 0);
if (sd->fd < 0)
sound_perror (file);
}
static void
vox_configure (sd)
struct sound_device *sd;
{
int val;
xassert (sd->fd >= 0);
turn_on_atimers (0);
#ifdef SIGIO
sigblock (sigmask (SIGIO));
#endif
val = sd->format;
if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
|| val != sd->format)
sound_perror ("Could not set sound 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->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;
ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
}
turn_on_atimers (1);
#ifdef SIGIO
sigunblock (sigmask (SIGIO));
#endif
}
static void
vox_close (sd)
struct sound_device *sd;
{
if (sd->fd >= 0)
{
#ifdef SIGIO
sigblock (sigmask (SIGIO));
#endif
turn_on_atimers (0);
ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
turn_on_atimers (1);
#ifdef SIGIO
sigunblock (sigmask (SIGIO));
#endif
emacs_close (sd->fd);
sd->fd = -1;
}
}
static void
vox_choose_format (sd, s)
struct sound_device *sd;
struct sound *s;
{
if (s->type == RIFF)
{
struct wav_header *h = (struct wav_header *) s->header;
if (h->precision == 8)
sd->format = AFMT_U8;
else if (h->precision == 16)
sd->format = AFMT_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:
case AU_ENCODING_IEEE32:
case AU_ENCODING_IEEE64:
sd->format = AFMT_MU_LAW;
break;
case AU_ENCODING_8:
case AU_ENCODING_16:
case AU_ENCODING_24:
case AU_ENCODING_32:
sd->format = AFMT_S16_LE;
break;
default:
error ("Unsupported AU file format");
}
}
else
abort ();
}
static int
vox_init (sd)
struct sound_device *sd;
{
char *file;
int fd;
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;
}
static void
vox_write (sd, buffer, nbytes)
struct sound_device *sd;
const char *buffer;
int nbytes;
{
int nwritten = emacs_write (sd->fd, buffer, nbytes);
if (nwritten < 0)
sound_perror ("Error writing to sound device");
}
#ifdef HAVE_ALSA
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;
};
static void
alsa_open (sd)
struct sound_device *sd;
{
char *file;
struct alsa_params *p;
int err;
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;
int fact = snd_pcm_format_size (sd->format, 1) * sd->channels;
return p->period_size * (fact > 0 ? fact : 1);
}
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);
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);
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);
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);
}
}
}
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);
}
}
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 ();
}
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;
int fact = snd_pcm_format_size (sd->format, 1) * sd->channels;
int nwritten = 0;
int err;
while (nwritten < nbytes)
{
snd_pcm_uframes_t frames = (nbytes - nwritten)/fact;
if (frames == 0) break;
err = snd_pcm_writei (p->handle, buffer + nwritten, frames);
if (err < 0)
{
if (err == -EPIPE)
{
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);
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;
{
}
static int
alsa_init (sd)
struct sound_device *sd;
{
char *file;
snd_pcm_t *handle;
int err;
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
#else
static int
do_play_sound (psz_file, 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;
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);
if (mci_error != 0)
{
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);
if (mm_result == MMSYSERR_NOERROR)
{
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.");
}
}
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.");
}
}
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);
if (mci_error != 0)
{
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);
if (b_reset_volume == TRUE)
{
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.");
}
}
return i_result;
}
#endif
DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
doc: )
(sound)
Lisp_Object sound;
{
Lisp_Object attrs[SOUND_ATTR_SENTINEL];
int count = SPECPDL_INDEX ();
#ifndef WINDOWSNT
Lisp_Object file;
struct gcpro gcpro1, gcpro2;
Lisp_Object args[2];
#else
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
if (!parse_sound (sound, attrs))
error ("Invalid sound specification");
#ifndef WINDOWSNT
file = Qnil;
GCPRO2 (sound, file);
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);
current_sound->header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
if (STRINGP (attrs[SOUND_FILE]))
{
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");
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
{
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_sound_type (current_sound);
if (STRINGP (attrs[SOUND_DEVICE]))
{
int len = SCHARS (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]))
current_sound_device->volume = XFASTINT (attrs[SOUND_VOLUME]);
else if (FLOATP (attrs[SOUND_VOLUME]))
current_sound_device->volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
args[0] = Qplay_sound_functions;
args[1] = sound;
Frun_hook_with_args (2, args);
#ifdef HAVE_ALSA
if (!alsa_init (current_sound_device))
#endif
if (!vox_init (current_sound_device))
error ("No usable sound device driver found");
current_sound_device->open (current_sound_device);
current_sound->play (current_sound, current_sound_device);
UNGCPRO;
#else
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]);
}
else if (FLOATP (attrs[SOUND_VOLUME]))
{
ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
}
if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
{
ui_volume = ui_volume_tmp * (UINT_MAX / 100);
}
i_result = do_play_sound (psz_file, ui_volume);
#endif
unbind_to (count, Qnil);
return Qnil;
}
void
syms_of_sound ()
{
QCdevice = intern (":device");
staticpro (&QCdevice);
QCvolume = intern (":volume");
staticpro (&QCvolume);
Qsound = intern ("sound");
staticpro (&Qsound);
Qplay_sound_functions = intern ("play-sound-functions");
staticpro (&Qplay_sound_functions);
defsubr (&Splay_sound_internal);
}
void
init_sound ()
{
}
#endif