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"
39 #include <sys/ioctl.h>
42 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
43 sys/soundcard.h. So, let's try whatever's there. */
45 #ifdef HAVE_MACHINE_SOUNDCARD_H
46 #include <machine/soundcard.h>
48 #ifdef HAVE_SYS_SOUNDCARD_H
49 #include <sys/soundcard.h>
51 #ifdef HAVE_SOUNDCARD_H
52 #include <soundcard.h>
55 #ifndef DEFAULT_SOUND_DEVICE
56 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
59 #define abs(X) ((X) < 0 ? -(X) : (X))
61 /* Structure forward declarations. */
66 /* The file header of RIFF-WAVE files (*.wav). Files are always in
67 little-endian byte-order. */
74 u_int32_t chunk_format
;
75 u_int32_t chunk_length
;
78 u_int32_t sample_rate
;
79 u_int32_t bytes_per_second
;
80 u_int16_t sample_size
;
83 u_int32_t data_length
;
86 /* The file header of Sun adio files (*.au). Files are always in
87 big-endian byte-order. */
92 u_int32_t magic_number
;
94 /* Offset of data part from start of file. Minimum value is 24. */
95 u_int32_t data_offset
;
97 /* Size of data part, 0xffffffff if unknown. */
100 /* Data encoding format.
102 2 8-bit linear PCM (REF-PCM)
106 6 32-bit IEEE floating-point
107 7 64-bit IEEE floating-point
108 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
112 /* Number of samples per second. */
113 u_int32_t sample_rate
;
115 /* Number of interleaved channels. */
119 /* Maximum of all sound file headers sizes. */
121 #define MAX_SOUND_HEADER_BYTES \
122 max (sizeof (struct wav_header), sizeof (struct au_header))
124 /* Interface structure for sound devices. */
128 /* The name of the device or null meaning use a default device name. */
131 /* File descriptor of the device. */
134 /* Device-dependent format. */
137 /* Volume (0..100). Zero means unspecified. */
146 /* Bytes per second. */
149 /* 1 = mono, 2 = stereo, 0 = don't set. */
152 /* Open device SD. */
153 void (* open
) P_ ((struct sound_device
*sd
));
155 /* Close device SD. */
156 void (* close
) P_ ((struct sound_device
*sd
));
158 /* Configure SD accoring to device-dependent parameters. */
159 void (* configure
) P_ ((struct sound_device
*device
));
161 /* Choose a device-dependent format for outputting sound S. */
162 void (* choose_format
) P_ ((struct sound_device
*sd
,
165 /* Write NYBTES bytes from BUFFER to device SD. */
166 void (* write
) P_ ((struct sound_device
*sd
, const char *buffer
,
169 /* A place for devices to store additional data. */
173 /* An enumerator for each supported sound file type. */
181 /* Interface structure for sound files. */
185 /* The type of the file. */
186 enum sound_type type
;
188 /* File descriptor of a sound file. */
191 /* Pointer to sound file header. This contains header_size bytes
192 read from the start of a sound file. */
195 /* Number of bytes raed from sound file. This is always <=
196 MAX_SOUND_HEADER_BYTES. */
199 /* Sound data, if a string. */
202 /* Play sound file S on device SD. */
203 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
206 /* Indices of attributes in a sound attributes vector. */
219 extern Lisp_Object QCfile
, QCdata
;
220 Lisp_Object QCvolume
, QCdevice
;
222 Lisp_Object Qplay_sound_functions
;
224 /* These are set during `play-sound-internal' so that sound_cleanup has
227 struct sound_device
*current_sound_device
;
228 struct sound
*current_sound
;
230 /* Function prototypes. */
232 static void vox_open
P_ ((struct sound_device
*));
233 static void vox_configure
P_ ((struct sound_device
*));
234 static void vox_close
P_ ((struct sound_device
*sd
));
235 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
236 static void vox_init
P_ ((struct sound_device
*));
237 static void vox_write
P_ ((struct sound_device
*, const char *, int));
238 static void sound_perror
P_ ((char *));
239 static void sound_warning
P_ ((char *));
240 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
241 static void find_sound_type
P_ ((struct sound
*));
242 static u_int32_t le2hl
P_ ((u_int32_t
));
243 static u_int16_t le2hs
P_ ((u_int16_t
));
244 static u_int32_t be2hl
P_ ((u_int32_t
));
245 static int wav_init
P_ ((struct sound
*));
246 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
247 static int au_init
P_ ((struct sound
*));
248 static void au_play
P_ ((struct sound
*, struct sound_device
*));
250 #if 0 /* Currently not used. */
251 static u_int16_t be2hs
P_ ((u_int16_t
));
256 /***********************************************************************
258 ***********************************************************************/
260 /* Like perror, but signals an error. */
266 int saved_errno
= errno
;
270 sigunblock (sigmask (SIGIO
));
272 if (saved_errno
!= 0)
273 error ("%s: %s", msg
, strerror (saved_errno
));
279 /* Display a warning message. */
289 /* Parse sound specification SOUND, and fill ATTRS with what is
290 found. Value is non-zero if SOUND Is a valid sound specification.
291 A valid sound specification is a list starting with the symbol
292 `sound'. The rest of the list is a property list which may
293 contain the following key/value pairs:
297 FILE is the sound file to play. If it isn't an absolute name,
298 it's searched under `data-directory'.
302 DATA is a string containing sound data. Either :file or :data
303 may be present, but not both.
307 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
308 If not specified, a default device is used.
312 VOL must be an integer in the range [0, 100], or a float in the
316 parse_sound (sound
, attrs
)
320 /* SOUND must be a list starting with the symbol `sound'. */
321 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
324 sound
= XCDR (sound
);
325 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
326 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
327 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
328 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
330 /* File name or data must be specified. */
331 if (!STRINGP (attrs
[SOUND_FILE
])
332 && !STRINGP (attrs
[SOUND_DATA
]))
335 /* Volume must be in the range 0..100 or unspecified. */
336 if (!NILP (attrs
[SOUND_VOLUME
]))
338 if (INTEGERP (attrs
[SOUND_VOLUME
]))
340 if (XINT (attrs
[SOUND_VOLUME
]) < 0
341 || XINT (attrs
[SOUND_VOLUME
]) > 100)
344 else if (FLOATP (attrs
[SOUND_VOLUME
]))
346 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
347 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
354 /* Device must be a string or unspecified. */
355 if (!NILP (attrs
[SOUND_DEVICE
])
356 && !STRINGP (attrs
[SOUND_DEVICE
]))
363 /* Find out the type of the sound file whose file descriptor is FD.
364 S is the sound file structure to fill in. */
370 if (!wav_init (s
) && !au_init (s
))
371 error ("Unknown sound format");
375 /* Function installed by play-sound-internal with record_unwind_protect. */
381 if (current_sound_device
)
383 if (current_sound_device
->close
)
384 current_sound_device
->close (current_sound_device
);
385 if (current_sound
->fd
> 0)
386 emacs_close (current_sound
->fd
);
393 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
394 doc
: /* Play sound SOUND.
396 Internal use only, use `play-sound' instead. */)
400 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
402 struct gcpro gcpro1
, gcpro2
;
403 struct sound_device sd
;
406 int count
= SPECPDL_INDEX ();
409 GCPRO2 (sound
, file
);
410 bzero (&sd
, sizeof sd
);
411 bzero (&s
, sizeof s
);
412 current_sound_device
= &sd
;
414 record_unwind_protect (sound_cleanup
, Qnil
);
415 s
.header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
417 /* Parse the sound specification. Give up if it is invalid. */
418 if (!parse_sound (sound
, attrs
))
419 error ("Invalid sound specification");
421 if (STRINGP (attrs
[SOUND_FILE
]))
423 /* Open the sound file. */
424 s
.fd
= openp (Fcons (Vdata_directory
, Qnil
),
425 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
427 sound_perror ("Could not open sound file");
429 /* Read the first bytes from the file. */
430 s
.header_size
= emacs_read (s
.fd
, s
.header
, MAX_SOUND_HEADER_BYTES
);
431 if (s
.header_size
< 0)
432 sound_perror ("Invalid sound file header");
436 s
.data
= attrs
[SOUND_DATA
];
437 s
.header_size
= min (MAX_SOUND_HEADER_BYTES
, SBYTES (s
.data
));
438 bcopy (SDATA (s
.data
), s
.header
, s
.header_size
);
441 /* Find out the type of sound. Give up if we can't tell. */
442 find_sound_type (&s
);
444 /* Set up a device. */
445 if (STRINGP (attrs
[SOUND_DEVICE
]))
447 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
448 sd
.file
= (char *) alloca (len
+ 1);
449 strcpy (sd
.file
, SDATA (attrs
[SOUND_DEVICE
]));
452 if (INTEGERP (attrs
[SOUND_VOLUME
]))
453 sd
.volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
454 else if (FLOATP (attrs
[SOUND_VOLUME
]))
455 sd
.volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
457 args
[0] = Qplay_sound_functions
;
459 Frun_hook_with_args (2, args
);
461 /* There is only one type of device we currently support, the VOX
462 sound driver. Set up the device interface functions for that
466 /* Open the device. */
469 /* Play the sound. */
472 /* Close the input file, if any. */
473 if (!STRINGP (s
.data
))
479 /* Close the device. */
483 current_sound_device
= NULL
;
484 current_sound
= NULL
;
486 unbind_to (count
, Qnil
);
491 /***********************************************************************
492 Byte-order Conversion
493 ***********************************************************************/
495 /* Convert 32-bit value VALUE which is in little-endian byte-order
496 to host byte-order. */
502 #ifdef WORDS_BIG_ENDIAN
503 unsigned char *p
= (unsigned char *) &value
;
504 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
510 /* Convert 16-bit value VALUE which is in little-endian byte-order
511 to host byte-order. */
517 #ifdef WORDS_BIG_ENDIAN
518 unsigned char *p
= (unsigned char *) &value
;
519 value
= p
[0] + (p
[1] << 8);
525 /* Convert 32-bit value VALUE which is in big-endian byte-order
526 to host byte-order. */
532 #ifndef WORDS_BIG_ENDIAN
533 unsigned char *p
= (unsigned char *) &value
;
534 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
540 #if 0 /* Currently not used. */
542 /* Convert 16-bit value VALUE which is in big-endian byte-order
543 to host byte-order. */
549 #ifndef WORDS_BIG_ENDIAN
550 unsigned char *p
= (unsigned char *) &value
;
551 value
= p
[1] + (p
[0] << 8);
559 /***********************************************************************
561 ***********************************************************************/
563 /* Try to initialize sound file S from S->header. S->header
564 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
565 sound file. If the file is a WAV-format file, set up interface
566 functions in S and convert header fields to host byte-order.
567 Value is non-zero if the file is a WAV file. */
573 struct wav_header
*header
= (struct wav_header
*) s
->header
;
575 if (s
->header_size
< sizeof *header
576 || bcmp (s
->header
, "RIFF", 4) != 0)
579 /* WAV files are in little-endian order. Convert the header
580 if on a big-endian machine. */
581 header
->magic
= le2hl (header
->magic
);
582 header
->length
= le2hl (header
->length
);
583 header
->chunk_type
= le2hl (header
->chunk_type
);
584 header
->chunk_format
= le2hl (header
->chunk_format
);
585 header
->chunk_length
= le2hl (header
->chunk_length
);
586 header
->format
= le2hs (header
->format
);
587 header
->channels
= le2hs (header
->channels
);
588 header
->sample_rate
= le2hl (header
->sample_rate
);
589 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
590 header
->sample_size
= le2hs (header
->sample_size
);
591 header
->precision
= le2hs (header
->precision
);
592 header
->chunk_data
= le2hl (header
->chunk_data
);
593 header
->data_length
= le2hl (header
->data_length
);
595 /* Set up the interface functions for WAV. */
603 /* Play RIFF-WAVE audio file S on sound device SD. */
608 struct sound_device
*sd
;
610 struct wav_header
*header
= (struct wav_header
*) s
->header
;
612 /* Let the device choose a suitable device-dependent format
614 sd
->choose_format (sd
, s
);
616 /* Configure the device. */
617 sd
->sample_size
= header
->sample_size
;
618 sd
->sample_rate
= header
->sample_rate
;
619 sd
->bps
= header
->bytes_per_second
;
620 sd
->channels
= header
->channels
;
623 /* Copy sound data to the device. The WAV file specification is
624 actually more complex. This simple scheme worked with all WAV
625 files I found so far. If someone feels inclined to implement the
626 whole RIFF-WAVE spec, please do. */
627 if (STRINGP (s
->data
))
628 sd
->write (sd
, SDATA (s
->data
) + sizeof *header
,
629 SBYTES (s
->data
) - sizeof *header
);
636 buffer
= (char *) alloca (blksize
);
637 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
639 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
640 sd
->write (sd
, buffer
, nbytes
);
643 sound_perror ("Error reading sound file");
649 /***********************************************************************
651 ***********************************************************************/
653 /* Sun audio file encodings. */
657 AU_ENCODING_ULAW_8
= 1,
668 /* Try to initialize sound file S from S->header. S->header
669 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
670 sound file. If the file is a AU-format file, set up interface
671 functions in S and convert header fields to host byte-order.
672 Value is non-zero if the file is an AU file. */
678 struct au_header
*header
= (struct au_header
*) s
->header
;
680 if (s
->header_size
< sizeof *header
681 || bcmp (s
->header
, ".snd", 4) != 0)
684 header
->magic_number
= be2hl (header
->magic_number
);
685 header
->data_offset
= be2hl (header
->data_offset
);
686 header
->data_size
= be2hl (header
->data_size
);
687 header
->encoding
= be2hl (header
->encoding
);
688 header
->sample_rate
= be2hl (header
->sample_rate
);
689 header
->channels
= be2hl (header
->channels
);
691 /* Set up the interface functions for AU. */
699 /* Play Sun audio file S on sound device SD. */
704 struct sound_device
*sd
;
706 struct au_header
*header
= (struct au_header
*) s
->header
;
709 sd
->sample_rate
= header
->sample_rate
;
711 sd
->channels
= header
->channels
;
712 sd
->choose_format (sd
, s
);
715 if (STRINGP (s
->data
))
716 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
717 SBYTES (s
->data
) - header
->data_offset
);
725 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
727 /* Copy sound data to the device. */
728 buffer
= (char *) alloca (blksize
);
729 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
730 sd
->write (sd
, buffer
, nbytes
);
733 sound_perror ("Error reading sound file");
739 /***********************************************************************
740 Voxware Driver Interface
741 ***********************************************************************/
743 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
744 has a compatible own driver aka Luigi's driver. */
747 /* Open device SD. If SD->file is non-null, open that device,
748 otherwise use a default device name. */
752 struct sound_device
*sd
;
756 /* Open the sound device. Default is /dev/dsp. */
760 file
= DEFAULT_SOUND_DEVICE
;
762 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
768 /* Configure device SD from parameters in it. */
772 struct sound_device
*sd
;
776 xassert (sd
->fd
>= 0);
778 /* On GNU/Linux, it seems that the device driver doesn't like to be
779 interrupted by a signal. Block the ones we know to cause
783 sigblock (sigmask (SIGIO
));
787 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
788 || val
!= sd
->format
)
789 sound_perror ("Could not set sound format");
791 val
= sd
->channels
!= 1;
792 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
793 || val
!= (sd
->channels
!= 1))
794 sound_perror ("Could not set stereo/mono");
796 /* I think bps and sampling_rate are the same, but who knows.
797 Check this. and use SND_DSP_SPEED for both. */
798 if (sd
->sample_rate
> 0)
800 val
= sd
->sample_rate
;
801 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0)
802 sound_perror ("Could not set sound speed");
803 else if (val
!= sd
->sample_rate
)
804 sound_warning ("Could not set sample rate");
809 int volume
= sd
->volume
& 0xff;
810 volume
|= volume
<< 8;
811 /* This may fail if there is no mixer. Ignore the failure. */
812 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
817 sigunblock (sigmask (SIGIO
));
822 /* Close device SD if it is open. */
826 struct sound_device
*sd
;
830 /* On GNU/Linux, it seems that the device driver doesn't like to
831 be interrupted by a signal. Block the ones we know to cause
834 sigblock (sigmask (SIGIO
));
838 /* Flush sound data, and reset the device. */
839 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
843 sigunblock (sigmask (SIGIO
));
846 /* Close the device. */
847 emacs_close (sd
->fd
);
853 /* Choose device-dependent format for device SD from sound file S. */
856 vox_choose_format (sd
, s
)
857 struct sound_device
*sd
;
862 struct wav_header
*h
= (struct wav_header
*) s
->header
;
863 if (h
->precision
== 8)
864 sd
->format
= AFMT_U8
;
865 else if (h
->precision
== 16)
866 sd
->format
= AFMT_S16_LE
;
868 error ("Unsupported WAV file format");
870 else if (s
->type
== SUN_AUDIO
)
872 struct au_header
*header
= (struct au_header
*) s
->header
;
873 switch (header
->encoding
)
875 case AU_ENCODING_ULAW_8
:
876 case AU_ENCODING_IEEE32
:
877 case AU_ENCODING_IEEE64
:
878 sd
->format
= AFMT_MU_LAW
;
885 sd
->format
= AFMT_S16_LE
;
889 error ("Unsupported AU file format");
897 /* Initialize device SD. Set up the interface functions in the device
902 struct sound_device
*sd
;
906 sd
->close
= vox_close
;
907 sd
->configure
= vox_configure
;
908 sd
->choose_format
= vox_choose_format
;
909 sd
->write
= vox_write
;
913 /* Write NBYTES bytes from BUFFER to device SD. */
916 vox_write (sd
, buffer
, nbytes
)
917 struct sound_device
*sd
;
921 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
923 sound_perror ("Error writing to sound device");
928 /***********************************************************************
930 ***********************************************************************/
935 QCdevice
= intern (":device");
936 staticpro (&QCdevice
);
937 QCvolume
= intern (":volume");
938 staticpro (&QCvolume
);
939 Qsound
= intern ("sound");
941 Qplay_sound_functions
= intern ("play-sound-functions");
942 staticpro (&Qplay_sound_functions
);
944 defsubr (&Splay_sound_internal
);
953 #endif /* HAVE_SOUND */