1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
22 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
26 #if defined HAVE_SOUND
30 #include <sys/types.h>
33 #include "dispextern.h"
36 #include "syssignal.h"
38 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
39 sys/soundcard.h. So, let's try whatever's there. */
41 #ifdef HAVE_MACHINE_SOUNDCARD_H
42 #include <machine/soundcard.h>
44 #ifdef HAVE_SYS_SOUNDCARD_H
45 #include <sys/soundcard.h>
47 #ifdef HAVE_SOUNDCARD_H
48 #include <sys/ioctl.h>
49 #include <soundcard.h>
52 #ifndef DEFAULT_SOUND_DEVICE
53 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
56 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
57 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
58 #define abs(X) ((X) < 0 ? -(X) : (X))
60 /* Structure forward declarations. */
65 /* The file header of RIFF-WAVE files (*.wav). Files are always in
66 little-endian byte-order. */
73 u_int32_t chunk_format
;
74 u_int32_t chunk_length
;
77 u_int32_t sample_rate
;
78 u_int32_t bytes_per_second
;
79 u_int16_t sample_size
;
82 u_int32_t data_length
;
85 /* The file header of Sun adio files (*.au). Files are always in
86 big-endian byte-order. */
91 u_int32_t magic_number
;
93 /* Offset of data part from start of file. Minimum value is 24. */
94 u_int32_t data_offset
;
96 /* Size of data part, 0xffffffff if unknown. */
99 /* Data encoding format.
101 2 8-bit linear PCM (REF-PCM)
105 6 32-bit IEEE floating-point
106 7 64-bit IEEE floating-point
107 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
111 /* Number of samples per second. */
112 u_int32_t sample_rate
;
114 /* Number of interleaved channels. */
118 /* Maximum of all sound file headers sizes. */
120 #define MAX_SOUND_HEADER_BYTES \
121 max (sizeof (struct wav_header), sizeof (struct au_header))
123 /* Interface structure for sound devices. */
127 /* The name of the device or null meaning use a default device name. */
130 /* File descriptor of the device. */
133 /* Device-dependent format. */
136 /* Volume (0..100). Zero means unspecified. */
145 /* Bytes per second. */
148 /* 1 = mono, 2 = stereo, 0 = don't set. */
151 /* Open device SD. */
152 void (* open
) P_ ((struct sound_device
*sd
));
154 /* Close device SD. */
155 void (* close
) P_ ((struct sound_device
*sd
));
157 /* Configure SD accoring to device-dependent parameters. */
158 void (* configure
) P_ ((struct sound_device
*device
));
160 /* Choose a device-dependent format for outputting sound S. */
161 void (* choose_format
) P_ ((struct sound_device
*sd
,
164 /* Write NYBTES bytes from BUFFER to device SD. */
165 void (* write
) P_ ((struct sound_device
*sd
, char *buffer
, int nbytes
));
167 /* A place for devices to store additional data. */
171 /* An enumerator for each supported sound file type. */
179 /* Interface structure for sound files. */
183 /* The type of the file. */
184 enum sound_type type
;
186 /* File descriptor of a sound file. */
189 /* Pointer to sound file header. This contains header_size bytes
190 read from the start of a sound file. */
193 /* Number of bytes raed from sound file. This is always <=
194 MAX_SOUND_HEADER_BYTES. */
197 /* Sound data, if a string. */
200 /* Play sound file S on device SD. */
201 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
204 /* Indices of attributes in a sound attributes vector. */
217 extern Lisp_Object QCfile
, QCdata
;
218 Lisp_Object QCvolume
, QCdevice
;
220 Lisp_Object Qplay_sound_functions
;
222 /* These are set during `play-sound' so that sound_cleanup has
225 struct sound_device
*current_sound_device
;
226 struct sound
*current_sound
;
228 /* Function prototypes. */
230 static void vox_open
P_ ((struct sound_device
*));
231 static void vox_configure
P_ ((struct sound_device
*));
232 static void vox_close
P_ ((struct sound_device
*sd
));
233 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
234 static void vox_init
P_ ((struct sound_device
*));
235 static void vox_write
P_ ((struct sound_device
*, char *, int));
236 static void sound_perror
P_ ((char *));
237 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
238 static void find_sound_type
P_ ((struct sound
*));
239 static u_int32_t le2hl
P_ ((u_int32_t
));
240 static u_int16_t le2hs
P_ ((u_int16_t
));
241 static u_int32_t be2hl
P_ ((u_int32_t
));
242 static int wav_init
P_ ((struct sound
*));
243 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
244 static int au_init
P_ ((struct sound
*));
245 static void au_play
P_ ((struct sound
*, struct sound_device
*));
247 #if 0 /* Currently not used. */
248 static u_int16_t be2hs
P_ ((u_int16_t
));
253 /***********************************************************************
255 ***********************************************************************/
257 /* Like perror, but signals an error. */
263 error ("%s: %s", msg
, strerror (errno
));
267 /* Parse sound specification SOUND, and fill ATTRS with what is
268 found. Value is non-zero if SOUND Is a valid sound specification.
269 A valid sound specification is a list starting with the symbol
270 `sound'. The rest of the list is a property list which may
271 contain the following key/value pairs:
275 FILE is the sound file to play. If it isn't an absolute name,
276 it's searched under `data-directory'.
280 DATA is a string containing sound data. Either :file or :data
281 may be present, but not both.
285 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
286 If not specified, a default device is used.
290 VOL must be an integer in the range [0, 100], or a float in the
294 parse_sound (sound
, attrs
)
298 /* SOUND must be a list starting with the symbol `sound'. */
299 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
302 sound
= XCDR (sound
);
303 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
304 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
305 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
306 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
308 /* File name or data must be specified. */
309 if (!STRINGP (attrs
[SOUND_FILE
])
310 && !STRINGP (attrs
[SOUND_DATA
]))
313 /* Volume must be in the range 0..100 or unspecified. */
314 if (!NILP (attrs
[SOUND_VOLUME
]))
316 if (INTEGERP (attrs
[SOUND_VOLUME
]))
318 if (XINT (attrs
[SOUND_VOLUME
]) < 0
319 || XINT (attrs
[SOUND_VOLUME
]) > 100)
322 else if (FLOATP (attrs
[SOUND_VOLUME
]))
324 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
325 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
332 /* Device must be a string or unspecified. */
333 if (!NILP (attrs
[SOUND_DEVICE
])
334 && !STRINGP (attrs
[SOUND_DEVICE
]))
341 /* Find out the type of the sound file whose file descriptor is FD.
342 S is the sound file structure to fill in. */
348 if (!wav_init (s
) && !au_init (s
))
349 error ("Unknown sound format");
353 /* Function installed by play-sound with record_unwind_protect. */
359 if (current_sound_device
)
361 if (current_sound_device
->close
)
362 current_sound_device
->close (current_sound_device
);
363 if (current_sound
->fd
> 0)
364 emacs_close (current_sound
->fd
);
371 DEFUN ("play-sound", Fplay_sound
, Splay_sound
, 1, 1, 0,
372 "Play sound SOUND.\n\
373 SOUND is a list of the form `(sound KEYWORD VALUE...)'.\n\
374 The following keywords are recognized:\n\
376 :file FILE.- read sound data from FILE. If FILE isn't an\n\
377 absolute file name, it is searched in `data-directory'.\n\
379 :data DATA - read sound data from string DATA.\n\
381 Exactly one of :file or :data must be present.\n\
383 :volume VOL - set volume to VOL. VOL must an integer in the\n\
384 range 0..100 or a float in the range 0..1.0. If not specified,\n\
385 don't change the volume setting of the sound device.\n\
387 :device DEVICE - play sound on DEVICE. If not specified,\n\
388 a system-dependent default device name is used.")
392 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
394 struct gcpro gcpro1
, gcpro2
;
395 struct sound_device sd
;
398 int count
= specpdl_ptr
- specpdl
;
401 GCPRO2 (sound
, file
);
402 bzero (&sd
, sizeof sd
);
403 bzero (&s
, sizeof s
);
404 current_sound_device
= &sd
;
406 record_unwind_protect (sound_cleanup
, Qnil
);
407 s
.header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
409 /* Parse the sound specification. Give up if it is invalid. */
410 if (!parse_sound (sound
, attrs
))
411 error ("Invalid sound specification");
413 if (STRINGP (attrs
[SOUND_FILE
]))
415 /* Open the sound file. */
416 s
.fd
= openp (Fcons (Vdata_directory
, Qnil
),
417 attrs
[SOUND_FILE
], "", &file
, 0);
419 sound_perror ("Open sound file");
421 /* Read the first bytes from the file. */
422 s
.header_size
= emacs_read (s
.fd
, s
.header
, MAX_SOUND_HEADER_BYTES
);
423 if (s
.header_size
< 0)
424 sound_perror ("Reading sound file header");
428 s
.data
= attrs
[SOUND_DATA
];
429 bcopy (XSTRING (s
.data
)->data
, s
.header
,
430 min (MAX_SOUND_HEADER_BYTES
, STRING_BYTES (XSTRING (s
.data
))));
433 /* Find out the type of sound. Give up if we can't tell. */
434 find_sound_type (&s
);
436 /* Set up a device. */
437 if (STRINGP (attrs
[SOUND_DEVICE
]))
439 int len
= XSTRING (attrs
[SOUND_DEVICE
])->size
;
440 sd
.file
= (char *) alloca (len
+ 1);
441 strcpy (sd
.file
, XSTRING (attrs
[SOUND_DEVICE
])->data
);
444 if (INTEGERP (attrs
[SOUND_VOLUME
]))
445 sd
.volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
446 else if (FLOATP (attrs
[SOUND_VOLUME
]))
447 sd
.volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
449 args
[0] = Qplay_sound_functions
;
451 Frun_hook_with_args (2, args
);
453 /* There is only one type of device we currently support, the VOX
454 sound driver. Set up the device interface functions for that
458 /* Open the device. */
461 /* Play the sound. */
464 /* Close the input file, if any. */
465 if (!STRINGP (s
.data
))
471 /* Close the device. */
475 current_sound_device
= NULL
;
476 current_sound
= NULL
;
478 unbind_to (count
, Qnil
);
483 /***********************************************************************
484 Byte-order Conversion
485 ***********************************************************************/
487 /* Convert 32-bit value VALUE which is in little-endian byte-order
488 to host byte-order. */
494 #ifdef WORDS_BIG_ENDIAN
495 unsigned char *p
= (unsigned char *) &value
;
496 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
502 /* Convert 16-bit value VALUE which is in little-endian byte-order
503 to host byte-order. */
509 #ifdef WORDS_BIG_ENDIAN
510 unsigned char *p
= (unsigned char *) &value
;
511 value
= p
[0] + (p
[1] << 8);
517 /* Convert 32-bit value VALUE which is in big-endian byte-order
518 to host byte-order. */
524 #ifndef WORDS_BIG_ENDIAN
525 unsigned char *p
= (unsigned char *) &value
;
526 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
532 #if 0 /* Currently not used. */
534 /* Convert 16-bit value VALUE which is in big-endian byte-order
535 to host byte-order. */
541 #ifndef WORDS_BIG_ENDIAN
542 unsigned char *p
= (unsigned char *) &value
;
543 value
= p
[1] + (p
[0] << 8);
551 /***********************************************************************
553 ***********************************************************************/
555 /* Try to initialize sound file S from S->header. S->header
556 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
557 sound file. If the file is a WAV-format file, set up interface
558 functions in S and convert header fields to host byte-order.
559 Value is non-zero if the file is a WAV file. */
565 struct wav_header
*header
= (struct wav_header
*) s
->header
;
567 if (s
->header_size
< sizeof *header
568 || bcmp (s
->header
, "RIFF", 4) != 0)
571 /* WAV files are in little-endian order. Convert the header
572 if on a big-endian machine. */
573 header
->magic
= le2hl (header
->magic
);
574 header
->length
= le2hl (header
->length
);
575 header
->chunk_type
= le2hl (header
->chunk_type
);
576 header
->chunk_format
= le2hl (header
->chunk_format
);
577 header
->chunk_length
= le2hl (header
->chunk_length
);
578 header
->format
= le2hs (header
->format
);
579 header
->channels
= le2hs (header
->channels
);
580 header
->sample_rate
= le2hl (header
->sample_rate
);
581 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
582 header
->sample_size
= le2hs (header
->sample_size
);
583 header
->precision
= le2hs (header
->precision
);
584 header
->chunk_data
= le2hl (header
->chunk_data
);
585 header
->data_length
= le2hl (header
->data_length
);
587 /* Set up the interface functions for WAV. */
595 /* Play RIFF-WAVE audio file S on sound device SD. */
600 struct sound_device
*sd
;
602 struct wav_header
*header
= (struct wav_header
*) s
->header
;
604 /* Let the device choose a suitable device-dependent format
606 sd
->choose_format (sd
, s
);
608 /* Configure the device. */
609 sd
->sample_size
= header
->sample_size
;
610 sd
->sample_rate
= header
->sample_rate
;
611 sd
->bps
= header
->bytes_per_second
;
612 sd
->channels
= header
->channels
;
615 /* Copy sound data to the device. The WAV file specification is
616 actually more complex. This simple scheme worked with all WAV
617 files I found so far. If someone feels inclined to implement the
618 whole RIFF-WAVE spec, please do. */
619 if (STRINGP (s
->data
))
620 sd
->write (sd
, XSTRING (s
->data
)->data
+ sizeof *header
,
621 STRING_BYTES (XSTRING (s
->data
)) - sizeof *header
);
628 buffer
= (char *) alloca (blksize
);
629 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
631 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
632 sd
->write (sd
, buffer
, nbytes
);
635 sound_perror ("Reading sound file");
641 /***********************************************************************
643 ***********************************************************************/
645 /* Sun audio file encodings. */
649 AU_ENCODING_ULAW_8
= 1,
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
, XSTRING (s
->data
)->data
+ header
->data_offset
,
709 STRING_BYTES (XSTRING (s
->data
)) - header
->data_offset
);
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 ("Reading sound file");
731 /***********************************************************************
732 Voxware Driver Interface
733 ***********************************************************************/
735 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
736 has a compatible own driver aka Luigi's driver. */
739 /* Open device SD. If SD->file is non-null, open that device,
740 otherwise use a default device name. */
744 struct sound_device
*sd
;
748 /* Open the sound device. Default is /dev/dsp. */
752 file
= DEFAULT_SOUND_DEVICE
;
754 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
760 /* Configure device SD from parameters in it. */
764 struct sound_device
*sd
;
768 xassert (sd
->fd
>= 0);
770 /* On GNU/Linux, it seems that the device driver doesn't like to be
771 interrupted by a signal. Block the ones we know to cause
775 sigblock (sigmask (SIGIO
));
779 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
780 || val
!= sd
->format
)
781 sound_perror ("Set sound format");
783 val
= sd
->channels
!= 1;
784 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
785 || val
!= (sd
->channels
!= 1))
786 sound_perror ("Set stereo/mono");
788 /* I think bps and sampling_rate are the same, but who knows.
789 Check this. and use SND_DSP_SPEED for both. */
790 if (sd
->sample_rate
> 0)
792 val
= sd
->sample_rate
;
793 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0
794 || val
!= sd
->sample_rate
)
795 sound_perror ("Set sound speed");
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
;
897 sd
->close
= vox_close
;
898 sd
->configure
= vox_configure
;
899 sd
->choose_format
= vox_choose_format
;
900 sd
->write
= vox_write
;
904 /* Write NBYTES bytes from BUFFER to device SD. */
907 vox_write (sd
, buffer
, nbytes
)
908 struct sound_device
*sd
;
912 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
914 sound_perror ("Writing to sound device");
919 /***********************************************************************
921 ***********************************************************************/
926 QCdevice
= intern (":device");
927 staticpro (&QCdevice
);
928 QCvolume
= intern (":volume");
929 staticpro (&QCvolume
);
930 Qsound
= intern ("sound");
932 Qplay_sound_functions
= intern ("play-sound-functions");
933 staticpro (&Qplay_sound_functions
);
935 defsubr (&Splay_sound
);
944 #endif /* HAVE_SOUND */