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