1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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, 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 #ifdef ALSA_SUBDIR_INCLUDE
78 #include <alsa/asoundlib.h>
80 #include <asoundlib.h>
81 #endif /* ALSA_SUBDIR_INCLUDE */
82 #endif /* HAVE_ALSA */
84 /* END: Non Windows Includes */
88 /* BEGIN: Windows Specific Includes */
95 /* END: Windows Specific Includes */
97 #endif /* WINDOWSNT */
99 /* BEGIN: Common Definitions */
103 extern Lisp_Object QCfile
, QCdata
;
104 Lisp_Object QCvolume
, QCdevice
;
106 Lisp_Object Qplay_sound_functions
;
108 /* Indices of attributes in a sound attributes vector. */
119 static void alsa_sound_perror
P_ ((char *, int)) NO_RETURN
;
120 static void sound_perror
P_ ((char *)) NO_RETURN
;
121 static void sound_warning
P_ ((char *));
122 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
124 /* END: Common Definitions */
126 /* BEGIN: Non Windows Definitions */
129 #ifndef DEFAULT_SOUND_DEVICE
130 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
132 #ifndef DEFAULT_ALSA_SOUND_DEVICE
133 #define DEFAULT_ALSA_SOUND_DEVICE "default"
137 /* Structure forward declarations. */
142 /* The file header of RIFF-WAVE files (*.wav). Files are always in
143 little-endian byte-order. */
149 u_int32_t chunk_type
;
150 u_int32_t chunk_format
;
151 u_int32_t chunk_length
;
154 u_int32_t sample_rate
;
155 u_int32_t bytes_per_second
;
156 u_int16_t sample_size
;
158 u_int32_t chunk_data
;
159 u_int32_t data_length
;
162 /* The file header of Sun adio files (*.au). Files are always in
163 big-endian byte-order. */
168 u_int32_t magic_number
;
170 /* Offset of data part from start of file. Minimum value is 24. */
171 u_int32_t data_offset
;
173 /* Size of data part, 0xffffffff if unknown. */
176 /* Data encoding format.
178 2 8-bit linear PCM (REF-PCM)
182 6 32-bit IEEE floating-point
183 7 64-bit IEEE floating-point
184 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
188 /* Number of samples per second. */
189 u_int32_t sample_rate
;
191 /* Number of interleaved channels. */
195 /* Maximum of all sound file headers sizes. */
197 #define MAX_SOUND_HEADER_BYTES \
198 max (sizeof (struct wav_header), sizeof (struct au_header))
200 /* Interface structure for sound devices. */
204 /* The name of the device or null meaning use a default device name. */
207 /* File descriptor of the device. */
210 /* Device-dependent format. */
213 /* Volume (0..100). Zero means unspecified. */
222 /* Bytes per second. */
225 /* 1 = mono, 2 = stereo, 0 = don't set. */
228 /* Open device SD. */
229 void (* open
) P_ ((struct sound_device
*sd
));
231 /* Close device SD. */
232 void (* close
) P_ ((struct sound_device
*sd
));
234 /* Configure SD accoring to device-dependent parameters. */
235 void (* configure
) P_ ((struct sound_device
*device
));
237 /* Choose a device-dependent format for outputting sound S. */
238 void (* choose_format
) P_ ((struct sound_device
*sd
,
241 /* Return a preferred data size in bytes to be sent to write (below)
242 each time. 2048 is used if this is NULL. */
243 int (* period_size
) P_ ((struct sound_device
*sd
));
245 /* Write NYBTES bytes from BUFFER to device SD. */
246 void (* write
) P_ ((struct sound_device
*sd
, const char *buffer
,
249 /* A place for devices to store additional data. */
253 /* An enumerator for each supported sound file type. */
261 /* Interface structure for sound files. */
265 /* The type of the file. */
266 enum sound_type type
;
268 /* File descriptor of a sound file. */
271 /* Pointer to sound file header. This contains header_size bytes
272 read from the start of a sound file. */
275 /* Number of bytes raed from sound file. This is always <=
276 MAX_SOUND_HEADER_BYTES. */
279 /* Sound data, if a string. */
282 /* Play sound file S on device SD. */
283 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
286 /* These are set during `play-sound-internal' so that sound_cleanup has
289 struct sound_device
*current_sound_device
;
290 struct sound
*current_sound
;
292 /* Function prototypes. */
294 static void vox_open
P_ ((struct sound_device
*));
295 static void vox_configure
P_ ((struct sound_device
*));
296 static void vox_close
P_ ((struct sound_device
*sd
));
297 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
298 static int vox_init
P_ ((struct sound_device
*));
299 static void vox_write
P_ ((struct sound_device
*, const char *, int));
300 static void find_sound_type
P_ ((struct sound
*));
301 static u_int32_t le2hl
P_ ((u_int32_t
));
302 static u_int16_t le2hs
P_ ((u_int16_t
));
303 static u_int32_t be2hl
P_ ((u_int32_t
));
304 static int wav_init
P_ ((struct sound
*));
305 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
306 static int au_init
P_ ((struct sound
*));
307 static void au_play
P_ ((struct sound
*, struct sound_device
*));
309 #if 0 /* Currently not used. */
310 static u_int16_t be2hs
P_ ((u_int16_t
));
313 /* END: Non Windows Definitions */
314 #else /* WINDOWSNT */
316 /* BEGIN: Windows Specific Definitions */
317 static int do_play_sound
P_ ((const char *, unsigned long));
319 END: Windows Specific Definitions */
320 #endif /* WINDOWSNT */
323 /***********************************************************************
325 ***********************************************************************/
327 /* BEGIN: Common functions */
329 /* Like perror, but signals an error. */
335 int saved_errno
= errno
;
339 sigunblock (sigmask (SIGIO
));
341 if (saved_errno
!= 0)
342 error ("%s: %s", msg
, strerror (saved_errno
));
348 /* Display a warning message. */
358 /* Parse sound specification SOUND, and fill ATTRS with what is
359 found. Value is non-zero if SOUND Is a valid sound specification.
360 A valid sound specification is a list starting with the symbol
361 `sound'. The rest of the list is a property list which may
362 contain the following key/value pairs:
366 FILE is the sound file to play. If it isn't an absolute name,
367 it's searched under `data-directory'.
371 DATA is a string containing sound data. Either :file or :data
372 may be present, but not both.
376 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
377 If not specified, a default device is used.
381 VOL must be an integer in the range [0, 100], or a float in the
385 parse_sound (sound
, attrs
)
389 /* SOUND must be a list starting with the symbol `sound'. */
390 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
393 sound
= XCDR (sound
);
394 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
395 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
396 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
397 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
400 /* File name or data must be specified. */
401 if (!STRINGP (attrs
[SOUND_FILE
])
402 && !STRINGP (attrs
[SOUND_DATA
]))
404 #else /* WINDOWSNT */
406 Data is not supported in Windows. Therefore a
407 File name MUST be supplied.
409 if (!STRINGP (attrs
[SOUND_FILE
]))
413 #endif /* WINDOWSNT */
415 /* Volume must be in the range 0..100 or unspecified. */
416 if (!NILP (attrs
[SOUND_VOLUME
]))
418 if (INTEGERP (attrs
[SOUND_VOLUME
]))
420 if (XINT (attrs
[SOUND_VOLUME
]) < 0
421 || XINT (attrs
[SOUND_VOLUME
]) > 100)
424 else if (FLOATP (attrs
[SOUND_VOLUME
]))
426 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
427 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
435 /* Device must be a string or unspecified. */
436 if (!NILP (attrs
[SOUND_DEVICE
])
437 && !STRINGP (attrs
[SOUND_DEVICE
]))
439 #endif /* WINDOWSNT */
441 Since device is ignored in Windows, it does not matter
447 /* END: Common functions */
449 /* BEGIN: Non Windows functions */
452 /* Find out the type of the sound file whose file descriptor is FD.
453 S is the sound file structure to fill in. */
459 if (!wav_init (s
) && !au_init (s
))
460 error ("Unknown sound format");
464 /* Function installed by play-sound-internal with record_unwind_protect. */
470 if (current_sound_device
->close
)
471 current_sound_device
->close (current_sound_device
);
472 if (current_sound
->fd
> 0)
473 emacs_close (current_sound
->fd
);
474 free (current_sound_device
);
475 free (current_sound
);
480 /***********************************************************************
481 Byte-order Conversion
482 ***********************************************************************/
484 /* Convert 32-bit value VALUE which is in little-endian byte-order
485 to host byte-order. */
491 #ifdef WORDS_BIG_ENDIAN
492 unsigned char *p
= (unsigned char *) &value
;
493 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
499 /* Convert 16-bit value VALUE which is in little-endian byte-order
500 to host byte-order. */
506 #ifdef WORDS_BIG_ENDIAN
507 unsigned char *p
= (unsigned char *) &value
;
508 value
= p
[0] + (p
[1] << 8);
514 /* Convert 32-bit value VALUE which is in big-endian byte-order
515 to host byte-order. */
521 #ifndef WORDS_BIG_ENDIAN
522 unsigned char *p
= (unsigned char *) &value
;
523 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
529 #if 0 /* Currently not used. */
531 /* Convert 16-bit value VALUE which is in big-endian byte-order
532 to host byte-order. */
538 #ifndef WORDS_BIG_ENDIAN
539 unsigned char *p
= (unsigned char *) &value
;
540 value
= p
[1] + (p
[0] << 8);
547 /***********************************************************************
549 ***********************************************************************/
551 /* Try to initialize sound file S from S->header. S->header
552 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
553 sound file. If the file is a WAV-format file, set up interface
554 functions in S and convert header fields to host byte-order.
555 Value is non-zero if the file is a WAV file. */
561 struct wav_header
*header
= (struct wav_header
*) s
->header
;
563 if (s
->header_size
< sizeof *header
564 || bcmp (s
->header
, "RIFF", 4) != 0)
567 /* WAV files are in little-endian order. Convert the header
568 if on a big-endian machine. */
569 header
->magic
= le2hl (header
->magic
);
570 header
->length
= le2hl (header
->length
);
571 header
->chunk_type
= le2hl (header
->chunk_type
);
572 header
->chunk_format
= le2hl (header
->chunk_format
);
573 header
->chunk_length
= le2hl (header
->chunk_length
);
574 header
->format
= le2hs (header
->format
);
575 header
->channels
= le2hs (header
->channels
);
576 header
->sample_rate
= le2hl (header
->sample_rate
);
577 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
578 header
->sample_size
= le2hs (header
->sample_size
);
579 header
->precision
= le2hs (header
->precision
);
580 header
->chunk_data
= le2hl (header
->chunk_data
);
581 header
->data_length
= le2hl (header
->data_length
);
583 /* Set up the interface functions for WAV. */
591 /* Play RIFF-WAVE audio file S on sound device SD. */
596 struct sound_device
*sd
;
598 struct wav_header
*header
= (struct wav_header
*) s
->header
;
600 /* Let the device choose a suitable device-dependent format
602 sd
->choose_format (sd
, s
);
604 /* Configure the device. */
605 sd
->sample_size
= header
->sample_size
;
606 sd
->sample_rate
= header
->sample_rate
;
607 sd
->bps
= header
->bytes_per_second
;
608 sd
->channels
= header
->channels
;
611 /* Copy sound data to the device. The WAV file specification is
612 actually more complex. This simple scheme worked with all WAV
613 files I found so far. If someone feels inclined to implement the
614 whole RIFF-WAVE spec, please do. */
615 if (STRINGP (s
->data
))
616 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
617 SBYTES (s
->data
) - sizeof *header
);
622 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
623 int data_left
= header
->data_length
;
625 buffer
= (char *) alloca (blksize
);
626 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
628 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
630 /* Don't play possible garbage at the end of file */
631 if (data_left
< nbytes
) nbytes
= data_left
;
633 sd
->write (sd
, buffer
, nbytes
);
637 sound_perror ("Error reading sound file");
642 /***********************************************************************
644 ***********************************************************************/
646 /* Sun audio file encodings. */
650 AU_ENCODING_ULAW_8
= 1,
658 AU_ENCODING_ALAW_8
= 27
662 /* Try to initialize sound file S from S->header. S->header
663 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
664 sound file. If the file is a AU-format file, set up interface
665 functions in S and convert header fields to host byte-order.
666 Value is non-zero if the file is an AU file. */
672 struct au_header
*header
= (struct au_header
*) s
->header
;
674 if (s
->header_size
< sizeof *header
675 || bcmp (s
->header
, ".snd", 4) != 0)
678 header
->magic_number
= be2hl (header
->magic_number
);
679 header
->data_offset
= be2hl (header
->data_offset
);
680 header
->data_size
= be2hl (header
->data_size
);
681 header
->encoding
= be2hl (header
->encoding
);
682 header
->sample_rate
= be2hl (header
->sample_rate
);
683 header
->channels
= be2hl (header
->channels
);
685 /* Set up the interface functions for AU. */
693 /* Play Sun audio file S on sound device SD. */
698 struct sound_device
*sd
;
700 struct au_header
*header
= (struct au_header
*) s
->header
;
703 sd
->sample_rate
= header
->sample_rate
;
705 sd
->channels
= header
->channels
;
706 sd
->choose_format (sd
, s
);
709 if (STRINGP (s
->data
))
710 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
711 SBYTES (s
->data
) - header
->data_offset
);
714 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
719 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
721 /* Copy sound data to the device. */
722 buffer
= (char *) alloca (blksize
);
723 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
724 sd
->write (sd
, buffer
, nbytes
);
727 sound_perror ("Error reading sound file");
732 /***********************************************************************
733 Voxware Driver Interface
734 ***********************************************************************/
736 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
737 has a compatible own driver aka Luigi's driver. */
740 /* Open device SD. If SD->file is non-null, open that device,
741 otherwise use a default device name. */
745 struct sound_device
*sd
;
749 /* Open the sound device. Default is /dev/dsp. */
753 file
= DEFAULT_SOUND_DEVICE
;
755 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
761 /* Configure device SD from parameters in it. */
765 struct sound_device
*sd
;
769 xassert (sd
->fd
>= 0);
771 /* On GNU/Linux, it seems that the device driver doesn't like to be
772 interrupted by a signal. Block the ones we know to cause
776 sigblock (sigmask (SIGIO
));
780 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
781 || val
!= sd
->format
)
782 sound_perror ("Could not set sound format");
784 val
= sd
->channels
!= 1;
785 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
786 || val
!= (sd
->channels
!= 1))
787 sound_perror ("Could not set stereo/mono");
789 /* I think bps and sampling_rate are the same, but who knows.
790 Check this. and use SND_DSP_SPEED for both. */
791 if (sd
->sample_rate
> 0)
793 val
= sd
->sample_rate
;
794 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
795 sound_perror ("Could not set sound speed");
796 else if (val
!= sd
->sample_rate
)
797 sound_warning ("Could not set sample rate");
802 int volume
= sd
->volume
& 0xff;
803 volume
|= volume
<< 8;
804 /* This may fail if there is no mixer. Ignore the failure. */
805 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
810 sigunblock (sigmask (SIGIO
));
815 /* Close device SD if it is open. */
819 struct sound_device
*sd
;
823 /* On GNU/Linux, it seems that the device driver doesn't like to
824 be interrupted by a signal. Block the ones we know to cause
827 sigblock (sigmask (SIGIO
));
831 /* Flush sound data, and reset the device. */
832 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
836 sigunblock (sigmask (SIGIO
));
839 /* Close the device. */
840 emacs_close (sd
->fd
);
846 /* Choose device-dependent format for device SD from sound file S. */
849 vox_choose_format (sd
, s
)
850 struct sound_device
*sd
;
855 struct wav_header
*h
= (struct wav_header
*) s
->header
;
856 if (h
->precision
== 8)
857 sd
->format
= AFMT_U8
;
858 else if (h
->precision
== 16)
859 sd
->format
= AFMT_S16_LE
;
861 error ("Unsupported WAV file format");
863 else if (s
->type
== SUN_AUDIO
)
865 struct au_header
*header
= (struct au_header
*) s
->header
;
866 switch (header
->encoding
)
868 case AU_ENCODING_ULAW_8
:
869 case AU_ENCODING_IEEE32
:
870 case AU_ENCODING_IEEE64
:
871 sd
->format
= AFMT_MU_LAW
;
878 sd
->format
= AFMT_S16_LE
;
882 error ("Unsupported AU file format");
890 /* Initialize device SD. Set up the interface functions in the device
895 struct sound_device
*sd
;
900 /* Open the sound device. Default is /dev/dsp. */
904 file
= DEFAULT_SOUND_DEVICE
;
905 fd
= emacs_open (file
, O_WRONLY
, 0);
913 sd
->close
= vox_close
;
914 sd
->configure
= vox_configure
;
915 sd
->choose_format
= vox_choose_format
;
916 sd
->write
= vox_write
;
917 sd
->period_size
= NULL
;
922 /* Write NBYTES bytes from BUFFER to device SD. */
925 vox_write (sd
, buffer
, nbytes
)
926 struct sound_device
*sd
;
930 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
932 sound_perror ("Error writing to sound device");
936 /***********************************************************************
937 ALSA Driver Interface
938 ***********************************************************************/
940 /* This driver is available on GNU/Linux. */
943 alsa_sound_perror (msg
, err
)
947 error ("%s: %s", msg
, snd_strerror (err
));
953 snd_pcm_hw_params_t
*hwparams
;
954 snd_pcm_sw_params_t
*swparams
;
955 snd_pcm_uframes_t period_size
;
958 /* Open device SD. If SD->file is non-null, open that device,
959 otherwise use a default device name. */
963 struct sound_device
*sd
;
966 struct alsa_params
*p
;
969 /* Open the sound device. Default is "default". */
973 file
= DEFAULT_ALSA_SOUND_DEVICE
;
975 p
= xmalloc (sizeof (*p
));
984 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
986 alsa_sound_perror (file
, err
);
990 alsa_period_size (sd
)
991 struct sound_device
*sd
;
993 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
994 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
995 return p
->period_size
* (fact
> 0 ? fact
: 1);
1000 struct sound_device
*sd
;
1004 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1005 snd_pcm_uframes_t buffer_size
;
1007 xassert (p
->handle
!= 0);
1009 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
1011 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
1013 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
1015 alsa_sound_perror ("Could not allocate software parameter structure", err
);
1017 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
1019 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
1021 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
1022 SND_PCM_ACCESS_RW_INTERLEAVED
);
1024 alsa_sound_perror ("Could not set access type", err
);
1027 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
1029 alsa_sound_perror ("Could not set sound format", err
);
1031 uval
= sd
->sample_rate
;
1032 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
1034 alsa_sound_perror ("Could not set sample rate", err
);
1037 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
1039 alsa_sound_perror ("Could not set channel count", err
);
1041 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
1043 alsa_sound_perror ("Could not set parameters", err
);
1046 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
1048 alsa_sound_perror ("Unable to get period size for playback", err
);
1050 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
1052 alsa_sound_perror("Unable to get buffer size for playback", err
);
1054 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1056 alsa_sound_perror ("Unable to determine current swparams for playback",
1059 /* Start the transfer when the buffer is almost full */
1060 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1061 (buffer_size
/ p
->period_size
)
1064 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1066 /* Allow the transfer when at least period_size samples can be processed */
1067 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1069 alsa_sound_perror ("Unable to set avail min for playback", err
);
1071 /* Align all transfers to 1 period */
1072 err
= snd_pcm_sw_params_set_xfer_align (p
->handle
, p
->swparams
,
1075 alsa_sound_perror ("Unable to set transfer align for playback", err
);
1077 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1079 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1081 snd_pcm_hw_params_free (p
->hwparams
);
1083 snd_pcm_sw_params_free (p
->swparams
);
1086 err
= snd_pcm_prepare (p
->handle
);
1088 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1093 snd_mixer_t
*handle
;
1094 snd_mixer_elem_t
*e
;
1095 char *file
= sd
->file
? sd
->file
: DEFAULT_ALSA_SOUND_DEVICE
;
1097 if (snd_mixer_open (&handle
, 0) >= 0)
1099 if (snd_mixer_attach (handle
, file
) >= 0
1100 && snd_mixer_load (handle
) >= 0
1101 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1102 for (e
= snd_mixer_first_elem (handle
);
1104 e
= snd_mixer_elem_next (e
))
1106 if (snd_mixer_selem_has_playback_volume (e
))
1109 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1110 long vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1112 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1113 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1116 snd_mixer_close(handle
);
1122 /* Close device SD if it is open. */
1126 struct sound_device
*sd
;
1128 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1132 snd_pcm_hw_params_free (p
->hwparams
);
1134 snd_pcm_sw_params_free (p
->swparams
);
1137 snd_pcm_drain (p
->handle
);
1138 snd_pcm_close (p
->handle
);
1144 /* Choose device-dependent format for device SD from sound file S. */
1147 alsa_choose_format (sd
, s
)
1148 struct sound_device
*sd
;
1151 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1152 if (s
->type
== RIFF
)
1154 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1155 if (h
->precision
== 8)
1156 sd
->format
= SND_PCM_FORMAT_U8
;
1157 else if (h
->precision
== 16)
1158 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1160 error ("Unsupported WAV file format");
1162 else if (s
->type
== SUN_AUDIO
)
1164 struct au_header
*header
= (struct au_header
*) s
->header
;
1165 switch (header
->encoding
)
1167 case AU_ENCODING_ULAW_8
:
1168 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1170 case AU_ENCODING_ALAW_8
:
1171 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1173 case AU_ENCODING_IEEE32
:
1174 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1176 case AU_ENCODING_IEEE64
:
1177 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1180 sd
->format
= SND_PCM_FORMAT_S8
;
1182 case AU_ENCODING_16
:
1183 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1185 case AU_ENCODING_24
:
1186 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1188 case AU_ENCODING_32
:
1189 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1193 error ("Unsupported AU file format");
1201 /* Write NBYTES bytes from BUFFER to device SD. */
1204 alsa_write (sd
, buffer
, nbytes
)
1205 struct sound_device
*sd
;
1209 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1211 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1212 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1216 while (nwritten
< nbytes
)
1218 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1219 if (frames
== 0) break;
1221 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1226 err
= snd_pcm_prepare (p
->handle
);
1228 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1231 else if (err
== -ESTRPIPE
)
1233 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1234 sleep(1); /* wait until the suspend flag is released */
1237 err
= snd_pcm_prepare (p
->handle
);
1239 alsa_sound_perror ("Can't recover from suspend, "
1245 alsa_sound_perror ("Error writing to sound device", err
);
1249 nwritten
+= err
* fact
;
1254 snd_error_quiet (file
, line
, function
, err
, fmt
)
1257 const char *function
;
1263 /* Initialize device SD. Set up the interface functions in the device
1268 struct sound_device
*sd
;
1274 /* Open the sound device. Default is "default". */
1278 file
= DEFAULT_ALSA_SOUND_DEVICE
;
1280 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1281 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1282 snd_lib_error_set_handler (NULL
);
1285 snd_pcm_close (handle
);
1288 sd
->open
= alsa_open
;
1289 sd
->close
= alsa_close
;
1290 sd
->configure
= alsa_configure
;
1291 sd
->choose_format
= alsa_choose_format
;
1292 sd
->write
= alsa_write
;
1293 sd
->period_size
= alsa_period_size
;
1298 #endif /* HAVE_ALSA */
1301 /* END: Non Windows functions */
1302 #else /* WINDOWSNT */
1304 /* BEGIN: Windows specific functions */
1307 do_play_sound (psz_file
, ui_volume
)
1308 const char *psz_file
;
1309 unsigned long ui_volume
;
1312 MCIERROR mci_error
= 0;
1313 char sz_cmd_buf
[520] = {0};
1314 char sz_ret_buf
[520] = {0};
1315 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1316 unsigned long ui_volume_org
= 0;
1317 BOOL b_reset_volume
= FALSE
;
1319 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
1320 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
1321 sprintf (sz_cmd_buf
,
1322 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1324 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
1327 sound_warning ("The open mciSendString command failed to open\n"
1328 "the specified sound file");
1329 i_result
= (int) mci_error
;
1332 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1334 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1335 if (mm_result
== MMSYSERR_NOERROR
)
1337 b_reset_volume
= TRUE
;
1338 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1339 if ( mm_result
!= MMSYSERR_NOERROR
)
1341 sound_warning ("waveOutSetVolume failed to set the volume level\n"
1342 "of the WAVE_MAPPER device.\n"
1343 "As a result, the user selected volume level will\n"
1349 sound_warning ("waveOutGetVolume failed to obtain the original\n"
1350 "volume level of the WAVE_MAPPER device.\n"
1351 "As a result, the user selected volume level will\n"
1355 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
1356 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
1357 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1358 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
1361 sound_warning ("The play mciSendString command failed to play the\n"
1362 "opened sound file.");
1363 i_result
= (int) mci_error
;
1365 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
1366 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
1367 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1368 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
1369 if (b_reset_volume
== TRUE
)
1371 mm_result
=waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1372 if (mm_result
!= MMSYSERR_NOERROR
)
1374 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
1375 "level of the WAVE_MAPPER device.");
1381 /* END: Windows specific functions */
1383 #endif /* WINDOWSNT */
1385 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1386 doc
: /* Play sound SOUND.
1388 Internal use only, use `play-sound' instead. */)
1392 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1393 int count
= SPECPDL_INDEX ();
1397 struct gcpro gcpro1
, gcpro2
;
1398 Lisp_Object args
[2];
1399 #else /* WINDOWSNT */
1401 Lisp_Object lo_file
= {0};
1402 char * psz_file
= NULL
;
1403 unsigned long ui_volume_tmp
= UINT_MAX
;
1404 unsigned long ui_volume
= UINT_MAX
;
1406 #endif /* WINDOWSNT */
1408 /* Parse the sound specification. Give up if it is invalid. */
1409 if (!parse_sound (sound
, attrs
))
1410 error ("Invalid sound specification");
1414 GCPRO2 (sound
, file
);
1415 current_sound_device
= (struct sound_device
*) xmalloc (sizeof (struct sound_device
));
1416 bzero (current_sound_device
, sizeof (struct sound_device
));
1417 current_sound
= (struct sound
*) xmalloc (sizeof (struct sound
));
1418 bzero (current_sound
, sizeof (struct sound
));
1419 record_unwind_protect (sound_cleanup
, Qnil
);
1420 current_sound
->header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1422 if (STRINGP (attrs
[SOUND_FILE
]))
1424 /* Open the sound file. */
1425 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1426 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1427 if (current_sound
->fd
< 0)
1428 sound_perror ("Could not open sound file");
1430 /* Read the first bytes from the file. */
1431 current_sound
->header_size
1432 = emacs_read (current_sound
->fd
, current_sound
->header
,
1433 MAX_SOUND_HEADER_BYTES
);
1434 if (current_sound
->header_size
< 0)
1435 sound_perror ("Invalid sound file header");
1439 current_sound
->data
= attrs
[SOUND_DATA
];
1440 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1441 bcopy (SDATA (current_sound
->data
), current_sound
->header
, current_sound
->header_size
);
1444 /* Find out the type of sound. Give up if we can't tell. */
1445 find_sound_type (current_sound
);
1447 /* Set up a device. */
1448 if (STRINGP (attrs
[SOUND_DEVICE
]))
1450 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1451 current_sound_device
->file
= (char *) alloca (len
+ 1);
1452 strcpy (current_sound_device
->file
, SDATA (attrs
[SOUND_DEVICE
]));
1455 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1456 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1457 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1458 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1460 args
[0] = Qplay_sound_functions
;
1462 Frun_hook_with_args (2, args
);
1465 if (!alsa_init (current_sound_device
))
1467 if (!vox_init (current_sound_device
))
1468 error ("No usable sound device driver found");
1470 /* Open the device. */
1471 current_sound_device
->open (current_sound_device
);
1473 /* Play the sound. */
1474 current_sound
->play (current_sound
, current_sound_device
);
1479 #else /* WINDOWSNT */
1481 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1482 len
= XSTRING (lo_file
)->size
;
1483 psz_file
= (char *) alloca (len
+ 1);
1484 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1485 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1487 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1489 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1491 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1494 Based on some experiments I have conducted, a value of 100 or less
1495 for the sound volume is much too low. You cannot even hear it.
1496 A value of UINT_MAX indicates that you wish for the sound to played
1497 at the maximum possible volume. A value of UINT_MAX/2 plays the
1498 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1499 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1500 The following code adjusts the user specified volume level appropriately.
1502 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1504 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1506 i_result
= do_play_sound (psz_file
, ui_volume
);
1508 #endif /* WINDOWSNT */
1510 unbind_to (count
, Qnil
);
1514 /***********************************************************************
1516 ***********************************************************************/
1521 QCdevice
= intern (":device");
1522 staticpro (&QCdevice
);
1523 QCvolume
= intern (":volume");
1524 staticpro (&QCvolume
);
1525 Qsound
= intern ("sound");
1526 staticpro (&Qsound
);
1527 Qplay_sound_functions
= intern ("play-sound-functions");
1528 staticpro (&Qplay_sound_functions
);
1530 defsubr (&Splay_sound_internal
);
1539 #endif /* HAVE_SOUND */
1541 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1542 (do not change this comment) */