(sound_perror): Save errno from being clobbered.
[bpt/emacs.git] / src / sound.c
1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001 Free Software Foundation.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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. */
20
21 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
22 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
23
24 #include <config.h>
25
26 #if defined HAVE_SOUND
27
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"
37
38 #ifndef MSDOS
39 #include <sys/ioctl.h>
40 #endif
41
42 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
43 sys/soundcard.h. So, let's try whatever's there. */
44
45 #ifdef HAVE_MACHINE_SOUNDCARD_H
46 #include <machine/soundcard.h>
47 #endif
48 #ifdef HAVE_SYS_SOUNDCARD_H
49 #include <sys/soundcard.h>
50 #endif
51 #ifdef HAVE_SOUNDCARD_H
52 #include <soundcard.h>
53 #endif
54
55 #ifndef DEFAULT_SOUND_DEVICE
56 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
57 #endif
58
59 #define abs(X) ((X) < 0 ? -(X) : (X))
60
61 /* Structure forward declarations. */
62
63 struct sound;
64 struct sound_device;
65
66 /* The file header of RIFF-WAVE files (*.wav). Files are always in
67 little-endian byte-order. */
68
69 struct wav_header
70 {
71 u_int32_t magic;
72 u_int32_t length;
73 u_int32_t chunk_type;
74 u_int32_t chunk_format;
75 u_int32_t chunk_length;
76 u_int16_t format;
77 u_int16_t channels;
78 u_int32_t sample_rate;
79 u_int32_t bytes_per_second;
80 u_int16_t sample_size;
81 u_int16_t precision;
82 u_int32_t chunk_data;
83 u_int32_t data_length;
84 };
85
86 /* The file header of Sun adio files (*.au). Files are always in
87 big-endian byte-order. */
88
89 struct au_header
90 {
91 /* ASCII ".snd" */
92 u_int32_t magic_number;
93
94 /* Offset of data part from start of file. Minimum value is 24. */
95 u_int32_t data_offset;
96
97 /* Size of data part, 0xffffffff if unknown. */
98 u_int32_t data_size;
99
100 /* Data encoding format.
101 1 8-bit ISDN u-law
102 2 8-bit linear PCM (REF-PCM)
103 3 16-bit linear PCM
104 4 24-bit linear PCM
105 5 32-bit linear 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
109 encoding scheme. */
110 u_int32_t encoding;
111
112 /* Number of samples per second. */
113 u_int32_t sample_rate;
114
115 /* Number of interleaved channels. */
116 u_int32_t channels;
117 };
118
119 /* Maximum of all sound file headers sizes. */
120
121 #define MAX_SOUND_HEADER_BYTES \
122 max (sizeof (struct wav_header), sizeof (struct au_header))
123
124 /* Interface structure for sound devices. */
125
126 struct sound_device
127 {
128 /* The name of the device or null meaning use a default device name. */
129 char *file;
130
131 /* File descriptor of the device. */
132 int fd;
133
134 /* Device-dependent format. */
135 int format;
136
137 /* Volume (0..100). Zero means unspecified. */
138 int volume;
139
140 /* Sample size. */
141 int sample_size;
142
143 /* Sample rate. */
144 int sample_rate;
145
146 /* Bytes per second. */
147 int bps;
148
149 /* 1 = mono, 2 = stereo, 0 = don't set. */
150 int channels;
151
152 /* Open device SD. */
153 void (* open) P_ ((struct sound_device *sd));
154
155 /* Close device SD. */
156 void (* close) P_ ((struct sound_device *sd));
157
158 /* Configure SD accoring to device-dependent parameters. */
159 void (* configure) P_ ((struct sound_device *device));
160
161 /* Choose a device-dependent format for outputting sound S. */
162 void (* choose_format) P_ ((struct sound_device *sd,
163 struct sound *s));
164
165 /* Write NYBTES bytes from BUFFER to device SD. */
166 void (* write) P_ ((struct sound_device *sd, char *buffer, int nbytes));
167
168 /* A place for devices to store additional data. */
169 void *data;
170 };
171
172 /* An enumerator for each supported sound file type. */
173
174 enum sound_type
175 {
176 RIFF,
177 SUN_AUDIO
178 };
179
180 /* Interface structure for sound files. */
181
182 struct sound
183 {
184 /* The type of the file. */
185 enum sound_type type;
186
187 /* File descriptor of a sound file. */
188 int fd;
189
190 /* Pointer to sound file header. This contains header_size bytes
191 read from the start of a sound file. */
192 char *header;
193
194 /* Number of bytes raed from sound file. This is always <=
195 MAX_SOUND_HEADER_BYTES. */
196 int header_size;
197
198 /* Sound data, if a string. */
199 Lisp_Object data;
200
201 /* Play sound file S on device SD. */
202 void (* play) P_ ((struct sound *s, struct sound_device *sd));
203 };
204
205 /* Indices of attributes in a sound attributes vector. */
206
207 enum sound_attr
208 {
209 SOUND_FILE,
210 SOUND_DATA,
211 SOUND_DEVICE,
212 SOUND_VOLUME,
213 SOUND_ATTR_SENTINEL
214 };
215
216 /* Symbols. */
217
218 extern Lisp_Object QCfile, QCdata;
219 Lisp_Object QCvolume, QCdevice;
220 Lisp_Object Qsound;
221 Lisp_Object Qplay_sound_functions;
222
223 /* These are set during `play-sound' so that sound_cleanup has
224 access to them. */
225
226 struct sound_device *current_sound_device;
227 struct sound *current_sound;
228
229 /* Function prototypes. */
230
231 static void vox_open P_ ((struct sound_device *));
232 static void vox_configure P_ ((struct sound_device *));
233 static void vox_close P_ ((struct sound_device *sd));
234 static void vox_choose_format P_ ((struct sound_device *, struct sound *));
235 static void vox_init P_ ((struct sound_device *));
236 static void vox_write P_ ((struct sound_device *, char *, int));
237 static void sound_perror P_ ((char *));
238 static void sound_warning P_ ((char *));
239 static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
240 static void find_sound_type P_ ((struct sound *));
241 static u_int32_t le2hl P_ ((u_int32_t));
242 static u_int16_t le2hs P_ ((u_int16_t));
243 static u_int32_t be2hl P_ ((u_int32_t));
244 static int wav_init P_ ((struct sound *));
245 static void wav_play P_ ((struct sound *, struct sound_device *));
246 static int au_init P_ ((struct sound *));
247 static void au_play P_ ((struct sound *, struct sound_device *));
248
249 #if 0 /* Currently not used. */
250 static u_int16_t be2hs P_ ((u_int16_t));
251 #endif
252
253
254 \f
255 /***********************************************************************
256 General
257 ***********************************************************************/
258
259 /* Like perror, but signals an error. */
260
261 static void
262 sound_perror (msg)
263 char *msg;
264 {
265 int saved_errno = errno;
266
267 turn_on_atimers (1);
268 #ifdef SIGIO
269 sigunblock (sigmask (SIGIO));
270 #endif
271 if (saved_errno != 0)
272 error ("%s: %s", msg, strerror (saved_errno));
273 else
274 error ("%s", msg);
275 }
276
277
278 /* Display a warning message. */
279
280 static void
281 sound_warning (msg)
282 char *msg;
283 {
284 message (msg);
285 }
286
287
288 /* Parse sound specification SOUND, and fill ATTRS with what is
289 found. Value is non-zero if SOUND Is a valid sound specification.
290 A valid sound specification is a list starting with the symbol
291 `sound'. The rest of the list is a property list which may
292 contain the following key/value pairs:
293
294 - `:file FILE'
295
296 FILE is the sound file to play. If it isn't an absolute name,
297 it's searched under `data-directory'.
298
299 - `:data DATA'
300
301 DATA is a string containing sound data. Either :file or :data
302 may be present, but not both.
303
304 - `:device DEVICE'
305
306 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
307 If not specified, a default device is used.
308
309 - `:volume VOL'
310
311 VOL must be an integer in the range [0, 100], or a float in the
312 range [0, 1]. */
313
314 static int
315 parse_sound (sound, attrs)
316 Lisp_Object sound;
317 Lisp_Object *attrs;
318 {
319 /* SOUND must be a list starting with the symbol `sound'. */
320 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
321 return 0;
322
323 sound = XCDR (sound);
324 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
325 attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
326 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
327 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
328
329 /* File name or data must be specified. */
330 if (!STRINGP (attrs[SOUND_FILE])
331 && !STRINGP (attrs[SOUND_DATA]))
332 return 0;
333
334 /* Volume must be in the range 0..100 or unspecified. */
335 if (!NILP (attrs[SOUND_VOLUME]))
336 {
337 if (INTEGERP (attrs[SOUND_VOLUME]))
338 {
339 if (XINT (attrs[SOUND_VOLUME]) < 0
340 || XINT (attrs[SOUND_VOLUME]) > 100)
341 return 0;
342 }
343 else if (FLOATP (attrs[SOUND_VOLUME]))
344 {
345 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
346 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
347 return 0;
348 }
349 else
350 return 0;
351 }
352
353 /* Device must be a string or unspecified. */
354 if (!NILP (attrs[SOUND_DEVICE])
355 && !STRINGP (attrs[SOUND_DEVICE]))
356 return 0;
357
358 return 1;
359 }
360
361
362 /* Find out the type of the sound file whose file descriptor is FD.
363 S is the sound file structure to fill in. */
364
365 static void
366 find_sound_type (s)
367 struct sound *s;
368 {
369 if (!wav_init (s) && !au_init (s))
370 error ("Unknown sound format");
371 }
372
373
374 /* Function installed by play-sound with record_unwind_protect. */
375
376 static Lisp_Object
377 sound_cleanup (arg)
378 Lisp_Object arg;
379 {
380 if (current_sound_device)
381 {
382 if (current_sound_device->close)
383 current_sound_device->close (current_sound_device);
384 if (current_sound->fd > 0)
385 emacs_close (current_sound->fd);
386 }
387
388 return Qnil;
389 }
390
391
392 DEFUN ("play-sound", Fplay_sound, Splay_sound, 1, 1, 0,
393 doc: /* Play sound SOUND.
394 SOUND is a list of the form `(sound KEYWORD VALUE...)'.
395 The following keywords are recognized:
396
397 :file FILE.- read sound data from FILE. If FILE isn't an
398 absolute file name, it is searched in `data-directory'.
399
400 :data DATA - read sound data from string DATA.
401
402 Exactly one of :file or :data must be present.
403
404 :volume VOL - set volume to VOL. VOL must an integer in the
405 range 0..100 or a float in the range 0..1.0. If not specified,
406 don't change the volume setting of the sound device.
407
408 :device DEVICE - play sound on DEVICE. If not specified,
409 a system-dependent default device name is used. */)
410 (sound)
411 Lisp_Object sound;
412 {
413 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
414 Lisp_Object file;
415 struct gcpro gcpro1, gcpro2;
416 struct sound_device sd;
417 struct sound s;
418 Lisp_Object args[2];
419 int count = specpdl_ptr - specpdl;
420
421 file = Qnil;
422 GCPRO2 (sound, file);
423 bzero (&sd, sizeof sd);
424 bzero (&s, sizeof s);
425 current_sound_device = &sd;
426 current_sound = &s;
427 record_unwind_protect (sound_cleanup, Qnil);
428 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
429
430 /* Parse the sound specification. Give up if it is invalid. */
431 if (!parse_sound (sound, attrs))
432 error ("Invalid sound specification");
433
434 if (STRINGP (attrs[SOUND_FILE]))
435 {
436 /* Open the sound file. */
437 s.fd = openp (Fcons (Vdata_directory, Qnil),
438 attrs[SOUND_FILE], Qnil, &file, 0);
439 if (s.fd < 0)
440 sound_perror ("Could not open sound file");
441
442 /* Read the first bytes from the file. */
443 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
444 if (s.header_size < 0)
445 sound_perror ("Invalid sound file header");
446 }
447 else
448 {
449 s.data = attrs[SOUND_DATA];
450 bcopy (XSTRING (s.data)->data, s.header,
451 min (MAX_SOUND_HEADER_BYTES, STRING_BYTES (XSTRING (s.data))));
452 }
453
454 /* Find out the type of sound. Give up if we can't tell. */
455 find_sound_type (&s);
456
457 /* Set up a device. */
458 if (STRINGP (attrs[SOUND_DEVICE]))
459 {
460 int len = XSTRING (attrs[SOUND_DEVICE])->size;
461 sd.file = (char *) alloca (len + 1);
462 strcpy (sd.file, XSTRING (attrs[SOUND_DEVICE])->data);
463 }
464
465 if (INTEGERP (attrs[SOUND_VOLUME]))
466 sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
467 else if (FLOATP (attrs[SOUND_VOLUME]))
468 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
469
470 args[0] = Qplay_sound_functions;
471 args[1] = sound;
472 Frun_hook_with_args (2, args);
473
474 /* There is only one type of device we currently support, the VOX
475 sound driver. Set up the device interface functions for that
476 device. */
477 vox_init (&sd);
478
479 /* Open the device. */
480 sd.open (&sd);
481
482 /* Play the sound. */
483 s.play (&s, &sd);
484
485 /* Close the input file, if any. */
486 if (!STRINGP (s.data))
487 {
488 emacs_close (s.fd);
489 s.fd = -1;
490 }
491
492 /* Close the device. */
493 sd.close (&sd);
494
495 /* Clean up. */
496 current_sound_device = NULL;
497 current_sound = NULL;
498 UNGCPRO;
499 unbind_to (count, Qnil);
500 return Qnil;
501 }
502
503 \f
504 /***********************************************************************
505 Byte-order Conversion
506 ***********************************************************************/
507
508 /* Convert 32-bit value VALUE which is in little-endian byte-order
509 to host byte-order. */
510
511 static u_int32_t
512 le2hl (value)
513 u_int32_t value;
514 {
515 #ifdef WORDS_BIG_ENDIAN
516 unsigned char *p = (unsigned char *) &value;
517 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
518 #endif
519 return value;
520 }
521
522
523 /* Convert 16-bit value VALUE which is in little-endian byte-order
524 to host byte-order. */
525
526 static u_int16_t
527 le2hs (value)
528 u_int16_t value;
529 {
530 #ifdef WORDS_BIG_ENDIAN
531 unsigned char *p = (unsigned char *) &value;
532 value = p[0] + (p[1] << 8);
533 #endif
534 return value;
535 }
536
537
538 /* Convert 32-bit value VALUE which is in big-endian byte-order
539 to host byte-order. */
540
541 static u_int32_t
542 be2hl (value)
543 u_int32_t value;
544 {
545 #ifndef WORDS_BIG_ENDIAN
546 unsigned char *p = (unsigned char *) &value;
547 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
548 #endif
549 return value;
550 }
551
552
553 #if 0 /* Currently not used. */
554
555 /* Convert 16-bit value VALUE which is in big-endian byte-order
556 to host byte-order. */
557
558 static u_int16_t
559 be2hs (value)
560 u_int16_t value;
561 {
562 #ifndef WORDS_BIG_ENDIAN
563 unsigned char *p = (unsigned char *) &value;
564 value = p[1] + (p[0] << 8);
565 #endif
566 return value;
567 }
568
569 #endif /* 0 */
570
571 \f
572 /***********************************************************************
573 RIFF-WAVE (*.wav)
574 ***********************************************************************/
575
576 /* Try to initialize sound file S from S->header. S->header
577 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
578 sound file. If the file is a WAV-format file, set up interface
579 functions in S and convert header fields to host byte-order.
580 Value is non-zero if the file is a WAV file. */
581
582 static int
583 wav_init (s)
584 struct sound *s;
585 {
586 struct wav_header *header = (struct wav_header *) s->header;
587
588 if (s->header_size < sizeof *header
589 || bcmp (s->header, "RIFF", 4) != 0)
590 return 0;
591
592 /* WAV files are in little-endian order. Convert the header
593 if on a big-endian machine. */
594 header->magic = le2hl (header->magic);
595 header->length = le2hl (header->length);
596 header->chunk_type = le2hl (header->chunk_type);
597 header->chunk_format = le2hl (header->chunk_format);
598 header->chunk_length = le2hl (header->chunk_length);
599 header->format = le2hs (header->format);
600 header->channels = le2hs (header->channels);
601 header->sample_rate = le2hl (header->sample_rate);
602 header->bytes_per_second = le2hl (header->bytes_per_second);
603 header->sample_size = le2hs (header->sample_size);
604 header->precision = le2hs (header->precision);
605 header->chunk_data = le2hl (header->chunk_data);
606 header->data_length = le2hl (header->data_length);
607
608 /* Set up the interface functions for WAV. */
609 s->type = RIFF;
610 s->play = wav_play;
611
612 return 1;
613 }
614
615
616 /* Play RIFF-WAVE audio file S on sound device SD. */
617
618 static void
619 wav_play (s, sd)
620 struct sound *s;
621 struct sound_device *sd;
622 {
623 struct wav_header *header = (struct wav_header *) s->header;
624
625 /* Let the device choose a suitable device-dependent format
626 for the file. */
627 sd->choose_format (sd, s);
628
629 /* Configure the device. */
630 sd->sample_size = header->sample_size;
631 sd->sample_rate = header->sample_rate;
632 sd->bps = header->bytes_per_second;
633 sd->channels = header->channels;
634 sd->configure (sd);
635
636 /* Copy sound data to the device. The WAV file specification is
637 actually more complex. This simple scheme worked with all WAV
638 files I found so far. If someone feels inclined to implement the
639 whole RIFF-WAVE spec, please do. */
640 if (STRINGP (s->data))
641 sd->write (sd, XSTRING (s->data)->data + sizeof *header,
642 STRING_BYTES (XSTRING (s->data)) - sizeof *header);
643 else
644 {
645 char *buffer;
646 int nbytes;
647 int blksize = 2048;
648
649 buffer = (char *) alloca (blksize);
650 lseek (s->fd, sizeof *header, SEEK_SET);
651
652 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
653 sd->write (sd, buffer, nbytes);
654
655 if (nbytes < 0)
656 sound_perror ("Error reading sound file");
657 }
658 }
659
660
661 \f
662 /***********************************************************************
663 Sun Audio (*.au)
664 ***********************************************************************/
665
666 /* Sun audio file encodings. */
667
668 enum au_encoding
669 {
670 AU_ENCODING_ULAW_8 = 1,
671 AU_ENCODING_8,
672 AU_ENCODING_16,
673 AU_ENCODING_24,
674 AU_ENCODING_32,
675 AU_ENCODING_IEEE32,
676 AU_ENCODING_IEEE64,
677 AU_COMPRESSED = 23
678 };
679
680
681 /* Try to initialize sound file S from S->header. S->header
682 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
683 sound file. If the file is a AU-format file, set up interface
684 functions in S and convert header fields to host byte-order.
685 Value is non-zero if the file is an AU file. */
686
687 static int
688 au_init (s)
689 struct sound *s;
690 {
691 struct au_header *header = (struct au_header *) s->header;
692
693 if (s->header_size < sizeof *header
694 || bcmp (s->header, ".snd", 4) != 0)
695 return 0;
696
697 header->magic_number = be2hl (header->magic_number);
698 header->data_offset = be2hl (header->data_offset);
699 header->data_size = be2hl (header->data_size);
700 header->encoding = be2hl (header->encoding);
701 header->sample_rate = be2hl (header->sample_rate);
702 header->channels = be2hl (header->channels);
703
704 /* Set up the interface functions for AU. */
705 s->type = SUN_AUDIO;
706 s->play = au_play;
707
708 return 1;
709 }
710
711
712 /* Play Sun audio file S on sound device SD. */
713
714 static void
715 au_play (s, sd)
716 struct sound *s;
717 struct sound_device *sd;
718 {
719 struct au_header *header = (struct au_header *) s->header;
720
721 sd->sample_size = 0;
722 sd->sample_rate = header->sample_rate;
723 sd->bps = 0;
724 sd->channels = header->channels;
725 sd->choose_format (sd, s);
726 sd->configure (sd);
727
728 if (STRINGP (s->data))
729 sd->write (sd, XSTRING (s->data)->data + header->data_offset,
730 STRING_BYTES (XSTRING (s->data)) - header->data_offset);
731 else
732 {
733 int blksize = 2048;
734 char *buffer;
735 int nbytes;
736
737 /* Seek */
738 lseek (s->fd, header->data_offset, SEEK_SET);
739
740 /* Copy sound data to the device. */
741 buffer = (char *) alloca (blksize);
742 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
743 sd->write (sd, buffer, nbytes);
744
745 if (nbytes < 0)
746 sound_perror ("Error reading sound file");
747 }
748 }
749
750
751 \f
752 /***********************************************************************
753 Voxware Driver Interface
754 ***********************************************************************/
755
756 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
757 has a compatible own driver aka Luigi's driver. */
758
759
760 /* Open device SD. If SD->file is non-null, open that device,
761 otherwise use a default device name. */
762
763 static void
764 vox_open (sd)
765 struct sound_device *sd;
766 {
767 char *file;
768
769 /* Open the sound device. Default is /dev/dsp. */
770 if (sd->file)
771 file = sd->file;
772 else
773 file = DEFAULT_SOUND_DEVICE;
774
775 sd->fd = emacs_open (file, O_WRONLY, 0);
776 if (sd->fd < 0)
777 sound_perror (file);
778 }
779
780
781 /* Configure device SD from parameters in it. */
782
783 static void
784 vox_configure (sd)
785 struct sound_device *sd;
786 {
787 int val;
788
789 xassert (sd->fd >= 0);
790
791 /* On GNU/Linux, it seems that the device driver doesn't like to be
792 interrupted by a signal. Block the ones we know to cause
793 troubles. */
794 turn_on_atimers (0);
795 #ifdef SIGIO
796 sigblock (sigmask (SIGIO));
797 #endif
798
799 val = sd->format;
800 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
801 || val != sd->format)
802 sound_perror ("Could not set sound format");
803
804 val = sd->channels != 1;
805 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
806 || val != (sd->channels != 1))
807 sound_perror ("Could not set stereo/mono");
808
809 /* I think bps and sampling_rate are the same, but who knows.
810 Check this. and use SND_DSP_SPEED for both. */
811 if (sd->sample_rate > 0)
812 {
813 val = sd->sample_rate;
814 if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0)
815 sound_perror ("Could not set sound speed");
816 else if (val != sd->sample_rate)
817 sound_warning ("Could not set sample rate");
818 }
819
820 if (sd->volume > 0)
821 {
822 int volume = sd->volume & 0xff;
823 volume |= volume << 8;
824 /* This may fail if there is no mixer. Ignore the failure. */
825 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
826 }
827
828 turn_on_atimers (1);
829 #ifdef SIGIO
830 sigunblock (sigmask (SIGIO));
831 #endif
832 }
833
834
835 /* Close device SD if it is open. */
836
837 static void
838 vox_close (sd)
839 struct sound_device *sd;
840 {
841 if (sd->fd >= 0)
842 {
843 /* On GNU/Linux, it seems that the device driver doesn't like to
844 be interrupted by a signal. Block the ones we know to cause
845 troubles. */
846 #ifdef SIGIO
847 sigblock (sigmask (SIGIO));
848 #endif
849 turn_on_atimers (0);
850
851 /* Flush sound data, and reset the device. */
852 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
853
854 turn_on_atimers (1);
855 #ifdef SIGIO
856 sigunblock (sigmask (SIGIO));
857 #endif
858
859 /* Close the device. */
860 emacs_close (sd->fd);
861 sd->fd = -1;
862 }
863 }
864
865
866 /* Choose device-dependent format for device SD from sound file S. */
867
868 static void
869 vox_choose_format (sd, s)
870 struct sound_device *sd;
871 struct sound *s;
872 {
873 if (s->type == RIFF)
874 {
875 struct wav_header *h = (struct wav_header *) s->header;
876 if (h->precision == 8)
877 sd->format = AFMT_U8;
878 else if (h->precision == 16)
879 sd->format = AFMT_S16_LE;
880 else
881 error ("Unsupported WAV file format");
882 }
883 else if (s->type == SUN_AUDIO)
884 {
885 struct au_header *header = (struct au_header *) s->header;
886 switch (header->encoding)
887 {
888 case AU_ENCODING_ULAW_8:
889 case AU_ENCODING_IEEE32:
890 case AU_ENCODING_IEEE64:
891 sd->format = AFMT_MU_LAW;
892 break;
893
894 case AU_ENCODING_8:
895 case AU_ENCODING_16:
896 case AU_ENCODING_24:
897 case AU_ENCODING_32:
898 sd->format = AFMT_S16_LE;
899 break;
900
901 default:
902 error ("Unsupported AU file format");
903 }
904 }
905 else
906 abort ();
907 }
908
909
910 /* Initialize device SD. Set up the interface functions in the device
911 structure. */
912
913 static void
914 vox_init (sd)
915 struct sound_device *sd;
916 {
917 sd->fd = -1;
918 sd->open = vox_open;
919 sd->close = vox_close;
920 sd->configure = vox_configure;
921 sd->choose_format = vox_choose_format;
922 sd->write = vox_write;
923 }
924
925
926 /* Write NBYTES bytes from BUFFER to device SD. */
927
928 static void
929 vox_write (sd, buffer, nbytes)
930 struct sound_device *sd;
931 char *buffer;
932 int nbytes;
933 {
934 int nwritten = emacs_write (sd->fd, buffer, nbytes);
935 if (nwritten < 0)
936 sound_perror ("Error writing to sound device");
937 }
938
939
940 \f
941 /***********************************************************************
942 Initialization
943 ***********************************************************************/
944
945 void
946 syms_of_sound ()
947 {
948 QCdevice = intern (":device");
949 staticpro (&QCdevice);
950 QCvolume = intern (":volume");
951 staticpro (&QCvolume);
952 Qsound = intern ("sound");
953 staticpro (&Qsound);
954 Qplay_sound_functions = intern ("play-sound-functions");
955 staticpro (&Qplay_sound_functions);
956
957 defsubr (&Splay_sound);
958 }
959
960
961 void
962 init_sound ()
963 {
964 }
965
966 #endif /* HAVE_SOUND */