1 /* sound.c -- sound support.
3 Copyright (C) 1998-1999, 2001-2013 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 3 of the License, or
10 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
21 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
24 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
25 implementation of the play-sound specification for Windows.
28 In the Windows implementation of play-sound-internal only the
29 :file and :volume keywords are supported. The :device keyword,
30 if present, is ignored. The :data keyword, if present, will
31 cause an error to be generated.
33 The Windows implementation of play-sound is implemented via the
34 Windows API functions mciSendString, waveOutGetVolume, and
35 waveOutSetVolume which are exported by Winmm.dll.
40 #if defined HAVE_SOUND
42 /* BEGIN: Common Includes */
45 #include <sys/types.h>
49 #include "dispextern.h"
51 #include "syssignal.h"
52 /* END: Common Includes */
55 /* BEGIN: Non Windows Includes */
58 #include <sys/ioctl.h>
60 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
61 sys/soundcard.h. So, let's try whatever's there. */
63 #ifdef HAVE_MACHINE_SOUNDCARD_H
64 #include <machine/soundcard.h>
66 #ifdef HAVE_SYS_SOUNDCARD_H
67 #include <sys/soundcard.h>
69 #ifdef HAVE_SOUNDCARD_H
70 #include <soundcard.h>
73 #ifdef ALSA_SUBDIR_INCLUDE
74 #include <alsa/asoundlib.h>
76 #include <asoundlib.h>
77 #endif /* ALSA_SUBDIR_INCLUDE */
78 #endif /* HAVE_ALSA */
80 /* END: Non Windows Includes */
84 /* BEGIN: Windows Specific Includes */
89 /* END: Windows Specific Includes */
91 #endif /* WINDOWSNT */
93 /* BEGIN: Common Definitions */
97 static Lisp_Object QCvolume
, QCdevice
;
98 static Lisp_Object Qsound
;
99 static Lisp_Object Qplay_sound_functions
;
101 /* Indices of attributes in a sound attributes vector. */
112 /* END: Common Definitions */
114 /* BEGIN: Non Windows Definitions */
117 /* Structure forward declarations. */
122 /* The file header of RIFF-WAVE files (*.wav). Files are always in
123 little-endian byte-order. */
129 u_int32_t chunk_type
;
130 u_int32_t chunk_format
;
131 u_int32_t chunk_length
;
134 u_int32_t sample_rate
;
135 u_int32_t bytes_per_second
;
136 u_int16_t sample_size
;
138 u_int32_t chunk_data
;
139 u_int32_t data_length
;
142 /* The file header of Sun adio files (*.au). Files are always in
143 big-endian byte-order. */
148 u_int32_t magic_number
;
150 /* Offset of data part from start of file. Minimum value is 24. */
151 u_int32_t data_offset
;
153 /* Size of data part, 0xffffffff if unknown. */
156 /* Data encoding format.
158 2 8-bit linear PCM (REF-PCM)
162 6 32-bit IEEE floating-point
163 7 64-bit IEEE floating-point
164 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
168 /* Number of samples per second. */
169 u_int32_t sample_rate
;
171 /* Number of interleaved channels. */
175 /* Maximum of all sound file headers sizes. */
177 #define MAX_SOUND_HEADER_BYTES \
178 max (sizeof (struct wav_header), sizeof (struct au_header))
180 /* Interface structure for sound devices. */
184 /* If a string, the name of the device; otherwise use a default. */
187 /* File descriptor of the device. */
190 /* Device-dependent format. */
193 /* Volume (0..100). Zero means unspecified. */
202 /* Bytes per second. */
205 /* 1 = mono, 2 = stereo, 0 = don't set. */
208 /* Open device SD. */
209 void (* open
) (struct sound_device
*sd
);
211 /* Close device SD. */
212 void (* close
) (struct sound_device
*sd
);
214 /* Configure SD according to device-dependent parameters. */
215 void (* configure
) (struct sound_device
*device
);
217 /* Choose a device-dependent format for outputting sound S. */
218 void (* choose_format
) (struct sound_device
*sd
,
221 /* Return a preferred data size in bytes to be sent to write (below)
222 each time. 2048 is used if this is NULL. */
223 ptrdiff_t (* period_size
) (struct sound_device
*sd
);
225 /* Write NYBTES bytes from BUFFER to device SD. */
226 void (* write
) (struct sound_device
*sd
, const char *buffer
,
229 /* A place for devices to store additional data. */
233 /* An enumerator for each supported sound file type. */
241 /* Interface structure for sound files. */
245 /* The type of the file. */
246 enum sound_type type
;
248 /* File descriptor of a sound file. */
251 /* Pointer to sound file header. This contains header_size bytes
252 read from the start of a sound file. */
255 /* Number of bytes read from sound file. This is always <=
256 MAX_SOUND_HEADER_BYTES. */
259 /* Sound data, if a string. */
262 /* Play sound file S on device SD. */
263 void (* play
) (struct sound
*s
, struct sound_device
*sd
);
266 /* These are set during `play-sound-internal' so that sound_cleanup has
269 static struct sound_device
*current_sound_device
;
270 static struct sound
*current_sound
;
272 /* Function prototypes. */
274 static void vox_write (struct sound_device
*, const char *, ptrdiff_t);
275 static bool wav_init (struct sound
*);
276 static void wav_play (struct sound
*, struct sound_device
*);
277 static bool au_init (struct sound
*);
278 static void au_play (struct sound
*, struct sound_device
*);
280 /* END: Non Windows Definitions */
281 #else /* WINDOWSNT */
283 /* BEGIN: Windows Specific Definitions */
284 static int do_play_sound (const char *, unsigned long);
286 END: Windows Specific Definitions */
287 #endif /* WINDOWSNT */
290 /***********************************************************************
292 ***********************************************************************/
294 /* BEGIN: Common functions */
296 /* Like perror, but signals an error. */
298 static _Noreturn
void
299 sound_perror (const char *msg
)
301 int saved_errno
= errno
;
307 sigemptyset (&unblocked
);
308 sigaddset (&unblocked
, SIGIO
);
309 pthread_sigmask (SIG_UNBLOCK
, &unblocked
, 0);
312 if (saved_errno
!= 0)
313 error ("%s: %s", msg
, strerror (saved_errno
));
319 /* Display a warning message. */
322 sound_warning (const char *msg
)
328 /* Parse sound specification SOUND, and fill ATTRS with what is
329 found. Value is non-zero if SOUND Is a valid sound specification.
330 A valid sound specification is a list starting with the symbol
331 `sound'. The rest of the list is a property list which may
332 contain the following key/value pairs:
336 FILE is the sound file to play. If it isn't an absolute name,
337 it's searched under `data-directory'.
341 DATA is a string containing sound data. Either :file or :data
342 may be present, but not both.
346 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
347 If not specified, a default device is used.
351 VOL must be an integer in the range [0, 100], or a float in the
355 parse_sound (Lisp_Object sound
, Lisp_Object
*attrs
)
357 /* SOUND must be a list starting with the symbol `sound'. */
358 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
361 sound
= XCDR (sound
);
362 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
363 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
364 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
365 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
368 /* File name or data must be specified. */
369 if (!STRINGP (attrs
[SOUND_FILE
])
370 && !STRINGP (attrs
[SOUND_DATA
]))
372 #else /* WINDOWSNT */
374 Data is not supported in Windows. Therefore a
375 File name MUST be supplied.
377 if (!STRINGP (attrs
[SOUND_FILE
]))
381 #endif /* WINDOWSNT */
383 /* Volume must be in the range 0..100 or unspecified. */
384 if (!NILP (attrs
[SOUND_VOLUME
]))
386 if (INTEGERP (attrs
[SOUND_VOLUME
]))
388 if (XINT (attrs
[SOUND_VOLUME
]) < 0
389 || XINT (attrs
[SOUND_VOLUME
]) > 100)
392 else if (FLOATP (attrs
[SOUND_VOLUME
]))
394 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
395 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
403 /* Device must be a string or unspecified. */
404 if (!NILP (attrs
[SOUND_DEVICE
])
405 && !STRINGP (attrs
[SOUND_DEVICE
]))
407 #endif /* WINDOWSNT */
409 Since device is ignored in Windows, it does not matter
415 /* END: Common functions */
417 /* BEGIN: Non Windows functions */
420 /* Return S's value as a string if S is a string, otherwise DEFAULT_VALUE. */
423 string_default (Lisp_Object s
, char const *default_value
)
425 return STRINGP (s
) ? SSDATA (s
) : default_value
;
429 /* Find out the type of the sound file whose file descriptor is FD.
430 S is the sound file structure to fill in. */
433 find_sound_type (struct sound
*s
)
435 if (!wav_init (s
) && !au_init (s
))
436 error ("Unknown sound format");
440 /* Function installed by play-sound-internal with record_unwind_protect. */
443 sound_cleanup (Lisp_Object arg
)
445 if (current_sound_device
->close
)
446 current_sound_device
->close (current_sound_device
);
447 if (current_sound
->fd
> 0)
448 emacs_close (current_sound
->fd
);
449 xfree (current_sound_device
);
450 xfree (current_sound
);
455 /***********************************************************************
456 Byte-order Conversion
457 ***********************************************************************/
459 /* Convert 32-bit value VALUE which is in little-endian byte-order
460 to host byte-order. */
463 le2hl (u_int32_t value
)
465 #ifdef WORDS_BIGENDIAN
466 unsigned char *p
= (unsigned char *) &value
;
467 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
473 /* Convert 16-bit value VALUE which is in little-endian byte-order
474 to host byte-order. */
477 le2hs (u_int16_t value
)
479 #ifdef WORDS_BIGENDIAN
480 unsigned char *p
= (unsigned char *) &value
;
481 value
= p
[0] + (p
[1] << 8);
487 /* Convert 32-bit value VALUE which is in big-endian byte-order
488 to host byte-order. */
491 be2hl (u_int32_t value
)
493 #ifndef WORDS_BIGENDIAN
494 unsigned char *p
= (unsigned char *) &value
;
495 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
501 #if 0 /* Currently not used. */
503 /* Convert 16-bit value VALUE which is in big-endian byte-order
504 to host byte-order. */
507 be2hs (u_int16_t value
)
509 #ifndef WORDS_BIGENDIAN
510 unsigned char *p
= (unsigned char *) &value
;
511 value
= p
[1] + (p
[0] << 8);
518 /***********************************************************************
520 ***********************************************************************/
522 /* Try to initialize sound file S from S->header. S->header
523 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
524 sound file. If the file is a WAV-format file, set up interface
525 functions in S and convert header fields to host byte-order.
526 Value is true if the file is a WAV file. */
529 wav_init (struct sound
*s
)
531 struct wav_header
*header
= (struct wav_header
*) s
->header
;
533 if (s
->header_size
< sizeof *header
534 || memcmp (s
->header
, "RIFF", 4) != 0)
537 /* WAV files are in little-endian order. Convert the header
538 if on a big-endian machine. */
539 header
->magic
= le2hl (header
->magic
);
540 header
->length
= le2hl (header
->length
);
541 header
->chunk_type
= le2hl (header
->chunk_type
);
542 header
->chunk_format
= le2hl (header
->chunk_format
);
543 header
->chunk_length
= le2hl (header
->chunk_length
);
544 header
->format
= le2hs (header
->format
);
545 header
->channels
= le2hs (header
->channels
);
546 header
->sample_rate
= le2hl (header
->sample_rate
);
547 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
548 header
->sample_size
= le2hs (header
->sample_size
);
549 header
->precision
= le2hs (header
->precision
);
550 header
->chunk_data
= le2hl (header
->chunk_data
);
551 header
->data_length
= le2hl (header
->data_length
);
553 /* Set up the interface functions for WAV. */
561 /* Play RIFF-WAVE audio file S on sound device SD. */
564 wav_play (struct sound
*s
, struct sound_device
*sd
)
566 struct wav_header
*header
= (struct wav_header
*) s
->header
;
568 /* Let the device choose a suitable device-dependent format
570 sd
->choose_format (sd
, s
);
572 /* Configure the device. */
573 sd
->sample_size
= header
->sample_size
;
574 sd
->sample_rate
= header
->sample_rate
;
575 sd
->bps
= header
->bytes_per_second
;
576 sd
->channels
= header
->channels
;
579 /* Copy sound data to the device. The WAV file specification is
580 actually more complex. This simple scheme worked with all WAV
581 files I found so far. If someone feels inclined to implement the
582 whole RIFF-WAVE spec, please do. */
583 if (STRINGP (s
->data
))
584 sd
->write (sd
, SSDATA (s
->data
) + sizeof *header
,
585 SBYTES (s
->data
) - sizeof *header
);
589 ptrdiff_t nbytes
= 0;
590 ptrdiff_t blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
591 ptrdiff_t data_left
= header
->data_length
;
593 buffer
= alloca (blksize
);
594 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
596 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
598 /* Don't play possible garbage at the end of file */
599 if (data_left
< nbytes
) nbytes
= data_left
;
601 sd
->write (sd
, buffer
, nbytes
);
605 sound_perror ("Error reading sound file");
610 /***********************************************************************
612 ***********************************************************************/
614 /* Sun audio file encodings. */
618 AU_ENCODING_ULAW_8
= 1,
626 AU_ENCODING_ALAW_8
= 27
630 /* Try to initialize sound file S from S->header. S->header
631 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
632 sound file. If the file is a AU-format file, set up interface
633 functions in S and convert header fields to host byte-order.
634 Value is true if the file is an AU file. */
637 au_init (struct sound
*s
)
639 struct au_header
*header
= (struct au_header
*) s
->header
;
641 if (s
->header_size
< sizeof *header
642 || memcmp (s
->header
, ".snd", 4) != 0)
645 header
->magic_number
= be2hl (header
->magic_number
);
646 header
->data_offset
= be2hl (header
->data_offset
);
647 header
->data_size
= be2hl (header
->data_size
);
648 header
->encoding
= be2hl (header
->encoding
);
649 header
->sample_rate
= be2hl (header
->sample_rate
);
650 header
->channels
= be2hl (header
->channels
);
652 /* Set up the interface functions for AU. */
660 /* Play Sun audio file S on sound device SD. */
663 au_play (struct sound
*s
, struct sound_device
*sd
)
665 struct au_header
*header
= (struct au_header
*) s
->header
;
668 sd
->sample_rate
= header
->sample_rate
;
670 sd
->channels
= header
->channels
;
671 sd
->choose_format (sd
, s
);
674 if (STRINGP (s
->data
))
675 sd
->write (sd
, SSDATA (s
->data
) + header
->data_offset
,
676 SBYTES (s
->data
) - header
->data_offset
);
679 ptrdiff_t blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
684 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
686 /* Copy sound data to the device. */
687 buffer
= alloca (blksize
);
688 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
689 sd
->write (sd
, buffer
, nbytes
);
692 sound_perror ("Error reading sound file");
697 /***********************************************************************
698 Voxware Driver Interface
699 ***********************************************************************/
701 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
702 has a compatible own driver aka Luigi's driver. */
705 /* Open device SD. If SD->file is a string, open that device,
706 otherwise use a default device name. */
709 vox_open (struct sound_device
*sd
)
711 /* Open the sound device (eg /dev/dsp). */
712 char const *file
= string_default (sd
->file
, DEFAULT_SOUND_DEVICE
);
713 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
719 /* Configure device SD from parameters in it. */
722 vox_configure (struct sound_device
*sd
)
729 eassert (sd
->fd
>= 0);
731 /* On GNU/Linux, it seems that the device driver doesn't like to be
732 interrupted by a signal. Block the ones we know to cause
736 sigemptyset (&blocked
);
737 sigaddset (&blocked
, SIGIO
);
738 pthread_sigmask (SIG_BLOCK
, &blocked
, 0);
742 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
743 || val
!= sd
->format
)
744 sound_perror ("Could not set sound format");
746 val
= sd
->channels
!= 1;
747 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
748 || val
!= (sd
->channels
!= 1))
749 sound_perror ("Could not set stereo/mono");
751 /* I think bps and sampling_rate are the same, but who knows.
752 Check this. and use SND_DSP_SPEED for both. */
753 if (sd
->sample_rate
> 0)
755 val
= sd
->sample_rate
;
756 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
757 sound_perror ("Could not set sound speed");
758 else if (val
!= sd
->sample_rate
)
759 sound_warning ("Could not set sample rate");
764 int volume
= sd
->volume
& 0xff;
765 volume
|= volume
<< 8;
766 /* This may fail if there is no mixer. Ignore the failure. */
767 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
772 pthread_sigmask (SIG_UNBLOCK
, &blocked
, 0);
777 /* Close device SD if it is open. */
780 vox_close (struct sound_device
*sd
)
784 /* On GNU/Linux, it seems that the device driver doesn't like to
785 be interrupted by a signal. Block the ones we know to cause
789 sigemptyset (&blocked
);
790 sigaddset (&blocked
, SIGIO
);
791 pthread_sigmask (SIG_BLOCK
, &blocked
, 0);
795 /* Flush sound data, and reset the device. */
796 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
800 pthread_sigmask (SIG_UNBLOCK
, &blocked
, 0);
803 /* Close the device. */
804 emacs_close (sd
->fd
);
810 /* Choose device-dependent format for device SD from sound file S. */
813 vox_choose_format (struct sound_device
*sd
, struct sound
*s
)
817 struct wav_header
*h
= (struct wav_header
*) s
->header
;
818 if (h
->precision
== 8)
819 sd
->format
= AFMT_U8
;
820 else if (h
->precision
== 16)
821 sd
->format
= AFMT_S16_LE
;
823 error ("Unsupported WAV file format");
825 else if (s
->type
== SUN_AUDIO
)
827 struct au_header
*header
= (struct au_header
*) s
->header
;
828 switch (header
->encoding
)
830 case AU_ENCODING_ULAW_8
:
831 case AU_ENCODING_IEEE32
:
832 case AU_ENCODING_IEEE64
:
833 sd
->format
= AFMT_MU_LAW
;
840 sd
->format
= AFMT_S16_LE
;
844 error ("Unsupported AU file format");
852 /* Initialize device SD. Set up the interface functions in the device
856 vox_init (struct sound_device
*sd
)
858 /* Open the sound device (eg /dev/dsp). */
859 char const *file
= string_default (sd
->file
, DEFAULT_SOUND_DEVICE
);
860 int fd
= emacs_open (file
, O_WRONLY
, 0);
868 sd
->close
= vox_close
;
869 sd
->configure
= vox_configure
;
870 sd
->choose_format
= vox_choose_format
;
871 sd
->write
= vox_write
;
872 sd
->period_size
= NULL
;
877 /* Write NBYTES bytes from BUFFER to device SD. */
880 vox_write (struct sound_device
*sd
, const char *buffer
, ptrdiff_t nbytes
)
882 if (emacs_write (sd
->fd
, buffer
, nbytes
) != nbytes
)
883 sound_perror ("Error writing to sound device");
887 /***********************************************************************
888 ALSA Driver Interface
889 ***********************************************************************/
891 /* This driver is available on GNU/Linux. */
893 #ifndef DEFAULT_ALSA_SOUND_DEVICE
894 #define DEFAULT_ALSA_SOUND_DEVICE "default"
897 static _Noreturn
void
898 alsa_sound_perror (const char *msg
, int err
)
900 error ("%s: %s", msg
, snd_strerror (err
));
906 snd_pcm_hw_params_t
*hwparams
;
907 snd_pcm_sw_params_t
*swparams
;
908 snd_pcm_uframes_t period_size
;
911 /* Open device SD. If SD->file is a string, open that device,
912 otherwise use a default device name. */
915 alsa_open (struct sound_device
*sd
)
917 /* Open the sound device. Default is "default". */
918 struct alsa_params
*p
= xmalloc (sizeof *p
);
919 char const *file
= string_default (sd
->file
, DEFAULT_ALSA_SOUND_DEVICE
);
930 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
932 alsa_sound_perror (file
, err
);
936 alsa_period_size (struct sound_device
*sd
)
938 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
939 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
940 return p
->period_size
* (fact
> 0 ? fact
: 1);
944 alsa_configure (struct sound_device
*sd
)
948 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
949 snd_pcm_uframes_t buffer_size
;
951 eassert (p
->handle
!= 0);
953 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
955 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
957 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
959 alsa_sound_perror ("Could not allocate software parameter structure", err
);
961 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
963 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
965 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
966 SND_PCM_ACCESS_RW_INTERLEAVED
);
968 alsa_sound_perror ("Could not set access type", err
);
971 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
973 alsa_sound_perror ("Could not set sound format", err
);
975 uval
= sd
->sample_rate
;
976 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
978 alsa_sound_perror ("Could not set sample rate", err
);
981 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
983 alsa_sound_perror ("Could not set channel count", err
);
985 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
987 alsa_sound_perror ("Could not set parameters", err
);
990 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
992 alsa_sound_perror ("Unable to get period size for playback", err
);
994 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
996 alsa_sound_perror ("Unable to get buffer size for playback", err
);
998 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1000 alsa_sound_perror ("Unable to determine current swparams for playback",
1003 /* Start the transfer when the buffer is almost full */
1004 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1005 (buffer_size
/ p
->period_size
)
1008 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1010 /* Allow the transfer when at least period_size samples can be processed */
1011 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1013 alsa_sound_perror ("Unable to set avail min for playback", err
);
1015 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1017 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1019 snd_pcm_hw_params_free (p
->hwparams
);
1021 snd_pcm_sw_params_free (p
->swparams
);
1024 err
= snd_pcm_prepare (p
->handle
);
1026 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1031 snd_mixer_t
*handle
;
1032 snd_mixer_elem_t
*e
;
1033 if (snd_mixer_open (&handle
, 0) >= 0)
1035 char const *file
= string_default (sd
->file
,
1036 DEFAULT_ALSA_SOUND_DEVICE
);
1037 if (snd_mixer_attach (handle
, file
) >= 0
1038 && snd_mixer_load (handle
) >= 0
1039 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1040 for (e
= snd_mixer_first_elem (handle
);
1042 e
= snd_mixer_elem_next (e
))
1044 if (snd_mixer_selem_has_playback_volume (e
))
1046 long pmin
, pmax
, vol
;
1047 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1048 vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1050 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1051 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1054 snd_mixer_close (handle
);
1060 /* Close device SD if it is open. */
1063 alsa_close (struct sound_device
*sd
)
1065 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1069 snd_pcm_hw_params_free (p
->hwparams
);
1071 snd_pcm_sw_params_free (p
->swparams
);
1074 snd_pcm_drain (p
->handle
);
1075 snd_pcm_close (p
->handle
);
1081 /* Choose device-dependent format for device SD from sound file S. */
1084 alsa_choose_format (struct sound_device
*sd
, struct sound
*s
)
1086 if (s
->type
== RIFF
)
1088 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1089 if (h
->precision
== 8)
1090 sd
->format
= SND_PCM_FORMAT_U8
;
1091 else if (h
->precision
== 16)
1092 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1094 error ("Unsupported WAV file format");
1096 else if (s
->type
== SUN_AUDIO
)
1098 struct au_header
*header
= (struct au_header
*) s
->header
;
1099 switch (header
->encoding
)
1101 case AU_ENCODING_ULAW_8
:
1102 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1104 case AU_ENCODING_ALAW_8
:
1105 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1107 case AU_ENCODING_IEEE32
:
1108 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1110 case AU_ENCODING_IEEE64
:
1111 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1114 sd
->format
= SND_PCM_FORMAT_S8
;
1116 case AU_ENCODING_16
:
1117 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1119 case AU_ENCODING_24
:
1120 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1122 case AU_ENCODING_32
:
1123 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1127 error ("Unsupported AU file format");
1135 /* Write NBYTES bytes from BUFFER to device SD. */
1138 alsa_write (struct sound_device
*sd
, const char *buffer
, ptrdiff_t nbytes
)
1140 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1142 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1143 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1144 ptrdiff_t nwritten
= 0;
1147 while (nwritten
< nbytes
)
1149 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1150 if (frames
== 0) break;
1152 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1157 err
= snd_pcm_prepare (p
->handle
);
1159 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1162 else if (err
== -ESTRPIPE
)
1164 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1165 sleep (1); /* wait until the suspend flag is released */
1168 err
= snd_pcm_prepare (p
->handle
);
1170 alsa_sound_perror ("Can't recover from suspend, "
1176 alsa_sound_perror ("Error writing to sound device", err
);
1180 nwritten
+= err
* fact
;
1185 snd_error_quiet (const char *file
, int line
, const char *function
, int err
,
1190 /* Initialize device SD. Set up the interface functions in the device
1194 alsa_init (struct sound_device
*sd
)
1196 /* Open the sound device. Default is "default". */
1197 char const *file
= string_default (sd
->file
, DEFAULT_ALSA_SOUND_DEVICE
);
1201 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1202 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1203 snd_lib_error_set_handler (NULL
);
1206 snd_pcm_close (handle
);
1209 sd
->open
= alsa_open
;
1210 sd
->close
= alsa_close
;
1211 sd
->configure
= alsa_configure
;
1212 sd
->choose_format
= alsa_choose_format
;
1213 sd
->write
= alsa_write
;
1214 sd
->period_size
= alsa_period_size
;
1219 #endif /* HAVE_ALSA */
1222 /* END: Non Windows functions */
1223 #else /* WINDOWSNT */
1225 /* BEGIN: Windows specific functions */
1227 #define SOUND_WARNING(fun, error, text) \
1230 char err_string[MAXERRORLENGTH]; \
1231 fun (error, err_string, sizeof (err_string)); \
1232 _snprintf (buf, sizeof (buf), "%s\nError: %s", \
1233 text, err_string); \
1234 sound_warning (buf); \
1238 do_play_sound (const char *psz_file
, unsigned long ui_volume
)
1241 MCIERROR mci_error
= 0;
1242 char sz_cmd_buf
[520] = {0};
1243 char sz_ret_buf
[520] = {0};
1244 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1245 unsigned long ui_volume_org
= 0;
1246 BOOL b_reset_volume
= FALSE
;
1248 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1249 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1250 sprintf (sz_cmd_buf
,
1251 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1253 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1256 SOUND_WARNING (mciGetErrorString
, mci_error
,
1257 "The open mciSendString command failed to open "
1258 "the specified sound file.");
1259 i_result
= (int) mci_error
;
1262 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1264 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1265 if (mm_result
== MMSYSERR_NOERROR
)
1267 b_reset_volume
= TRUE
;
1268 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1269 if (mm_result
!= MMSYSERR_NOERROR
)
1271 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1272 "waveOutSetVolume failed to set the volume level "
1273 "of the WAVE_MAPPER device.\n"
1274 "As a result, the user selected volume level will "
1280 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1281 "waveOutGetVolume failed to obtain the original "
1282 "volume level of the WAVE_MAPPER device.\n"
1283 "As a result, the user selected volume level will "
1287 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1288 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1289 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1290 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1293 SOUND_WARNING (mciGetErrorString
, mci_error
,
1294 "The play mciSendString command failed to play the "
1295 "opened sound file.");
1296 i_result
= (int) mci_error
;
1298 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1299 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1300 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1301 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1302 if (b_reset_volume
== TRUE
)
1304 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1305 if (mm_result
!= MMSYSERR_NOERROR
)
1307 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1308 "waveOutSetVolume failed to reset the original volume "
1309 "level of the WAVE_MAPPER device.");
1315 /* END: Windows specific functions */
1317 #endif /* WINDOWSNT */
1319 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1320 doc
: /* Play sound SOUND.
1322 Internal use only, use `play-sound' instead. */)
1325 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1326 ptrdiff_t count
= SPECPDL_INDEX ();
1330 struct gcpro gcpro1
, gcpro2
;
1331 Lisp_Object args
[2];
1332 #else /* WINDOWSNT */
1334 Lisp_Object lo_file
= {0};
1335 char * psz_file
= NULL
;
1336 unsigned long ui_volume_tmp
= UINT_MAX
;
1337 unsigned long ui_volume
= UINT_MAX
;
1338 #endif /* WINDOWSNT */
1340 /* Parse the sound specification. Give up if it is invalid. */
1341 if (!parse_sound (sound
, attrs
))
1342 error ("Invalid sound specification");
1346 GCPRO2 (sound
, file
);
1347 current_sound_device
= xzalloc (sizeof *current_sound_device
);
1348 current_sound
= xzalloc (sizeof *current_sound
);
1349 record_unwind_protect (sound_cleanup
, Qnil
);
1350 current_sound
->header
= alloca (MAX_SOUND_HEADER_BYTES
);
1352 if (STRINGP (attrs
[SOUND_FILE
]))
1354 /* Open the sound file. */
1355 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1356 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1357 if (current_sound
->fd
< 0)
1358 sound_perror ("Could not open sound file");
1360 /* Read the first bytes from the file. */
1361 current_sound
->header_size
1362 = emacs_read (current_sound
->fd
, current_sound
->header
,
1363 MAX_SOUND_HEADER_BYTES
);
1364 if (current_sound
->header_size
< 0)
1365 sound_perror ("Invalid sound file header");
1369 current_sound
->data
= attrs
[SOUND_DATA
];
1370 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1371 memcpy (current_sound
->header
, SDATA (current_sound
->data
),
1372 current_sound
->header_size
);
1375 /* Find out the type of sound. Give up if we can't tell. */
1376 find_sound_type (current_sound
);
1378 /* Set up a device. */
1379 current_sound_device
->file
= attrs
[SOUND_DEVICE
];
1381 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1382 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1383 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1384 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1386 args
[0] = Qplay_sound_functions
;
1388 Frun_hook_with_args (2, args
);
1391 if (!alsa_init (current_sound_device
))
1393 if (!vox_init (current_sound_device
))
1394 error ("No usable sound device driver found");
1396 /* Open the device. */
1397 current_sound_device
->open (current_sound_device
);
1399 /* Play the sound. */
1400 current_sound
->play (current_sound
, current_sound_device
);
1405 #else /* WINDOWSNT */
1407 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1408 len
= XSTRING (lo_file
)->size
;
1409 psz_file
= alloca (len
+ 1);
1410 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1411 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1413 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1415 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1417 ui_volume_tmp
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1420 Based on some experiments I have conducted, a value of 100 or less
1421 for the sound volume is much too low. You cannot even hear it.
1422 A value of UINT_MAX indicates that you wish for the sound to played
1423 at the maximum possible volume. A value of UINT_MAX/2 plays the
1424 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1425 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1426 The following code adjusts the user specified volume level appropriately.
1428 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1430 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1432 do_play_sound (psz_file
, ui_volume
);
1434 #endif /* WINDOWSNT */
1436 unbind_to (count
, Qnil
);
1440 /***********************************************************************
1442 ***********************************************************************/
1445 syms_of_sound (void)
1447 DEFSYM (QCdevice
, ":device");
1448 DEFSYM (QCvolume
, ":volume");
1449 DEFSYM (Qsound
, "sound");
1450 DEFSYM (Qplay_sound_functions
, "play-sound-functions");
1452 defsubr (&Splay_sound_internal
);
1455 #endif /* HAVE_SOUND */