1 /* sound.c -- sound support.
3 Copyright (C) 1998-1999, 2001-2014 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 */
60 #include <sys/ioctl.h>
62 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
63 sys/soundcard.h. So, let's try whatever's there. */
65 #ifdef HAVE_MACHINE_SOUNDCARD_H
66 #include <machine/soundcard.h>
68 #ifdef HAVE_SYS_SOUNDCARD_H
69 #include <sys/soundcard.h>
71 #ifdef HAVE_SOUNDCARD_H
72 #include <soundcard.h>
75 #ifdef ALSA_SUBDIR_INCLUDE
76 #include <alsa/asoundlib.h>
78 #include <asoundlib.h>
79 #endif /* ALSA_SUBDIR_INCLUDE */
80 #endif /* HAVE_ALSA */
82 /* END: Non Windows Includes */
86 /* BEGIN: Windows Specific Includes */
91 /* END: Windows Specific Includes */
93 #endif /* WINDOWSNT */
95 /* BEGIN: Common Definitions */
99 static Lisp_Object QCvolume
, QCdevice
;
100 static Lisp_Object Qsound
;
101 static Lisp_Object Qplay_sound_functions
;
103 /* Indices of attributes in a sound attributes vector. */
114 /* END: Common Definitions */
116 /* BEGIN: Non Windows Definitions */
119 /* Structure forward declarations. */
124 /* The file header of RIFF-WAVE files (*.wav). Files are always in
125 little-endian byte-order. */
131 u_int32_t chunk_type
;
132 u_int32_t chunk_format
;
133 u_int32_t chunk_length
;
136 u_int32_t sample_rate
;
137 u_int32_t bytes_per_second
;
138 u_int16_t sample_size
;
140 u_int32_t chunk_data
;
141 u_int32_t data_length
;
144 /* The file header of Sun adio files (*.au). Files are always in
145 big-endian byte-order. */
150 u_int32_t magic_number
;
152 /* Offset of data part from start of file. Minimum value is 24. */
153 u_int32_t data_offset
;
155 /* Size of data part, 0xffffffff if unknown. */
158 /* Data encoding format.
160 2 8-bit linear PCM (REF-PCM)
164 6 32-bit IEEE floating-point
165 7 64-bit IEEE floating-point
166 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
170 /* Number of samples per second. */
171 u_int32_t sample_rate
;
173 /* Number of interleaved channels. */
177 /* Maximum of all sound file headers sizes. */
179 #define MAX_SOUND_HEADER_BYTES \
180 max (sizeof (struct wav_header), sizeof (struct au_header))
182 /* Interface structure for sound devices. */
186 /* If a string, the name of the device; otherwise use a default. */
189 /* File descriptor of the device. */
192 /* Device-dependent format. */
195 /* Volume (0..100). Zero means unspecified. */
204 /* Bytes per second. */
207 /* 1 = mono, 2 = stereo, 0 = don't set. */
210 /* Open device SD. */
211 void (* open
) (struct sound_device
*sd
);
213 /* Close device SD. */
214 void (* close
) (struct sound_device
*sd
);
216 /* Configure SD according to device-dependent parameters. */
217 void (* configure
) (struct sound_device
*device
);
219 /* Choose a device-dependent format for outputting sound S. */
220 void (* choose_format
) (struct sound_device
*sd
,
223 /* Return a preferred data size in bytes to be sent to write (below)
224 each time. 2048 is used if this is NULL. */
225 ptrdiff_t (* period_size
) (struct sound_device
*sd
);
227 /* Write NYBTES bytes from BUFFER to device SD. */
228 void (* write
) (struct sound_device
*sd
, const char *buffer
,
231 /* A place for devices to store additional data. */
235 /* An enumerator for each supported sound file type. */
243 /* Interface structure for sound files. */
247 /* The type of the file. */
248 enum sound_type type
;
250 /* File descriptor of a sound file. */
253 /* Pointer to sound file header. This contains header_size bytes
254 read from the start of a sound file. */
257 /* Number of bytes read from sound file. This is always <=
258 MAX_SOUND_HEADER_BYTES. */
261 /* Sound data, if a string. */
264 /* Play sound file S on device SD. */
265 void (* play
) (struct sound
*s
, struct sound_device
*sd
);
268 /* These are set during `play-sound-internal' so that sound_cleanup has
271 static struct sound_device
*current_sound_device
;
272 static struct sound
*current_sound
;
274 /* Function prototypes. */
276 static void vox_write (struct sound_device
*, const char *, ptrdiff_t);
277 static bool wav_init (struct sound
*);
278 static void wav_play (struct sound
*, struct sound_device
*);
279 static bool au_init (struct sound
*);
280 static void au_play (struct sound
*, struct sound_device
*);
282 /* END: Non Windows Definitions */
283 #else /* WINDOWSNT */
285 /* BEGIN: Windows Specific Definitions */
286 static int do_play_sound (const char *, unsigned long);
288 END: Windows Specific Definitions */
289 #endif /* WINDOWSNT */
292 /***********************************************************************
294 ***********************************************************************/
296 /* BEGIN: Common functions */
298 /* Like perror, but signals an error. */
300 static _Noreturn
void
301 sound_perror (const char *msg
)
303 int saved_errno
= errno
;
309 sigemptyset (&unblocked
);
310 sigaddset (&unblocked
, SIGIO
);
311 pthread_sigmask (SIG_UNBLOCK
, &unblocked
, 0);
314 if (saved_errno
!= 0)
315 error ("%s: %s", msg
, strerror (saved_errno
));
321 /* Display a warning message. */
324 sound_warning (const char *msg
)
330 /* Parse sound specification SOUND, and fill ATTRS with what is
331 found. Value is non-zero if SOUND Is a valid sound specification.
332 A valid sound specification is a list starting with the symbol
333 `sound'. The rest of the list is a property list which may
334 contain the following key/value pairs:
338 FILE is the sound file to play. If it isn't an absolute name,
339 it's searched under `data-directory'.
343 DATA is a string containing sound data. Either :file or :data
344 may be present, but not both.
348 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
349 If not specified, a default device is used.
353 VOL must be an integer in the range [0, 100], or a float in the
357 parse_sound (Lisp_Object sound
, Lisp_Object
*attrs
)
359 /* SOUND must be a list starting with the symbol `sound'. */
360 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
363 sound
= XCDR (sound
);
364 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
365 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
366 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
367 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
370 /* File name or data must be specified. */
371 if (!STRINGP (attrs
[SOUND_FILE
])
372 && !STRINGP (attrs
[SOUND_DATA
]))
374 #else /* WINDOWSNT */
376 Data is not supported in Windows. Therefore a
377 File name MUST be supplied.
379 if (!STRINGP (attrs
[SOUND_FILE
]))
383 #endif /* WINDOWSNT */
385 /* Volume must be in the range 0..100 or unspecified. */
386 if (!NILP (attrs
[SOUND_VOLUME
]))
388 if (INTEGERP (attrs
[SOUND_VOLUME
]))
390 if (XINT (attrs
[SOUND_VOLUME
]) < 0
391 || XINT (attrs
[SOUND_VOLUME
]) > 100)
394 else if (FLOATP (attrs
[SOUND_VOLUME
]))
396 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
397 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
405 /* Device must be a string or unspecified. */
406 if (!NILP (attrs
[SOUND_DEVICE
])
407 && !STRINGP (attrs
[SOUND_DEVICE
]))
409 #endif /* WINDOWSNT */
411 Since device is ignored in Windows, it does not matter
417 /* END: Common functions */
419 /* BEGIN: Non Windows functions */
422 /* Return S's value as a string if S is a string, otherwise DEFAULT_VALUE. */
425 string_default (Lisp_Object s
, char const *default_value
)
427 return STRINGP (s
) ? SSDATA (s
) : default_value
;
431 /* Find out the type of the sound file whose file descriptor is FD.
432 S is the sound file structure to fill in. */
435 find_sound_type (struct sound
*s
)
437 if (!wav_init (s
) && !au_init (s
))
438 error ("Unknown sound format");
442 /* Function installed by play-sound-internal with record_unwind_protect_void. */
447 if (current_sound_device
->close
)
448 current_sound_device
->close (current_sound_device
);
449 if (current_sound
->fd
> 0)
450 emacs_close (current_sound
->fd
);
451 xfree (current_sound_device
);
452 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 value
= bswap_32 (value
);
472 /* Convert 16-bit value VALUE which is in little-endian byte-order
473 to host byte-order. */
476 le2hs (u_int16_t value
)
478 #ifdef WORDS_BIGENDIAN
479 value
= bswap_16 (value
);
485 /* Convert 32-bit value VALUE which is in big-endian byte-order
486 to host byte-order. */
489 be2hl (u_int32_t value
)
491 #ifndef WORDS_BIGENDIAN
492 value
= bswap_32 (value
);
497 /***********************************************************************
499 ***********************************************************************/
501 /* Try to initialize sound file S from S->header. S->header
502 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
503 sound file. If the file is a WAV-format file, set up interface
504 functions in S and convert header fields to host byte-order.
505 Value is true if the file is a WAV file. */
508 wav_init (struct sound
*s
)
510 struct wav_header
*header
= (struct wav_header
*) s
->header
;
512 if (s
->header_size
< sizeof *header
513 || memcmp (s
->header
, "RIFF", 4) != 0)
516 /* WAV files are in little-endian order. Convert the header
517 if on a big-endian machine. */
518 header
->magic
= le2hl (header
->magic
);
519 header
->length
= le2hl (header
->length
);
520 header
->chunk_type
= le2hl (header
->chunk_type
);
521 header
->chunk_format
= le2hl (header
->chunk_format
);
522 header
->chunk_length
= le2hl (header
->chunk_length
);
523 header
->format
= le2hs (header
->format
);
524 header
->channels
= le2hs (header
->channels
);
525 header
->sample_rate
= le2hl (header
->sample_rate
);
526 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
527 header
->sample_size
= le2hs (header
->sample_size
);
528 header
->precision
= le2hs (header
->precision
);
529 header
->chunk_data
= le2hl (header
->chunk_data
);
530 header
->data_length
= le2hl (header
->data_length
);
532 /* Set up the interface functions for WAV. */
540 /* Play RIFF-WAVE audio file S on sound device SD. */
543 wav_play (struct sound
*s
, struct sound_device
*sd
)
545 struct wav_header
*header
= (struct wav_header
*) s
->header
;
547 /* Let the device choose a suitable device-dependent format
549 sd
->choose_format (sd
, s
);
551 /* Configure the device. */
552 sd
->sample_size
= header
->sample_size
;
553 sd
->sample_rate
= header
->sample_rate
;
554 sd
->bps
= header
->bytes_per_second
;
555 sd
->channels
= header
->channels
;
558 /* Copy sound data to the device. The WAV file specification is
559 actually more complex. This simple scheme worked with all WAV
560 files I found so far. If someone feels inclined to implement the
561 whole RIFF-WAVE spec, please do. */
562 if (STRINGP (s
->data
))
563 sd
->write (sd
, SSDATA (s
->data
) + sizeof *header
,
564 SBYTES (s
->data
) - sizeof *header
);
568 ptrdiff_t nbytes
= 0;
569 ptrdiff_t blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
570 ptrdiff_t data_left
= header
->data_length
;
572 buffer
= alloca (blksize
);
573 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
575 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
577 /* Don't play possible garbage at the end of file */
578 if (data_left
< nbytes
) nbytes
= data_left
;
580 sd
->write (sd
, buffer
, nbytes
);
584 sound_perror ("Error reading sound file");
589 /***********************************************************************
591 ***********************************************************************/
593 /* Sun audio file encodings. */
597 AU_ENCODING_ULAW_8
= 1,
605 AU_ENCODING_ALAW_8
= 27
609 /* Try to initialize sound file S from S->header. S->header
610 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
611 sound file. If the file is a AU-format file, set up interface
612 functions in S and convert header fields to host byte-order.
613 Value is true if the file is an AU file. */
616 au_init (struct sound
*s
)
618 struct au_header
*header
= (struct au_header
*) s
->header
;
620 if (s
->header_size
< sizeof *header
621 || memcmp (s
->header
, ".snd", 4) != 0)
624 header
->magic_number
= be2hl (header
->magic_number
);
625 header
->data_offset
= be2hl (header
->data_offset
);
626 header
->data_size
= be2hl (header
->data_size
);
627 header
->encoding
= be2hl (header
->encoding
);
628 header
->sample_rate
= be2hl (header
->sample_rate
);
629 header
->channels
= be2hl (header
->channels
);
631 /* Set up the interface functions for AU. */
639 /* Play Sun audio file S on sound device SD. */
642 au_play (struct sound
*s
, struct sound_device
*sd
)
644 struct au_header
*header
= (struct au_header
*) s
->header
;
647 sd
->sample_rate
= header
->sample_rate
;
649 sd
->channels
= header
->channels
;
650 sd
->choose_format (sd
, s
);
653 if (STRINGP (s
->data
))
654 sd
->write (sd
, SSDATA (s
->data
) + header
->data_offset
,
655 SBYTES (s
->data
) - header
->data_offset
);
658 ptrdiff_t blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
663 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
665 /* Copy sound data to the device. */
666 buffer
= alloca (blksize
);
667 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
668 sd
->write (sd
, buffer
, nbytes
);
671 sound_perror ("Error reading sound file");
676 /***********************************************************************
677 Voxware Driver Interface
678 ***********************************************************************/
680 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
681 has a compatible own driver aka Luigi's driver. */
684 /* Open device SD. If SD->file is a string, open that device,
685 otherwise use a default device name. */
688 vox_open (struct sound_device
*sd
)
690 /* Open the sound device (eg /dev/dsp). */
691 char const *file
= string_default (sd
->file
, DEFAULT_SOUND_DEVICE
);
692 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
698 /* Configure device SD from parameters in it. */
701 vox_configure (struct sound_device
*sd
)
705 sigset_t oldset
, blocked
;
708 eassert (sd
->fd
>= 0);
710 /* On GNU/Linux, it seems that the device driver doesn't like to be
711 interrupted by a signal. Block the ones we know to cause
715 sigemptyset (&blocked
);
716 sigaddset (&blocked
, SIGIO
);
717 pthread_sigmask (SIG_BLOCK
, &blocked
, &oldset
);
721 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
722 || val
!= sd
->format
)
723 sound_perror ("Could not set sound format");
725 val
= sd
->channels
!= 1;
726 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
727 || val
!= (sd
->channels
!= 1))
728 sound_perror ("Could not set stereo/mono");
730 /* I think bps and sampling_rate are the same, but who knows.
731 Check this. and use SND_DSP_SPEED for both. */
732 if (sd
->sample_rate
> 0)
734 val
= sd
->sample_rate
;
735 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
736 sound_perror ("Could not set sound speed");
737 else if (val
!= sd
->sample_rate
)
738 sound_warning ("Could not set sample rate");
743 int volume
= sd
->volume
& 0xff;
744 volume
|= volume
<< 8;
745 /* This may fail if there is no mixer. Ignore the failure. */
746 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
751 pthread_sigmask (SIG_SETMASK
, &oldset
, 0);
756 /* Close device SD if it is open. */
759 vox_close (struct sound_device
*sd
)
763 /* On GNU/Linux, it seems that the device driver doesn't like to
764 be interrupted by a signal. Block the ones we know to cause
767 sigset_t blocked
, oldset
;
768 sigemptyset (&blocked
);
769 sigaddset (&blocked
, SIGIO
);
770 pthread_sigmask (SIG_BLOCK
, &blocked
, &oldset
);
774 /* Flush sound data, and reset the device. */
775 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
779 pthread_sigmask (SIG_SETMASK
, &oldset
, 0);
782 /* Close the device. */
783 emacs_close (sd
->fd
);
789 /* Choose device-dependent format for device SD from sound file S. */
792 vox_choose_format (struct sound_device
*sd
, struct sound
*s
)
796 struct wav_header
*h
= (struct wav_header
*) s
->header
;
797 if (h
->precision
== 8)
798 sd
->format
= AFMT_U8
;
799 else if (h
->precision
== 16)
800 sd
->format
= AFMT_S16_LE
;
802 error ("Unsupported WAV file format");
804 else if (s
->type
== SUN_AUDIO
)
806 struct au_header
*header
= (struct au_header
*) s
->header
;
807 switch (header
->encoding
)
809 case AU_ENCODING_ULAW_8
:
810 case AU_ENCODING_IEEE32
:
811 case AU_ENCODING_IEEE64
:
812 sd
->format
= AFMT_MU_LAW
;
819 sd
->format
= AFMT_S16_LE
;
823 error ("Unsupported AU file format");
831 /* Initialize device SD. Set up the interface functions in the device
835 vox_init (struct sound_device
*sd
)
837 /* Open the sound device (eg /dev/dsp). */
838 char const *file
= string_default (sd
->file
, DEFAULT_SOUND_DEVICE
);
839 int fd
= emacs_open (file
, O_WRONLY
, 0);
847 sd
->close
= vox_close
;
848 sd
->configure
= vox_configure
;
849 sd
->choose_format
= vox_choose_format
;
850 sd
->write
= vox_write
;
851 sd
->period_size
= NULL
;
856 /* Write NBYTES bytes from BUFFER to device SD. */
859 vox_write (struct sound_device
*sd
, const char *buffer
, ptrdiff_t nbytes
)
861 if (emacs_write_sig (sd
->fd
, buffer
, nbytes
) != nbytes
)
862 sound_perror ("Error writing to sound device");
866 /***********************************************************************
867 ALSA Driver Interface
868 ***********************************************************************/
870 /* This driver is available on GNU/Linux. */
872 #ifndef DEFAULT_ALSA_SOUND_DEVICE
873 #define DEFAULT_ALSA_SOUND_DEVICE "default"
876 static _Noreturn
void
877 alsa_sound_perror (const char *msg
, int err
)
879 error ("%s: %s", msg
, snd_strerror (err
));
885 snd_pcm_hw_params_t
*hwparams
;
886 snd_pcm_sw_params_t
*swparams
;
887 snd_pcm_uframes_t period_size
;
890 /* Open device SD. If SD->file is a string, open that device,
891 otherwise use a default device name. */
894 alsa_open (struct sound_device
*sd
)
896 /* Open the sound device. Default is "default". */
897 struct alsa_params
*p
= xmalloc (sizeof *p
);
898 char const *file
= string_default (sd
->file
, DEFAULT_ALSA_SOUND_DEVICE
);
909 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
911 alsa_sound_perror (file
, err
);
915 alsa_period_size (struct sound_device
*sd
)
917 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
918 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
919 return p
->period_size
* (fact
> 0 ? fact
: 1);
923 alsa_configure (struct sound_device
*sd
)
927 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
928 snd_pcm_uframes_t buffer_size
;
930 eassert (p
->handle
!= 0);
932 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
934 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
936 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
938 alsa_sound_perror ("Could not allocate software parameter structure", err
);
940 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
942 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
944 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
945 SND_PCM_ACCESS_RW_INTERLEAVED
);
947 alsa_sound_perror ("Could not set access type", err
);
950 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
952 alsa_sound_perror ("Could not set sound format", err
);
954 uval
= sd
->sample_rate
;
955 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
957 alsa_sound_perror ("Could not set sample rate", err
);
960 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
962 alsa_sound_perror ("Could not set channel count", err
);
964 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
966 alsa_sound_perror ("Could not set parameters", err
);
969 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
971 alsa_sound_perror ("Unable to get period size for playback", err
);
973 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
975 alsa_sound_perror ("Unable to get buffer size for playback", err
);
977 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
979 alsa_sound_perror ("Unable to determine current swparams for playback",
982 /* Start the transfer when the buffer is almost full */
983 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
984 (buffer_size
/ p
->period_size
)
987 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
989 /* Allow the transfer when at least period_size samples can be processed */
990 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
992 alsa_sound_perror ("Unable to set avail min for playback", err
);
994 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
996 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
998 snd_pcm_hw_params_free (p
->hwparams
);
1000 snd_pcm_sw_params_free (p
->swparams
);
1003 err
= snd_pcm_prepare (p
->handle
);
1005 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1010 snd_mixer_t
*handle
;
1011 snd_mixer_elem_t
*e
;
1012 if (snd_mixer_open (&handle
, 0) >= 0)
1014 char const *file
= string_default (sd
->file
,
1015 DEFAULT_ALSA_SOUND_DEVICE
);
1016 if (snd_mixer_attach (handle
, file
) >= 0
1017 && snd_mixer_load (handle
) >= 0
1018 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1019 for (e
= snd_mixer_first_elem (handle
);
1021 e
= snd_mixer_elem_next (e
))
1023 if (snd_mixer_selem_has_playback_volume (e
))
1025 long pmin
, pmax
, vol
;
1026 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1027 vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1029 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1030 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1033 snd_mixer_close (handle
);
1039 /* Close device SD if it is open. */
1042 alsa_close (struct sound_device
*sd
)
1044 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1048 snd_pcm_hw_params_free (p
->hwparams
);
1050 snd_pcm_sw_params_free (p
->swparams
);
1053 snd_pcm_drain (p
->handle
);
1054 snd_pcm_close (p
->handle
);
1060 /* Choose device-dependent format for device SD from sound file S. */
1063 alsa_choose_format (struct sound_device
*sd
, struct sound
*s
)
1065 if (s
->type
== RIFF
)
1067 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1068 if (h
->precision
== 8)
1069 sd
->format
= SND_PCM_FORMAT_U8
;
1070 else if (h
->precision
== 16)
1071 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1073 error ("Unsupported WAV file format");
1075 else if (s
->type
== SUN_AUDIO
)
1077 struct au_header
*header
= (struct au_header
*) s
->header
;
1078 switch (header
->encoding
)
1080 case AU_ENCODING_ULAW_8
:
1081 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1083 case AU_ENCODING_ALAW_8
:
1084 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1086 case AU_ENCODING_IEEE32
:
1087 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1089 case AU_ENCODING_IEEE64
:
1090 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1093 sd
->format
= SND_PCM_FORMAT_S8
;
1095 case AU_ENCODING_16
:
1096 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1098 case AU_ENCODING_24
:
1099 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1101 case AU_ENCODING_32
:
1102 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1106 error ("Unsupported AU file format");
1114 /* Write NBYTES bytes from BUFFER to device SD. */
1117 alsa_write (struct sound_device
*sd
, const char *buffer
, ptrdiff_t nbytes
)
1119 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1121 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1122 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1123 ptrdiff_t nwritten
= 0;
1126 while (nwritten
< nbytes
)
1128 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1129 if (frames
== 0) break;
1131 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1136 err
= snd_pcm_prepare (p
->handle
);
1138 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1141 else if (err
== -ESTRPIPE
)
1143 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1144 sleep (1); /* wait until the suspend flag is released */
1147 err
= snd_pcm_prepare (p
->handle
);
1149 alsa_sound_perror ("Can't recover from suspend, "
1155 alsa_sound_perror ("Error writing to sound device", err
);
1159 nwritten
+= err
* fact
;
1164 snd_error_quiet (const char *file
, int line
, const char *function
, int err
,
1169 /* Initialize device SD. Set up the interface functions in the device
1173 alsa_init (struct sound_device
*sd
)
1175 /* Open the sound device. Default is "default". */
1176 char const *file
= string_default (sd
->file
, DEFAULT_ALSA_SOUND_DEVICE
);
1180 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1181 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1182 snd_lib_error_set_handler (NULL
);
1185 snd_pcm_close (handle
);
1188 sd
->open
= alsa_open
;
1189 sd
->close
= alsa_close
;
1190 sd
->configure
= alsa_configure
;
1191 sd
->choose_format
= alsa_choose_format
;
1192 sd
->write
= alsa_write
;
1193 sd
->period_size
= alsa_period_size
;
1198 #endif /* HAVE_ALSA */
1201 /* END: Non Windows functions */
1202 #else /* WINDOWSNT */
1204 /* BEGIN: Windows specific functions */
1206 #define SOUND_WARNING(fun, error, text) \
1209 char err_string[MAXERRORLENGTH]; \
1210 fun (error, err_string, sizeof (err_string)); \
1211 _snprintf (buf, sizeof (buf), "%s\nError: %s", \
1212 text, err_string); \
1213 sound_warning (buf); \
1217 do_play_sound (const char *psz_file
, unsigned long ui_volume
)
1220 MCIERROR mci_error
= 0;
1221 char sz_cmd_buf
[520] = {0};
1222 char sz_ret_buf
[520] = {0};
1223 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1224 unsigned long ui_volume_org
= 0;
1225 BOOL b_reset_volume
= FALSE
;
1227 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1228 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1229 sprintf (sz_cmd_buf
,
1230 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1232 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1235 SOUND_WARNING (mciGetErrorString
, mci_error
,
1236 "The open mciSendString command failed to open "
1237 "the specified sound file.");
1238 i_result
= (int) mci_error
;
1241 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1243 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1244 if (mm_result
== MMSYSERR_NOERROR
)
1246 b_reset_volume
= TRUE
;
1247 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1248 if (mm_result
!= MMSYSERR_NOERROR
)
1250 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1251 "waveOutSetVolume failed to set the volume level "
1252 "of the WAVE_MAPPER device.\n"
1253 "As a result, the user selected volume level will "
1259 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1260 "waveOutGetVolume failed to obtain the original "
1261 "volume level of the WAVE_MAPPER device.\n"
1262 "As a result, the user selected volume level will "
1266 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1267 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1268 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1269 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1272 SOUND_WARNING (mciGetErrorString
, mci_error
,
1273 "The play mciSendString command failed to play the "
1274 "opened sound file.");
1275 i_result
= (int) mci_error
;
1277 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1278 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1279 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1280 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1281 if (b_reset_volume
== TRUE
)
1283 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1284 if (mm_result
!= MMSYSERR_NOERROR
)
1286 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1287 "waveOutSetVolume failed to reset the original volume "
1288 "level of the WAVE_MAPPER device.");
1294 /* END: Windows specific functions */
1296 #endif /* WINDOWSNT */
1298 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1299 doc
: /* Play sound SOUND.
1301 Internal use only, use `play-sound' instead. */)
1304 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1305 ptrdiff_t count
= SPECPDL_INDEX ();
1309 struct gcpro gcpro1
, gcpro2
;
1310 Lisp_Object args
[2];
1311 #else /* WINDOWSNT */
1313 Lisp_Object lo_file
= {0};
1314 char * psz_file
= NULL
;
1315 unsigned long ui_volume_tmp
= UINT_MAX
;
1316 unsigned long ui_volume
= UINT_MAX
;
1317 #endif /* WINDOWSNT */
1319 /* Parse the sound specification. Give up if it is invalid. */
1320 if (!parse_sound (sound
, attrs
))
1321 error ("Invalid sound specification");
1325 GCPRO2 (sound
, file
);
1326 current_sound_device
= xzalloc (sizeof *current_sound_device
);
1327 current_sound
= xzalloc (sizeof *current_sound
);
1328 record_unwind_protect_void (sound_cleanup
);
1329 current_sound
->header
= alloca (MAX_SOUND_HEADER_BYTES
);
1331 if (STRINGP (attrs
[SOUND_FILE
]))
1333 /* Open the sound file. */
1334 current_sound
->fd
= openp (list1 (Vdata_directory
),
1335 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
, false);
1336 if (current_sound
->fd
< 0)
1337 sound_perror ("Could not open sound file");
1339 /* Read the first bytes from the file. */
1340 current_sound
->header_size
1341 = emacs_read (current_sound
->fd
, current_sound
->header
,
1342 MAX_SOUND_HEADER_BYTES
);
1343 if (current_sound
->header_size
< 0)
1344 sound_perror ("Invalid sound file header");
1348 current_sound
->data
= attrs
[SOUND_DATA
];
1349 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1350 memcpy (current_sound
->header
, SDATA (current_sound
->data
),
1351 current_sound
->header_size
);
1354 /* Find out the type of sound. Give up if we can't tell. */
1355 find_sound_type (current_sound
);
1357 /* Set up a device. */
1358 current_sound_device
->file
= attrs
[SOUND_DEVICE
];
1360 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1361 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1362 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1363 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1365 args
[0] = Qplay_sound_functions
;
1367 Frun_hook_with_args (2, args
);
1370 if (!alsa_init (current_sound_device
))
1372 if (!vox_init (current_sound_device
))
1373 error ("No usable sound device driver found");
1375 /* Open the device. */
1376 current_sound_device
->open (current_sound_device
);
1378 /* Play the sound. */
1379 current_sound
->play (current_sound
, current_sound_device
);
1384 #else /* WINDOWSNT */
1386 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1387 len
= XSTRING (lo_file
)->size
;
1388 psz_file
= alloca (len
+ 1);
1389 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1390 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1392 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1394 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1396 ui_volume_tmp
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1399 Based on some experiments I have conducted, a value of 100 or less
1400 for the sound volume is much too low. You cannot even hear it.
1401 A value of UINT_MAX indicates that you wish for the sound to played
1402 at the maximum possible volume. A value of UINT_MAX/2 plays the
1403 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1404 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1405 The following code adjusts the user specified volume level appropriately.
1407 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1409 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1411 do_play_sound (psz_file
, ui_volume
);
1413 #endif /* WINDOWSNT */
1415 unbind_to (count
, Qnil
);
1419 /***********************************************************************
1421 ***********************************************************************/
1424 syms_of_sound (void)
1426 DEFSYM (QCdevice
, ":device");
1427 DEFSYM (QCvolume
, ":volume");
1428 DEFSYM (Qsound
, "sound");
1429 DEFSYM (Qplay_sound_functions
, "play-sound-functions");
1431 defsubr (&Splay_sound_internal
);
1434 #endif /* HAVE_SOUND */