*** empty log message ***
[emacs.git] / src / sound.c
blobe5c800596565e159c284f050188b5a2f3cc85f3f
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)
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 <fcntl.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include "lisp.h"
33 #include "dispextern.h"
34 #include "atimer.h"
35 #include <signal.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>
43 #endif
44 #ifdef HAVE_SYS_SOUNDCARD_H
45 #include <sys/soundcard.h>
46 #endif
47 #ifdef HAVE_SOUNDCARD_H
48 #include <sys/ioctl.h>
49 #include <soundcard.h>
50 #endif
52 #ifndef DEFAULT_SOUND_DEVICE
53 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
54 #endif
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. */
62 struct sound;
63 struct sound_device;
65 /* The file header of RIFF-WAVE files (*.wav). Files are always in
66 little-endian byte-order. */
68 struct wav_header
70 u_int32_t magic;
71 u_int32_t length;
72 u_int32_t chunk_type;
73 u_int32_t chunk_format;
74 u_int32_t chunk_length;
75 u_int16_t format;
76 u_int16_t channels;
77 u_int32_t sample_rate;
78 u_int32_t bytes_per_second;
79 u_int16_t sample_size;
80 u_int16_t precision;
81 u_int32_t chunk_data;
82 u_int32_t data_length;
85 /* The file header of Sun adio files (*.au). Files are always in
86 big-endian byte-order. */
88 struct au_header
90 /* ASCII ".snd" */
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. */
97 u_int32_t data_size;
99 /* Data encoding format.
100 1 8-bit ISDN u-law
101 2 8-bit linear PCM (REF-PCM)
102 3 16-bit linear PCM
103 4 24-bit linear PCM
104 5 32-bit linear 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
108 encoding scheme. */
109 u_int32_t encoding;
111 /* Number of samples per second. */
112 u_int32_t sample_rate;
114 /* Number of interleaved channels. */
115 u_int32_t 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. */
125 struct sound_device
127 /* The name of the device or null meaning use a default device name. */
128 char *file;
130 /* File descriptor of the device. */
131 int fd;
133 /* Device-dependent format. */
134 int format;
136 /* Volume (0..100). Zero means unspecified. */
137 int volume;
139 /* Sample size. */
140 int sample_size;
142 /* Sample rate. */
143 int sample_rate;
145 /* Bytes per second. */
146 int bps;
148 /* 1 = mono, 2 = stereo, 0 = don't set. */
149 int channels;
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,
162 struct sound *s));
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. */
168 void *data;
171 /* An enumerator for each supported sound file type. */
173 enum sound_type
175 RIFF,
176 SUN_AUDIO
179 /* Interface structure for sound files. */
181 struct sound
183 /* The type of the file. */
184 enum sound_type type;
186 /* File descriptor of a sound file. */
187 int fd;
189 /* Pointer to sound file header. This contains header_size bytes
190 read from the start of a sound file. */
191 char *header;
193 /* Number of bytes raed from sound file. This is always <=
194 MAX_SOUND_HEADER_BYTES. */
195 int header_size;
197 /* Sound data, if a string. */
198 Lisp_Object data;
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. */
206 enum sound_attr
208 SOUND_FILE,
209 SOUND_DATA,
210 SOUND_DEVICE,
211 SOUND_VOLUME,
212 SOUND_ATTR_SENTINEL
215 /* Symbols. */
217 extern Lisp_Object QCfile, QCdata;
218 Lisp_Object QCvolume, QCdevice;
219 Lisp_Object Qsound;
220 Lisp_Object Qplay_sound_functions;
222 /* These are set during `play-sound' so that sound_cleanup has
223 access to them. */
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));
249 #endif
253 /***********************************************************************
254 General
255 ***********************************************************************/
257 /* Like perror, but signals an error. */
259 static void
260 sound_perror (msg)
261 char *msg;
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:
273 - `:file FILE'
275 FILE is the sound file to play. If it isn't an absolute name,
276 it's searched under `data-directory'.
278 - `:data DATA'
280 DATA is a string containing sound data. Either :file or :data
281 may be present, but not both.
283 - `:device DEVICE'
285 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
286 If not specified, a default device is used.
288 - `:volume VOL'
290 VOL must be an integer in the range [0, 100], or a float in the
291 range [0, 1]. */
293 static int
294 parse_sound (sound, attrs)
295 Lisp_Object sound;
296 Lisp_Object *attrs;
298 /* SOUND must be a list starting with the symbol `sound'. */
299 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
300 return 0;
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]))
311 return 0;
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)
320 return 0;
322 else if (FLOATP (attrs[SOUND_VOLUME]))
324 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
325 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
326 return 0;
328 else
329 return 0;
332 /* Device must be a string or unspecified. */
333 if (!NILP (attrs[SOUND_DEVICE])
334 && !STRINGP (attrs[SOUND_DEVICE]))
335 return 0;
337 return 1;
341 /* Find out the type of the sound file whose file descriptor is FD.
342 S is the sound file structure to fill in. */
344 static void
345 find_sound_type (s)
346 struct sound *s;
348 if (!wav_init (s) && !au_init (s))
349 error ("Unknown sound format");
353 /* Function installed by play-sound with record_unwind_protect. */
355 static Lisp_Object
356 sound_cleanup (arg)
357 Lisp_Object arg;
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);
367 return Qnil;
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.")
389 (sound)
390 Lisp_Object sound;
392 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
393 Lisp_Object file;
394 struct gcpro gcpro1, gcpro2;
395 struct sound_device sd;
396 struct sound s;
397 Lisp_Object args[2];
398 int count = specpdl_ptr - specpdl;
400 file = Qnil;
401 GCPRO2 (sound, file);
402 bzero (&sd, sizeof sd);
403 bzero (&s, sizeof s);
404 current_sound_device = &sd;
405 current_sound = &s;
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);
418 if (s.fd < 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");
426 else
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;
450 args[1] = sound;
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
455 device. */
456 vox_init (&sd);
458 /* Open the device. */
459 sd.open (&sd);
461 /* Play the sound. */
462 s.play (&s, &sd);
464 /* Close the input file, if any. */
465 if (!STRINGP (s.data))
467 emacs_close (s.fd);
468 s.fd = -1;
471 /* Close the device. */
472 sd.close (&sd);
474 /* Clean up. */
475 current_sound_device = NULL;
476 current_sound = NULL;
477 UNGCPRO;
478 unbind_to (count, Qnil);
479 return 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. */
490 static u_int32_t
491 le2hl (value)
492 u_int32_t value;
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);
497 #endif
498 return value;
502 /* Convert 16-bit value VALUE which is in little-endian byte-order
503 to host byte-order. */
505 static u_int16_t
506 le2hs (value)
507 u_int16_t value;
509 #ifdef WORDS_BIG_ENDIAN
510 unsigned char *p = (unsigned char *) &value;
511 value = p[0] + (p[1] << 8);
512 #endif
513 return value;
517 /* Convert 32-bit value VALUE which is in big-endian byte-order
518 to host byte-order. */
520 static u_int32_t
521 be2hl (value)
522 u_int32_t value;
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);
527 #endif
528 return value;
532 #if 0 /* Currently not used. */
534 /* Convert 16-bit value VALUE which is in big-endian byte-order
535 to host byte-order. */
537 static u_int16_t
538 be2hs (value)
539 u_int16_t value;
541 #ifndef WORDS_BIG_ENDIAN
542 unsigned char *p = (unsigned char *) &value;
543 value = p[1] + (p[0] << 8);
544 #endif
545 return value;
548 #endif /* 0 */
551 /***********************************************************************
552 RIFF-WAVE (*.wav)
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. */
561 static int
562 wav_init (s)
563 struct sound *s;
565 struct wav_header *header = (struct wav_header *) s->header;
567 if (s->header_size < sizeof *header
568 || bcmp (s->header, "RIFF", 4) != 0)
569 return 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. */
588 s->type = RIFF;
589 s->play = wav_play;
591 return 1;
595 /* Play RIFF-WAVE audio file S on sound device SD. */
597 static void
598 wav_play (s, sd)
599 struct sound *s;
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
605 for the file. */
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;
613 sd->configure (sd);
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);
622 else
624 char *buffer;
625 int nbytes;
626 int blksize = 2048;
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);
634 if (nbytes < 0)
635 sound_perror ("Reading sound file");
641 /***********************************************************************
642 Sun Audio (*.au)
643 ***********************************************************************/
645 /* Sun audio file encodings. */
647 enum au_encoding
649 AU_ENCODING_ULAW_8 = 1,
650 AU_ENCODING_8,
651 AU_ENCODING_16,
652 AU_ENCODING_24,
653 AU_ENCODING_32,
654 AU_ENCODING_IEEE32,
655 AU_ENCODING_IEEE64,
656 AU_COMPRESSED = 23
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. */
666 static int
667 au_init (s)
668 struct sound *s;
670 struct au_header *header = (struct au_header *) s->header;
672 if (s->header_size < sizeof *header
673 || bcmp (s->header, ".snd", 4) != 0)
674 return 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. */
684 s->type = SUN_AUDIO;
685 s->play = au_play;
687 return 1;
691 /* Play Sun audio file S on sound device SD. */
693 static void
694 au_play (s, sd)
695 struct sound *s;
696 struct sound_device *sd;
698 struct au_header *header = (struct au_header *) s->header;
700 sd->sample_size = 0;
701 sd->sample_rate = header->sample_rate;
702 sd->bps = 0;
703 sd->channels = header->channels;
704 sd->choose_format (sd, s);
705 sd->configure (sd);
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);
710 else
712 int blksize = 2048;
713 char *buffer;
714 int nbytes;
716 /* Seek */
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);
724 if (nbytes < 0)
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. */
742 static void
743 vox_open (sd)
744 struct sound_device *sd;
746 char *file;
748 /* Open the sound device. Default is /dev/dsp. */
749 if (sd->file)
750 file = sd->file;
751 else
752 file = DEFAULT_SOUND_DEVICE;
754 sd->fd = emacs_open (file, O_WRONLY, 0);
755 if (sd->fd < 0)
756 sound_perror (file);
760 /* Configure device SD from parameters in it. */
762 static void
763 vox_configure (sd)
764 struct sound_device *sd;
766 int val;
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
772 troubles. */
773 turn_on_atimers (0);
774 #ifdef SIGIO
775 sigblock (sigmask (SIGIO));
776 #endif
778 val = sd->format;
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");
798 if (sd->volume > 0)
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);
806 turn_on_atimers (1);
807 #ifdef SIGIO
808 sigunblock (sigmask (SIGIO));
809 #endif
813 /* Close device SD if it is open. */
815 static void
816 vox_close (sd)
817 struct sound_device *sd;
819 if (sd->fd >= 0)
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
823 troubles. */
824 #ifdef SIGIO
825 sigblock (sigmask (SIGIO));
826 #endif
827 turn_on_atimers (0);
829 /* Flush sound data, and reset the device. */
830 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
832 turn_on_atimers (1);
833 #ifdef SIGIO
834 sigunblock (sigmask (SIGIO));
835 #endif
837 /* Close the device. */
838 emacs_close (sd->fd);
839 sd->fd = -1;
844 /* Choose device-dependent format for device SD from sound file S. */
846 static void
847 vox_choose_format (sd, s)
848 struct sound_device *sd;
849 struct sound *s;
851 if (s->type == RIFF)
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;
858 else
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;
870 break;
872 case AU_ENCODING_8:
873 case AU_ENCODING_16:
874 case AU_ENCODING_24:
875 case AU_ENCODING_32:
876 sd->format = AFMT_S16_LE;
877 break;
879 default:
880 error ("Unsupported AU file format");
883 else
884 abort ();
888 /* Initialize device SD. Set up the interface functions in the device
889 structure. */
891 static void
892 vox_init (sd)
893 struct sound_device *sd;
895 sd->fd = -1;
896 sd->open = vox_open;
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. */
906 static void
907 vox_write (sd, buffer, nbytes)
908 struct sound_device *sd;
909 char *buffer;
910 int nbytes;
912 int nwritten = emacs_write (sd->fd, buffer, nbytes);
913 if (nwritten < 0)
914 sound_perror ("Writing to sound device");
919 /***********************************************************************
920 Initialization
921 ***********************************************************************/
923 void
924 syms_of_sound ()
926 QCdevice = intern (":device");
927 staticpro (&QCdevice);
928 QCvolume = intern (":volume");
929 staticpro (&QCvolume);
930 Qsound = intern ("sound");
931 staticpro (&Qsound);
932 Qplay_sound_functions = intern ("play-sound-functions");
933 staticpro (&Qplay_sound_functions);
935 defsubr (&Splay_sound);
939 void
940 init_sound ()
944 #endif /* HAVE_SOUND */