1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999 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
31 #include <sys/types.h>
32 #include <dispextern.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>
41 #ifdef HAVE_SYS_SOUNDCARD_H
42 #include <sys/soundcard.h>
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. */
54 /* The file header of RIFF-WAVE files (*.wav). Files are always in
55 little-endian byte-order. */
62 u_int32_t chunk_format
;
63 u_int32_t chunk_length
;
66 u_int32_t sample_rate
;
67 u_int32_t bytes_per_second
;
68 u_int16_t sample_size
;
71 u_int32_t data_length
;
74 /* The file header of Sun adio files (*.au). Files are always in
75 big-endian byte-order. */
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. */
88 /* Data encoding format.
90 2 8-bit linear PCM (REF-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
100 /* Number of samples per second. */
101 u_int32_t sample_rate
;
103 /* Number of interleaved 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. */
116 /* The name of the device or null meaning use a default device name. */
119 /* File descriptor of the device. */
122 /* Device-dependent format. */
125 /* Volume (0..100). Zero means unspecified. */
134 /* Bytes per second. */
137 /* 1 = mono, 2 = stereo, 0 = don't set. */
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 S. */
150 void (* choose_format
) P_ ((struct sound_device
*sd
,
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. */
160 /* An enumerator for each supported sound file type. */
168 /* Interface structure for sound files. */
172 /* The type of the file. */
173 enum sound_type type
;
175 /* File descriptor of a sound file. */
178 /* Pointer to sound file header. This contains header_size bytes
179 read from the start of a sound file. */
182 /* Number of bytes raed from sound file. This is always <=
183 MAX_SOUND_HEADER_BYTES. */
186 /* Sound data, if a string. */
189 /* Play sound file S on device SD. */
190 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
193 /* Indices of attributes in a sound attributes vector. */
206 extern Lisp_Object QCfile
, QCdata
;
207 Lisp_Object QCvolume
, QCdevice
;
209 Lisp_Object Qplay_sound_functions
;
211 /* These are set during `play-sound' so that sound_cleanup has
214 struct sound_device
*current_sound_device
;
215 struct sound
*current_sound
;
217 /* Function prototypes. */
219 static void vox_open
P_ ((struct sound_device
*));
220 static void vox_configure
P_ ((struct sound_device
*));
221 static void vox_close
P_ ((struct sound_device
*sd
));
222 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
223 static void vox_init
P_ ((struct sound_device
*));
224 static void vox_write
P_ ((struct sound_device
*, char *, int));
225 static void sound_perror
P_ ((char *));
226 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
227 static void find_sound_type
P_ ((struct sound
*));
228 static u_int32_t le2hl
P_ ((u_int32_t
));
229 static u_int16_t le2hs
P_ ((u_int16_t
));
230 static u_int32_t be2hl
P_ ((u_int32_t
));
231 static int wav_init
P_ ((struct sound
*));
232 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
233 static int au_init
P_ ((struct sound
*));
234 static void au_play
P_ ((struct sound
*, struct sound_device
*));
236 #if 0 /* Currently not used. */
237 static u_int16_t be2hs
P_ ((u_int16_t
));
242 /***********************************************************************
244 ***********************************************************************/
246 /* Like perror, but signals an error. */
252 error ("%s: %s", msg
, strerror (errno
));
256 /* Parse sound specification SOUND, and fill ATTRS with what is
257 found. Value is non-zero if SOUND Is a valid sound specification.
258 A valid sound specification is a list starting with the symbol
259 `sound'. The rest of the list is a property list which may
260 contain the following key/value pairs:
264 FILE is the sound file to play. If it isn't an absolute name,
265 it's searched under `data-directory'.
269 DATA is a string containing sound data. Either :file or :data
270 may be present, but not both.
274 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
275 If not specified, a default device is used.
279 VOL must be an integer in the range [0, 100], or a float in the
283 parse_sound (sound
, attrs
)
287 /* SOUND must be a list starting with the symbol `sound'. */
288 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
291 sound
= XCDR (sound
);
292 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
293 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
294 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
295 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
297 /* File name or data must be specified. */
298 if (!STRINGP (attrs
[SOUND_FILE
])
299 && !STRINGP (attrs
[SOUND_DATA
]))
302 /* Volume must be in the range 0..100 or unspecified. */
303 if (!NILP (attrs
[SOUND_VOLUME
]))
305 if (INTEGERP (attrs
[SOUND_VOLUME
]))
307 if (XINT (attrs
[SOUND_VOLUME
]) < 0
308 || XINT (attrs
[SOUND_VOLUME
]) > 100)
311 else if (FLOATP (attrs
[SOUND_VOLUME
]))
313 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
314 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
321 /* Device must be a string or unspecified. */
322 if (!NILP (attrs
[SOUND_DEVICE
])
323 && !STRINGP (attrs
[SOUND_DEVICE
]))
330 /* Find out the type of the sound file whose file descriptor is FD.
331 S is the sound file structure to fill in. */
337 if (!wav_init (s
) && !au_init (s
))
338 error ("Unknown sound format");
342 /* Function installed by play-sound with record_unwind_protect. */
348 if (current_sound_device
)
350 if (current_sound_device
->close
)
351 current_sound_device
->close (current_sound_device
);
352 if (current_sound
->fd
> 0)
353 emacs_close (current_sound
->fd
);
358 DEFUN ("play-sound", Fplay_sound
, Splay_sound
, 1, 1, 0,
359 "Play sound SOUND.\n\
360 SOUND is a list of the form `(sound KEYWORD VALUE...)'.\n\
361 The following keywords are recognized:\n\
363 :file FILE.- read sound data from FILE. If FILE Isn't an\n\
364 absolute file name, it is searched in `data-directory'.\n\
366 :data DATA - read sound data from string DATA.\n\
368 Exactly one of :file or :data must be present.\n\
370 :volume VOL - set volume to VOL. VOL must an integer in the\n\
371 range 0..100 or a float in the range 0..1.0. If not specified,\n\
372 don't change the volume setting of the sound device.\n\
374 :device DEVICE - play sound on DEVICE. If not specified,\n\
375 a system-dependent default device name is used.")
379 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
381 struct gcpro gcpro1
, gcpro2
;
382 struct sound_device sd
;
385 int count
= specpdl_ptr
- specpdl
;
388 GCPRO2 (sound
, file
);
389 bzero (&sd
, sizeof sd
);
390 bzero (&s
, sizeof s
);
391 current_sound_device
= &sd
;
393 record_unwind_protect (sound_cleanup
, Qnil
);
394 s
.header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
396 /* Parse the sound specification. Give up if it is invalid. */
397 if (!parse_sound (sound
, attrs
))
398 error ("Invalid sound specification");
400 if (STRINGP (attrs
[SOUND_FILE
]))
402 /* Open the sound file. */
403 s
.fd
= openp (Fcons (Vdata_directory
, Qnil
),
404 attrs
[SOUND_FILE
], "", &file
, 0);
406 sound_perror ("Open sound file");
408 /* Read the first bytes from the file. */
409 s
.header_size
= emacs_read (s
.fd
, s
.header
, MAX_SOUND_HEADER_BYTES
);
410 if (s
.header_size
< 0)
411 sound_perror ("Reading sound file header");
415 s
.data
= attrs
[SOUND_DATA
];
416 bcopy (XSTRING (s
.data
)->data
, s
.header
,
417 min (MAX_SOUND_HEADER_BYTES
, STRING_BYTES (XSTRING (s
.data
))));
420 /* Find out the type of sound. Give up if we can't tell. */
421 find_sound_type (&s
);
423 /* Set up a device. */
424 if (STRINGP (attrs
[SOUND_DEVICE
]))
426 int len
= XSTRING (attrs
[SOUND_DEVICE
])->size
;
427 sd
.file
= (char *) alloca (len
+ 1);
428 strcpy (sd
.file
, XSTRING (attrs
[SOUND_DEVICE
])->data
);
431 if (INTEGERP (attrs
[SOUND_VOLUME
]))
432 sd
.volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
433 else if (FLOATP (attrs
[SOUND_VOLUME
]))
434 sd
.volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
436 args
[0] = Qplay_sound_functions
;
438 Frun_hook_with_args (make_number (2), args
);
440 /* There is only one type of device we currently support, the VOX
441 sound driver. Set up the device interface functions for that
445 /* Open the device. */
448 /* Play the sound. */
451 /* Close the input file, if any. */
452 if (!STRINGP (s
.data
))
458 /* Close the device. */
462 current_sound_device
= NULL
;
463 current_sound
= NULL
;
465 unbind_to (count
, Qnil
);
470 /***********************************************************************
471 Byte-order Conversion
472 ***********************************************************************/
474 /* Convert 32-bit value VALUE which is in little-endian byte-order
475 to host byte-order. */
481 #ifdef WORDS_BIG_ENDIAN
482 unsigned char *p
= (unsigned char *) &value
;
483 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
489 /* Convert 16-bit value VALUE which is in little-endian byte-order
490 to host byte-order. */
496 #ifdef WORDS_BIG_ENDIAN
497 unsigned char *p
= (unsigned char *) &value
;
498 value
= p
[0] + (p
[1] << 8);
504 /* Convert 32-bit value VALUE which is in big-endian byte-order
505 to host byte-order. */
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. */
528 #ifndef WORDS_BIG_ENDIAN
529 unsigned char *p
= (unsigned char *) &value
;
530 value
= p
[1] + (p
[0] << 8);
538 /***********************************************************************
540 ***********************************************************************/
542 /* Try to initialize sound file S from S->header. S->header
543 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
544 sound file. If the file is a WAV-format file, set up interface
545 functions in S and convert header fields to host byte-order.
546 Value is non-zero if the file is a WAV file. */
552 struct wav_header
*header
= (struct wav_header
*) s
->header
;
554 if (s
->header_size
< sizeof *header
555 || bcmp (s
->header
, "RIFF", 4) != 0)
558 /* WAV files are in little-endian order. Convert the header
559 if on a big-endian machine. */
560 header
->magic
= le2hl (header
->magic
);
561 header
->length
= le2hl (header
->length
);
562 header
->chunk_type
= le2hl (header
->chunk_type
);
563 header
->chunk_format
= le2hl (header
->chunk_format
);
564 header
->chunk_length
= le2hl (header
->chunk_length
);
565 header
->format
= le2hs (header
->format
);
566 header
->channels
= le2hs (header
->channels
);
567 header
->sample_rate
= le2hl (header
->sample_rate
);
568 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
569 header
->sample_size
= le2hs (header
->sample_size
);
570 header
->precision
= le2hs (header
->precision
);
571 header
->chunk_data
= le2hl (header
->chunk_data
);
572 header
->data_length
= le2hl (header
->data_length
);
574 /* Set up the interface functions for WAV. */
582 /* Play RIFF-WAVE audio file S on sound device SD. */
587 struct sound_device
*sd
;
589 struct wav_header
*header
= (struct wav_header
*) s
->header
;
591 /* Let the device choose a suitable device-dependent format
593 sd
->choose_format (sd
, s
);
595 /* Configure the device. */
596 sd
->sample_size
= header
->sample_size
;
597 sd
->sample_rate
= header
->sample_rate
;
598 sd
->bps
= header
->bytes_per_second
;
599 sd
->channels
= header
->channels
;
602 /* Copy sound data to the device. The WAV file specification is
603 actually more complex. This simple scheme worked with all WAV
604 files I found so far. If someone feels inclined to implement the
605 whole RIFF-WAVE spec, please do. */
606 if (STRINGP (s
->data
))
607 sd
->write (sd
, XSTRING (s
->data
)->data
+ sizeof *header
,
608 STRING_BYTES (XSTRING (s
->data
)) - sizeof *header
);
615 buffer
= (char *) alloca (blksize
);
616 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
618 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
619 sd
->write (sd
, buffer
, nbytes
);
622 sound_perror ("Reading sound file");
628 /***********************************************************************
630 ***********************************************************************/
632 /* Sun audio file encodings. */
636 AU_ENCODING_ULAW_8
= 1,
647 /* Try to initialize sound file S from S->header. S->header
648 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
649 sound file. If the file is a AU-format file, set up interface
650 functions in S and convert header fields to host byte-order.
651 Value is non-zero if the file is an AU file. */
657 struct au_header
*header
= (struct au_header
*) s
->header
;
659 if (s
->header_size
< sizeof *header
660 || bcmp (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. */
683 struct sound_device
*sd
;
685 struct au_header
*header
= (struct au_header
*) s
->header
;
688 sd
->sample_rate
= header
->sample_rate
;
690 sd
->channels
= header
->channels
;
691 sd
->choose_format (sd
, s
);
694 if (STRINGP (s
->data
))
695 sd
->write (sd
, XSTRING (s
->data
)->data
+ header
->data_offset
,
696 STRING_BYTES (XSTRING (s
->data
)) - header
->data_offset
);
704 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
706 /* Copy sound data to the device. */
707 buffer
= (char *) alloca (blksize
);
708 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
709 sd
->write (sd
, buffer
, nbytes
);
712 sound_perror ("Reading sound file");
718 /***********************************************************************
719 Voxware Driver Interface
720 ***********************************************************************/
722 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
723 has a compatible own driver aka Luigi's driver. */
726 /* Open device SD. If SD->file is non-null, open that device,
727 otherwise use a default device name. */
731 struct sound_device
*sd
;
735 /* Open the sound device. Default is /dev/dsp. */
741 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
747 /* Configure device SD from parameters in it. */
751 struct sound_device
*sd
;
755 xassert (sd
->fd
>= 0);
757 /* Device parameters apparently depend on each other in undocumented
758 ways (not to imply that there is any real documentation). Be
759 careful when reordering the calls below. */
760 if (sd
->sample_size
> 0
761 && ioctl (sd
->fd
, SNDCTL_DSP_SAMPLESIZE
, &sd
->sample_size
) < 0)
762 sound_perror ("Setting sample size");
765 && ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->bps
) < 0)
766 sound_perror ("Setting speed");
768 if (sd
->sample_rate
> 0
769 && ioctl (sd
->fd
, SOUND_PCM_WRITE_RATE
, &sd
->sample_rate
) < 0)
770 sound_perror ("Setting sample rate");
772 requested
= sd
->format
;
773 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0)
774 sound_perror ("Setting format");
775 else if (requested
!= sd
->format
)
776 error ("Setting format");
779 && ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &sd
->channels
) < 0)
780 sound_perror ("Setting channels");
783 && ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &sd
->volume
) < 0)
784 sound_perror ("Setting volume");
788 /* Close device SD if it is open. */
792 struct sound_device
*sd
;
796 /* Flush sound data, and reset the device. */
797 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
798 ioctl (sd
->fd
, SNDCTL_DSP_RESET
, NULL
);
800 /* Close the device. */
801 emacs_close (sd
->fd
);
807 /* Choose device-dependent format for device SD from sound file S. */
810 vox_choose_format (sd
, s
)
811 struct sound_device
*sd
;
816 struct wav_header
*h
= (struct wav_header
*) s
->header
;
817 if (h
->precision
== 8)
818 sd
->format
= AFMT_U8
;
819 else if (h
->precision
== 16)
820 sd
->format
= AFMT_S16_LE
;
822 error ("Unsupported WAV file format");
824 else if (s
->type
== SUN_AUDIO
)
826 struct au_header
*header
= (struct au_header
*) s
->header
;
827 switch (header
->encoding
)
829 case AU_ENCODING_ULAW_8
:
830 case AU_ENCODING_IEEE32
:
831 case AU_ENCODING_IEEE64
:
832 sd
->format
= AFMT_MU_LAW
;
839 sd
->format
= AFMT_S16_LE
;
843 error ("Unsupported AU file format");
851 /* Initialize device SD. Set up the interface functions in the device
856 struct sound_device
*sd
;
860 sd
->close
= vox_close
;
861 sd
->configure
= vox_configure
;
862 sd
->choose_format
= vox_choose_format
;
863 sd
->write
= vox_write
;
867 /* Write NBYTES bytes from BUFFER to device SD. */
870 vox_write (sd
, buffer
, nbytes
)
871 struct sound_device
*sd
;
875 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
877 sound_perror ("Writing to sound device");
882 /***********************************************************************
884 ***********************************************************************/
889 QCdevice
= intern (":device");
890 staticpro (&QCdevice
);
891 QCvolume
= intern (":volume");
892 staticpro (&QCvolume
);
893 Qsound
= intern ("sound");
895 Qplay_sound_functions
= intern ("play-sound-functions");
896 staticpro (&Qplay_sound_functions
);
898 defsubr (&Splay_sound
);
907 #endif /* HAVE_SOUND */