Initial revision, known outside of Emacs as version 1.8.
[emacs.git] / src / sound.c
blob871833dba9e078a048195d9fea955c3b0c61a1d1
1 /* sound.c -- sound support.
2 Copyright (C) 1998 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)
9 any later version.
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. */
24 #include <config.h>
26 #if defined HAVE_SOUND
28 #include <lisp.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <dispextern.h>
33 #include <errno.h>
35 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
36 sys/soundcard.h. So, let's try whatever's there. */
38 #ifdef HAVE_MACHINE_SOUNDCARD_H
39 #include <machine/soundcard.h>
40 #endif
41 #ifdef HAVE_SYS_SOUNDCARD_H
42 #include <sys/soundcard.h>
43 #endif
45 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
46 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
47 #define abs(X) ((X) < 0 ? -(X) : (X))
49 /* Structure forward declarations. */
51 struct sound_file;
52 struct sound_device;
54 /* The file header of RIFF-WAVE files (*.wav). Files are always in
55 little-endian byte-order. */
57 struct wav_header
59 u_int32_t magic;
60 u_int32_t length;
61 u_int32_t chunk_type;
62 u_int32_t chunk_format;
63 u_int32_t chunk_length;
64 u_int16_t format;
65 u_int16_t channels;
66 u_int32_t sample_rate;
67 u_int32_t bytes_per_second;
68 u_int16_t sample_size;
69 u_int16_t precision;
70 u_int32_t chunk_data;
71 u_int32_t data_length;
74 /* The file header of Sun adio files (*.au). Files are always in
75 big-endian byte-order. */
77 struct au_header
79 /* ASCII ".snd" */
80 u_int32_t magic_number;
82 /* Offset of data part from start of file. Minimum value is 24. */
83 u_int32_t data_offset;
85 /* Size of data part, 0xffffffff if unknown. */
86 u_int32_t data_size;
88 /* Data encoding format.
89 1 8-bit ISDN u-law
90 2 8-bit linear PCM (REF-PCM)
91 3 16-bit linear PCM
92 4 24-bit linear PCM
93 5 32-bit linear PCM
94 6 32-bit IEEE floating-point
95 7 64-bit IEEE floating-point
96 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
97 encoding scheme. */
98 u_int32_t encoding;
100 /* Number of samples per second. */
101 u_int32_t sample_rate;
103 /* Number of interleaved channels. */
104 u_int32_t channels;
107 /* Maximum of all sound file headers sizes. */
109 #define MAX_SOUND_HEADER_BYTES \
110 max (sizeof (struct wav_header), sizeof (struct au_header))
112 /* Interface structure for sound devices. */
114 struct sound_device
116 /* The name of the device or null meaning use a default device name. */
117 char *file;
119 /* File descriptor of the device. */
120 int fd;
122 /* Device-dependent format. */
123 int format;
125 /* Volume (0..100). Zero means unspecified. */
126 int volume;
128 /* Sample size. */
129 int sample_size;
131 /* Sample rate. */
132 int sample_rate;
134 /* Bytes per second. */
135 int bps;
137 /* 1 = mono, 2 = stereo, 0 = don't set. */
138 int channels;
140 /* Open device SD. */
141 void (* open) P_ ((struct sound_device *sd));
143 /* Close device SD. */
144 void (* close) P_ ((struct sound_device *sd));
146 /* Configure SD accoring to device-dependent parameters. */
147 void (* configure) P_ ((struct sound_device *device));
149 /* Choose a device-dependent format for outputting sound file SF. */
150 void (* choose_format) P_ ((struct sound_device *sd,
151 struct sound_file *sf));
153 /* Write NYBTES bytes from BUFFER to device SD. */
154 void (* write) P_ ((struct sound_device *sd, char *buffer, int nbytes));
156 /* A place for devices to store additional data. */
157 void *data;
160 /* An enumerator for each supported sound file type. */
162 enum sound_type
164 RIFF,
165 SUN_AUDIO
168 /* Interface structure for sound files. */
170 struct sound_file
172 /* The type of the file. */
173 enum sound_type type;
175 /* File descriptor of the file. */
176 int fd;
178 /* Pointer to sound file header. This contains the first
179 MAX_SOUND_HEADER_BYTES read from the file. */
180 char *header;
182 /* Play sound file SF on device SD. */
183 void (* play) P_ ((struct sound_file *sf, struct sound_device *sd));
186 /* Indices of attributes in a sound attributes vector. */
188 enum sound_attr
190 SOUND_FILE,
191 SOUND_DEVICE,
192 SOUND_VOLUME,
193 SOUND_ATTR_SENTINEL
196 /* Symbols. */
198 extern Lisp_Object QCfile;
199 Lisp_Object QCvolume, QCdevice;
200 Lisp_Object Qsound;
201 Lisp_Object Qplay_sound_functions;
203 /* These are set during `play-sound' so that sound_cleanup has
204 access to them. */
206 struct sound_device *sound_device;
207 struct sound_file *sound_file;
209 /* Function prototypes. */
211 static void vox_open P_ ((struct sound_device *));
212 static void vox_configure P_ ((struct sound_device *));
213 static void vox_close P_ ((struct sound_device *sd));
214 static void vox_choose_format P_ ((struct sound_device *, struct sound_file *));
215 static void vox_init P_ ((struct sound_device *));
216 static void vox_write P_ ((struct sound_device *, char *, int));
217 static void sound_perror P_ ((char *));
218 static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
219 static void find_sound_file_type P_ ((struct sound_file *));
220 static u_int32_t le2hl P_ ((u_int32_t));
221 static u_int16_t le2hs P_ ((u_int16_t));
222 static u_int32_t be2hl P_ ((u_int32_t));
223 static int wav_init P_ ((struct sound_file *));
224 static void wav_play P_ ((struct sound_file *, struct sound_device *));
225 static int au_init P_ ((struct sound_file *));
226 static void au_play P_ ((struct sound_file *, struct sound_device *));
228 #if 0 /* Currently not used. */
229 static u_int16_t be2hs P_ ((u_int16_t));
230 #endif
234 /***********************************************************************
235 General
236 ***********************************************************************/
238 /* Like perror, but signals an error. */
240 static void
241 sound_perror (msg)
242 char *msg;
244 error ("%s: %s", msg, strerror (errno));
248 /* Parse sound specification SOUND, and fill ATTRS with what is
249 found. Value is non-zero if SOUND Is a valid sound specification.
250 A valid sound specification is a list starting with the symbol
251 `sound'. The rest of the list is a property list which may
252 contain the following key/value pairs:
254 - `:file FILE'
256 FILE is the sound file to play. If it isn't an absolute name,
257 it's searched under `data-directory'.
259 - `:device DEVICE'
261 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
262 If not specified, a default device is used.
264 - `:volume VOL'
266 VOL must be an integer in the range [0, 100], or a float in the
267 range [0, 1]. */
269 static int
270 parse_sound (sound, attrs)
271 Lisp_Object sound;
272 Lisp_Object *attrs;
274 /* SOUND must be a list starting with the symbol `sound'. */
275 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
276 return 0;
278 sound = XCDR (sound);
279 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
280 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
281 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
283 /* File name must be specified. */
284 if (!STRINGP (attrs[SOUND_FILE]))
285 return 0;
287 /* Volume must be in the range 0..100 or unspecified. */
288 if (!NILP (attrs[SOUND_VOLUME]))
290 if (INTEGERP (attrs[SOUND_VOLUME]))
292 if (XINT (attrs[SOUND_VOLUME]) < 0
293 || XINT (attrs[SOUND_VOLUME]) > 100)
294 return 0;
296 else if (FLOATP (attrs[SOUND_VOLUME]))
298 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
299 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
300 return 0;
302 else
303 return 0;
306 /* Device must be a string or unspecified. */
307 if (!NILP (attrs[SOUND_DEVICE])
308 && !STRINGP (attrs[SOUND_DEVICE]))
309 return 0;
311 return 1;
315 /* Find out the type of the sound file whose file descriptor is FD.
316 SF is the sound file structure to fill in. */
318 static void
319 find_sound_file_type (sf)
320 struct sound_file *sf;
322 if (!wav_init (sf)
323 && !au_init (sf))
324 error ("Unknown sound file format");
328 /* Function installed by play-sound with record_unwind_protect. */
330 static Lisp_Object
331 sound_cleanup (arg)
332 Lisp_Object arg;
334 if (sound_device)
336 sound_device->close (sound_device);
337 if (sound_file->fd > 0)
338 close (sound_file->fd);
343 DEFUN ("play-sound", Fplay_sound, Splay_sound, 1, 1, 0,
344 "Play sound SOUND.")
345 (sound)
346 Lisp_Object sound;
348 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
349 Lisp_Object file;
350 struct gcpro gcpro1, gcpro2;
351 int nbytes;
352 struct sound_device sd;
353 struct sound_file sf;
354 Lisp_Object args[2];
355 int count = specpdl_ptr - specpdl;
357 file = Qnil;
358 GCPRO2 (sound, file);
359 bzero (&sd, sizeof sd);
360 bzero (&sf, sizeof sf);
361 sf.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
363 sound_device = &sd;
364 sound_file = &sf;
365 record_unwind_protect (sound_cleanup, Qnil);
367 /* Parse the sound specification. Give up if it is invalid. */
368 if (!parse_sound (sound, attrs))
370 UNGCPRO;
371 error ("Invalid sound specification");
374 /* Open the sound file. */
375 sf.fd = openp (Fcons (Vdata_directory, Qnil),
376 attrs[SOUND_FILE], "", &file, 0);
377 if (sf.fd < 0)
378 sound_perror ("Open sound file");
380 /* Read the first bytes from the file. */
381 nbytes = read (sf.fd, sf.header, MAX_SOUND_HEADER_BYTES);
382 if (nbytes < 0)
383 sound_perror ("Reading sound file header");
385 /* Find out the type of sound file. Give up if we can't tell. */
386 find_sound_file_type (&sf);
388 /* Set up a device. */
389 if (STRINGP (attrs[SOUND_DEVICE]))
391 int len = XSTRING (attrs[SOUND_DEVICE])->size;
392 sd.file = (char *) alloca (len + 1);
393 strcpy (sd.file, XSTRING (attrs[SOUND_DEVICE])->data);
395 if (INTEGERP (attrs[SOUND_VOLUME]))
396 sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
397 else if (FLOATP (attrs[SOUND_VOLUME]))
398 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
400 args[0] = Qplay_sound_functions;
401 args[1] = sound;
402 Frun_hook_with_args (make_number (2), args);
404 vox_init (&sd);
405 sd.open (&sd);
407 sf.play (&sf, &sd);
408 close (sf.fd);
409 sf.fd = -1;
410 sd.close (&sd);
411 sound_device = NULL;
412 sound_file = NULL;
413 UNGCPRO;
414 unbind_to (count, Qnil);
415 return Qnil;
419 /***********************************************************************
420 Byte-order Conversion
421 ***********************************************************************/
423 /* Convert 32-bit value VALUE which is in little-endian byte-order
424 to host byte-order. */
426 static u_int32_t
427 le2hl (value)
428 u_int32_t value;
430 #ifdef WORDS_BIG_ENDIAN
431 unsigned char *p = (unsigned char *) &value;
432 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
433 #endif
434 return value;
438 /* Convert 16-bit value VALUE which is in little-endian byte-order
439 to host byte-order. */
441 static u_int16_t
442 le2hs (value)
443 u_int16_t value;
445 #ifdef WORDS_BIG_ENDIAN
446 unsigned char *p = (unsigned char *) &value;
447 value = p[0] + (p[1] << 8);
448 #endif
449 return value;
453 /* Convert 32-bit value VALUE which is in big-endian byte-order
454 to host byte-order. */
456 static u_int32_t
457 be2hl (value)
458 u_int32_t value;
460 #ifndef WORDS_BIG_ENDIAN
461 unsigned char *p = (unsigned char *) &value;
462 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
463 #endif
464 return value;
468 #if 0 /* Currently not used. */
470 /* Convert 16-bit value VALUE which is in big-endian byte-order
471 to host byte-order. */
473 static u_int16_t
474 be2hs (value)
475 u_int16_t value;
477 #ifndef WORDS_BIG_ENDIAN
478 unsigned char *p = (unsigned char *) &value;
479 value = p[1] + (p[0] << 8);
480 #endif
481 return value;
484 #endif /* 0 */
487 /***********************************************************************
488 RIFF-WAVE (*.wav)
489 ***********************************************************************/
491 /* Try to initialize sound file SF from SF->header. SF->header
492 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
493 sound file. If the file is a WAV-format file, set up interface
494 functions in SF and convert header fields to host byte-order.
495 Value is non-zero if the file is a WAV file. */
497 static int
498 wav_init (sf)
499 struct sound_file *sf;
501 struct wav_header *header = (struct wav_header *) sf->header;
503 if (bcmp (sf->header, "RIFF", 4) != 0)
504 return 0;
506 /* WAV files are in little-endian order. Convert the header
507 if on a big-endian machine. */
508 header->magic = le2hl (header->magic);
509 header->length = le2hl (header->length);
510 header->chunk_type = le2hl (header->chunk_type);
511 header->chunk_format = le2hl (header->chunk_format);
512 header->chunk_length = le2hl (header->chunk_length);
513 header->format = le2hs (header->format);
514 header->channels = le2hs (header->channels);
515 header->sample_rate = le2hl (header->sample_rate);
516 header->bytes_per_second = le2hl (header->bytes_per_second);
517 header->sample_size = le2hs (header->sample_size);
518 header->precision = le2hs (header->precision);
519 header->chunk_data = le2hl (header->chunk_data);
520 header->data_length = le2hl (header->data_length);
522 /* Set up the interface functions for WAV. */
523 sf->type = RIFF;
524 sf->play = wav_play;
526 return 1;
530 /* Play RIFF-WAVE audio file SF on sound device SD. */
532 static void
533 wav_play (sf, sd)
534 struct sound_file *sf;
535 struct sound_device *sd;
537 struct wav_header *header = (struct wav_header *) sf->header;
538 char *buffer;
539 int nbytes;
540 int blksize = 2048;
542 /* Let the device choose a suitable device-dependent format
543 for the file. */
544 sd->choose_format (sd, sf);
546 /* Configure the device. */
547 sd->sample_size = header->sample_size;
548 sd->sample_rate = header->sample_rate;
549 sd->bps = header->bytes_per_second;
550 sd->channels = header->channels;
551 sd->configure (sd);
553 /* Copy sound data to the device. The WAV file specification is
554 actually more complex. This simple scheme worked with all WAV
555 files I found so far. If someone feels inclined to implement the
556 whole RIFF-WAVE spec, please do. */
557 buffer = (char *) alloca (blksize);
558 lseek (sf->fd, sizeof *header, SEEK_SET);
560 while ((nbytes = read (sf->fd, buffer, blksize)) > 0)
561 sd->write (sd, buffer, nbytes);
563 if (nbytes < 0)
564 sound_perror ("Reading sound file");
569 /***********************************************************************
570 Sun Audio (*.au)
571 ***********************************************************************/
573 /* Sun audio file encodings. */
575 enum au_encoding
577 AU_ENCODING_ULAW_8 = 1,
578 AU_ENCODING_8,
579 AU_ENCODING_16,
580 AU_ENCODING_24,
581 AU_ENCODING_32,
582 AU_ENCODING_IEEE32,
583 AU_ENCODING_IEEE64,
584 AU_COMPRESSED = 23
588 /* Try to initialize sound file SF from SF->header. SF->header
589 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
590 sound file. If the file is a AU-format file, set up interface
591 functions in SF and convert header fields to host byte-order.
592 Value is non-zero if the file is an AU file. */
594 static int
595 au_init (sf)
596 struct sound_file *sf;
598 struct au_header *header = (struct au_header *) sf->header;
600 if (bcmp (sf->header, ".snd", 4) != 0)
601 return 0;
603 header->magic_number = be2hl (header->magic_number);
604 header->data_offset = be2hl (header->data_offset);
605 header->data_size = be2hl (header->data_size);
606 header->encoding = be2hl (header->encoding);
607 header->sample_rate = be2hl (header->sample_rate);
608 header->channels = be2hl (header->channels);
610 /* Set up the interface functions for AU. */
611 sf->type = SUN_AUDIO;
612 sf->play = au_play;
614 return 1;
618 /* Play Sun audio file SF on sound device SD. */
620 static void
621 au_play (sf, sd)
622 struct sound_file *sf;
623 struct sound_device *sd;
625 struct au_header *header = (struct au_header *) sf->header;
626 int blksize = 2048;
627 char *buffer;
628 int nbytes;
630 sd->sample_size = 0;
631 sd->sample_rate = header->sample_rate;
632 sd->bps = 0;
633 sd->channels = header->channels;
634 sd->choose_format (sd, sf);
635 sd->configure (sd);
637 /* Seek */
638 lseek (sf->fd, header->data_offset, SEEK_SET);
640 /* Copy sound data to the device. */
641 buffer = (char *) alloca (blksize);
642 while ((nbytes = read (sf->fd, buffer, blksize)) > 0)
643 sd->write (sd, buffer, nbytes);
645 if (nbytes < 0)
646 sound_perror ("Reading sound file");
651 /***********************************************************************
652 Voxware Driver Interface
653 ***********************************************************************/
655 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
656 has a compatible own driver aka Luigi's driver. */
659 /* Open device SD. If SD->file is non-null, open that device,
660 otherwise use a default device name. */
662 static void
663 vox_open (sd)
664 struct sound_device *sd;
666 char *file;
668 /* Open the sound device. Default is /dev/dsp. */
669 if (sd->file)
670 file = sd->file;
671 else
672 file = "/dev/dsp";
674 sd->fd = open (file, O_WRONLY);
675 if (sd->fd < 0)
676 sound_perror (file);
680 /* Configure device SD from parameters in it. */
682 static void
683 vox_configure (sd)
684 struct sound_device *sd;
686 int requested;
688 xassert (sd->fd >= 0);
690 /* Device parameters apparently depend on each other in undocumented
691 ways (not to imply that there is any real documentation). Be
692 careful when reordering the calls below. */
693 if (sd->sample_size > 0
694 && ioctl (sd->fd, SNDCTL_DSP_SAMPLESIZE, &sd->sample_size) < 0)
695 sound_perror ("Setting sample size");
697 if (sd->bps > 0
698 && ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->bps) < 0)
699 sound_perror ("Setting speed");
701 if (sd->sample_rate > 0
702 && ioctl (sd->fd, SOUND_PCM_WRITE_RATE, &sd->sample_rate) < 0)
703 sound_perror ("Setting sample rate");
705 requested = sd->format;
706 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0)
707 sound_perror ("Setting format");
708 else if (requested != sd->format)
709 error ("Setting format");
711 if (sd->channels > 1
712 && ioctl (sd->fd, SNDCTL_DSP_STEREO, &sd->channels) < 0)
713 sound_perror ("Setting channels");
715 if (sd->volume > 0
716 && ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &sd->volume) < 0)
717 sound_perror ("Setting volume");
721 /* Close device SD if it is open. */
723 static void
724 vox_close (sd)
725 struct sound_device *sd;
727 if (sd->fd >= 0)
729 /* Flush sound data, and reset the device. */
730 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
731 ioctl (sd->fd, SNDCTL_DSP_RESET, NULL);
733 /* Close the device. */
734 close (sd->fd);
735 sd->fd = -1;
740 /* Choose device-dependent format for device SD from sound file SF. */
742 static void
743 vox_choose_format (sd, sf)
744 struct sound_device *sd;
745 struct sound_file *sf;
747 if (sf->type == RIFF)
749 struct wav_header *h = (struct wav_header *) sf->header;
750 if (h->precision == 8)
751 sd->format = AFMT_U8;
752 else if (h->precision == 16)
753 sd->format = AFMT_S16_LE;
754 else
755 error ("Unsupported WAV file format");
757 else if (sf->type == SUN_AUDIO)
759 struct au_header *header = (struct au_header *) sf->header;
760 switch (header->encoding)
762 case AU_ENCODING_ULAW_8:
763 case AU_ENCODING_IEEE32:
764 case AU_ENCODING_IEEE64:
765 sd->format = AFMT_MU_LAW;
766 break;
768 case AU_ENCODING_8:
769 case AU_ENCODING_16:
770 case AU_ENCODING_24:
771 case AU_ENCODING_32:
772 sd->format = AFMT_S16_LE;
773 break;
775 default:
776 error ("Unsupported AU file format");
779 else
780 abort ();
784 /* Initialize device SD. Set up the interface functions in the device
785 structure. */
787 static void
788 vox_init (sd)
789 struct sound_device *sd;
791 sd->fd = -1;
792 sd->open = vox_open;
793 sd->close = vox_close;
794 sd->configure = vox_configure;
795 sd->choose_format = vox_choose_format;
796 sd->write = vox_write;
800 /* Write NBYTES bytes from BUFFER to device SD. */
802 static void
803 vox_write (sd, buffer, nbytes)
804 struct sound_device *sd;
805 char *buffer;
806 int nbytes;
808 int nwritten = write (sd->fd, buffer, nbytes);
809 if (nwritten < 0)
810 sound_perror ("Writing to sound device");
815 /***********************************************************************
816 Initialization
817 ***********************************************************************/
819 void
820 syms_of_sound ()
822 QCdevice = intern (":device");
823 staticpro (&QCdevice);
824 QCvolume = intern (":volume");
825 staticpro (&QCvolume);
826 Qsound = intern ("sound");
827 staticpro (&Qsound);
828 Qplay_sound_functions = intern ("play-sound-functions");
829 staticpro (&Qplay_sound_functions);
831 defsubr (&Splay_sound);
835 void
836 init_sound ()
840 #endif /* HAVE_SOUND */