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 */
59 #include <sys/ioctl.h>
61 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
62 sys/soundcard.h. So, let's try whatever's there. */
64 #ifdef HAVE_MACHINE_SOUNDCARD_H
65 #include <machine/soundcard.h>
67 #ifdef HAVE_SYS_SOUNDCARD_H
68 #include <sys/soundcard.h>
70 #ifdef HAVE_SOUNDCARD_H
71 #include <soundcard.h>
74 #ifdef ALSA_SUBDIR_INCLUDE
75 #include <alsa/asoundlib.h>
77 #include <asoundlib.h>
78 #endif /* ALSA_SUBDIR_INCLUDE */
79 #endif /* HAVE_ALSA */
81 /* END: Non Windows Includes */
85 /* BEGIN: Windows Specific Includes */
90 /* END: Windows Specific Includes */
92 #endif /* WINDOWSNT */
94 /* BEGIN: Common Definitions */
98 Lisp_Object QCvolume
, QCdevice
;
100 Lisp_Object Qplay_sound_functions
;
102 /* Indices of attributes in a sound attributes vector. */
114 static void alsa_sound_perror (const char *, int) NO_RETURN
;
116 static void sound_perror (const char *) NO_RETURN
;
117 static void sound_warning (const char *);
118 static int parse_sound (Lisp_Object
, Lisp_Object
*);
120 /* END: Common Definitions */
122 /* BEGIN: Non Windows Definitions */
125 #ifndef DEFAULT_SOUND_DEVICE
126 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
128 #ifndef DEFAULT_ALSA_SOUND_DEVICE
129 #define DEFAULT_ALSA_SOUND_DEVICE "default"
133 /* Structure forward declarations. */
138 /* The file header of RIFF-WAVE files (*.wav). Files are always in
139 little-endian byte-order. */
145 u_int32_t chunk_type
;
146 u_int32_t chunk_format
;
147 u_int32_t chunk_length
;
150 u_int32_t sample_rate
;
151 u_int32_t bytes_per_second
;
152 u_int16_t sample_size
;
154 u_int32_t chunk_data
;
155 u_int32_t data_length
;
158 /* The file header of Sun adio files (*.au). Files are always in
159 big-endian byte-order. */
164 u_int32_t magic_number
;
166 /* Offset of data part from start of file. Minimum value is 24. */
167 u_int32_t data_offset
;
169 /* Size of data part, 0xffffffff if unknown. */
172 /* Data encoding format.
174 2 8-bit linear PCM (REF-PCM)
178 6 32-bit IEEE floating-point
179 7 64-bit IEEE floating-point
180 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
184 /* Number of samples per second. */
185 u_int32_t sample_rate
;
187 /* Number of interleaved channels. */
191 /* Maximum of all sound file headers sizes. */
193 #define MAX_SOUND_HEADER_BYTES \
194 max (sizeof (struct wav_header), sizeof (struct au_header))
196 /* Interface structure for sound devices. */
200 /* The name of the device or null meaning use a default device name. */
203 /* File descriptor of the device. */
206 /* Device-dependent format. */
209 /* Volume (0..100). Zero means unspecified. */
218 /* Bytes per second. */
221 /* 1 = mono, 2 = stereo, 0 = don't set. */
224 /* Open device SD. */
225 void (* open
) (struct sound_device
*sd
);
227 /* Close device SD. */
228 void (* close
) (struct sound_device
*sd
);
230 /* Configure SD accoring to device-dependent parameters. */
231 void (* configure
) (struct sound_device
*device
);
233 /* Choose a device-dependent format for outputting sound S. */
234 void (* choose_format
) (struct sound_device
*sd
,
237 /* Return a preferred data size in bytes to be sent to write (below)
238 each time. 2048 is used if this is NULL. */
239 int (* period_size
) (struct sound_device
*sd
);
241 /* Write NYBTES bytes from BUFFER to device SD. */
242 void (* write
) (struct sound_device
*sd
, const char *buffer
,
245 /* A place for devices to store additional data. */
249 /* An enumerator for each supported sound file type. */
257 /* Interface structure for sound files. */
261 /* The type of the file. */
262 enum sound_type type
;
264 /* File descriptor of a sound file. */
267 /* Pointer to sound file header. This contains header_size bytes
268 read from the start of a sound file. */
271 /* Number of bytes raed from sound file. This is always <=
272 MAX_SOUND_HEADER_BYTES. */
275 /* Sound data, if a string. */
278 /* Play sound file S on device SD. */
279 void (* play
) (struct sound
*s
, struct sound_device
*sd
);
282 /* These are set during `play-sound-internal' so that sound_cleanup has
285 struct sound_device
*current_sound_device
;
286 struct sound
*current_sound
;
288 /* Function prototypes. */
290 static void vox_open (struct sound_device
*);
291 static void vox_configure (struct sound_device
*);
292 static void vox_close (struct sound_device
*sd
);
293 static void vox_choose_format (struct sound_device
*, struct sound
*);
294 static int vox_init (struct sound_device
*);
295 static void vox_write (struct sound_device
*, const char *, int);
296 static void find_sound_type (struct sound
*);
297 static u_int32_t
le2hl (u_int32_t
);
298 static u_int16_t
le2hs (u_int16_t
);
299 static u_int32_t
be2hl (u_int32_t
);
300 static int wav_init (struct sound
*);
301 static void wav_play (struct sound
*, struct sound_device
*);
302 static int au_init (struct sound
*);
303 static void au_play (struct sound
*, struct sound_device
*);
305 #if 0 /* Currently not used. */
306 static u_int16_t
be2hs (u_int16_t
);
309 /* END: Non Windows Definitions */
310 #else /* WINDOWSNT */
312 /* BEGIN: Windows Specific Definitions */
313 static int do_play_sound (const char *, unsigned long);
315 END: Windows Specific Definitions */
316 #endif /* WINDOWSNT */
319 /***********************************************************************
321 ***********************************************************************/
323 /* BEGIN: Common functions */
325 /* Like perror, but signals an error. */
328 sound_perror (const char *msg
)
330 int saved_errno
= errno
;
334 sigunblock (sigmask (SIGIO
));
336 if (saved_errno
!= 0)
337 error ("%s: %s", msg
, strerror (saved_errno
));
343 /* Display a warning message. */
346 sound_warning (const char *msg
)
352 /* Parse sound specification SOUND, and fill ATTRS with what is
353 found. Value is non-zero if SOUND Is a valid sound specification.
354 A valid sound specification is a list starting with the symbol
355 `sound'. The rest of the list is a property list which may
356 contain the following key/value pairs:
360 FILE is the sound file to play. If it isn't an absolute name,
361 it's searched under `data-directory'.
365 DATA is a string containing sound data. Either :file or :data
366 may be present, but not both.
370 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
371 If not specified, a default device is used.
375 VOL must be an integer in the range [0, 100], or a float in the
379 parse_sound (Lisp_Object sound
, Lisp_Object
*attrs
)
381 /* SOUND must be a list starting with the symbol `sound'. */
382 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
385 sound
= XCDR (sound
);
386 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
387 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
388 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
389 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
392 /* File name or data must be specified. */
393 if (!STRINGP (attrs
[SOUND_FILE
])
394 && !STRINGP (attrs
[SOUND_DATA
]))
396 #else /* WINDOWSNT */
398 Data is not supported in Windows. Therefore a
399 File name MUST be supplied.
401 if (!STRINGP (attrs
[SOUND_FILE
]))
405 #endif /* WINDOWSNT */
407 /* Volume must be in the range 0..100 or unspecified. */
408 if (!NILP (attrs
[SOUND_VOLUME
]))
410 if (INTEGERP (attrs
[SOUND_VOLUME
]))
412 if (XINT (attrs
[SOUND_VOLUME
]) < 0
413 || XINT (attrs
[SOUND_VOLUME
]) > 100)
416 else if (FLOATP (attrs
[SOUND_VOLUME
]))
418 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
419 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
427 /* Device must be a string or unspecified. */
428 if (!NILP (attrs
[SOUND_DEVICE
])
429 && !STRINGP (attrs
[SOUND_DEVICE
]))
431 #endif /* WINDOWSNT */
433 Since device is ignored in Windows, it does not matter
439 /* END: Common functions */
441 /* BEGIN: Non Windows functions */
444 /* Find out the type of the sound file whose file descriptor is FD.
445 S is the sound file structure to fill in. */
448 find_sound_type (struct sound
*s
)
450 if (!wav_init (s
) && !au_init (s
))
451 error ("Unknown sound format");
455 /* Function installed by play-sound-internal with record_unwind_protect. */
458 sound_cleanup (Lisp_Object arg
)
460 if (current_sound_device
->close
)
461 current_sound_device
->close (current_sound_device
);
462 if (current_sound
->fd
> 0)
463 emacs_close (current_sound
->fd
);
464 free (current_sound_device
);
465 free (current_sound
);
470 /***********************************************************************
471 Byte-order Conversion
472 ***********************************************************************/
474 /* Convert 32-bit value VALUE which is in little-endian byte-order
475 to host byte-order. */
478 le2hl (u_int32_t value
)
480 #ifdef WORDS_BIGENDIAN
481 unsigned char *p
= (unsigned char *) &value
;
482 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
488 /* Convert 16-bit value VALUE which is in little-endian byte-order
489 to host byte-order. */
492 le2hs (u_int16_t value
)
494 #ifdef WORDS_BIGENDIAN
495 unsigned char *p
= (unsigned char *) &value
;
496 value
= p
[0] + (p
[1] << 8);
502 /* Convert 32-bit value VALUE which is in big-endian byte-order
503 to host byte-order. */
506 be2hl (u_int32_t value
)
508 #ifndef WORDS_BIGENDIAN
509 unsigned char *p
= (unsigned char *) &value
;
510 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
516 #if 0 /* Currently not used. */
518 /* Convert 16-bit value VALUE which is in big-endian byte-order
519 to host byte-order. */
522 be2hs (u_int16_t value
)
524 #ifndef WORDS_BIGENDIAN
525 unsigned char *p
= (unsigned char *) &value
;
526 value
= p
[1] + (p
[0] << 8);
533 /***********************************************************************
535 ***********************************************************************/
537 /* Try to initialize sound file S from S->header. S->header
538 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
539 sound file. If the file is a WAV-format file, set up interface
540 functions in S and convert header fields to host byte-order.
541 Value is non-zero if the file is a WAV file. */
544 wav_init (struct sound
*s
)
546 struct wav_header
*header
= (struct wav_header
*) s
->header
;
548 if (s
->header_size
< sizeof *header
549 || memcmp (s
->header
, "RIFF", 4) != 0)
552 /* WAV files are in little-endian order. Convert the header
553 if on a big-endian machine. */
554 header
->magic
= le2hl (header
->magic
);
555 header
->length
= le2hl (header
->length
);
556 header
->chunk_type
= le2hl (header
->chunk_type
);
557 header
->chunk_format
= le2hl (header
->chunk_format
);
558 header
->chunk_length
= le2hl (header
->chunk_length
);
559 header
->format
= le2hs (header
->format
);
560 header
->channels
= le2hs (header
->channels
);
561 header
->sample_rate
= le2hl (header
->sample_rate
);
562 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
563 header
->sample_size
= le2hs (header
->sample_size
);
564 header
->precision
= le2hs (header
->precision
);
565 header
->chunk_data
= le2hl (header
->chunk_data
);
566 header
->data_length
= le2hl (header
->data_length
);
568 /* Set up the interface functions for WAV. */
576 /* Play RIFF-WAVE audio file S on sound device SD. */
579 wav_play (struct sound
*s
, struct sound_device
*sd
)
581 struct wav_header
*header
= (struct wav_header
*) s
->header
;
583 /* Let the device choose a suitable device-dependent format
585 sd
->choose_format (sd
, s
);
587 /* Configure the device. */
588 sd
->sample_size
= header
->sample_size
;
589 sd
->sample_rate
= header
->sample_rate
;
590 sd
->bps
= header
->bytes_per_second
;
591 sd
->channels
= header
->channels
;
594 /* Copy sound data to the device. The WAV file specification is
595 actually more complex. This simple scheme worked with all WAV
596 files I found so far. If someone feels inclined to implement the
597 whole RIFF-WAVE spec, please do. */
598 if (STRINGP (s
->data
))
599 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
600 SBYTES (s
->data
) - sizeof *header
);
605 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
606 int data_left
= header
->data_length
;
608 buffer
= (char *) alloca (blksize
);
609 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
611 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
613 /* Don't play possible garbage at the end of file */
614 if (data_left
< nbytes
) nbytes
= data_left
;
616 sd
->write (sd
, buffer
, nbytes
);
620 sound_perror ("Error reading sound file");
625 /***********************************************************************
627 ***********************************************************************/
629 /* Sun audio file encodings. */
633 AU_ENCODING_ULAW_8
= 1,
641 AU_ENCODING_ALAW_8
= 27
645 /* Try to initialize sound file S from S->header. S->header
646 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
647 sound file. If the file is a AU-format file, set up interface
648 functions in S and convert header fields to host byte-order.
649 Value is non-zero if the file is an AU file. */
652 au_init (struct sound
*s
)
654 struct au_header
*header
= (struct au_header
*) s
->header
;
656 if (s
->header_size
< sizeof *header
657 || memcmp (s
->header
, ".snd", 4) != 0)
660 header
->magic_number
= be2hl (header
->magic_number
);
661 header
->data_offset
= be2hl (header
->data_offset
);
662 header
->data_size
= be2hl (header
->data_size
);
663 header
->encoding
= be2hl (header
->encoding
);
664 header
->sample_rate
= be2hl (header
->sample_rate
);
665 header
->channels
= be2hl (header
->channels
);
667 /* Set up the interface functions for AU. */
675 /* Play Sun audio file S on sound device SD. */
678 au_play (struct sound
*s
, struct sound_device
*sd
)
680 struct au_header
*header
= (struct au_header
*) s
->header
;
683 sd
->sample_rate
= header
->sample_rate
;
685 sd
->channels
= header
->channels
;
686 sd
->choose_format (sd
, s
);
689 if (STRINGP (s
->data
))
690 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
691 SBYTES (s
->data
) - header
->data_offset
);
694 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
699 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
701 /* Copy sound data to the device. */
702 buffer
= (char *) alloca (blksize
);
703 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
704 sd
->write (sd
, buffer
, nbytes
);
707 sound_perror ("Error reading sound file");
712 /***********************************************************************
713 Voxware Driver Interface
714 ***********************************************************************/
716 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
717 has a compatible own driver aka Luigi's driver. */
720 /* Open device SD. If SD->file is non-null, open that device,
721 otherwise use a default device name. */
724 vox_open (struct sound_device
*sd
)
728 /* Open the sound device. Default is /dev/dsp. */
732 file
= DEFAULT_SOUND_DEVICE
;
734 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
740 /* Configure device SD from parameters in it. */
743 vox_configure (struct sound_device
*sd
)
747 xassert (sd
->fd
>= 0);
749 /* On GNU/Linux, it seems that the device driver doesn't like to be
750 interrupted by a signal. Block the ones we know to cause
754 sigblock (sigmask (SIGIO
));
758 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
759 || val
!= sd
->format
)
760 sound_perror ("Could not set sound format");
762 val
= sd
->channels
!= 1;
763 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
764 || val
!= (sd
->channels
!= 1))
765 sound_perror ("Could not set stereo/mono");
767 /* I think bps and sampling_rate are the same, but who knows.
768 Check this. and use SND_DSP_SPEED for both. */
769 if (sd
->sample_rate
> 0)
771 val
= sd
->sample_rate
;
772 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
773 sound_perror ("Could not set sound speed");
774 else if (val
!= sd
->sample_rate
)
775 sound_warning ("Could not set sample rate");
780 int volume
= sd
->volume
& 0xff;
781 volume
|= volume
<< 8;
782 /* This may fail if there is no mixer. Ignore the failure. */
783 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
788 sigunblock (sigmask (SIGIO
));
793 /* Close device SD if it is open. */
796 vox_close (struct sound_device
*sd
)
800 /* On GNU/Linux, it seems that the device driver doesn't like to
801 be interrupted by a signal. Block the ones we know to cause
804 sigblock (sigmask (SIGIO
));
808 /* Flush sound data, and reset the device. */
809 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
813 sigunblock (sigmask (SIGIO
));
816 /* Close the device. */
817 emacs_close (sd
->fd
);
823 /* Choose device-dependent format for device SD from sound file S. */
826 vox_choose_format (struct sound_device
*sd
, struct sound
*s
)
830 struct wav_header
*h
= (struct wav_header
*) s
->header
;
831 if (h
->precision
== 8)
832 sd
->format
= AFMT_U8
;
833 else if (h
->precision
== 16)
834 sd
->format
= AFMT_S16_LE
;
836 error ("Unsupported WAV file format");
838 else if (s
->type
== SUN_AUDIO
)
840 struct au_header
*header
= (struct au_header
*) s
->header
;
841 switch (header
->encoding
)
843 case AU_ENCODING_ULAW_8
:
844 case AU_ENCODING_IEEE32
:
845 case AU_ENCODING_IEEE64
:
846 sd
->format
= AFMT_MU_LAW
;
853 sd
->format
= AFMT_S16_LE
;
857 error ("Unsupported AU file format");
865 /* Initialize device SD. Set up the interface functions in the device
869 vox_init (struct sound_device
*sd
)
874 /* Open the sound device. Default is /dev/dsp. */
878 file
= DEFAULT_SOUND_DEVICE
;
879 fd
= emacs_open (file
, O_WRONLY
, 0);
887 sd
->close
= vox_close
;
888 sd
->configure
= vox_configure
;
889 sd
->choose_format
= vox_choose_format
;
890 sd
->write
= vox_write
;
891 sd
->period_size
= NULL
;
896 /* Write NBYTES bytes from BUFFER to device SD. */
899 vox_write (struct sound_device
*sd
, const char *buffer
, int nbytes
)
901 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
903 sound_perror ("Error writing to sound device");
907 /***********************************************************************
908 ALSA Driver Interface
909 ***********************************************************************/
911 /* This driver is available on GNU/Linux. */
914 alsa_sound_perror (const char *msg
, int err
)
916 error ("%s: %s", msg
, snd_strerror (err
));
922 snd_pcm_hw_params_t
*hwparams
;
923 snd_pcm_sw_params_t
*swparams
;
924 snd_pcm_uframes_t period_size
;
927 /* Open device SD. If SD->file is non-null, open that device,
928 otherwise use a default device name. */
931 alsa_open (struct sound_device
*sd
)
934 struct alsa_params
*p
;
937 /* Open the sound device. Default is "default". */
941 file
= DEFAULT_ALSA_SOUND_DEVICE
;
943 p
= xmalloc (sizeof (*p
));
952 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
954 alsa_sound_perror (file
, err
);
958 alsa_period_size (struct sound_device
*sd
)
960 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
961 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
962 return p
->period_size
* (fact
> 0 ? fact
: 1);
966 alsa_configure (struct sound_device
*sd
)
970 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
971 snd_pcm_uframes_t buffer_size
;
973 xassert (p
->handle
!= 0);
975 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
977 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
979 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
981 alsa_sound_perror ("Could not allocate software parameter structure", err
);
983 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
985 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
987 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
988 SND_PCM_ACCESS_RW_INTERLEAVED
);
990 alsa_sound_perror ("Could not set access type", err
);
993 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
995 alsa_sound_perror ("Could not set sound format", err
);
997 uval
= sd
->sample_rate
;
998 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
1000 alsa_sound_perror ("Could not set sample rate", err
);
1003 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
1005 alsa_sound_perror ("Could not set channel count", err
);
1007 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
1009 alsa_sound_perror ("Could not set parameters", err
);
1012 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
1014 alsa_sound_perror ("Unable to get period size for playback", err
);
1016 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
1018 alsa_sound_perror("Unable to get buffer size for playback", err
);
1020 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1022 alsa_sound_perror ("Unable to determine current swparams for playback",
1025 /* Start the transfer when the buffer is almost full */
1026 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1027 (buffer_size
/ p
->period_size
)
1030 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1032 /* Allow the transfer when at least period_size samples can be processed */
1033 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1035 alsa_sound_perror ("Unable to set avail min for playback", err
);
1037 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1039 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1041 snd_pcm_hw_params_free (p
->hwparams
);
1043 snd_pcm_sw_params_free (p
->swparams
);
1046 err
= snd_pcm_prepare (p
->handle
);
1048 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1053 snd_mixer_t
*handle
;
1054 snd_mixer_elem_t
*e
;
1055 const char *file
= sd
->file
? sd
->file
: DEFAULT_ALSA_SOUND_DEVICE
;
1057 if (snd_mixer_open (&handle
, 0) >= 0)
1059 if (snd_mixer_attach (handle
, file
) >= 0
1060 && snd_mixer_load (handle
) >= 0
1061 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1062 for (e
= snd_mixer_first_elem (handle
);
1064 e
= snd_mixer_elem_next (e
))
1066 if (snd_mixer_selem_has_playback_volume (e
))
1068 long pmin
, pmax
, vol
;
1069 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1070 vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1072 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1073 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1076 snd_mixer_close(handle
);
1082 /* Close device SD if it is open. */
1085 alsa_close (struct sound_device
*sd
)
1087 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1091 snd_pcm_hw_params_free (p
->hwparams
);
1093 snd_pcm_sw_params_free (p
->swparams
);
1096 snd_pcm_drain (p
->handle
);
1097 snd_pcm_close (p
->handle
);
1103 /* Choose device-dependent format for device SD from sound file S. */
1106 alsa_choose_format (struct sound_device
*sd
, struct sound
*s
)
1108 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1109 if (s
->type
== RIFF
)
1111 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1112 if (h
->precision
== 8)
1113 sd
->format
= SND_PCM_FORMAT_U8
;
1114 else if (h
->precision
== 16)
1115 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1117 error ("Unsupported WAV file format");
1119 else if (s
->type
== SUN_AUDIO
)
1121 struct au_header
*header
= (struct au_header
*) s
->header
;
1122 switch (header
->encoding
)
1124 case AU_ENCODING_ULAW_8
:
1125 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1127 case AU_ENCODING_ALAW_8
:
1128 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1130 case AU_ENCODING_IEEE32
:
1131 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1133 case AU_ENCODING_IEEE64
:
1134 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1137 sd
->format
= SND_PCM_FORMAT_S8
;
1139 case AU_ENCODING_16
:
1140 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1142 case AU_ENCODING_24
:
1143 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1145 case AU_ENCODING_32
:
1146 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1150 error ("Unsupported AU file format");
1158 /* Write NBYTES bytes from BUFFER to device SD. */
1161 alsa_write (struct sound_device
*sd
, const char *buffer
, int nbytes
)
1163 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1165 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1166 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1170 while (nwritten
< nbytes
)
1172 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1173 if (frames
== 0) break;
1175 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1180 err
= snd_pcm_prepare (p
->handle
);
1182 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1185 else if (err
== -ESTRPIPE
)
1187 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1188 sleep(1); /* wait until the suspend flag is released */
1191 err
= snd_pcm_prepare (p
->handle
);
1193 alsa_sound_perror ("Can't recover from suspend, "
1199 alsa_sound_perror ("Error writing to sound device", err
);
1203 nwritten
+= err
* fact
;
1208 snd_error_quiet (const char *file
, int line
, const char *function
, int err
,
1213 /* Initialize device SD. Set up the interface functions in the device
1217 alsa_init (struct sound_device
*sd
)
1223 /* Open the sound device. Default is "default". */
1227 file
= DEFAULT_ALSA_SOUND_DEVICE
;
1229 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1230 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1231 snd_lib_error_set_handler (NULL
);
1234 snd_pcm_close (handle
);
1237 sd
->open
= alsa_open
;
1238 sd
->close
= alsa_close
;
1239 sd
->configure
= alsa_configure
;
1240 sd
->choose_format
= alsa_choose_format
;
1241 sd
->write
= alsa_write
;
1242 sd
->period_size
= alsa_period_size
;
1247 #endif /* HAVE_ALSA */
1250 /* END: Non Windows functions */
1251 #else /* WINDOWSNT */
1253 /* BEGIN: Windows specific functions */
1255 #define SOUND_WARNING(fun, error, text) \
1258 char err_string[MAXERRORLENGTH]; \
1259 fun (error, err_string, sizeof (err_string)); \
1260 _snprintf (buf, sizeof (buf), "%s\nError: %s", \
1261 text, err_string); \
1262 sound_warning (buf); \
1266 do_play_sound (const char *psz_file
, unsigned long ui_volume
)
1269 MCIERROR mci_error
= 0;
1270 char sz_cmd_buf
[520] = {0};
1271 char sz_ret_buf
[520] = {0};
1272 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1273 unsigned long ui_volume_org
= 0;
1274 BOOL b_reset_volume
= FALSE
;
1276 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1277 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1278 sprintf (sz_cmd_buf
,
1279 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1281 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1284 SOUND_WARNING (mciGetErrorString
, mci_error
,
1285 "The open mciSendString command failed to open "
1286 "the specified sound file.");
1287 i_result
= (int) mci_error
;
1290 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1292 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1293 if (mm_result
== MMSYSERR_NOERROR
)
1295 b_reset_volume
= TRUE
;
1296 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1297 if (mm_result
!= MMSYSERR_NOERROR
)
1299 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1300 "waveOutSetVolume failed to set the volume level "
1301 "of the WAVE_MAPPER device.\n"
1302 "As a result, the user selected volume level will "
1308 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1309 "waveOutGetVolume failed to obtain the original "
1310 "volume level of the WAVE_MAPPER device.\n"
1311 "As a result, the user selected volume level will "
1315 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1316 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1317 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1318 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1321 SOUND_WARNING (mciGetErrorString
, mci_error
,
1322 "The play mciSendString command failed to play the "
1323 "opened sound file.");
1324 i_result
= (int) mci_error
;
1326 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1327 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1328 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1329 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1330 if (b_reset_volume
== TRUE
)
1332 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1333 if (mm_result
!= MMSYSERR_NOERROR
)
1335 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1336 "waveOutSetVolume failed to reset the original volume "
1337 "level of the WAVE_MAPPER device.");
1343 /* END: Windows specific functions */
1345 #endif /* WINDOWSNT */
1347 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1348 doc
: /* Play sound SOUND.
1350 Internal use only, use `play-sound' instead. */)
1353 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1354 int count
= SPECPDL_INDEX ();
1358 struct gcpro gcpro1
, gcpro2
;
1359 Lisp_Object args
[2];
1360 #else /* WINDOWSNT */
1362 Lisp_Object lo_file
= {0};
1363 char * psz_file
= NULL
;
1364 unsigned long ui_volume_tmp
= UINT_MAX
;
1365 unsigned long ui_volume
= UINT_MAX
;
1367 #endif /* WINDOWSNT */
1369 /* Parse the sound specification. Give up if it is invalid. */
1370 if (!parse_sound (sound
, attrs
))
1371 error ("Invalid sound specification");
1375 GCPRO2 (sound
, file
);
1376 current_sound_device
= (struct sound_device
*) xmalloc (sizeof (struct sound_device
));
1377 memset (current_sound_device
, 0, sizeof (struct sound_device
));
1378 current_sound
= (struct sound
*) xmalloc (sizeof (struct sound
));
1379 memset (current_sound
, 0, sizeof (struct sound
));
1380 record_unwind_protect (sound_cleanup
, Qnil
);
1381 current_sound
->header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1383 if (STRINGP (attrs
[SOUND_FILE
]))
1385 /* Open the sound file. */
1386 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1387 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1388 if (current_sound
->fd
< 0)
1389 sound_perror ("Could not open sound file");
1391 /* Read the first bytes from the file. */
1392 current_sound
->header_size
1393 = emacs_read (current_sound
->fd
, current_sound
->header
,
1394 MAX_SOUND_HEADER_BYTES
);
1395 if (current_sound
->header_size
< 0)
1396 sound_perror ("Invalid sound file header");
1400 current_sound
->data
= attrs
[SOUND_DATA
];
1401 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1402 memcpy (current_sound
->header
, SDATA (current_sound
->data
),
1403 current_sound
->header_size
);
1406 /* Find out the type of sound. Give up if we can't tell. */
1407 find_sound_type (current_sound
);
1409 /* Set up a device. */
1410 if (STRINGP (attrs
[SOUND_DEVICE
]))
1412 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1413 current_sound_device
->file
= (char *) alloca (len
+ 1);
1414 strcpy (current_sound_device
->file
, SDATA (attrs
[SOUND_DEVICE
]));
1417 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1418 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1419 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1420 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1422 args
[0] = Qplay_sound_functions
;
1424 Frun_hook_with_args (2, args
);
1427 if (!alsa_init (current_sound_device
))
1429 if (!vox_init (current_sound_device
))
1430 error ("No usable sound device driver found");
1432 /* Open the device. */
1433 current_sound_device
->open (current_sound_device
);
1435 /* Play the sound. */
1436 current_sound
->play (current_sound
, current_sound_device
);
1441 #else /* WINDOWSNT */
1443 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1444 len
= XSTRING (lo_file
)->size
;
1445 psz_file
= (char *) alloca (len
+ 1);
1446 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1447 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1449 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1451 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1453 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1456 Based on some experiments I have conducted, a value of 100 or less
1457 for the sound volume is much too low. You cannot even hear it.
1458 A value of UINT_MAX indicates that you wish for the sound to played
1459 at the maximum possible volume. A value of UINT_MAX/2 plays the
1460 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1461 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1462 The following code adjusts the user specified volume level appropriately.
1464 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1466 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1468 i_result
= do_play_sound (psz_file
, ui_volume
);
1470 #endif /* WINDOWSNT */
1472 unbind_to (count
, Qnil
);
1476 /***********************************************************************
1478 ***********************************************************************/
1481 syms_of_sound (void)
1483 QCdevice
= intern_c_string(":device");
1484 staticpro (&QCdevice
);
1485 QCvolume
= intern_c_string (":volume");
1486 staticpro (&QCvolume
);
1487 Qsound
= intern_c_string ("sound");
1488 staticpro (&Qsound
);
1489 Qplay_sound_functions
= intern_c_string ("play-sound-functions");
1490 staticpro (&Qplay_sound_functions
);
1492 defsubr (&Splay_sound_internal
);
1501 #endif /* HAVE_SOUND */
1503 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1504 (do not change this comment) */