1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009 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>
48 #include "dispextern.h"
51 #include "syssignal.h"
52 /* END: Common Includes */
55 /* BEGIN: Non Windows Includes */
59 #include <sys/ioctl.h>
62 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
63 sys/soundcard.h. So, let's try whatever's there. */
65 #ifdef HAVE_MACHINE_SOUNDCARD_H
66 #include <machine/soundcard.h>
68 #ifdef HAVE_SYS_SOUNDCARD_H
69 #include <sys/soundcard.h>
71 #ifdef HAVE_SOUNDCARD_H
72 #include <soundcard.h>
75 #ifdef ALSA_SUBDIR_INCLUDE
76 #include <alsa/asoundlib.h>
78 #include <asoundlib.h>
79 #endif /* ALSA_SUBDIR_INCLUDE */
80 #endif /* HAVE_ALSA */
82 /* END: Non Windows Includes */
86 /* BEGIN: Windows Specific Includes */
93 /* END: Windows Specific Includes */
95 #endif /* WINDOWSNT */
97 /* BEGIN: Common Definitions */
101 extern Lisp_Object QCfile
, QCdata
;
102 Lisp_Object QCvolume
, QCdevice
;
104 Lisp_Object Qplay_sound_functions
;
106 /* Indices of attributes in a sound attributes vector. */
117 static void alsa_sound_perror
P_ ((char *, int)) NO_RETURN
;
118 static void sound_perror
P_ ((char *)) NO_RETURN
;
119 static void sound_warning
P_ ((char *));
120 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
122 /* END: Common Definitions */
124 /* BEGIN: Non Windows Definitions */
127 #ifndef DEFAULT_SOUND_DEVICE
128 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
130 #ifndef DEFAULT_ALSA_SOUND_DEVICE
131 #define DEFAULT_ALSA_SOUND_DEVICE "default"
135 /* Structure forward declarations. */
140 /* The file header of RIFF-WAVE files (*.wav). Files are always in
141 little-endian byte-order. */
147 u_int32_t chunk_type
;
148 u_int32_t chunk_format
;
149 u_int32_t chunk_length
;
152 u_int32_t sample_rate
;
153 u_int32_t bytes_per_second
;
154 u_int16_t sample_size
;
156 u_int32_t chunk_data
;
157 u_int32_t data_length
;
160 /* The file header of Sun adio files (*.au). Files are always in
161 big-endian byte-order. */
166 u_int32_t magic_number
;
168 /* Offset of data part from start of file. Minimum value is 24. */
169 u_int32_t data_offset
;
171 /* Size of data part, 0xffffffff if unknown. */
174 /* Data encoding format.
176 2 8-bit linear PCM (REF-PCM)
180 6 32-bit IEEE floating-point
181 7 64-bit IEEE floating-point
182 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
186 /* Number of samples per second. */
187 u_int32_t sample_rate
;
189 /* Number of interleaved channels. */
193 /* Maximum of all sound file headers sizes. */
195 #define MAX_SOUND_HEADER_BYTES \
196 max (sizeof (struct wav_header), sizeof (struct au_header))
198 /* Interface structure for sound devices. */
202 /* The name of the device or null meaning use a default device name. */
205 /* File descriptor of the device. */
208 /* Device-dependent format. */
211 /* Volume (0..100). Zero means unspecified. */
220 /* Bytes per second. */
223 /* 1 = mono, 2 = stereo, 0 = don't set. */
226 /* Open device SD. */
227 void (* open
) P_ ((struct sound_device
*sd
));
229 /* Close device SD. */
230 void (* close
) P_ ((struct sound_device
*sd
));
232 /* Configure SD accoring to device-dependent parameters. */
233 void (* configure
) P_ ((struct sound_device
*device
));
235 /* Choose a device-dependent format for outputting sound S. */
236 void (* choose_format
) P_ ((struct sound_device
*sd
,
239 /* Return a preferred data size in bytes to be sent to write (below)
240 each time. 2048 is used if this is NULL. */
241 int (* period_size
) P_ ((struct sound_device
*sd
));
243 /* Write NYBTES bytes from BUFFER to device SD. */
244 void (* write
) P_ ((struct sound_device
*sd
, const char *buffer
,
247 /* A place for devices to store additional data. */
251 /* An enumerator for each supported sound file type. */
259 /* Interface structure for sound files. */
263 /* The type of the file. */
264 enum sound_type type
;
266 /* File descriptor of a sound file. */
269 /* Pointer to sound file header. This contains header_size bytes
270 read from the start of a sound file. */
273 /* Number of bytes raed from sound file. This is always <=
274 MAX_SOUND_HEADER_BYTES. */
277 /* Sound data, if a string. */
280 /* Play sound file S on device SD. */
281 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
284 /* These are set during `play-sound-internal' so that sound_cleanup has
287 struct sound_device
*current_sound_device
;
288 struct sound
*current_sound
;
290 /* Function prototypes. */
292 static void vox_open
P_ ((struct sound_device
*));
293 static void vox_configure
P_ ((struct sound_device
*));
294 static void vox_close
P_ ((struct sound_device
*sd
));
295 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
296 static int vox_init
P_ ((struct sound_device
*));
297 static void vox_write
P_ ((struct sound_device
*, const char *, int));
298 static void find_sound_type
P_ ((struct sound
*));
299 static u_int32_t le2hl
P_ ((u_int32_t
));
300 static u_int16_t le2hs
P_ ((u_int16_t
));
301 static u_int32_t be2hl
P_ ((u_int32_t
));
302 static int wav_init
P_ ((struct sound
*));
303 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
304 static int au_init
P_ ((struct sound
*));
305 static void au_play
P_ ((struct sound
*, struct sound_device
*));
307 #if 0 /* Currently not used. */
308 static u_int16_t be2hs
P_ ((u_int16_t
));
311 /* END: Non Windows Definitions */
312 #else /* WINDOWSNT */
314 /* BEGIN: Windows Specific Definitions */
315 static int do_play_sound
P_ ((const char *, unsigned long));
317 END: Windows Specific Definitions */
318 #endif /* WINDOWSNT */
321 /***********************************************************************
323 ***********************************************************************/
325 /* BEGIN: Common functions */
327 /* Like perror, but signals an error. */
333 int saved_errno
= errno
;
337 sigunblock (sigmask (SIGIO
));
339 if (saved_errno
!= 0)
340 error ("%s: %s", msg
, strerror (saved_errno
));
346 /* Display a warning message. */
356 /* Parse sound specification SOUND, and fill ATTRS with what is
357 found. Value is non-zero if SOUND Is a valid sound specification.
358 A valid sound specification is a list starting with the symbol
359 `sound'. The rest of the list is a property list which may
360 contain the following key/value pairs:
364 FILE is the sound file to play. If it isn't an absolute name,
365 it's searched under `data-directory'.
369 DATA is a string containing sound data. Either :file or :data
370 may be present, but not both.
374 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
375 If not specified, a default device is used.
379 VOL must be an integer in the range [0, 100], or a float in the
383 parse_sound (sound
, attrs
)
387 /* SOUND must be a list starting with the symbol `sound'. */
388 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
391 sound
= XCDR (sound
);
392 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
393 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
394 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
395 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
398 /* File name or data must be specified. */
399 if (!STRINGP (attrs
[SOUND_FILE
])
400 && !STRINGP (attrs
[SOUND_DATA
]))
402 #else /* WINDOWSNT */
404 Data is not supported in Windows. Therefore a
405 File name MUST be supplied.
407 if (!STRINGP (attrs
[SOUND_FILE
]))
411 #endif /* WINDOWSNT */
413 /* Volume must be in the range 0..100 or unspecified. */
414 if (!NILP (attrs
[SOUND_VOLUME
]))
416 if (INTEGERP (attrs
[SOUND_VOLUME
]))
418 if (XINT (attrs
[SOUND_VOLUME
]) < 0
419 || XINT (attrs
[SOUND_VOLUME
]) > 100)
422 else if (FLOATP (attrs
[SOUND_VOLUME
]))
424 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
425 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
433 /* Device must be a string or unspecified. */
434 if (!NILP (attrs
[SOUND_DEVICE
])
435 && !STRINGP (attrs
[SOUND_DEVICE
]))
437 #endif /* WINDOWSNT */
439 Since device is ignored in Windows, it does not matter
445 /* END: Common functions */
447 /* BEGIN: Non Windows functions */
450 /* Find out the type of the sound file whose file descriptor is FD.
451 S is the sound file structure to fill in. */
457 if (!wav_init (s
) && !au_init (s
))
458 error ("Unknown sound format");
462 /* Function installed by play-sound-internal with record_unwind_protect. */
468 if (current_sound_device
->close
)
469 current_sound_device
->close (current_sound_device
);
470 if (current_sound
->fd
> 0)
471 emacs_close (current_sound
->fd
);
472 free (current_sound_device
);
473 free (current_sound
);
478 /***********************************************************************
479 Byte-order Conversion
480 ***********************************************************************/
482 /* Convert 32-bit value VALUE which is in little-endian byte-order
483 to host byte-order. */
489 #ifdef WORDS_BIG_ENDIAN
490 unsigned char *p
= (unsigned char *) &value
;
491 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
497 /* Convert 16-bit value VALUE which is in little-endian byte-order
498 to host byte-order. */
504 #ifdef WORDS_BIG_ENDIAN
505 unsigned char *p
= (unsigned char *) &value
;
506 value
= p
[0] + (p
[1] << 8);
512 /* Convert 32-bit value VALUE which is in big-endian byte-order
513 to host byte-order. */
519 #ifndef WORDS_BIG_ENDIAN
520 unsigned char *p
= (unsigned char *) &value
;
521 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
527 #if 0 /* Currently not used. */
529 /* Convert 16-bit value VALUE which is in big-endian byte-order
530 to host byte-order. */
536 #ifndef WORDS_BIG_ENDIAN
537 unsigned char *p
= (unsigned char *) &value
;
538 value
= p
[1] + (p
[0] << 8);
545 /***********************************************************************
547 ***********************************************************************/
549 /* Try to initialize sound file S from S->header. S->header
550 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
551 sound file. If the file is a WAV-format file, set up interface
552 functions in S and convert header fields to host byte-order.
553 Value is non-zero if the file is a WAV file. */
559 struct wav_header
*header
= (struct wav_header
*) s
->header
;
561 if (s
->header_size
< sizeof *header
562 || bcmp (s
->header
, "RIFF", 4) != 0)
565 /* WAV files are in little-endian order. Convert the header
566 if on a big-endian machine. */
567 header
->magic
= le2hl (header
->magic
);
568 header
->length
= le2hl (header
->length
);
569 header
->chunk_type
= le2hl (header
->chunk_type
);
570 header
->chunk_format
= le2hl (header
->chunk_format
);
571 header
->chunk_length
= le2hl (header
->chunk_length
);
572 header
->format
= le2hs (header
->format
);
573 header
->channels
= le2hs (header
->channels
);
574 header
->sample_rate
= le2hl (header
->sample_rate
);
575 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
576 header
->sample_size
= le2hs (header
->sample_size
);
577 header
->precision
= le2hs (header
->precision
);
578 header
->chunk_data
= le2hl (header
->chunk_data
);
579 header
->data_length
= le2hl (header
->data_length
);
581 /* Set up the interface functions for WAV. */
589 /* Play RIFF-WAVE audio file S on sound device SD. */
594 struct sound_device
*sd
;
596 struct wav_header
*header
= (struct wav_header
*) s
->header
;
598 /* Let the device choose a suitable device-dependent format
600 sd
->choose_format (sd
, s
);
602 /* Configure the device. */
603 sd
->sample_size
= header
->sample_size
;
604 sd
->sample_rate
= header
->sample_rate
;
605 sd
->bps
= header
->bytes_per_second
;
606 sd
->channels
= header
->channels
;
609 /* Copy sound data to the device. The WAV file specification is
610 actually more complex. This simple scheme worked with all WAV
611 files I found so far. If someone feels inclined to implement the
612 whole RIFF-WAVE spec, please do. */
613 if (STRINGP (s
->data
))
614 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
615 SBYTES (s
->data
) - sizeof *header
);
620 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
621 int data_left
= header
->data_length
;
623 buffer
= (char *) alloca (blksize
);
624 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
626 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
628 /* Don't play possible garbage at the end of file */
629 if (data_left
< nbytes
) nbytes
= data_left
;
631 sd
->write (sd
, buffer
, nbytes
);
635 sound_perror ("Error reading sound file");
640 /***********************************************************************
642 ***********************************************************************/
644 /* Sun audio file encodings. */
648 AU_ENCODING_ULAW_8
= 1,
656 AU_ENCODING_ALAW_8
= 27
660 /* Try to initialize sound file S from S->header. S->header
661 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
662 sound file. If the file is a AU-format file, set up interface
663 functions in S and convert header fields to host byte-order.
664 Value is non-zero if the file is an AU file. */
670 struct au_header
*header
= (struct au_header
*) s
->header
;
672 if (s
->header_size
< sizeof *header
673 || bcmp (s
->header
, ".snd", 4) != 0)
676 header
->magic_number
= be2hl (header
->magic_number
);
677 header
->data_offset
= be2hl (header
->data_offset
);
678 header
->data_size
= be2hl (header
->data_size
);
679 header
->encoding
= be2hl (header
->encoding
);
680 header
->sample_rate
= be2hl (header
->sample_rate
);
681 header
->channels
= be2hl (header
->channels
);
683 /* Set up the interface functions for AU. */
691 /* Play Sun audio file S on sound device SD. */
696 struct sound_device
*sd
;
698 struct au_header
*header
= (struct au_header
*) s
->header
;
701 sd
->sample_rate
= header
->sample_rate
;
703 sd
->channels
= header
->channels
;
704 sd
->choose_format (sd
, s
);
707 if (STRINGP (s
->data
))
708 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
709 SBYTES (s
->data
) - header
->data_offset
);
712 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
717 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
719 /* Copy sound data to the device. */
720 buffer
= (char *) alloca (blksize
);
721 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
722 sd
->write (sd
, buffer
, nbytes
);
725 sound_perror ("Error reading sound file");
730 /***********************************************************************
731 Voxware Driver Interface
732 ***********************************************************************/
734 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
735 has a compatible own driver aka Luigi's driver. */
738 /* Open device SD. If SD->file is non-null, open that device,
739 otherwise use a default device name. */
743 struct sound_device
*sd
;
747 /* Open the sound device. Default is /dev/dsp. */
751 file
= DEFAULT_SOUND_DEVICE
;
753 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
759 /* Configure device SD from parameters in it. */
763 struct sound_device
*sd
;
767 xassert (sd
->fd
>= 0);
769 /* On GNU/Linux, it seems that the device driver doesn't like to be
770 interrupted by a signal. Block the ones we know to cause
774 sigblock (sigmask (SIGIO
));
778 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
779 || val
!= sd
->format
)
780 sound_perror ("Could not set sound format");
782 val
= sd
->channels
!= 1;
783 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
784 || val
!= (sd
->channels
!= 1))
785 sound_perror ("Could not set stereo/mono");
787 /* I think bps and sampling_rate are the same, but who knows.
788 Check this. and use SND_DSP_SPEED for both. */
789 if (sd
->sample_rate
> 0)
791 val
= sd
->sample_rate
;
792 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
793 sound_perror ("Could not set sound speed");
794 else if (val
!= sd
->sample_rate
)
795 sound_warning ("Could not set sample rate");
800 int volume
= sd
->volume
& 0xff;
801 volume
|= volume
<< 8;
802 /* This may fail if there is no mixer. Ignore the failure. */
803 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
808 sigunblock (sigmask (SIGIO
));
813 /* Close device SD if it is open. */
817 struct sound_device
*sd
;
821 /* On GNU/Linux, it seems that the device driver doesn't like to
822 be interrupted by a signal. Block the ones we know to cause
825 sigblock (sigmask (SIGIO
));
829 /* Flush sound data, and reset the device. */
830 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
834 sigunblock (sigmask (SIGIO
));
837 /* Close the device. */
838 emacs_close (sd
->fd
);
844 /* Choose device-dependent format for device SD from sound file S. */
847 vox_choose_format (sd
, s
)
848 struct sound_device
*sd
;
853 struct wav_header
*h
= (struct wav_header
*) s
->header
;
854 if (h
->precision
== 8)
855 sd
->format
= AFMT_U8
;
856 else if (h
->precision
== 16)
857 sd
->format
= AFMT_S16_LE
;
859 error ("Unsupported WAV file format");
861 else if (s
->type
== SUN_AUDIO
)
863 struct au_header
*header
= (struct au_header
*) s
->header
;
864 switch (header
->encoding
)
866 case AU_ENCODING_ULAW_8
:
867 case AU_ENCODING_IEEE32
:
868 case AU_ENCODING_IEEE64
:
869 sd
->format
= AFMT_MU_LAW
;
876 sd
->format
= AFMT_S16_LE
;
880 error ("Unsupported AU file format");
888 /* Initialize device SD. Set up the interface functions in the device
893 struct sound_device
*sd
;
898 /* Open the sound device. Default is /dev/dsp. */
902 file
= DEFAULT_SOUND_DEVICE
;
903 fd
= emacs_open (file
, O_WRONLY
, 0);
911 sd
->close
= vox_close
;
912 sd
->configure
= vox_configure
;
913 sd
->choose_format
= vox_choose_format
;
914 sd
->write
= vox_write
;
915 sd
->period_size
= NULL
;
920 /* Write NBYTES bytes from BUFFER to device SD. */
923 vox_write (sd
, buffer
, nbytes
)
924 struct sound_device
*sd
;
928 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
930 sound_perror ("Error writing to sound device");
934 /***********************************************************************
935 ALSA Driver Interface
936 ***********************************************************************/
938 /* This driver is available on GNU/Linux. */
941 alsa_sound_perror (msg
, err
)
945 error ("%s: %s", msg
, snd_strerror (err
));
951 snd_pcm_hw_params_t
*hwparams
;
952 snd_pcm_sw_params_t
*swparams
;
953 snd_pcm_uframes_t period_size
;
956 /* Open device SD. If SD->file is non-null, open that device,
957 otherwise use a default device name. */
961 struct sound_device
*sd
;
964 struct alsa_params
*p
;
967 /* Open the sound device. Default is "default". */
971 file
= DEFAULT_ALSA_SOUND_DEVICE
;
973 p
= xmalloc (sizeof (*p
));
982 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
984 alsa_sound_perror (file
, err
);
988 alsa_period_size (sd
)
989 struct sound_device
*sd
;
991 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
992 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
993 return p
->period_size
* (fact
> 0 ? fact
: 1);
998 struct sound_device
*sd
;
1002 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1003 snd_pcm_uframes_t buffer_size
;
1005 xassert (p
->handle
!= 0);
1007 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
1009 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
1011 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
1013 alsa_sound_perror ("Could not allocate software parameter structure", err
);
1015 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
1017 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
1019 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
1020 SND_PCM_ACCESS_RW_INTERLEAVED
);
1022 alsa_sound_perror ("Could not set access type", err
);
1025 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
1027 alsa_sound_perror ("Could not set sound format", err
);
1029 uval
= sd
->sample_rate
;
1030 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
1032 alsa_sound_perror ("Could not set sample rate", err
);
1035 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
1037 alsa_sound_perror ("Could not set channel count", err
);
1039 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
1041 alsa_sound_perror ("Could not set parameters", err
);
1044 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
1046 alsa_sound_perror ("Unable to get period size for playback", err
);
1048 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
1050 alsa_sound_perror("Unable to get buffer size for playback", err
);
1052 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1054 alsa_sound_perror ("Unable to determine current swparams for playback",
1057 /* Start the transfer when the buffer is almost full */
1058 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1059 (buffer_size
/ p
->period_size
)
1062 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1064 /* Allow the transfer when at least period_size samples can be processed */
1065 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1067 alsa_sound_perror ("Unable to set avail min for playback", err
);
1069 /* Align all transfers to 1 period */
1070 err
= snd_pcm_sw_params_set_xfer_align (p
->handle
, p
->swparams
,
1073 alsa_sound_perror ("Unable to set transfer align for playback", err
);
1075 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1077 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1079 snd_pcm_hw_params_free (p
->hwparams
);
1081 snd_pcm_sw_params_free (p
->swparams
);
1084 err
= snd_pcm_prepare (p
->handle
);
1086 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1091 snd_mixer_t
*handle
;
1092 snd_mixer_elem_t
*e
;
1093 char *file
= sd
->file
? sd
->file
: DEFAULT_ALSA_SOUND_DEVICE
;
1095 if (snd_mixer_open (&handle
, 0) >= 0)
1097 if (snd_mixer_attach (handle
, file
) >= 0
1098 && snd_mixer_load (handle
) >= 0
1099 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1100 for (e
= snd_mixer_first_elem (handle
);
1102 e
= snd_mixer_elem_next (e
))
1104 if (snd_mixer_selem_has_playback_volume (e
))
1106 long pmin
, pmax
, vol
;
1107 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1108 vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1110 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1111 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1114 snd_mixer_close(handle
);
1120 /* Close device SD if it is open. */
1124 struct sound_device
*sd
;
1126 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1130 snd_pcm_hw_params_free (p
->hwparams
);
1132 snd_pcm_sw_params_free (p
->swparams
);
1135 snd_pcm_drain (p
->handle
);
1136 snd_pcm_close (p
->handle
);
1142 /* Choose device-dependent format for device SD from sound file S. */
1145 alsa_choose_format (sd
, s
)
1146 struct sound_device
*sd
;
1149 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1150 if (s
->type
== RIFF
)
1152 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1153 if (h
->precision
== 8)
1154 sd
->format
= SND_PCM_FORMAT_U8
;
1155 else if (h
->precision
== 16)
1156 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1158 error ("Unsupported WAV file format");
1160 else if (s
->type
== SUN_AUDIO
)
1162 struct au_header
*header
= (struct au_header
*) s
->header
;
1163 switch (header
->encoding
)
1165 case AU_ENCODING_ULAW_8
:
1166 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1168 case AU_ENCODING_ALAW_8
:
1169 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1171 case AU_ENCODING_IEEE32
:
1172 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1174 case AU_ENCODING_IEEE64
:
1175 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1178 sd
->format
= SND_PCM_FORMAT_S8
;
1180 case AU_ENCODING_16
:
1181 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1183 case AU_ENCODING_24
:
1184 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1186 case AU_ENCODING_32
:
1187 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1191 error ("Unsupported AU file format");
1199 /* Write NBYTES bytes from BUFFER to device SD. */
1202 alsa_write (sd
, buffer
, nbytes
)
1203 struct sound_device
*sd
;
1207 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1209 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1210 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1214 while (nwritten
< nbytes
)
1216 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1217 if (frames
== 0) break;
1219 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1224 err
= snd_pcm_prepare (p
->handle
);
1226 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1229 else if (err
== -ESTRPIPE
)
1231 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1232 sleep(1); /* wait until the suspend flag is released */
1235 err
= snd_pcm_prepare (p
->handle
);
1237 alsa_sound_perror ("Can't recover from suspend, "
1243 alsa_sound_perror ("Error writing to sound device", err
);
1247 nwritten
+= err
* fact
;
1252 snd_error_quiet (file
, line
, function
, err
, fmt
)
1255 const char *function
;
1261 /* Initialize device SD. Set up the interface functions in the device
1266 struct sound_device
*sd
;
1272 /* Open the sound device. Default is "default". */
1276 file
= DEFAULT_ALSA_SOUND_DEVICE
;
1278 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1279 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1280 snd_lib_error_set_handler (NULL
);
1283 snd_pcm_close (handle
);
1286 sd
->open
= alsa_open
;
1287 sd
->close
= alsa_close
;
1288 sd
->configure
= alsa_configure
;
1289 sd
->choose_format
= alsa_choose_format
;
1290 sd
->write
= alsa_write
;
1291 sd
->period_size
= alsa_period_size
;
1296 #endif /* HAVE_ALSA */
1299 /* END: Non Windows functions */
1300 #else /* WINDOWSNT */
1302 /* BEGIN: Windows specific functions */
1304 #define SOUND_WARNING(fun, error, text) \
1307 char err_string[MAXERRORLENGTH]; \
1308 fun (error, err_string, sizeof (err_string)); \
1309 _snprintf (buf, sizeof (buf), "%s\nError: %s", \
1310 text, err_string); \
1311 sound_warning (buf); \
1315 do_play_sound (psz_file
, ui_volume
)
1316 const char *psz_file
;
1317 unsigned long ui_volume
;
1320 MCIERROR mci_error
= 0;
1321 char sz_cmd_buf
[520] = {0};
1322 char sz_ret_buf
[520] = {0};
1323 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1324 unsigned long ui_volume_org
= 0;
1325 BOOL b_reset_volume
= FALSE
;
1327 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1328 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1329 sprintf (sz_cmd_buf
,
1330 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1332 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1335 SOUND_WARNING (mciGetErrorString
, mci_error
,
1336 "The open mciSendString command failed to open "
1337 "the specified sound file.");
1338 i_result
= (int) mci_error
;
1341 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1343 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1344 if (mm_result
== MMSYSERR_NOERROR
)
1346 b_reset_volume
= TRUE
;
1347 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1348 if (mm_result
!= MMSYSERR_NOERROR
)
1350 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1351 "waveOutSetVolume failed to set the volume level "
1352 "of the WAVE_MAPPER device.\n"
1353 "As a result, the user selected volume level will "
1359 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1360 "waveOutGetVolume failed to obtain the original "
1361 "volume level of the WAVE_MAPPER device.\n"
1362 "As a result, the user selected volume level will "
1366 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1367 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1368 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1369 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1372 SOUND_WARNING (mciGetErrorString
, mci_error
,
1373 "The play mciSendString command failed to play the "
1374 "opened sound file.");
1375 i_result
= (int) mci_error
;
1377 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1378 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1379 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1380 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1381 if (b_reset_volume
== TRUE
)
1383 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1384 if (mm_result
!= MMSYSERR_NOERROR
)
1386 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1387 "waveOutSetVolume failed to reset the original volume "
1388 "level of the WAVE_MAPPER device.");
1394 /* END: Windows specific functions */
1396 #endif /* WINDOWSNT */
1398 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1399 doc
: /* Play sound SOUND.
1401 Internal use only, use `play-sound' instead. */)
1405 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1406 int count
= SPECPDL_INDEX ();
1410 struct gcpro gcpro1
, gcpro2
;
1411 Lisp_Object args
[2];
1412 #else /* WINDOWSNT */
1414 Lisp_Object lo_file
= {0};
1415 char * psz_file
= NULL
;
1416 unsigned long ui_volume_tmp
= UINT_MAX
;
1417 unsigned long ui_volume
= UINT_MAX
;
1419 #endif /* WINDOWSNT */
1421 /* Parse the sound specification. Give up if it is invalid. */
1422 if (!parse_sound (sound
, attrs
))
1423 error ("Invalid sound specification");
1427 GCPRO2 (sound
, file
);
1428 current_sound_device
= (struct sound_device
*) xmalloc (sizeof (struct sound_device
));
1429 bzero (current_sound_device
, sizeof (struct sound_device
));
1430 current_sound
= (struct sound
*) xmalloc (sizeof (struct sound
));
1431 bzero (current_sound
, sizeof (struct sound
));
1432 record_unwind_protect (sound_cleanup
, Qnil
);
1433 current_sound
->header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1435 if (STRINGP (attrs
[SOUND_FILE
]))
1437 /* Open the sound file. */
1438 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1439 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1440 if (current_sound
->fd
< 0)
1441 sound_perror ("Could not open sound file");
1443 /* Read the first bytes from the file. */
1444 current_sound
->header_size
1445 = emacs_read (current_sound
->fd
, current_sound
->header
,
1446 MAX_SOUND_HEADER_BYTES
);
1447 if (current_sound
->header_size
< 0)
1448 sound_perror ("Invalid sound file header");
1452 current_sound
->data
= attrs
[SOUND_DATA
];
1453 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1454 bcopy (SDATA (current_sound
->data
), current_sound
->header
, current_sound
->header_size
);
1457 /* Find out the type of sound. Give up if we can't tell. */
1458 find_sound_type (current_sound
);
1460 /* Set up a device. */
1461 if (STRINGP (attrs
[SOUND_DEVICE
]))
1463 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1464 current_sound_device
->file
= (char *) alloca (len
+ 1);
1465 strcpy (current_sound_device
->file
, SDATA (attrs
[SOUND_DEVICE
]));
1468 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1469 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1470 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1471 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1473 args
[0] = Qplay_sound_functions
;
1475 Frun_hook_with_args (2, args
);
1478 if (!alsa_init (current_sound_device
))
1480 if (!vox_init (current_sound_device
))
1481 error ("No usable sound device driver found");
1483 /* Open the device. */
1484 current_sound_device
->open (current_sound_device
);
1486 /* Play the sound. */
1487 current_sound
->play (current_sound
, current_sound_device
);
1492 #else /* WINDOWSNT */
1494 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1495 len
= XSTRING (lo_file
)->size
;
1496 psz_file
= (char *) alloca (len
+ 1);
1497 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1498 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1500 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1502 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1504 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1507 Based on some experiments I have conducted, a value of 100 or less
1508 for the sound volume is much too low. You cannot even hear it.
1509 A value of UINT_MAX indicates that you wish for the sound to played
1510 at the maximum possible volume. A value of UINT_MAX/2 plays the
1511 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1512 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1513 The following code adjusts the user specified volume level appropriately.
1515 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1517 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1519 i_result
= do_play_sound (psz_file
, ui_volume
);
1521 #endif /* WINDOWSNT */
1523 unbind_to (count
, Qnil
);
1527 /***********************************************************************
1529 ***********************************************************************/
1534 QCdevice
= intern (":device");
1535 staticpro (&QCdevice
);
1536 QCvolume
= intern (":volume");
1537 staticpro (&QCvolume
);
1538 Qsound
= intern ("sound");
1539 staticpro (&Qsound
);
1540 Qplay_sound_functions
= intern ("play-sound-functions");
1541 staticpro (&Qplay_sound_functions
);
1543 defsubr (&Splay_sound_internal
);
1552 #endif /* HAVE_SOUND */
1554 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1555 (do not change this comment) */