1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
23 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
26 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
27 implementation of the play-sound specification for Windows.
30 In the Windows implementation of play-sound-internal only the
31 :file and :volume keywords are supported. The :device keyword,
32 if present, is ignored. The :data keyword, if present, will
33 cause an error to be generated.
35 The Windows implementation of play-sound is implemented via the
36 Win32 API functions mciSendString, waveOutGetVolume, and
37 waveOutSetVolume which are exported by Winmm.dll.
42 #if defined HAVE_SOUND
44 /* BEGIN: Common Includes */
47 #include <sys/types.h>
50 #include "dispextern.h"
53 #include "syssignal.h"
54 /* END: Common Includes */
57 /* BEGIN: Non Windows Includes */
61 #include <sys/ioctl.h>
64 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
65 sys/soundcard.h. So, let's try whatever's there. */
67 #ifdef HAVE_MACHINE_SOUNDCARD_H
68 #include <machine/soundcard.h>
70 #ifdef HAVE_SYS_SOUNDCARD_H
71 #include <sys/soundcard.h>
73 #ifdef HAVE_SOUNDCARD_H
74 #include <soundcard.h>
77 #include <asoundlib.h>
80 /* END: Non Windows Includes */
84 /* BEGIN: Windows Specific Includes */
91 /* END: Windows Specific Includes */
93 #endif /* WINDOWSNT */
95 /* BEGIN: Common Definitions */
96 #define abs(X) ((X) < 0 ? -(X) : (X))
100 extern Lisp_Object QCfile
, QCdata
;
101 Lisp_Object QCvolume
, QCdevice
;
103 Lisp_Object Qplay_sound_functions
;
105 /* Indices of attributes in a sound attributes vector. */
116 static void alsa_sound_perror
P_ ((char *, int)) NO_RETURN
;
117 static void sound_perror
P_ ((char *)) NO_RETURN
;
118 static void sound_warning
P_ ((char *));
119 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
121 /* END: Common Definitions */
123 /* BEGIN: Non Windows Definitions */
126 #ifndef DEFAULT_SOUND_DEVICE
127 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
129 #ifndef DEFAULT_ALSA_SOUND_DEVICE
130 #define DEFAULT_ALSA_SOUND_DEVICE "default"
134 /* Structure forward declarations. */
139 /* The file header of RIFF-WAVE files (*.wav). Files are always in
140 little-endian byte-order. */
146 u_int32_t chunk_type
;
147 u_int32_t chunk_format
;
148 u_int32_t chunk_length
;
151 u_int32_t sample_rate
;
152 u_int32_t bytes_per_second
;
153 u_int16_t sample_size
;
155 u_int32_t chunk_data
;
156 u_int32_t data_length
;
159 /* The file header of Sun adio files (*.au). Files are always in
160 big-endian byte-order. */
165 u_int32_t magic_number
;
167 /* Offset of data part from start of file. Minimum value is 24. */
168 u_int32_t data_offset
;
170 /* Size of data part, 0xffffffff if unknown. */
173 /* Data encoding format.
175 2 8-bit linear PCM (REF-PCM)
179 6 32-bit IEEE floating-point
180 7 64-bit IEEE floating-point
181 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
185 /* Number of samples per second. */
186 u_int32_t sample_rate
;
188 /* Number of interleaved channels. */
192 /* Maximum of all sound file headers sizes. */
194 #define MAX_SOUND_HEADER_BYTES \
195 max (sizeof (struct wav_header), sizeof (struct au_header))
197 /* Interface structure for sound devices. */
201 /* The name of the device or null meaning use a default device name. */
204 /* File descriptor of the device. */
207 /* Device-dependent format. */
210 /* Volume (0..100). Zero means unspecified. */
219 /* Bytes per second. */
222 /* 1 = mono, 2 = stereo, 0 = don't set. */
225 /* Open device SD. */
226 void (* open
) P_ ((struct sound_device
*sd
));
228 /* Close device SD. */
229 void (* close
) P_ ((struct sound_device
*sd
));
231 /* Configure SD accoring to device-dependent parameters. */
232 void (* configure
) P_ ((struct sound_device
*device
));
234 /* Choose a device-dependent format for outputting sound S. */
235 void (* choose_format
) P_ ((struct sound_device
*sd
,
238 /* Return a preferred data size in bytes to be sent to write (below)
239 each time. 2048 is used if this is NULL. */
240 int (* period_size
) P_ ((struct sound_device
*sd
));
242 /* Write NYBTES bytes from BUFFER to device SD. */
243 void (* write
) P_ ((struct sound_device
*sd
, const char *buffer
,
246 /* A place for devices to store additional data. */
250 /* An enumerator for each supported sound file type. */
258 /* Interface structure for sound files. */
262 /* The type of the file. */
263 enum sound_type type
;
265 /* File descriptor of a sound file. */
268 /* Pointer to sound file header. This contains header_size bytes
269 read from the start of a sound file. */
272 /* Number of bytes raed from sound file. This is always <=
273 MAX_SOUND_HEADER_BYTES. */
276 /* Sound data, if a string. */
279 /* Play sound file S on device SD. */
280 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
283 /* These are set during `play-sound-internal' so that sound_cleanup has
286 struct sound_device
*current_sound_device
;
287 struct sound
*current_sound
;
289 /* Function prototypes. */
291 static void vox_open
P_ ((struct sound_device
*));
292 static void vox_configure
P_ ((struct sound_device
*));
293 static void vox_close
P_ ((struct sound_device
*sd
));
294 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
295 static int vox_init
P_ ((struct sound_device
*));
296 static void vox_write
P_ ((struct sound_device
*, const char *, int));
297 static void find_sound_type
P_ ((struct sound
*));
298 static u_int32_t le2hl
P_ ((u_int32_t
));
299 static u_int16_t le2hs
P_ ((u_int16_t
));
300 static u_int32_t be2hl
P_ ((u_int32_t
));
301 static int wav_init
P_ ((struct sound
*));
302 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
303 static int au_init
P_ ((struct sound
*));
304 static void au_play
P_ ((struct sound
*, struct sound_device
*));
306 #if 0 /* Currently not used. */
307 static u_int16_t be2hs
P_ ((u_int16_t
));
310 /* END: Non Windows Definitions */
311 #else /* WINDOWSNT */
313 /* BEGIN: Windows Specific Definitions */
314 static int do_play_sound
P_ ((const char *, unsigned long));
316 END: Windows Specific Definitions */
317 #endif /* WINDOWSNT */
320 /***********************************************************************
322 ***********************************************************************/
324 /* BEGIN: Common functions */
326 /* Like perror, but signals an error. */
332 int saved_errno
= errno
;
336 sigunblock (sigmask (SIGIO
));
338 if (saved_errno
!= 0)
339 error ("%s: %s", msg
, strerror (saved_errno
));
345 /* Display a warning message. */
355 /* Parse sound specification SOUND, and fill ATTRS with what is
356 found. Value is non-zero if SOUND Is a valid sound specification.
357 A valid sound specification is a list starting with the symbol
358 `sound'. The rest of the list is a property list which may
359 contain the following key/value pairs:
363 FILE is the sound file to play. If it isn't an absolute name,
364 it's searched under `data-directory'.
368 DATA is a string containing sound data. Either :file or :data
369 may be present, but not both.
373 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
374 If not specified, a default device is used.
378 VOL must be an integer in the range [0, 100], or a float in the
382 parse_sound (sound
, attrs
)
386 /* SOUND must be a list starting with the symbol `sound'. */
387 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
390 sound
= XCDR (sound
);
391 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
392 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
393 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
394 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
397 /* File name or data must be specified. */
398 if (!STRINGP (attrs
[SOUND_FILE
])
399 && !STRINGP (attrs
[SOUND_DATA
]))
401 #else /* WINDOWSNT */
403 Data is not supported in Windows. Therefore a
404 File name MUST be supplied.
406 if (!STRINGP (attrs
[SOUND_FILE
]))
410 #endif /* WINDOWSNT */
412 /* Volume must be in the range 0..100 or unspecified. */
413 if (!NILP (attrs
[SOUND_VOLUME
]))
415 if (INTEGERP (attrs
[SOUND_VOLUME
]))
417 if (XINT (attrs
[SOUND_VOLUME
]) < 0
418 || XINT (attrs
[SOUND_VOLUME
]) > 100)
421 else if (FLOATP (attrs
[SOUND_VOLUME
]))
423 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
424 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
432 /* Device must be a string or unspecified. */
433 if (!NILP (attrs
[SOUND_DEVICE
])
434 && !STRINGP (attrs
[SOUND_DEVICE
]))
436 #endif /* WINDOWSNT */
438 Since device is ignored in Windows, it does not matter
444 /* END: Common functions */
446 /* BEGIN: Non Windows functions */
449 /* Find out the type of the sound file whose file descriptor is FD.
450 S is the sound file structure to fill in. */
456 if (!wav_init (s
) && !au_init (s
))
457 error ("Unknown sound format");
461 /* Function installed by play-sound-internal with record_unwind_protect. */
467 if (current_sound_device
->close
)
468 current_sound_device
->close (current_sound_device
);
469 if (current_sound
->fd
> 0)
470 emacs_close (current_sound
->fd
);
471 free (current_sound_device
);
472 free (current_sound
);
477 /***********************************************************************
478 Byte-order Conversion
479 ***********************************************************************/
481 /* Convert 32-bit value VALUE which is in little-endian byte-order
482 to host byte-order. */
488 #ifdef WORDS_BIG_ENDIAN
489 unsigned char *p
= (unsigned char *) &value
;
490 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
496 /* Convert 16-bit value VALUE which is in little-endian byte-order
497 to host byte-order. */
503 #ifdef WORDS_BIG_ENDIAN
504 unsigned char *p
= (unsigned char *) &value
;
505 value
= p
[0] + (p
[1] << 8);
511 /* Convert 32-bit value VALUE which is in big-endian byte-order
512 to host byte-order. */
518 #ifndef WORDS_BIG_ENDIAN
519 unsigned char *p
= (unsigned char *) &value
;
520 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
526 #if 0 /* Currently not used. */
528 /* Convert 16-bit value VALUE which is in big-endian byte-order
529 to host byte-order. */
535 #ifndef WORDS_BIG_ENDIAN
536 unsigned char *p
= (unsigned char *) &value
;
537 value
= p
[1] + (p
[0] << 8);
544 /***********************************************************************
546 ***********************************************************************/
548 /* Try to initialize sound file S from S->header. S->header
549 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
550 sound file. If the file is a WAV-format file, set up interface
551 functions in S and convert header fields to host byte-order.
552 Value is non-zero if the file is a WAV file. */
558 struct wav_header
*header
= (struct wav_header
*) s
->header
;
560 if (s
->header_size
< sizeof *header
561 || bcmp (s
->header
, "RIFF", 4) != 0)
564 /* WAV files are in little-endian order. Convert the header
565 if on a big-endian machine. */
566 header
->magic
= le2hl (header
->magic
);
567 header
->length
= le2hl (header
->length
);
568 header
->chunk_type
= le2hl (header
->chunk_type
);
569 header
->chunk_format
= le2hl (header
->chunk_format
);
570 header
->chunk_length
= le2hl (header
->chunk_length
);
571 header
->format
= le2hs (header
->format
);
572 header
->channels
= le2hs (header
->channels
);
573 header
->sample_rate
= le2hl (header
->sample_rate
);
574 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
575 header
->sample_size
= le2hs (header
->sample_size
);
576 header
->precision
= le2hs (header
->precision
);
577 header
->chunk_data
= le2hl (header
->chunk_data
);
578 header
->data_length
= le2hl (header
->data_length
);
580 /* Set up the interface functions for WAV. */
588 /* Play RIFF-WAVE audio file S on sound device SD. */
593 struct sound_device
*sd
;
595 struct wav_header
*header
= (struct wav_header
*) s
->header
;
597 /* Let the device choose a suitable device-dependent format
599 sd
->choose_format (sd
, s
);
601 /* Configure the device. */
602 sd
->sample_size
= header
->sample_size
;
603 sd
->sample_rate
= header
->sample_rate
;
604 sd
->bps
= header
->bytes_per_second
;
605 sd
->channels
= header
->channels
;
608 /* Copy sound data to the device. The WAV file specification is
609 actually more complex. This simple scheme worked with all WAV
610 files I found so far. If someone feels inclined to implement the
611 whole RIFF-WAVE spec, please do. */
612 if (STRINGP (s
->data
))
613 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
614 SBYTES (s
->data
) - sizeof *header
);
619 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
621 buffer
= (char *) alloca (blksize
);
622 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
624 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
625 sd
->write (sd
, buffer
, nbytes
);
628 sound_perror ("Error reading sound file");
633 /***********************************************************************
635 ***********************************************************************/
637 /* Sun audio file encodings. */
641 AU_ENCODING_ULAW_8
= 1,
649 AU_ENCODING_ALAW_8
= 27
653 /* Try to initialize sound file S from S->header. S->header
654 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
655 sound file. If the file is a AU-format file, set up interface
656 functions in S and convert header fields to host byte-order.
657 Value is non-zero if the file is an AU file. */
663 struct au_header
*header
= (struct au_header
*) s
->header
;
665 if (s
->header_size
< sizeof *header
666 || bcmp (s
->header
, ".snd", 4) != 0)
669 header
->magic_number
= be2hl (header
->magic_number
);
670 header
->data_offset
= be2hl (header
->data_offset
);
671 header
->data_size
= be2hl (header
->data_size
);
672 header
->encoding
= be2hl (header
->encoding
);
673 header
->sample_rate
= be2hl (header
->sample_rate
);
674 header
->channels
= be2hl (header
->channels
);
676 /* Set up the interface functions for AU. */
684 /* Play Sun audio file S on sound device SD. */
689 struct sound_device
*sd
;
691 struct au_header
*header
= (struct au_header
*) s
->header
;
694 sd
->sample_rate
= header
->sample_rate
;
696 sd
->channels
= header
->channels
;
697 sd
->choose_format (sd
, s
);
700 if (STRINGP (s
->data
))
701 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
702 SBYTES (s
->data
) - header
->data_offset
);
705 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
710 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
712 /* Copy sound data to the device. */
713 buffer
= (char *) alloca (blksize
);
714 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
715 sd
->write (sd
, buffer
, nbytes
);
718 sound_perror ("Error reading sound file");
723 /***********************************************************************
724 Voxware Driver Interface
725 ***********************************************************************/
727 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
728 has a compatible own driver aka Luigi's driver. */
731 /* Open device SD. If SD->file is non-null, open that device,
732 otherwise use a default device name. */
736 struct sound_device
*sd
;
740 /* Open the sound device. Default is /dev/dsp. */
744 file
= DEFAULT_SOUND_DEVICE
;
746 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
752 /* Configure device SD from parameters in it. */
756 struct sound_device
*sd
;
760 xassert (sd
->fd
>= 0);
762 /* On GNU/Linux, it seems that the device driver doesn't like to be
763 interrupted by a signal. Block the ones we know to cause
767 sigblock (sigmask (SIGIO
));
771 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
772 || val
!= sd
->format
)
773 sound_perror ("Could not set sound format");
775 val
= sd
->channels
!= 1;
776 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
777 || val
!= (sd
->channels
!= 1))
778 sound_perror ("Could not set stereo/mono");
780 /* I think bps and sampling_rate are the same, but who knows.
781 Check this. and use SND_DSP_SPEED for both. */
782 if (sd
->sample_rate
> 0)
784 val
= sd
->sample_rate
;
785 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
786 sound_perror ("Could not set sound speed");
787 else if (val
!= sd
->sample_rate
)
788 sound_warning ("Could not set sample rate");
793 int volume
= sd
->volume
& 0xff;
794 volume
|= volume
<< 8;
795 /* This may fail if there is no mixer. Ignore the failure. */
796 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
801 sigunblock (sigmask (SIGIO
));
806 /* Close device SD if it is open. */
810 struct sound_device
*sd
;
814 /* On GNU/Linux, it seems that the device driver doesn't like to
815 be interrupted by a signal. Block the ones we know to cause
818 sigblock (sigmask (SIGIO
));
822 /* Flush sound data, and reset the device. */
823 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
827 sigunblock (sigmask (SIGIO
));
830 /* Close the device. */
831 emacs_close (sd
->fd
);
837 /* Choose device-dependent format for device SD from sound file S. */
840 vox_choose_format (sd
, s
)
841 struct sound_device
*sd
;
846 struct wav_header
*h
= (struct wav_header
*) s
->header
;
847 if (h
->precision
== 8)
848 sd
->format
= AFMT_U8
;
849 else if (h
->precision
== 16)
850 sd
->format
= AFMT_S16_LE
;
852 error ("Unsupported WAV file format");
854 else if (s
->type
== SUN_AUDIO
)
856 struct au_header
*header
= (struct au_header
*) s
->header
;
857 switch (header
->encoding
)
859 case AU_ENCODING_ULAW_8
:
860 case AU_ENCODING_IEEE32
:
861 case AU_ENCODING_IEEE64
:
862 sd
->format
= AFMT_MU_LAW
;
869 sd
->format
= AFMT_S16_LE
;
873 error ("Unsupported AU file format");
881 /* Initialize device SD. Set up the interface functions in the device
886 struct sound_device
*sd
;
891 /* Open the sound device. Default is /dev/dsp. */
895 file
= DEFAULT_SOUND_DEVICE
;
896 fd
= emacs_open (file
, O_WRONLY
, 0);
904 sd
->close
= vox_close
;
905 sd
->configure
= vox_configure
;
906 sd
->choose_format
= vox_choose_format
;
907 sd
->write
= vox_write
;
908 sd
->period_size
= NULL
;
913 /* Write NBYTES bytes from BUFFER to device SD. */
916 vox_write (sd
, buffer
, nbytes
)
917 struct sound_device
*sd
;
921 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
923 sound_perror ("Error writing to sound device");
927 /***********************************************************************
928 ALSA Driver Interface
929 ***********************************************************************/
931 /* This driver is available on GNU/Linux. */
934 alsa_sound_perror (msg
, err
)
938 error ("%s: %s", msg
, snd_strerror (err
));
944 snd_pcm_hw_params_t
*hwparams
;
945 snd_pcm_sw_params_t
*swparams
;
946 snd_pcm_uframes_t period_size
;
949 /* Open device SD. If SD->file is non-null, open that device,
950 otherwise use a default device name. */
954 struct sound_device
*sd
;
957 struct alsa_params
*p
;
960 /* Open the sound device. Default is "default". */
964 file
= DEFAULT_ALSA_SOUND_DEVICE
;
966 p
= xmalloc (sizeof (*p
));
975 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
977 alsa_sound_perror (file
, err
);
981 alsa_period_size (sd
)
982 struct sound_device
*sd
;
984 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
985 return p
->period_size
;
990 struct sound_device
*sd
;
994 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
995 snd_pcm_uframes_t buffer_size
;
997 xassert (p
->handle
!= 0);
999 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
1001 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
1003 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
1005 alsa_sound_perror ("Could not allocate software parameter structure", err
);
1007 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
1009 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
1011 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
1012 SND_PCM_ACCESS_RW_INTERLEAVED
);
1014 alsa_sound_perror ("Could not set access type", err
);
1017 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
1019 alsa_sound_perror ("Could not set sound format", err
);
1021 uval
= sd
->sample_rate
;
1022 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
1024 alsa_sound_perror ("Could not set sample rate", err
);
1027 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
1029 alsa_sound_perror ("Could not set channel count", err
);
1031 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
1033 alsa_sound_perror ("Could not set parameters", err
);
1036 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
1038 alsa_sound_perror ("Unable to get period size for playback", err
);
1040 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
1042 alsa_sound_perror("Unable to get buffer size for playback", err
);
1044 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1046 alsa_sound_perror ("Unable to determine current swparams for playback",
1049 /* Start the transfer when the buffer is almost full */
1050 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1051 (buffer_size
/ p
->period_size
)
1054 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1056 /* Allow the transfer when at least period_size samples can be processed */
1057 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1059 alsa_sound_perror ("Unable to set avail min for playback", err
);
1061 /* Align all transfers to 1 period */
1062 err
= snd_pcm_sw_params_set_xfer_align (p
->handle
, p
->swparams
,
1065 alsa_sound_perror ("Unable to set transfer align for playback", err
);
1067 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1069 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1071 snd_pcm_hw_params_free (p
->hwparams
);
1073 snd_pcm_sw_params_free (p
->swparams
);
1076 err
= snd_pcm_prepare (p
->handle
);
1078 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1083 snd_mixer_t
*handle
;
1084 snd_mixer_elem_t
*e
;
1085 char *file
= sd
->file
? sd
->file
: DEFAULT_ALSA_SOUND_DEVICE
;
1087 if (snd_mixer_open (&handle
, 0) >= 0)
1089 if (snd_mixer_attach (handle
, file
) >= 0
1090 && snd_mixer_load (handle
) >= 0
1091 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1092 for (e
= snd_mixer_first_elem (handle
);
1094 e
= snd_mixer_elem_next (e
))
1096 if (snd_mixer_selem_has_playback_volume (e
))
1099 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1100 long vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1102 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1103 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1106 snd_mixer_close(handle
);
1112 /* Close device SD if it is open. */
1116 struct sound_device
*sd
;
1118 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1122 snd_pcm_hw_params_free (p
->hwparams
);
1124 snd_pcm_sw_params_free (p
->swparams
);
1127 snd_pcm_drain (p
->handle
);
1128 snd_pcm_close (p
->handle
);
1134 /* Choose device-dependent format for device SD from sound file S. */
1137 alsa_choose_format (sd
, s
)
1138 struct sound_device
*sd
;
1141 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1142 if (s
->type
== RIFF
)
1144 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1145 if (h
->precision
== 8)
1146 sd
->format
= SND_PCM_FORMAT_U8
;
1147 else if (h
->precision
== 16)
1148 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1150 error ("Unsupported WAV file format");
1152 else if (s
->type
== SUN_AUDIO
)
1154 struct au_header
*header
= (struct au_header
*) s
->header
;
1155 switch (header
->encoding
)
1157 case AU_ENCODING_ULAW_8
:
1158 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1160 case AU_ENCODING_ALAW_8
:
1161 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1163 case AU_ENCODING_IEEE32
:
1164 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1166 case AU_ENCODING_IEEE64
:
1167 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1170 sd
->format
= SND_PCM_FORMAT_S8
;
1172 case AU_ENCODING_16
:
1173 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1175 case AU_ENCODING_24
:
1176 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1178 case AU_ENCODING_32
:
1179 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1183 error ("Unsupported AU file format");
1191 /* Write NBYTES bytes from BUFFER to device SD. */
1194 alsa_write (sd
, buffer
, nbytes
)
1195 struct sound_device
*sd
;
1199 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1201 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1202 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1206 while (nwritten
< nbytes
)
1208 err
= snd_pcm_writei (p
->handle
,
1210 (nbytes
- nwritten
)/fact
);
1215 err
= snd_pcm_prepare (p
->handle
);
1217 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1220 else if (err
== -ESTRPIPE
)
1222 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1223 sleep(1); /* wait until the suspend flag is released */
1226 err
= snd_pcm_prepare (p
->handle
);
1228 alsa_sound_perror ("Can't recover from suspend, "
1234 alsa_sound_perror ("Error writing to sound device", err
);
1238 nwritten
+= err
* fact
;
1243 snd_error_quiet (file
, line
, function
, err
, fmt
)
1246 const char *function
;
1252 /* Initialize device SD. Set up the interface functions in the device
1257 struct sound_device
*sd
;
1263 /* Open the sound device. Default is "default". */
1267 file
= DEFAULT_ALSA_SOUND_DEVICE
;
1269 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1270 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1271 snd_lib_error_set_handler (NULL
);
1274 snd_pcm_close (handle
);
1277 sd
->open
= alsa_open
;
1278 sd
->close
= alsa_close
;
1279 sd
->configure
= alsa_configure
;
1280 sd
->choose_format
= alsa_choose_format
;
1281 sd
->write
= alsa_write
;
1282 sd
->period_size
= alsa_period_size
;
1287 #endif /* HAVE_ALSA */
1290 /* END: Non Windows functions */
1291 #else /* WINDOWSNT */
1293 /* BEGIN: Windows specific functions */
1296 do_play_sound (psz_file
, ui_volume
)
1297 const char *psz_file
;
1298 unsigned long ui_volume
;
1301 MCIERROR mci_error
= 0;
1302 char sz_cmd_buf
[520] = {0};
1303 char sz_ret_buf
[520] = {0};
1304 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1305 unsigned long ui_volume_org
= 0;
1306 BOOL b_reset_volume
= FALSE
;
1308 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
1309 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
1310 sprintf (sz_cmd_buf
,
1311 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1313 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
1316 sound_warning ("The open mciSendString command failed to open\n"
1317 "the specified sound file");
1318 i_result
= (int) mci_error
;
1321 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1323 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1324 if (mm_result
== MMSYSERR_NOERROR
)
1326 b_reset_volume
= TRUE
;
1327 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1328 if ( mm_result
!= MMSYSERR_NOERROR
)
1330 sound_warning ("waveOutSetVolume failed to set the volume level\n"
1331 "of the WAVE_MAPPER device.\n"
1332 "As a result, the user selected volume level will\n"
1338 sound_warning ("waveOutGetVolume failed to obtain the original\n"
1339 "volume level of the WAVE_MAPPER device.\n"
1340 "As a result, the user selected volume level will\n"
1344 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
1345 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
1346 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1347 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
1350 sound_warning ("The play mciSendString command failed to play the\n"
1351 "opened sound file.");
1352 i_result
= (int) mci_error
;
1354 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
1355 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
1356 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1357 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
1358 if (b_reset_volume
== TRUE
)
1360 mm_result
=waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1361 if (mm_result
!= MMSYSERR_NOERROR
)
1363 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
1364 "level of the WAVE_MAPPER device.");
1370 /* END: Windows specific functions */
1372 #endif /* WINDOWSNT */
1374 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1375 doc
: /* Play sound SOUND.
1377 Internal use only, use `play-sound' instead.\n */)
1381 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1382 int count
= SPECPDL_INDEX ();
1386 struct gcpro gcpro1
, gcpro2
;
1387 Lisp_Object args
[2];
1388 #else /* WINDOWSNT */
1390 Lisp_Object lo_file
= {0};
1391 char * psz_file
= NULL
;
1392 unsigned long ui_volume_tmp
= UINT_MAX
;
1393 unsigned long ui_volume
= UINT_MAX
;
1395 #endif /* WINDOWSNT */
1397 /* Parse the sound specification. Give up if it is invalid. */
1398 if (!parse_sound (sound
, attrs
))
1399 error ("Invalid sound specification");
1403 GCPRO2 (sound
, file
);
1404 current_sound_device
= (struct sound_device
*) xmalloc (sizeof (struct sound_device
));
1405 bzero (current_sound_device
, sizeof (struct sound_device
));
1406 current_sound
= (struct sound
*) xmalloc (sizeof (struct sound
));
1407 bzero (current_sound
, sizeof (struct sound
));
1408 record_unwind_protect (sound_cleanup
, Qnil
);
1409 current_sound
->header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1411 if (STRINGP (attrs
[SOUND_FILE
]))
1413 /* Open the sound file. */
1414 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1415 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1416 if (current_sound
->fd
< 0)
1417 sound_perror ("Could not open sound file");
1419 /* Read the first bytes from the file. */
1420 current_sound
->header_size
1421 = emacs_read (current_sound
->fd
, current_sound
->header
,
1422 MAX_SOUND_HEADER_BYTES
);
1423 if (current_sound
->header_size
< 0)
1424 sound_perror ("Invalid sound file header");
1428 current_sound
->data
= attrs
[SOUND_DATA
];
1429 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1430 bcopy (SDATA (current_sound
->data
), current_sound
->header
, current_sound
->header_size
);
1433 /* Find out the type of sound. Give up if we can't tell. */
1434 find_sound_type (current_sound
);
1436 /* Set up a device. */
1437 if (STRINGP (attrs
[SOUND_DEVICE
]))
1439 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1440 current_sound_device
->file
= (char *) alloca (len
+ 1);
1441 strcpy (current_sound_device
->file
, SDATA (attrs
[SOUND_DEVICE
]));
1444 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1445 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1446 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1447 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1449 args
[0] = Qplay_sound_functions
;
1451 Frun_hook_with_args (2, args
);
1454 if (!alsa_init (current_sound_device
))
1456 if (!vox_init (current_sound_device
))
1457 error ("No usable sound device driver found");
1459 /* Open the device. */
1460 current_sound_device
->open (current_sound_device
);
1462 /* Play the sound. */
1463 current_sound
->play (current_sound
, current_sound_device
);
1468 #else /* WINDOWSNT */
1470 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1471 len
= XSTRING (lo_file
)->size
;
1472 psz_file
= (char *) alloca (len
+ 1);
1473 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1474 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1476 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1478 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1480 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1483 Based on some experiments I have conducted, a value of 100 or less
1484 for the sound volume is much too low. You cannot even hear it.
1485 A value of UINT_MAX indicates that you wish for the sound to played
1486 at the maximum possible volume. A value of UINT_MAX/2 plays the
1487 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1488 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1489 The following code adjusts the user specified volume level appropriately.
1491 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1493 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1495 i_result
= do_play_sound (psz_file
, ui_volume
);
1497 #endif /* WINDOWSNT */
1499 unbind_to (count
, Qnil
);
1503 /***********************************************************************
1505 ***********************************************************************/
1510 QCdevice
= intern (":device");
1511 staticpro (&QCdevice
);
1512 QCvolume
= intern (":volume");
1513 staticpro (&QCvolume
);
1514 Qsound
= intern ("sound");
1515 staticpro (&Qsound
);
1516 Qplay_sound_functions
= intern ("play-sound-functions");
1517 staticpro (&Qplay_sound_functions
);
1519 defsubr (&Splay_sound_internal
);
1528 #endif /* HAVE_SOUND */
1530 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1531 (do not change this comment) */