1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
21 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
24 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
25 implementation of the play-sound specification for Windows.
28 In the Windows implementation of play-sound-internal only the
29 :file and :volume keywords are supported. The :device keyword,
30 if present, is ignored. The :data keyword, if present, will
31 cause an error to be generated.
33 The Windows implementation of play-sound is implemented via the
34 Win32 API functions mciSendString, waveOutGetVolume, and
35 waveOutSetVolume which are exported by Winmm.dll.
40 #if defined HAVE_SOUND
42 /* BEGIN: Common Includes */
45 #include <sys/types.h>
49 #include "dispextern.h"
52 #include "syssignal.h"
53 /* END: Common Includes */
56 /* BEGIN: Non Windows Includes */
60 #include <sys/ioctl.h>
63 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
64 sys/soundcard.h. So, let's try whatever's there. */
66 #ifdef HAVE_MACHINE_SOUNDCARD_H
67 #include <machine/soundcard.h>
69 #ifdef HAVE_SYS_SOUNDCARD_H
70 #include <sys/soundcard.h>
72 #ifdef HAVE_SOUNDCARD_H
73 #include <soundcard.h>
76 #ifdef ALSA_SUBDIR_INCLUDE
77 #include <alsa/asoundlib.h>
79 #include <asoundlib.h>
80 #endif /* ALSA_SUBDIR_INCLUDE */
81 #endif /* HAVE_ALSA */
83 /* END: Non Windows Includes */
87 /* BEGIN: Windows Specific Includes */
94 /* END: Windows Specific Includes */
96 #endif /* WINDOWSNT */
98 /* BEGIN: Common Definitions */
102 extern Lisp_Object QCfile
, QCdata
;
103 Lisp_Object QCvolume
, QCdevice
;
105 Lisp_Object Qplay_sound_functions
;
107 /* Indices of attributes in a sound attributes vector. */
118 static void alsa_sound_perror (char *, int) NO_RETURN
;
119 static void sound_perror (char *) NO_RETURN
;
120 static void sound_warning (char *);
121 static int parse_sound (Lisp_Object
, Lisp_Object
*);
123 /* END: Common Definitions */
125 /* BEGIN: Non Windows Definitions */
128 #ifndef DEFAULT_SOUND_DEVICE
129 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
131 #ifndef DEFAULT_ALSA_SOUND_DEVICE
132 #define DEFAULT_ALSA_SOUND_DEVICE "default"
136 /* Structure forward declarations. */
141 /* The file header of RIFF-WAVE files (*.wav). Files are always in
142 little-endian byte-order. */
148 u_int32_t chunk_type
;
149 u_int32_t chunk_format
;
150 u_int32_t chunk_length
;
153 u_int32_t sample_rate
;
154 u_int32_t bytes_per_second
;
155 u_int16_t sample_size
;
157 u_int32_t chunk_data
;
158 u_int32_t data_length
;
161 /* The file header of Sun adio files (*.au). Files are always in
162 big-endian byte-order. */
167 u_int32_t magic_number
;
169 /* Offset of data part from start of file. Minimum value is 24. */
170 u_int32_t data_offset
;
172 /* Size of data part, 0xffffffff if unknown. */
175 /* Data encoding format.
177 2 8-bit linear PCM (REF-PCM)
181 6 32-bit IEEE floating-point
182 7 64-bit IEEE floating-point
183 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
187 /* Number of samples per second. */
188 u_int32_t sample_rate
;
190 /* Number of interleaved channels. */
194 /* Maximum of all sound file headers sizes. */
196 #define MAX_SOUND_HEADER_BYTES \
197 max (sizeof (struct wav_header), sizeof (struct au_header))
199 /* Interface structure for sound devices. */
203 /* The name of the device or null meaning use a default device name. */
206 /* File descriptor of the device. */
209 /* Device-dependent format. */
212 /* Volume (0..100). Zero means unspecified. */
221 /* Bytes per second. */
224 /* 1 = mono, 2 = stereo, 0 = don't set. */
227 /* Open device SD. */
228 void (* open
) (struct sound_device
*sd
);
230 /* Close device SD. */
231 void (* close
) (struct sound_device
*sd
);
233 /* Configure SD accoring to device-dependent parameters. */
234 void (* configure
) (struct sound_device
*device
);
236 /* Choose a device-dependent format for outputting sound S. */
237 void (* choose_format
) (struct sound_device
*sd
,
240 /* Return a preferred data size in bytes to be sent to write (below)
241 each time. 2048 is used if this is NULL. */
242 int (* period_size
) (struct sound_device
*sd
);
244 /* Write NYBTES bytes from BUFFER to device SD. */
245 void (* write
) (struct sound_device
*sd
, const char *buffer
,
248 /* A place for devices to store additional data. */
252 /* An enumerator for each supported sound file type. */
260 /* Interface structure for sound files. */
264 /* The type of the file. */
265 enum sound_type type
;
267 /* File descriptor of a sound file. */
270 /* Pointer to sound file header. This contains header_size bytes
271 read from the start of a sound file. */
274 /* Number of bytes raed from sound file. This is always <=
275 MAX_SOUND_HEADER_BYTES. */
278 /* Sound data, if a string. */
281 /* Play sound file S on device SD. */
282 void (* play
) (struct sound
*s
, struct sound_device
*sd
);
285 /* These are set during `play-sound-internal' so that sound_cleanup has
288 struct sound_device
*current_sound_device
;
289 struct sound
*current_sound
;
291 /* Function prototypes. */
293 static void vox_open (struct sound_device
*);
294 static void vox_configure (struct sound_device
*);
295 static void vox_close (struct sound_device
*sd
);
296 static void vox_choose_format (struct sound_device
*, struct sound
*);
297 static int vox_init (struct sound_device
*);
298 static void vox_write (struct sound_device
*, const char *, int);
299 static void find_sound_type (struct sound
*);
300 static u_int32_t
le2hl (u_int32_t
);
301 static u_int16_t
le2hs (u_int16_t
);
302 static u_int32_t
be2hl (u_int32_t
);
303 static int wav_init (struct sound
*);
304 static void wav_play (struct sound
*, struct sound_device
*);
305 static int au_init (struct sound
*);
306 static void au_play (struct sound
*, struct sound_device
*);
308 #if 0 /* Currently not used. */
309 static u_int16_t
be2hs (u_int16_t
);
312 /* END: Non Windows Definitions */
313 #else /* WINDOWSNT */
315 /* BEGIN: Windows Specific Definitions */
316 static int do_play_sound (const char *, unsigned long);
318 END: Windows Specific Definitions */
319 #endif /* WINDOWSNT */
322 /***********************************************************************
324 ***********************************************************************/
326 /* BEGIN: Common functions */
328 /* Like perror, but signals an error. */
331 sound_perror (char *msg
)
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. */
349 sound_warning (char *msg
)
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 (Lisp_Object sound
, Lisp_Object
*attrs
)
384 /* SOUND must be a list starting with the symbol `sound'. */
385 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
388 sound
= XCDR (sound
);
389 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
390 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
391 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
392 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
395 /* File name or data must be specified. */
396 if (!STRINGP (attrs
[SOUND_FILE
])
397 && !STRINGP (attrs
[SOUND_DATA
]))
399 #else /* WINDOWSNT */
401 Data is not supported in Windows. Therefore a
402 File name MUST be supplied.
404 if (!STRINGP (attrs
[SOUND_FILE
]))
408 #endif /* WINDOWSNT */
410 /* Volume must be in the range 0..100 or unspecified. */
411 if (!NILP (attrs
[SOUND_VOLUME
]))
413 if (INTEGERP (attrs
[SOUND_VOLUME
]))
415 if (XINT (attrs
[SOUND_VOLUME
]) < 0
416 || XINT (attrs
[SOUND_VOLUME
]) > 100)
419 else if (FLOATP (attrs
[SOUND_VOLUME
]))
421 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
422 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
430 /* Device must be a string or unspecified. */
431 if (!NILP (attrs
[SOUND_DEVICE
])
432 && !STRINGP (attrs
[SOUND_DEVICE
]))
434 #endif /* WINDOWSNT */
436 Since device is ignored in Windows, it does not matter
442 /* END: Common functions */
444 /* BEGIN: Non Windows functions */
447 /* Find out the type of the sound file whose file descriptor is FD.
448 S is the sound file structure to fill in. */
451 find_sound_type (struct sound
*s
)
453 if (!wav_init (s
) && !au_init (s
))
454 error ("Unknown sound format");
458 /* Function installed by play-sound-internal with record_unwind_protect. */
461 sound_cleanup (Lisp_Object arg
)
463 if (current_sound_device
->close
)
464 current_sound_device
->close (current_sound_device
);
465 if (current_sound
->fd
> 0)
466 emacs_close (current_sound
->fd
);
467 free (current_sound_device
);
468 free (current_sound
);
473 /***********************************************************************
474 Byte-order Conversion
475 ***********************************************************************/
477 /* Convert 32-bit value VALUE which is in little-endian byte-order
478 to host byte-order. */
481 le2hl (u_int32_t value
)
483 #ifdef WORDS_BIG_ENDIAN
484 unsigned char *p
= (unsigned char *) &value
;
485 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
491 /* Convert 16-bit value VALUE which is in little-endian byte-order
492 to host byte-order. */
495 le2hs (u_int16_t value
)
497 #ifdef WORDS_BIG_ENDIAN
498 unsigned char *p
= (unsigned char *) &value
;
499 value
= p
[0] + (p
[1] << 8);
505 /* Convert 32-bit value VALUE which is in big-endian byte-order
506 to host byte-order. */
509 be2hl (u_int32_t value
)
511 #ifndef WORDS_BIG_ENDIAN
512 unsigned char *p
= (unsigned char *) &value
;
513 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
519 #if 0 /* Currently not used. */
521 /* Convert 16-bit value VALUE which is in big-endian byte-order
522 to host byte-order. */
525 be2hs (u_int16_t value
)
527 #ifndef WORDS_BIG_ENDIAN
528 unsigned char *p
= (unsigned char *) &value
;
529 value
= p
[1] + (p
[0] << 8);
536 /***********************************************************************
538 ***********************************************************************/
540 /* Try to initialize sound file S from S->header. S->header
541 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
542 sound file. If the file is a WAV-format file, set up interface
543 functions in S and convert header fields to host byte-order.
544 Value is non-zero if the file is a WAV file. */
547 wav_init (struct sound
*s
)
549 struct wav_header
*header
= (struct wav_header
*) s
->header
;
551 if (s
->header_size
< sizeof *header
552 || memcmp (s
->header
, "RIFF", 4) != 0)
555 /* WAV files are in little-endian order. Convert the header
556 if on a big-endian machine. */
557 header
->magic
= le2hl (header
->magic
);
558 header
->length
= le2hl (header
->length
);
559 header
->chunk_type
= le2hl (header
->chunk_type
);
560 header
->chunk_format
= le2hl (header
->chunk_format
);
561 header
->chunk_length
= le2hl (header
->chunk_length
);
562 header
->format
= le2hs (header
->format
);
563 header
->channels
= le2hs (header
->channels
);
564 header
->sample_rate
= le2hl (header
->sample_rate
);
565 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
566 header
->sample_size
= le2hs (header
->sample_size
);
567 header
->precision
= le2hs (header
->precision
);
568 header
->chunk_data
= le2hl (header
->chunk_data
);
569 header
->data_length
= le2hl (header
->data_length
);
571 /* Set up the interface functions for WAV. */
579 /* Play RIFF-WAVE audio file S on sound device SD. */
582 wav_play (struct sound
*s
, struct sound_device
*sd
)
584 struct wav_header
*header
= (struct wav_header
*) s
->header
;
586 /* Let the device choose a suitable device-dependent format
588 sd
->choose_format (sd
, s
);
590 /* Configure the device. */
591 sd
->sample_size
= header
->sample_size
;
592 sd
->sample_rate
= header
->sample_rate
;
593 sd
->bps
= header
->bytes_per_second
;
594 sd
->channels
= header
->channels
;
597 /* Copy sound data to the device. The WAV file specification is
598 actually more complex. This simple scheme worked with all WAV
599 files I found so far. If someone feels inclined to implement the
600 whole RIFF-WAVE spec, please do. */
601 if (STRINGP (s
->data
))
602 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
603 SBYTES (s
->data
) - sizeof *header
);
608 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
609 int data_left
= header
->data_length
;
611 buffer
= (char *) alloca (blksize
);
612 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
614 && (nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
616 /* Don't play possible garbage at the end of file */
617 if (data_left
< nbytes
) nbytes
= data_left
;
619 sd
->write (sd
, buffer
, nbytes
);
623 sound_perror ("Error reading sound file");
628 /***********************************************************************
630 ***********************************************************************/
632 /* Sun audio file encodings. */
636 AU_ENCODING_ULAW_8
= 1,
644 AU_ENCODING_ALAW_8
= 27
648 /* Try to initialize sound file S from S->header. S->header
649 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
650 sound file. If the file is a AU-format file, set up interface
651 functions in S and convert header fields to host byte-order.
652 Value is non-zero if the file is an AU file. */
655 au_init (struct sound
*s
)
657 struct au_header
*header
= (struct au_header
*) s
->header
;
659 if (s
->header_size
< sizeof *header
660 || memcmp (s
->header
, ".snd", 4) != 0)
663 header
->magic_number
= be2hl (header
->magic_number
);
664 header
->data_offset
= be2hl (header
->data_offset
);
665 header
->data_size
= be2hl (header
->data_size
);
666 header
->encoding
= be2hl (header
->encoding
);
667 header
->sample_rate
= be2hl (header
->sample_rate
);
668 header
->channels
= be2hl (header
->channels
);
670 /* Set up the interface functions for AU. */
678 /* Play Sun audio file S on sound device SD. */
681 au_play (struct sound
*s
, struct sound_device
*sd
)
683 struct au_header
*header
= (struct au_header
*) s
->header
;
686 sd
->sample_rate
= header
->sample_rate
;
688 sd
->channels
= header
->channels
;
689 sd
->choose_format (sd
, s
);
692 if (STRINGP (s
->data
))
693 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
694 SBYTES (s
->data
) - header
->data_offset
);
697 int blksize
= sd
->period_size
? sd
->period_size (sd
) : 2048;
702 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
704 /* Copy sound data to the device. */
705 buffer
= (char *) alloca (blksize
);
706 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
707 sd
->write (sd
, buffer
, nbytes
);
710 sound_perror ("Error reading sound file");
715 /***********************************************************************
716 Voxware Driver Interface
717 ***********************************************************************/
719 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
720 has a compatible own driver aka Luigi's driver. */
723 /* Open device SD. If SD->file is non-null, open that device,
724 otherwise use a default device name. */
727 vox_open (struct sound_device
*sd
)
731 /* Open the sound device. Default is /dev/dsp. */
735 file
= DEFAULT_SOUND_DEVICE
;
737 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
743 /* Configure device SD from parameters in it. */
746 vox_configure (struct sound_device
*sd
)
750 xassert (sd
->fd
>= 0);
752 /* On GNU/Linux, it seems that the device driver doesn't like to be
753 interrupted by a signal. Block the ones we know to cause
757 sigblock (sigmask (SIGIO
));
761 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
762 || val
!= sd
->format
)
763 sound_perror ("Could not set sound format");
765 val
= sd
->channels
!= 1;
766 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
767 || val
!= (sd
->channels
!= 1))
768 sound_perror ("Could not set stereo/mono");
770 /* I think bps and sampling_rate are the same, but who knows.
771 Check this. and use SND_DSP_SPEED for both. */
772 if (sd
->sample_rate
> 0)
774 val
= sd
->sample_rate
;
775 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
776 sound_perror ("Could not set sound speed");
777 else if (val
!= sd
->sample_rate
)
778 sound_warning ("Could not set sample rate");
783 int volume
= sd
->volume
& 0xff;
784 volume
|= volume
<< 8;
785 /* This may fail if there is no mixer. Ignore the failure. */
786 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
791 sigunblock (sigmask (SIGIO
));
796 /* Close device SD if it is open. */
799 vox_close (struct sound_device
*sd
)
803 /* On GNU/Linux, it seems that the device driver doesn't like to
804 be interrupted by a signal. Block the ones we know to cause
807 sigblock (sigmask (SIGIO
));
811 /* Flush sound data, and reset the device. */
812 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
816 sigunblock (sigmask (SIGIO
));
819 /* Close the device. */
820 emacs_close (sd
->fd
);
826 /* Choose device-dependent format for device SD from sound file S. */
829 vox_choose_format (struct sound_device
*sd
, struct sound
*s
)
833 struct wav_header
*h
= (struct wav_header
*) s
->header
;
834 if (h
->precision
== 8)
835 sd
->format
= AFMT_U8
;
836 else if (h
->precision
== 16)
837 sd
->format
= AFMT_S16_LE
;
839 error ("Unsupported WAV file format");
841 else if (s
->type
== SUN_AUDIO
)
843 struct au_header
*header
= (struct au_header
*) s
->header
;
844 switch (header
->encoding
)
846 case AU_ENCODING_ULAW_8
:
847 case AU_ENCODING_IEEE32
:
848 case AU_ENCODING_IEEE64
:
849 sd
->format
= AFMT_MU_LAW
;
856 sd
->format
= AFMT_S16_LE
;
860 error ("Unsupported AU file format");
868 /* Initialize device SD. Set up the interface functions in the device
872 vox_init (struct sound_device
*sd
)
877 /* Open the sound device. Default is /dev/dsp. */
881 file
= DEFAULT_SOUND_DEVICE
;
882 fd
= emacs_open (file
, O_WRONLY
, 0);
890 sd
->close
= vox_close
;
891 sd
->configure
= vox_configure
;
892 sd
->choose_format
= vox_choose_format
;
893 sd
->write
= vox_write
;
894 sd
->period_size
= NULL
;
899 /* Write NBYTES bytes from BUFFER to device SD. */
902 vox_write (struct sound_device
*sd
, const char *buffer
, int nbytes
)
904 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
906 sound_perror ("Error writing to sound device");
910 /***********************************************************************
911 ALSA Driver Interface
912 ***********************************************************************/
914 /* This driver is available on GNU/Linux. */
917 alsa_sound_perror (char *msg
, int err
)
919 error ("%s: %s", msg
, snd_strerror (err
));
925 snd_pcm_hw_params_t
*hwparams
;
926 snd_pcm_sw_params_t
*swparams
;
927 snd_pcm_uframes_t period_size
;
930 /* Open device SD. If SD->file is non-null, open that device,
931 otherwise use a default device name. */
934 alsa_open (struct sound_device
*sd
)
937 struct alsa_params
*p
;
940 /* Open the sound device. Default is "default". */
944 file
= DEFAULT_ALSA_SOUND_DEVICE
;
946 p
= xmalloc (sizeof (*p
));
955 err
= snd_pcm_open (&p
->handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
957 alsa_sound_perror (file
, err
);
961 alsa_period_size (struct sound_device
*sd
)
963 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
964 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
965 return p
->period_size
* (fact
> 0 ? fact
: 1);
969 alsa_configure (struct sound_device
*sd
)
973 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
974 snd_pcm_uframes_t buffer_size
;
976 xassert (p
->handle
!= 0);
978 err
= snd_pcm_hw_params_malloc (&p
->hwparams
);
980 alsa_sound_perror ("Could not allocate hardware parameter structure", err
);
982 err
= snd_pcm_sw_params_malloc (&p
->swparams
);
984 alsa_sound_perror ("Could not allocate software parameter structure", err
);
986 err
= snd_pcm_hw_params_any (p
->handle
, p
->hwparams
);
988 alsa_sound_perror ("Could not initialize hardware parameter structure", err
);
990 err
= snd_pcm_hw_params_set_access (p
->handle
, p
->hwparams
,
991 SND_PCM_ACCESS_RW_INTERLEAVED
);
993 alsa_sound_perror ("Could not set access type", err
);
996 err
= snd_pcm_hw_params_set_format (p
->handle
, p
->hwparams
, val
);
998 alsa_sound_perror ("Could not set sound format", err
);
1000 uval
= sd
->sample_rate
;
1001 err
= snd_pcm_hw_params_set_rate_near (p
->handle
, p
->hwparams
, &uval
, 0);
1003 alsa_sound_perror ("Could not set sample rate", err
);
1006 err
= snd_pcm_hw_params_set_channels (p
->handle
, p
->hwparams
, val
);
1008 alsa_sound_perror ("Could not set channel count", err
);
1010 err
= snd_pcm_hw_params (p
->handle
, p
->hwparams
);
1012 alsa_sound_perror ("Could not set parameters", err
);
1015 err
= snd_pcm_hw_params_get_period_size (p
->hwparams
, &p
->period_size
, &dir
);
1017 alsa_sound_perror ("Unable to get period size for playback", err
);
1019 err
= snd_pcm_hw_params_get_buffer_size (p
->hwparams
, &buffer_size
);
1021 alsa_sound_perror("Unable to get buffer size for playback", err
);
1023 err
= snd_pcm_sw_params_current (p
->handle
, p
->swparams
);
1025 alsa_sound_perror ("Unable to determine current swparams for playback",
1028 /* Start the transfer when the buffer is almost full */
1029 err
= snd_pcm_sw_params_set_start_threshold (p
->handle
, p
->swparams
,
1030 (buffer_size
/ p
->period_size
)
1033 alsa_sound_perror ("Unable to set start threshold mode for playback", err
);
1035 /* Allow the transfer when at least period_size samples can be processed */
1036 err
= snd_pcm_sw_params_set_avail_min (p
->handle
, p
->swparams
, p
->period_size
);
1038 alsa_sound_perror ("Unable to set avail min for playback", err
);
1040 err
= snd_pcm_sw_params (p
->handle
, p
->swparams
);
1042 alsa_sound_perror ("Unable to set sw params for playback\n", err
);
1044 snd_pcm_hw_params_free (p
->hwparams
);
1046 snd_pcm_sw_params_free (p
->swparams
);
1049 err
= snd_pcm_prepare (p
->handle
);
1051 alsa_sound_perror ("Could not prepare audio interface for use", err
);
1056 snd_mixer_t
*handle
;
1057 snd_mixer_elem_t
*e
;
1058 char *file
= sd
->file
? sd
->file
: DEFAULT_ALSA_SOUND_DEVICE
;
1060 if (snd_mixer_open (&handle
, 0) >= 0)
1062 if (snd_mixer_attach (handle
, file
) >= 0
1063 && snd_mixer_load (handle
) >= 0
1064 && snd_mixer_selem_register (handle
, NULL
, NULL
) >= 0)
1065 for (e
= snd_mixer_first_elem (handle
);
1067 e
= snd_mixer_elem_next (e
))
1069 if (snd_mixer_selem_has_playback_volume (e
))
1071 long pmin
, pmax
, vol
;
1072 snd_mixer_selem_get_playback_volume_range (e
, &pmin
, &pmax
);
1073 vol
= pmin
+ (sd
->volume
* (pmax
- pmin
)) / 100;
1075 for (chn
= 0; chn
<= SND_MIXER_SCHN_LAST
; chn
++)
1076 snd_mixer_selem_set_playback_volume (e
, chn
, vol
);
1079 snd_mixer_close(handle
);
1085 /* Close device SD if it is open. */
1088 alsa_close (struct sound_device
*sd
)
1090 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1094 snd_pcm_hw_params_free (p
->hwparams
);
1096 snd_pcm_sw_params_free (p
->swparams
);
1099 snd_pcm_drain (p
->handle
);
1100 snd_pcm_close (p
->handle
);
1106 /* Choose device-dependent format for device SD from sound file S. */
1109 alsa_choose_format (struct sound_device
*sd
, struct sound
*s
)
1111 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1112 if (s
->type
== RIFF
)
1114 struct wav_header
*h
= (struct wav_header
*) s
->header
;
1115 if (h
->precision
== 8)
1116 sd
->format
= SND_PCM_FORMAT_U8
;
1117 else if (h
->precision
== 16)
1118 sd
->format
= SND_PCM_FORMAT_S16_LE
;
1120 error ("Unsupported WAV file format");
1122 else if (s
->type
== SUN_AUDIO
)
1124 struct au_header
*header
= (struct au_header
*) s
->header
;
1125 switch (header
->encoding
)
1127 case AU_ENCODING_ULAW_8
:
1128 sd
->format
= SND_PCM_FORMAT_MU_LAW
;
1130 case AU_ENCODING_ALAW_8
:
1131 sd
->format
= SND_PCM_FORMAT_A_LAW
;
1133 case AU_ENCODING_IEEE32
:
1134 sd
->format
= SND_PCM_FORMAT_FLOAT_BE
;
1136 case AU_ENCODING_IEEE64
:
1137 sd
->format
= SND_PCM_FORMAT_FLOAT64_BE
;
1140 sd
->format
= SND_PCM_FORMAT_S8
;
1142 case AU_ENCODING_16
:
1143 sd
->format
= SND_PCM_FORMAT_S16_BE
;
1145 case AU_ENCODING_24
:
1146 sd
->format
= SND_PCM_FORMAT_S24_BE
;
1148 case AU_ENCODING_32
:
1149 sd
->format
= SND_PCM_FORMAT_S32_BE
;
1153 error ("Unsupported AU file format");
1161 /* Write NBYTES bytes from BUFFER to device SD. */
1164 alsa_write (struct sound_device
*sd
, const char *buffer
, int nbytes
)
1166 struct alsa_params
*p
= (struct alsa_params
*) sd
->data
;
1168 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1169 int fact
= snd_pcm_format_size (sd
->format
, 1) * sd
->channels
;
1173 while (nwritten
< nbytes
)
1175 snd_pcm_uframes_t frames
= (nbytes
- nwritten
)/fact
;
1176 if (frames
== 0) break;
1178 err
= snd_pcm_writei (p
->handle
, buffer
+ nwritten
, frames
);
1183 err
= snd_pcm_prepare (p
->handle
);
1185 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1188 else if (err
== -ESTRPIPE
)
1190 while ((err
= snd_pcm_resume (p
->handle
)) == -EAGAIN
)
1191 sleep(1); /* wait until the suspend flag is released */
1194 err
= snd_pcm_prepare (p
->handle
);
1196 alsa_sound_perror ("Can't recover from suspend, "
1202 alsa_sound_perror ("Error writing to sound device", err
);
1206 nwritten
+= err
* fact
;
1211 snd_error_quiet (const char *file
, int line
, const char *function
, int err
,
1216 /* Initialize device SD. Set up the interface functions in the device
1220 alsa_init (struct sound_device
*sd
)
1226 /* Open the sound device. Default is "default". */
1230 file
= DEFAULT_ALSA_SOUND_DEVICE
;
1232 snd_lib_error_set_handler ((snd_lib_error_handler_t
) snd_error_quiet
);
1233 err
= snd_pcm_open (&handle
, file
, SND_PCM_STREAM_PLAYBACK
, 0);
1234 snd_lib_error_set_handler (NULL
);
1237 snd_pcm_close (handle
);
1240 sd
->open
= alsa_open
;
1241 sd
->close
= alsa_close
;
1242 sd
->configure
= alsa_configure
;
1243 sd
->choose_format
= alsa_choose_format
;
1244 sd
->write
= alsa_write
;
1245 sd
->period_size
= alsa_period_size
;
1250 #endif /* HAVE_ALSA */
1253 /* END: Non Windows functions */
1254 #else /* WINDOWSNT */
1256 /* BEGIN: Windows specific functions */
1258 #define SOUND_WARNING(fun, error, text) \
1261 char err_string[MAXERRORLENGTH]; \
1262 fun (error, err_string, sizeof (err_string)); \
1263 _snprintf (buf, sizeof (buf), "%s\nError: %s", \
1264 text, err_string); \
1265 sound_warning (buf); \
1269 do_play_sound (const char *psz_file
, unsigned long ui_volume
)
1272 MCIERROR mci_error
= 0;
1273 char sz_cmd_buf
[520] = {0};
1274 char sz_ret_buf
[520] = {0};
1275 MMRESULT mm_result
= MMSYSERR_NOERROR
;
1276 unsigned long ui_volume_org
= 0;
1277 BOOL b_reset_volume
= FALSE
;
1279 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1280 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1281 sprintf (sz_cmd_buf
,
1282 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1284 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1287 SOUND_WARNING (mciGetErrorString
, mci_error
,
1288 "The open mciSendString command failed to open "
1289 "the specified sound file.");
1290 i_result
= (int) mci_error
;
1293 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
1295 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
1296 if (mm_result
== MMSYSERR_NOERROR
)
1298 b_reset_volume
= TRUE
;
1299 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
1300 if (mm_result
!= MMSYSERR_NOERROR
)
1302 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1303 "waveOutSetVolume failed to set the volume level "
1304 "of the WAVE_MAPPER device.\n"
1305 "As a result, the user selected volume level will "
1311 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1312 "waveOutGetVolume failed to obtain the original "
1313 "volume level of the WAVE_MAPPER device.\n"
1314 "As a result, the user selected volume level will "
1318 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1319 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1320 strcpy (sz_cmd_buf
, "play GNUEmacs_PlaySound_Device wait");
1321 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1324 SOUND_WARNING (mciGetErrorString
, mci_error
,
1325 "The play mciSendString command failed to play the "
1326 "opened sound file.");
1327 i_result
= (int) mci_error
;
1329 memset (sz_cmd_buf
, 0, sizeof (sz_cmd_buf
));
1330 memset (sz_ret_buf
, 0, sizeof (sz_ret_buf
));
1331 strcpy (sz_cmd_buf
, "close GNUEmacs_PlaySound_Device wait");
1332 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, sizeof (sz_ret_buf
), NULL
);
1333 if (b_reset_volume
== TRUE
)
1335 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
1336 if (mm_result
!= MMSYSERR_NOERROR
)
1338 SOUND_WARNING (waveOutGetErrorText
, mm_result
,
1339 "waveOutSetVolume failed to reset the original volume "
1340 "level of the WAVE_MAPPER device.");
1346 /* END: Windows specific functions */
1348 #endif /* WINDOWSNT */
1350 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
1351 doc
: /* Play sound SOUND.
1353 Internal use only, use `play-sound' instead. */)
1356 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
1357 int count
= SPECPDL_INDEX ();
1361 struct gcpro gcpro1
, gcpro2
;
1362 Lisp_Object args
[2];
1363 #else /* WINDOWSNT */
1365 Lisp_Object lo_file
= {0};
1366 char * psz_file
= NULL
;
1367 unsigned long ui_volume_tmp
= UINT_MAX
;
1368 unsigned long ui_volume
= UINT_MAX
;
1370 #endif /* WINDOWSNT */
1372 /* Parse the sound specification. Give up if it is invalid. */
1373 if (!parse_sound (sound
, attrs
))
1374 error ("Invalid sound specification");
1378 GCPRO2 (sound
, file
);
1379 current_sound_device
= (struct sound_device
*) xmalloc (sizeof (struct sound_device
));
1380 memset (current_sound_device
, 0, sizeof (struct sound_device
));
1381 current_sound
= (struct sound
*) xmalloc (sizeof (struct sound
));
1382 memset (current_sound
, 0, sizeof (struct sound
));
1383 record_unwind_protect (sound_cleanup
, Qnil
);
1384 current_sound
->header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1386 if (STRINGP (attrs
[SOUND_FILE
]))
1388 /* Open the sound file. */
1389 current_sound
->fd
= openp (Fcons (Vdata_directory
, Qnil
),
1390 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1391 if (current_sound
->fd
< 0)
1392 sound_perror ("Could not open sound file");
1394 /* Read the first bytes from the file. */
1395 current_sound
->header_size
1396 = emacs_read (current_sound
->fd
, current_sound
->header
,
1397 MAX_SOUND_HEADER_BYTES
);
1398 if (current_sound
->header_size
< 0)
1399 sound_perror ("Invalid sound file header");
1403 current_sound
->data
= attrs
[SOUND_DATA
];
1404 current_sound
->header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (current_sound
->data
));
1405 memcpy (current_sound
->header
, SDATA (current_sound
->data
),
1406 current_sound
->header_size
);
1409 /* Find out the type of sound. Give up if we can't tell. */
1410 find_sound_type (current_sound
);
1412 /* Set up a device. */
1413 if (STRINGP (attrs
[SOUND_DEVICE
]))
1415 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1416 current_sound_device
->file
= (char *) alloca (len
+ 1);
1417 strcpy (current_sound_device
->file
, SDATA (attrs
[SOUND_DEVICE
]));
1420 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1421 current_sound_device
->volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
1422 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1423 current_sound_device
->volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1425 args
[0] = Qplay_sound_functions
;
1427 Frun_hook_with_args (2, args
);
1430 if (!alsa_init (current_sound_device
))
1432 if (!vox_init (current_sound_device
))
1433 error ("No usable sound device driver found");
1435 /* Open the device. */
1436 current_sound_device
->open (current_sound_device
);
1438 /* Play the sound. */
1439 current_sound
->play (current_sound
, current_sound_device
);
1444 #else /* WINDOWSNT */
1446 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1447 len
= XSTRING (lo_file
)->size
;
1448 psz_file
= (char *) alloca (len
+ 1);
1449 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1450 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1452 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1454 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1456 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
1459 Based on some experiments I have conducted, a value of 100 or less
1460 for the sound volume is much too low. You cannot even hear it.
1461 A value of UINT_MAX indicates that you wish for the sound to played
1462 at the maximum possible volume. A value of UINT_MAX/2 plays the
1463 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1464 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1465 The following code adjusts the user specified volume level appropriately.
1467 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1469 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1471 i_result
= do_play_sound (psz_file
, ui_volume
);
1473 #endif /* WINDOWSNT */
1475 unbind_to (count
, Qnil
);
1479 /***********************************************************************
1481 ***********************************************************************/
1484 syms_of_sound (void)
1486 QCdevice
= intern_c_string(":device");
1487 staticpro (&QCdevice
);
1488 QCvolume
= intern_c_string (":volume");
1489 staticpro (&QCvolume
);
1490 Qsound
= intern_c_string ("sound");
1491 staticpro (&Qsound
);
1492 Qplay_sound_functions
= intern_c_string ("play-sound-functions");
1493 staticpro (&Qplay_sound_functions
);
1495 defsubr (&Splay_sound_internal
);
1504 #endif /* HAVE_SOUND */
1506 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1507 (do not change this comment) */