1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 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 Win32 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"
52 #include "syssignal.h"
53 /* END: Common Includes */
56 /* BEGIN: Non Windows Includes */
60 #include <sys/ioctl.h>
63 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
64 sys/soundcard.h. So, let's try whatever's there. */
66 #ifdef HAVE_MACHINE_SOUNDCARD_H
67 #include <machine/soundcard.h>
69 #ifdef HAVE_SYS_SOUNDCARD_H
70 #include <sys/soundcard.h>
72 #ifdef HAVE_SOUNDCARD_H
73 #include <soundcard.h>
76 #ifdef ALSA_SUBDIR_INCLUDE
77 #include <alsa/asoundlib.h>
79 #include <asoundlib.h>
80 #endif /* ALSA_SUBDIR_INCLUDE */
81 #endif /* HAVE_ALSA */
83 /* END: Non Windows Includes */
87 /* BEGIN: Windows Specific Includes */
94 /* END: Windows Specific Includes */
96 #endif /* WINDOWSNT */
98 /* BEGIN: Common Definitions */
102 extern Lisp_Object QCfile
, QCdata
;
103 Lisp_Object QCvolume
, QCdevice
;
105 Lisp_Object Qplay_sound_functions
;
107 /* Indices of attributes in a sound attributes vector. */
119 static void alsa_sound_perror (char *, int) NO_RETURN
;
121 static void sound_perror (char *) NO_RETURN
;
122 static void sound_warning (char *);
123 static int parse_sound (Lisp_Object
, Lisp_Object
*);
125 /* END: Common Definitions */
127 /* BEGIN: Non Windows Definitions */
130 #ifndef DEFAULT_SOUND_DEVICE
131 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
133 #ifndef DEFAULT_ALSA_SOUND_DEVICE
134 #define DEFAULT_ALSA_SOUND_DEVICE "default"
138 /* Structure forward declarations. */
143 /* The file header of RIFF-WAVE files (*.wav). Files are always in
144 little-endian byte-order. */
150 u_int32_t chunk_type
;
151 u_int32_t chunk_format
;
152 u_int32_t chunk_length
;
155 u_int32_t sample_rate
;
156 u_int32_t bytes_per_second
;
157 u_int16_t sample_size
;
159 u_int32_t chunk_data
;
160 u_int32_t data_length
;
163 /* The file header of Sun adio files (*.au). Files are always in
164 big-endian byte-order. */
169 u_int32_t magic_number
;
171 /* Offset of data part from start of file. Minimum value is 24. */
172 u_int32_t data_offset
;
174 /* Size of data part, 0xffffffff if unknown. */
177 /* Data encoding format.
179 2 8-bit linear PCM (REF-PCM)
183 6 32-bit IEEE floating-point
184 7 64-bit IEEE floating-point
185 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
189 /* Number of samples per second. */
190 u_int32_t sample_rate
;
192 /* Number of interleaved channels. */
196 /* Maximum of all sound file headers sizes. */
198 #define MAX_SOUND_HEADER_BYTES \
199 max (sizeof (struct wav_header), sizeof (struct au_header))
201 /* Interface structure for sound devices. */
205 /* The name of the device or null meaning use a default device name. */
208 /* File descriptor of the device. */
211 /* Device-dependent format. */
214 /* Volume (0..100). Zero means unspecified. */
223 /* Bytes per second. */
226 /* 1 = mono, 2 = stereo, 0 = don't set. */
229 /* Open device SD. */
230 void (* open
) (struct sound_device
*sd
);
232 /* Close device SD. */
233 void (* close
) (struct sound_device
*sd
);
235 /* Configure SD accoring to device-dependent parameters. */
236 void (* configure
) (struct sound_device
*device
);
238 /* Choose a device-dependent format for outputting sound S. */
239 void (* choose_format
) (struct sound_device
*sd
,
242 /* Return a preferred data size in bytes to be sent to write (below)
243 each time. 2048 is used if this is NULL. */
244 int (* period_size
) (struct sound_device
*sd
);
246 /* Write NYBTES bytes from BUFFER to device SD. */
247 void (* write
) (struct sound_device
*sd
, const char *buffer
,
250 /* A place for devices to store additional data. */
254 /* An enumerator for each supported sound file type. */
262 /* Interface structure for sound files. */
266 /* The type of the file. */
267 enum sound_type type
;
269 /* File descriptor of a sound file. */
272 /* Pointer to sound file header. This contains header_size bytes
273 read from the start of a sound file. */
276 /* Number of bytes raed from sound file. This is always <=
277 MAX_SOUND_HEADER_BYTES. */
280 /* Sound data, if a string. */
283 /* Play sound file S on device SD. */
284 void (* play
) (struct sound
*s
, struct sound_device
*sd
);
287 /* These are set during `play-sound-internal' so that sound_cleanup has
290 struct sound_device
*current_sound_device
;
291 struct sound
*current_sound
;
293 /* Function prototypes. */
295 static void vox_open (struct sound_device
*);
296 static void vox_configure (struct sound_device
*);
297 static void vox_close (struct sound_device
*sd
);
298 static void vox_choose_format (struct sound_device
*, struct sound
*);
299 static int vox_init (struct sound_device
*);
300 static void vox_write (struct sound_device
*, const char *, int);
301 static void find_sound_type (struct sound
*);
302 static u_int32_t
le2hl (u_int32_t
);
303 static u_int16_t
le2hs (u_int16_t
);
304 static u_int32_t
be2hl (u_int32_t
);
305 static int wav_init (struct sound
*);
306 static void wav_play (struct sound
*, struct sound_device
*);
307 static int au_init (struct sound
*);
308 static void au_play (struct sound
*, struct sound_device
*);
310 #if 0 /* Currently not used. */
311 static u_int16_t
be2hs (u_int16_t
);
314 /* END: Non Windows Definitions */
315 #else /* WINDOWSNT */
317 /* BEGIN: Windows Specific Definitions */
318 static int do_play_sound (const char *, unsigned long);
320 END: Windows Specific Definitions */
321 #endif /* WINDOWSNT */
324 /***********************************************************************
326 ***********************************************************************/
328 /* BEGIN: Common functions */
330 /* Like perror, but signals an error. */
333 sound_perror (char *msg
)
335 int saved_errno
= errno
;
339 sigunblock (sigmask (SIGIO
));
341 if (saved_errno
!= 0)
342 error ("%s: %s", msg
, strerror (saved_errno
));
348 /* Display a warning message. */
351 sound_warning (char *msg
)
357 /* Parse sound specification SOUND, and fill ATTRS with what is
358 found. Value is non-zero if SOUND Is a valid sound specification.
359 A valid sound specification is a list starting with the symbol
360 `sound'. The rest of the list is a property list which may
361 contain the following key/value pairs:
365 FILE is the sound file to play. If it isn't an absolute name,
366 it's searched under `data-directory'.
370 DATA is a string containing sound data. Either :file or :data
371 may be present, but not both.
375 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
376 If not specified, a default device is used.
380 VOL must be an integer in the range [0, 100], or a float in the
384 parse_sound (Lisp_Object sound
, Lisp_Object
*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. */
453 find_sound_type (struct sound
*s
)
455 if (!wav_init (s
) && !au_init (s
))
456 error ("Unknown sound format");
460 /* Function installed by play-sound-internal with record_unwind_protect. */
463 sound_cleanup (Lisp_Object arg
)
465 if (current_sound_device
->close
)
466 current_sound_device
->close (current_sound_device
);
467 if (current_sound
->fd
> 0)
468 emacs_close (current_sound
->fd
);
469 free (current_sound_device
);
470 free (current_sound
);
475 /***********************************************************************
476 Byte-order Conversion
477 ***********************************************************************/
479 /* Convert 32-bit value VALUE which is in little-endian byte-order
480 to host byte-order. */
483 le2hl (u_int32_t value
)
485 #ifdef WORDS_BIG_ENDIAN
486 unsigned char *p
= (unsigned char *) &value
;
487 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
493 /* Convert 16-bit value VALUE which is in little-endian byte-order
494 to host byte-order. */
497 le2hs (u_int16_t value
)
499 #ifdef WORDS_BIG_ENDIAN
500 unsigned char *p
= (unsigned char *) &value
;
501 value
= p
[0] + (p
[1] << 8);
507 /* Convert 32-bit value VALUE which is in big-endian byte-order
508 to host byte-order. */
511 be2hl (u_int32_t value
)
513 #ifndef WORDS_BIG_ENDIAN
514 unsigned char *p
= (unsigned char *) &value
;
515 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
521 #if 0 /* Currently not used. */
523 /* Convert 16-bit value VALUE which is in big-endian byte-order
524 to host byte-order. */
527 be2hs (u_int16_t value
)
529 #ifndef WORDS_BIG_ENDIAN
530 unsigned char *p
= (unsigned char *) &value
;
531 value
= p
[1] + (p
[0] << 8);
538 /***********************************************************************
540 ***********************************************************************/
542 /* Try to initialize sound file S from S->header. S->header
543 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
544 sound file. If the file is a WAV-format file, set up interface
545 functions in S and convert header fields to host byte-order.
546 Value is non-zero if the file is a WAV file. */
549 wav_init (struct sound
*s
)
551 struct wav_header
*header
= (struct wav_header
*) s
->header
;
553 if (s
->header_size
< sizeof *header
554 || memcmp (s
->header
, "RIFF", 4) != 0)
557 /* WAV files are in little-endian order. Convert the header
558 if on a big-endian machine. */
559 header
->magic
= le2hl (header
->magic
);
560 header
->length
= le2hl (header
->length
);
561 header
->chunk_type
= le2hl (header
->chunk_type
);
562 header
->chunk_format
= le2hl (header
->chunk_format
);
563 header
->chunk_length
= le2hl (header
->chunk_length
);
564 header
->format
= le2hs (header
->format
);
565 header
->channels
= le2hs (header
->channels
);
566 header
->sample_rate
= le2hl (header
->sample_rate
);
567 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
568 header
->sample_size
= le2hs (header
->sample_size
);
569 header
->precision
= le2hs (header
->precision
);
570 header
->chunk_data
= le2hl (header
->chunk_data
);
571 header
->data_length
= le2hl (header
->data_length
);
573 /* Set up the interface functions for WAV. */
581 /* Play RIFF-WAVE audio file S on sound device SD. */
584 wav_play (struct sound
*s
, struct sound_device
*sd
)
586 struct wav_header
*header
= (struct wav_header
*) s
->header
;
588 /* Let the device choose a suitable device-dependent format
590 sd
->choose_format (sd
, s
);
592 /* Configure the device. */
593 sd
->sample_size
= header
->sample_size
;
594 sd
->sample_rate
= header
->sample_rate
;
595 sd
->bps
= header
->bytes_per_second
;
596 sd
->channels
= header
->channels
;
599 /* Copy sound data to the device. The WAV file specification is
600 actually more complex. This simple scheme worked with all WAV
601 files I found so far. If someone feels inclined to implement the
602 whole RIFF-WAVE spec, please do. */
603 if (STRINGP (s
->data
))
604 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
605 SBYTES (s
->data
) - sizeof *header
);
610 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
611 int data_left
= header
->data_length
;
613 buffer
= (char *) alloca (blksize
);
614 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
616 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
618 /* Don't play possible garbage at the end of file */
619 if (data_left
< nbytes
) nbytes
= data_left
;
621 sd
->write (sd
, buffer
, nbytes
);
625 sound_perror ("Error reading sound file");
630 /***********************************************************************
632 ***********************************************************************/
634 /* Sun audio file encodings. */
638 AU_ENCODING_ULAW_8
= 1,
646 AU_ENCODING_ALAW_8
= 27
650 /* Try to initialize sound file S from S->header. S->header
651 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
652 sound file. If the file is a AU-format file, set up interface
653 functions in S and convert header fields to host byte-order.
654 Value is non-zero if the file is an AU file. */
657 au_init (struct sound
*s
)
659 struct au_header
*header
= (struct au_header
*) s
->header
;
661 if (s
->header_size
< sizeof *header
662 || memcmp (s
->header
, ".snd", 4) != 0)
665 header
->magic_number
= be2hl (header
->magic_number
);
666 header
->data_offset
= be2hl (header
->data_offset
);
667 header
->data_size
= be2hl (header
->data_size
);
668 header
->encoding
= be2hl (header
->encoding
);
669 header
->sample_rate
= be2hl (header
->sample_rate
);
670 header
->channels
= be2hl (header
->channels
);
672 /* Set up the interface functions for AU. */
680 /* Play Sun audio file S on sound device SD. */
683 au_play (struct sound
*s
, struct sound_device
*sd
)
685 struct au_header
*header
= (struct au_header
*) s
->header
;
688 sd
->sample_rate
= header
->sample_rate
;
690 sd
->channels
= header
->channels
;
691 sd
->choose_format (sd
, s
);
694 if (STRINGP (s
->data
))
695 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
696 SBYTES (s
->data
) - header
->data_offset
);
699 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
704 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
706 /* Copy sound data to the device. */
707 buffer
= (char *) alloca (blksize
);
708 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
709 sd
->write (sd
, buffer
, nbytes
);
712 sound_perror ("Error reading sound file");
717 /***********************************************************************
718 Voxware Driver Interface
719 ***********************************************************************/
721 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
722 has a compatible own driver aka Luigi's driver. */
725 /* Open device SD. If SD->file is non-null, open that device,
726 otherwise use a default device name. */
729 vox_open (struct sound_device
*sd
)
733 /* Open the sound device. Default is /dev/dsp. */
737 file
= DEFAULT_SOUND_DEVICE
;
739 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
745 /* Configure device SD from parameters in it. */
748 vox_configure (struct sound_device
*sd
)
752 xassert (sd
->fd
>= 0);
754 /* On GNU/Linux, it seems that the device driver doesn't like to be
755 interrupted by a signal. Block the ones we know to cause
759 sigblock (sigmask (SIGIO
));
763 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
764 || val
!= sd
->format
)
765 sound_perror ("Could not set sound format");
767 val
= sd
->channels
!= 1;
768 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
769 || val
!= (sd
->channels
!= 1))
770 sound_perror ("Could not set stereo/mono");
772 /* I think bps and sampling_rate are the same, but who knows.
773 Check this. and use SND_DSP_SPEED for both. */
774 if (sd
->sample_rate
> 0)
776 val
= sd
->sample_rate
;
777 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
778 sound_perror ("Could not set sound speed");
779 else if (val
!= sd
->sample_rate
)
780 sound_warning ("Could not set sample rate");
785 int volume
= sd
->volume
& 0xff;
786 volume
|= volume
<< 8;
787 /* This may fail if there is no mixer. Ignore the failure. */
788 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
793 sigunblock (sigmask (SIGIO
));
798 /* Close device SD if it is open. */
801 vox_close (struct sound_device
*sd
)
805 /* On GNU/Linux, it seems that the device driver doesn't like to
806 be interrupted by a signal. Block the ones we know to cause
809 sigblock (sigmask (SIGIO
));
813 /* Flush sound data, and reset the device. */
814 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
818 sigunblock (sigmask (SIGIO
));
821 /* Close the device. */
822 emacs_close (sd
->fd
);
828 /* Choose device-dependent format for device SD from sound file S. */
831 vox_choose_format (struct sound_device
*sd
, struct sound
*s
)
835 struct wav_header
*h
= (struct wav_header
*) s
->header
;
836 if (h
->precision
== 8)
837 sd
->format
= AFMT_U8
;
838 else if (h
->precision
== 16)
839 sd
->format
= AFMT_S16_LE
;
841 error ("Unsupported WAV file format");
843 else if (s
->type
== SUN_AUDIO
)
845 struct au_header
*header
= (struct au_header
*) s
->header
;
846 switch (header
->encoding
)
848 case AU_ENCODING_ULAW_8
:
849 case AU_ENCODING_IEEE32
:
850 case AU_ENCODING_IEEE64
:
851 sd
->format
= AFMT_MU_LAW
;
858 sd
->format
= AFMT_S16_LE
;
862 error ("Unsupported AU file format");
870 /* Initialize device SD. Set up the interface functions in the device
874 vox_init (struct sound_device
*sd
)
879 /* Open the sound device. Default is /dev/dsp. */
883 file
= DEFAULT_SOUND_DEVICE
;
884 fd
= emacs_open (file
, O_WRONLY
, 0);
892 sd
->close
= vox_close
;
893 sd
->configure
= vox_configure
;
894 sd
->choose_format
= vox_choose_format
;
895 sd
->write
= vox_write
;
896 sd
->period_size
= NULL
;
901 /* Write NBYTES bytes from BUFFER to device SD. */
904 vox_write (struct sound_device
*sd
, const char *buffer
, int nbytes
)
906 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
908 sound_perror ("Error writing to sound device");
912 /***********************************************************************
913 ALSA Driver Interface
914 ***********************************************************************/
916 /* This driver is available on GNU/Linux. */
919 alsa_sound_perror (char *msg
, int err
)
921 error ("%s: %s", msg
, snd_strerror (err
));
927 snd_pcm_hw_params_t
*hwparams
;
928 snd_pcm_sw_params_t
*swparams
;
929 snd_pcm_uframes_t period_size
;
932 /* Open device SD. If SD->file is non-null, open that device,
933 otherwise use a default device name. */
936 alsa_open (struct sound_device
*sd
)
939 struct alsa_params
*p
;
942 /* Open the sound device. Default is "default". */
946 file
= DEFAULT_ALSA_SOUND_DEVICE
;
948 p
= xmalloc (sizeof (*p
));
957 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
959 alsa_sound_perror (file
, err
);
963 alsa_period_size (struct sound_device
*sd
)
965 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
966 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
967 return p
->period_size
* (fact
> 0 ? fact
: 1);
971 alsa_configure (struct sound_device
*sd
)
975 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
976 snd_pcm_uframes_t buffer_size
;
978 xassert (p
->handle
!= 0);
980 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
982 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
984 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
986 alsa_sound_perror ("Could not allocate software parameter structure", err
);
988 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
990 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
992 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
993 SND_PCM_ACCESS_RW_INTERLEAVED
);
995 alsa_sound_perror ("Could not set access type", err
);
998 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
1000 alsa_sound_perror ("Could not set sound format", err
);
1002 uval
= sd
->sample_rate
;
1003 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
1005 alsa_sound_perror ("Could not set sample rate", err
);
1008 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
1010 alsa_sound_perror ("Could not set channel count", err
);
1012 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
1014 alsa_sound_perror ("Could not set parameters", err
);
1017 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
1019 alsa_sound_perror ("Unable to get period size for playback", err
);
1021 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
1023 alsa_sound_perror("Unable to get buffer size for playback", err
);
1025 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1027 alsa_sound_perror ("Unable to determine current swparams for playback",
1030 /* Start the transfer when the buffer is almost full */
1031 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1032 (buffer_size
/ p
->period_size
)
1035 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1037 /* Allow the transfer when at least period_size samples can be processed */
1038 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1040 alsa_sound_perror ("Unable to set avail min for playback", err
);
1042 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1044 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1046 snd_pcm_hw_params_free (p
->hwparams
);
1048 snd_pcm_sw_params_free (p
->swparams
);
1051 err
= snd_pcm_prepare (p
->handle
);
1053 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1058 snd_mixer_t
*handle
;
1059 snd_mixer_elem_t
*e
;
1060 char *file
= sd
->file
? sd
->file
: DEFAULT_ALSA_SOUND_DEVICE
;
1062 if (snd_mixer_open (&handle
, 0) >= 0)
1064 if (snd_mixer_attach (handle
, file
) >= 0
1065 && snd_mixer_load (handle
) >= 0
1066 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1067 for (e
= snd_mixer_first_elem (handle
);
1069 e
= snd_mixer_elem_next (e
))
1071 if (snd_mixer_selem_has_playback_volume (e
))
1073 long pmin
, pmax
, vol
;
1074 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1075 vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1077 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1078 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1081 snd_mixer_close(handle
);
1087 /* Close device SD if it is open. */
1090 alsa_close (struct sound_device
*sd
)
1092 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1096 snd_pcm_hw_params_free (p
->hwparams
);
1098 snd_pcm_sw_params_free (p
->swparams
);
1101 snd_pcm_drain (p
->handle
);
1102 snd_pcm_close (p
->handle
);
1108 /* Choose device-dependent format for device SD from sound file S. */
1111 alsa_choose_format (struct sound_device
*sd
, struct sound
*s
)
1113 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1114 if (s
->type
== RIFF
)
1116 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1117 if (h
->precision
== 8)
1118 sd
->format
= SND_PCM_FORMAT_U8
;
1119 else if (h
->precision
== 16)
1120 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1122 error ("Unsupported WAV file format");
1124 else if (s
->type
== SUN_AUDIO
)
1126 struct au_header
*header
= (struct au_header
*) s
->header
;
1127 switch (header
->encoding
)
1129 case AU_ENCODING_ULAW_8
:
1130 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1132 case AU_ENCODING_ALAW_8
:
1133 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1135 case AU_ENCODING_IEEE32
:
1136 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1138 case AU_ENCODING_IEEE64
:
1139 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1142 sd
->format
= SND_PCM_FORMAT_S8
;
1144 case AU_ENCODING_16
:
1145 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1147 case AU_ENCODING_24
:
1148 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1150 case AU_ENCODING_32
:
1151 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1155 error ("Unsupported AU file format");
1163 /* Write NBYTES bytes from BUFFER to device SD. */
1166 alsa_write (struct sound_device
*sd
, const char *buffer
, int nbytes
)
1168 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1170 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1171 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1175 while (nwritten
< nbytes
)
1177 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1178 if (frames
== 0) break;
1180 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1185 err
= snd_pcm_prepare (p
->handle
);
1187 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1190 else if (err
== -ESTRPIPE
)
1192 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1193 sleep(1); /* wait until the suspend flag is released */
1196 err
= snd_pcm_prepare (p
->handle
);
1198 alsa_sound_perror ("Can't recover from suspend, "
1204 alsa_sound_perror ("Error writing to sound device", err
);
1208 nwritten
+= err
* fact
;
1213 snd_error_quiet (const char *file
, int line
, const char *function
, int err
,
1218 /* Initialize device SD. Set up the interface functions in the device
1222 alsa_init (struct sound_device
*sd
)
1228 /* Open the sound device. Default is "default". */
1232 file
= DEFAULT_ALSA_SOUND_DEVICE
;
1234 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1235 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1236 snd_lib_error_set_handler (NULL
);
1239 snd_pcm_close (handle
);
1242 sd
->open
= alsa_open
;
1243 sd
->close
= alsa_close
;
1244 sd
->configure
= alsa_configure
;
1245 sd
->choose_format
= alsa_choose_format
;
1246 sd
->write
= alsa_write
;
1247 sd
->period_size
= alsa_period_size
;
1252 #endif /* HAVE_ALSA */
1255 /* END: Non Windows functions */
1256 #else /* WINDOWSNT */
1258 /* BEGIN: Windows specific functions */
1260 #define SOUND_WARNING(fun, error, text) \
1263 char err_string[MAXERRORLENGTH]; \
1264 fun (error, err_string, sizeof (err_string)); \
1265 _snprintf (buf, sizeof (buf), "%s\nError: %s", \
1266 text, err_string); \
1267 sound_warning (buf); \
1271 do_play_sound (const char *psz_file
, unsigned long ui_volume
)
1274 MCIERROR mci_error
= 0;
1275 char sz_cmd_buf
[520] = {0};
1276 char sz_ret_buf
[520] = {0};
1277 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1278 unsigned long ui_volume_org
= 0;
1279 BOOL b_reset_volume
= FALSE
;
1281 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1282 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1283 sprintf (sz_cmd_buf
,
1284 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1286 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1289 SOUND_WARNING (mciGetErrorString
, mci_error
,
1290 "The open mciSendString command failed to open "
1291 "the specified sound file.");
1292 i_result
= (int) mci_error
;
1295 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1297 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1298 if (mm_result
== MMSYSERR_NOERROR
)
1300 b_reset_volume
= TRUE
;
1301 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1302 if (mm_result
!= MMSYSERR_NOERROR
)
1304 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1305 "waveOutSetVolume failed to set the volume level "
1306 "of the WAVE_MAPPER device.\n"
1307 "As a result, the user selected volume level will "
1313 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1314 "waveOutGetVolume failed to obtain the original "
1315 "volume level of the WAVE_MAPPER device.\n"
1316 "As a result, the user selected volume level will "
1320 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1321 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1322 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1323 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1326 SOUND_WARNING (mciGetErrorString
, mci_error
,
1327 "The play mciSendString command failed to play the "
1328 "opened sound file.");
1329 i_result
= (int) mci_error
;
1331 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1332 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1333 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1334 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1335 if (b_reset_volume
== TRUE
)
1337 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1338 if (mm_result
!= MMSYSERR_NOERROR
)
1340 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1341 "waveOutSetVolume failed to reset the original volume "
1342 "level of the WAVE_MAPPER device.");
1348 /* END: Windows specific functions */
1350 #endif /* WINDOWSNT */
1352 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1353 doc
: /* Play sound SOUND.
1355 Internal use only, use `play-sound' instead. */)
1358 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1359 int count
= SPECPDL_INDEX ();
1363 struct gcpro gcpro1
, gcpro2
;
1364 Lisp_Object args
[2];
1365 #else /* WINDOWSNT */
1367 Lisp_Object lo_file
= {0};
1368 char * psz_file
= NULL
;
1369 unsigned long ui_volume_tmp
= UINT_MAX
;
1370 unsigned long ui_volume
= UINT_MAX
;
1372 #endif /* WINDOWSNT */
1374 /* Parse the sound specification. Give up if it is invalid. */
1375 if (!parse_sound (sound
, attrs
))
1376 error ("Invalid sound specification");
1380 GCPRO2 (sound
, file
);
1381 current_sound_device
= (struct sound_device
*) xmalloc (sizeof (struct sound_device
));
1382 memset (current_sound_device
, 0, sizeof (struct sound_device
));
1383 current_sound
= (struct sound
*) xmalloc (sizeof (struct sound
));
1384 memset (current_sound
, 0, sizeof (struct sound
));
1385 record_unwind_protect (sound_cleanup
, Qnil
);
1386 current_sound
->header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1388 if (STRINGP (attrs
[SOUND_FILE
]))
1390 /* Open the sound file. */
1391 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1392 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1393 if (current_sound
->fd
< 0)
1394 sound_perror ("Could not open sound file");
1396 /* Read the first bytes from the file. */
1397 current_sound
->header_size
1398 = emacs_read (current_sound
->fd
, current_sound
->header
,
1399 MAX_SOUND_HEADER_BYTES
);
1400 if (current_sound
->header_size
< 0)
1401 sound_perror ("Invalid sound file header");
1405 current_sound
->data
= attrs
[SOUND_DATA
];
1406 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1407 memcpy (current_sound
->header
, SDATA (current_sound
->data
),
1408 current_sound
->header_size
);
1411 /* Find out the type of sound. Give up if we can't tell. */
1412 find_sound_type (current_sound
);
1414 /* Set up a device. */
1415 if (STRINGP (attrs
[SOUND_DEVICE
]))
1417 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1418 current_sound_device
->file
= (char *) alloca (len
+ 1);
1419 strcpy (current_sound_device
->file
, SDATA (attrs
[SOUND_DEVICE
]));
1422 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1423 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1424 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1425 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1427 args
[0] = Qplay_sound_functions
;
1429 Frun_hook_with_args (2, args
);
1432 if (!alsa_init (current_sound_device
))
1434 if (!vox_init (current_sound_device
))
1435 error ("No usable sound device driver found");
1437 /* Open the device. */
1438 current_sound_device
->open (current_sound_device
);
1440 /* Play the sound. */
1441 current_sound
->play (current_sound
, current_sound_device
);
1446 #else /* WINDOWSNT */
1448 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1449 len
= XSTRING (lo_file
)->size
;
1450 psz_file
= (char *) alloca (len
+ 1);
1451 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1452 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1454 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1456 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1458 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1461 Based on some experiments I have conducted, a value of 100 or less
1462 for the sound volume is much too low. You cannot even hear it.
1463 A value of UINT_MAX indicates that you wish for the sound to played
1464 at the maximum possible volume. A value of UINT_MAX/2 plays the
1465 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1466 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1467 The following code adjusts the user specified volume level appropriately.
1469 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1471 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1473 i_result
= do_play_sound (psz_file
, ui_volume
);
1475 #endif /* WINDOWSNT */
1477 unbind_to (count
, Qnil
);
1481 /***********************************************************************
1483 ***********************************************************************/
1486 syms_of_sound (void)
1488 QCdevice
= intern_c_string(":device");
1489 staticpro (&QCdevice
);
1490 QCvolume
= intern_c_string (":volume");
1491 staticpro (&QCvolume
);
1492 Qsound
= intern_c_string ("sound");
1493 staticpro (&Qsound
);
1494 Qplay_sound_functions
= intern_c_string ("play-sound-functions");
1495 staticpro (&Qplay_sound_functions
);
1497 defsubr (&Splay_sound_internal
);
1506 #endif /* HAVE_SOUND */
1508 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1509 (do not change this comment) */