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>
44 #ifdef HAVE_SOUNDCARD_H
45 #include <sys/ioctl.h>
46 #include <soundcard.h>
49 #ifndef DEFAULT_SOUND_DEVICE
50 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
53 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
54 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
55 #define abs(X) ((X) < 0 ? -(X) : (X))
57 /* Structure forward declarations. */
62 /* The file header of RIFF-WAVE files (*.wav). Files are always in
63 little-endian byte-order. */
70 u_int32_t chunk_format
;
71 u_int32_t chunk_length
;
74 u_int32_t sample_rate
;
75 u_int32_t bytes_per_second
;
76 u_int16_t sample_size
;
79 u_int32_t data_length
;
82 /* The file header of Sun adio files (*.au). Files are always in
83 big-endian byte-order. */
88 u_int32_t magic_number
;
90 /* Offset of data part from start of file. Minimum value is 24. */
91 u_int32_t data_offset
;
93 /* Size of data part, 0xffffffff if unknown. */
96 /* Data encoding format.
98 2 8-bit linear PCM (REF-PCM)
102 6 32-bit IEEE floating-point
103 7 64-bit IEEE floating-point
104 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
108 /* Number of samples per second. */
109 u_int32_t sample_rate
;
111 /* Number of interleaved channels. */
115 /* Maximum of all sound file headers sizes. */
117 #define MAX_SOUND_HEADER_BYTES \
118 max (sizeof (struct wav_header), sizeof (struct au_header))
120 /* Interface structure for sound devices. */
124 /* The name of the device or null meaning use a default device name. */
127 /* File descriptor of the device. */
130 /* Device-dependent format. */
133 /* Volume (0..100). Zero means unspecified. */
142 /* Bytes per second. */
145 /* 1 = mono, 2 = stereo, 0 = don't set. */
148 /* Open device SD. */
149 void (* open
) P_ ((struct sound_device
*sd
));
151 /* Close device SD. */
152 void (* close
) P_ ((struct sound_device
*sd
));
154 /* Configure SD accoring to device-dependent parameters. */
155 void (* configure
) P_ ((struct sound_device
*device
));
157 /* Choose a device-dependent format for outputting sound S. */
158 void (* choose_format
) P_ ((struct sound_device
*sd
,
161 /* Write NYBTES bytes from BUFFER to device SD. */
162 void (* write
) P_ ((struct sound_device
*sd
, char *buffer
, int nbytes
));
164 /* A place for devices to store additional data. */
168 /* An enumerator for each supported sound file type. */
176 /* Interface structure for sound files. */
180 /* The type of the file. */
181 enum sound_type type
;
183 /* File descriptor of a sound file. */
186 /* Pointer to sound file header. This contains header_size bytes
187 read from the start of a sound file. */
190 /* Number of bytes raed from sound file. This is always <=
191 MAX_SOUND_HEADER_BYTES. */
194 /* Sound data, if a string. */
197 /* Play sound file S on device SD. */
198 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
201 /* Indices of attributes in a sound attributes vector. */
214 extern Lisp_Object QCfile
, QCdata
;
215 Lisp_Object QCvolume
, QCdevice
;
217 Lisp_Object Qplay_sound_functions
;
219 /* These are set during `play-sound' so that sound_cleanup has
222 struct sound_device
*current_sound_device
;
223 struct sound
*current_sound
;
225 /* Function prototypes. */
227 static void vox_open
P_ ((struct sound_device
*));
228 static void vox_configure
P_ ((struct sound_device
*));
229 static void vox_close
P_ ((struct sound_device
*sd
));
230 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
231 static void vox_init
P_ ((struct sound_device
*));
232 static void vox_write
P_ ((struct sound_device
*, char *, int));
233 static void sound_perror
P_ ((char *));
234 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
235 static void find_sound_type
P_ ((struct sound
*));
236 static u_int32_t le2hl
P_ ((u_int32_t
));
237 static u_int16_t le2hs
P_ ((u_int16_t
));
238 static u_int32_t be2hl
P_ ((u_int32_t
));
239 static int wav_init
P_ ((struct sound
*));
240 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
241 static int au_init
P_ ((struct sound
*));
242 static void au_play
P_ ((struct sound
*, struct sound_device
*));
244 #if 0 /* Currently not used. */
245 static u_int16_t be2hs
P_ ((u_int16_t
));
250 /***********************************************************************
252 ***********************************************************************/
254 /* Like perror, but signals an error. */
260 error ("%s: %s", msg
, strerror (errno
));
264 /* Parse sound specification SOUND, and fill ATTRS with what is
265 found. Value is non-zero if SOUND Is a valid sound specification.
266 A valid sound specification is a list starting with the symbol
267 `sound'. The rest of the list is a property list which may
268 contain the following key/value pairs:
272 FILE is the sound file to play. If it isn't an absolute name,
273 it's searched under `data-directory'.
277 DATA is a string containing sound data. Either :file or :data
278 may be present, but not both.
282 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
283 If not specified, a default device is used.
287 VOL must be an integer in the range [0, 100], or a float in the
291 parse_sound (sound
, attrs
)
295 /* SOUND must be a list starting with the symbol `sound'. */
296 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
299 sound
= XCDR (sound
);
300 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
301 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
302 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
303 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
305 /* File name or data must be specified. */
306 if (!STRINGP (attrs
[SOUND_FILE
])
307 && !STRINGP (attrs
[SOUND_DATA
]))
310 /* Volume must be in the range 0..100 or unspecified. */
311 if (!NILP (attrs
[SOUND_VOLUME
]))
313 if (INTEGERP (attrs
[SOUND_VOLUME
]))
315 if (XINT (attrs
[SOUND_VOLUME
]) < 0
316 || XINT (attrs
[SOUND_VOLUME
]) > 100)
319 else if (FLOATP (attrs
[SOUND_VOLUME
]))
321 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
322 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
329 /* Device must be a string or unspecified. */
330 if (!NILP (attrs
[SOUND_DEVICE
])
331 && !STRINGP (attrs
[SOUND_DEVICE
]))
338 /* Find out the type of the sound file whose file descriptor is FD.
339 S is the sound file structure to fill in. */
345 if (!wav_init (s
) && !au_init (s
))
346 error ("Unknown sound format");
350 /* Function installed by play-sound with record_unwind_protect. */
356 if (current_sound_device
)
358 if (current_sound_device
->close
)
359 current_sound_device
->close (current_sound_device
);
360 if (current_sound
->fd
> 0)
361 emacs_close (current_sound
->fd
);
368 DEFUN ("play-sound", Fplay_sound
, Splay_sound
, 1, 1, 0,
369 "Play sound SOUND.\n\
370 SOUND is a list of the form `(sound KEYWORD VALUE...)'.\n\
371 The following keywords are recognized:\n\
373 :file FILE.- read sound data from FILE. If FILE isn't an\n\
374 absolute file name, it is searched in `data-directory'.\n\
376 :data DATA - read sound data from string DATA.\n\
378 Exactly one of :file or :data must be present.\n\
380 :volume VOL - set volume to VOL. VOL must an integer in the\n\
381 range 0..100 or a float in the range 0..1.0. If not specified,\n\
382 don't change the volume setting of the sound device.\n\
384 :device DEVICE - play sound on DEVICE. If not specified,\n\
385 a system-dependent default device name is used.")
389 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
391 struct gcpro gcpro1
, gcpro2
;
392 struct sound_device sd
;
395 int count
= specpdl_ptr
- specpdl
;
398 GCPRO2 (sound
, file
);
399 bzero (&sd
, sizeof sd
);
400 bzero (&s
, sizeof s
);
401 current_sound_device
= &sd
;
403 record_unwind_protect (sound_cleanup
, Qnil
);
404 s
.header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
406 /* Parse the sound specification. Give up if it is invalid. */
407 if (!parse_sound (sound
, attrs
))
408 error ("Invalid sound specification");
410 if (STRINGP (attrs
[SOUND_FILE
]))
412 /* Open the sound file. */
413 s
.fd
= openp (Fcons (Vdata_directory
, Qnil
),
414 attrs
[SOUND_FILE
], "", &file
, 0);
416 sound_perror ("Open sound file");
418 /* Read the first bytes from the file. */
419 s
.header_size
= emacs_read (s
.fd
, s
.header
, MAX_SOUND_HEADER_BYTES
);
420 if (s
.header_size
< 0)
421 sound_perror ("Reading sound file header");
425 s
.data
= attrs
[SOUND_DATA
];
426 bcopy (XSTRING (s
.data
)->data
, s
.header
,
427 min (MAX_SOUND_HEADER_BYTES
, STRING_BYTES (XSTRING (s
.data
))));
430 /* Find out the type of sound. Give up if we can't tell. */
431 find_sound_type (&s
);
433 /* Set up a device. */
434 if (STRINGP (attrs
[SOUND_DEVICE
]))
436 int len
= XSTRING (attrs
[SOUND_DEVICE
])->size
;
437 sd
.file
= (char *) alloca (len
+ 1);
438 strcpy (sd
.file
, XSTRING (attrs
[SOUND_DEVICE
])->data
);
441 if (INTEGERP (attrs
[SOUND_VOLUME
]))
442 sd
.volume
= XFASTINT (attrs
[SOUND_VOLUME
]);
443 else if (FLOATP (attrs
[SOUND_VOLUME
]))
444 sd
.volume
= XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
446 args
[0] = Qplay_sound_functions
;
448 Frun_hook_with_args (2, args
);
450 /* There is only one type of device we currently support, the VOX
451 sound driver. Set up the device interface functions for that
455 /* Open the device. */
458 /* Play the sound. */
461 /* Close the input file, if any. */
462 if (!STRINGP (s
.data
))
468 /* Close the device. */
472 current_sound_device
= NULL
;
473 current_sound
= NULL
;
475 unbind_to (count
, Qnil
);
480 /***********************************************************************
481 Byte-order Conversion
482 ***********************************************************************/
484 /* Convert 32-bit value VALUE which is in little-endian byte-order
485 to host byte-order. */
491 #ifdef WORDS_BIG_ENDIAN
492 unsigned char *p
= (unsigned char *) &value
;
493 value
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
499 /* Convert 16-bit value VALUE which is in little-endian byte-order
500 to host byte-order. */
506 #ifdef WORDS_BIG_ENDIAN
507 unsigned char *p
= (unsigned char *) &value
;
508 value
= p
[0] + (p
[1] << 8);
514 /* Convert 32-bit value VALUE which is in big-endian byte-order
515 to host byte-order. */
521 #ifndef WORDS_BIG_ENDIAN
522 unsigned char *p
= (unsigned char *) &value
;
523 value
= p
[3] + (p
[2] << 8) + (p
[1] << 16) + (p
[0] << 24);
529 #if 0 /* Currently not used. */
531 /* Convert 16-bit value VALUE which is in big-endian byte-order
532 to host byte-order. */
538 #ifndef WORDS_BIG_ENDIAN
539 unsigned char *p
= (unsigned char *) &value
;
540 value
= p
[1] + (p
[0] << 8);
548 /***********************************************************************
550 ***********************************************************************/
552 /* Try to initialize sound file S from S->header. S->header
553 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
554 sound file. If the file is a WAV-format file, set up interface
555 functions in S and convert header fields to host byte-order.
556 Value is non-zero if the file is a WAV file. */
562 struct wav_header
*header
= (struct wav_header
*) s
->header
;
564 if (s
->header_size
< sizeof *header
565 || bcmp (s
->header
, "RIFF", 4) != 0)
568 /* WAV files are in little-endian order. Convert the header
569 if on a big-endian machine. */
570 header
->magic
= le2hl (header
->magic
);
571 header
->length
= le2hl (header
->length
);
572 header
->chunk_type
= le2hl (header
->chunk_type
);
573 header
->chunk_format
= le2hl (header
->chunk_format
);
574 header
->chunk_length
= le2hl (header
->chunk_length
);
575 header
->format
= le2hs (header
->format
);
576 header
->channels
= le2hs (header
->channels
);
577 header
->sample_rate
= le2hl (header
->sample_rate
);
578 header
->bytes_per_second
= le2hl (header
->bytes_per_second
);
579 header
->sample_size
= le2hs (header
->sample_size
);
580 header
->precision
= le2hs (header
->precision
);
581 header
->chunk_data
= le2hl (header
->chunk_data
);
582 header
->data_length
= le2hl (header
->data_length
);
584 /* Set up the interface functions for WAV. */
592 /* Play RIFF-WAVE audio file S on sound device SD. */
597 struct sound_device
*sd
;
599 struct wav_header
*header
= (struct wav_header
*) s
->header
;
601 /* Let the device choose a suitable device-dependent format
603 sd
->choose_format (sd
, s
);
605 /* Configure the device. */
606 sd
->sample_size
= header
->sample_size
;
607 sd
->sample_rate
= header
->sample_rate
;
608 sd
->bps
= header
->bytes_per_second
;
609 sd
->channels
= header
->channels
;
612 /* Copy sound data to the device. The WAV file specification is
613 actually more complex. This simple scheme worked with all WAV
614 files I found so far. If someone feels inclined to implement the
615 whole RIFF-WAVE spec, please do. */
616 if (STRINGP (s
->data
))
617 sd
->write (sd
, XSTRING (s
->data
)->data
+ sizeof *header
,
618 STRING_BYTES (XSTRING (s
->data
)) - sizeof *header
);
625 buffer
= (char *) alloca (blksize
);
626 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
628 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
629 sd
->write (sd
, buffer
, nbytes
);
632 sound_perror ("Reading sound file");
638 /***********************************************************************
640 ***********************************************************************/
642 /* Sun audio file encodings. */
646 AU_ENCODING_ULAW_8
= 1,
657 /* Try to initialize sound file S from S->header. S->header
658 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
659 sound file. If the file is a AU-format file, set up interface
660 functions in S and convert header fields to host byte-order.
661 Value is non-zero if the file is an AU file. */
667 struct au_header
*header
= (struct au_header
*) s
->header
;
669 if (s
->header_size
< sizeof *header
670 || bcmp (s
->header
, ".snd", 4) != 0)
673 header
->magic_number
= be2hl (header
->magic_number
);
674 header
->data_offset
= be2hl (header
->data_offset
);
675 header
->data_size
= be2hl (header
->data_size
);
676 header
->encoding
= be2hl (header
->encoding
);
677 header
->sample_rate
= be2hl (header
->sample_rate
);
678 header
->channels
= be2hl (header
->channels
);
680 /* Set up the interface functions for AU. */
688 /* Play Sun audio file S on sound device SD. */
693 struct sound_device
*sd
;
695 struct au_header
*header
= (struct au_header
*) s
->header
;
698 sd
->sample_rate
= header
->sample_rate
;
700 sd
->channels
= header
->channels
;
701 sd
->choose_format (sd
, s
);
704 if (STRINGP (s
->data
))
705 sd
->write (sd
, XSTRING (s
->data
)->data
+ header
->data_offset
,
706 STRING_BYTES (XSTRING (s
->data
)) - header
->data_offset
);
714 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
716 /* Copy sound data to the device. */
717 buffer
= (char *) alloca (blksize
);
718 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
719 sd
->write (sd
, buffer
, nbytes
);
722 sound_perror ("Reading sound file");
728 /***********************************************************************
729 Voxware Driver Interface
730 ***********************************************************************/
732 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
733 has a compatible own driver aka Luigi's driver. */
736 /* Open device SD. If SD->file is non-null, open that device,
737 otherwise use a default device name. */
741 struct sound_device
*sd
;
745 /* Open the sound device. Default is /dev/dsp. */
749 file
= DEFAULT_SOUND_DEVICE
;
751 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
757 /* Configure device SD from parameters in it. */
761 struct sound_device
*sd
;
765 xassert (sd
->fd
>= 0);
768 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
769 || val
!= sd
->format
)
770 sound_perror ("Set sound format");
772 val
= sd
->channels
!= 1;
773 if (ioctl (sd
->fd
, SNDCTL_DSP_STEREO
, &val
) < 0
774 || val
!= (sd
->channels
!= 1))
775 sound_perror ("Set stereo/mono");
777 /* I think bps and sampling_rate are the same, but who knows.
778 Check this. and use SND_DSP_SPEED for both. */
779 if (sd
->sample_rate
> 0)
781 val
= sd
->sample_rate
;
782 if (ioctl (sd
->fd
, SNDCTL_DSP_SPEED
, &sd
->sample_rate
) < 0
783 || val
!= sd
->sample_rate
)
784 sound_perror ("Set sound speed");
789 int volume
= sd
->volume
& 0xff;
790 volume
|= volume
<< 8;
791 /* This may fail if there is no mixer. Ignore the failure. */
792 ioctl (sd
->fd
, SOUND_MIXER_WRITE_PCM
, &volume
);
797 /* Close device SD if it is open. */
801 struct sound_device
*sd
;
805 /* Flush sound data, and reset the device. */
806 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
808 /* Close the device. */
809 emacs_close (sd
->fd
);
815 /* Choose device-dependent format for device SD from sound file S. */
818 vox_choose_format (sd
, s
)
819 struct sound_device
*sd
;
824 struct wav_header
*h
= (struct wav_header
*) s
->header
;
825 if (h
->precision
== 8)
826 sd
->format
= AFMT_U8
;
827 else if (h
->precision
== 16)
828 sd
->format
= AFMT_S16_LE
;
830 error ("Unsupported WAV file format");
832 else if (s
->type
== SUN_AUDIO
)
834 struct au_header
*header
= (struct au_header
*) s
->header
;
835 switch (header
->encoding
)
837 case AU_ENCODING_ULAW_8
:
838 case AU_ENCODING_IEEE32
:
839 case AU_ENCODING_IEEE64
:
840 sd
->format
= AFMT_MU_LAW
;
847 sd
->format
= AFMT_S16_LE
;
851 error ("Unsupported AU file format");
859 /* Initialize device SD. Set up the interface functions in the device
864 struct sound_device
*sd
;
868 sd
->close
= vox_close
;
869 sd
->configure
= vox_configure
;
870 sd
->choose_format
= vox_choose_format
;
871 sd
->write
= vox_write
;
875 /* Write NBYTES bytes from BUFFER to device SD. */
878 vox_write (sd
, buffer
, nbytes
)
879 struct sound_device
*sd
;
883 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
885 sound_perror ("Writing to sound device");
890 /***********************************************************************
892 ***********************************************************************/
897 QCdevice
= intern (":device");
898 staticpro (&QCdevice
);
899 QCvolume
= intern (":volume");
900 staticpro (&QCvolume
);
901 Qsound
= intern ("sound");
903 Qplay_sound_functions
= intern ("play-sound-functions");
904 staticpro (&Qplay_sound_functions
);
906 defsubr (&Splay_sound
);
915 #endif /* HAVE_SOUND */