Put doc strings in comments.
[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 int parse_sound P_ ((Lisp_Object, Lisp_Object *));
239 static void find_sound_type P_ ((struct sound *));
240 static u_int32_t le2hl P_ ((u_int32_t));
241 static u_int16_t le2hs P_ ((u_int16_t));
242 static u_int32_t be2hl P_ ((u_int32_t));
243 static int wav_init P_ ((struct sound *));
244 static void wav_play P_ ((struct sound *, struct sound_device *));
245 static int au_init P_ ((struct sound *));
246 static void au_play P_ ((struct sound *, struct sound_device *));
247
248 #if 0 /* Currently not used. */
249 static u_int16_t be2hs P_ ((u_int16_t));
250 #endif
251
252
253 \f
254 /***********************************************************************
255 General
256 ***********************************************************************/
257
258 /* Like perror, but signals an error. */
259
260 static void
261 sound_perror (msg)
262 char *msg;
263 {
264 error ("%s: %s", msg, strerror (errno));
265 }
266
267
268 /* Parse sound specification SOUND, and fill ATTRS with what is
269 found. Value is non-zero if SOUND Is a valid sound specification.
270 A valid sound specification is a list starting with the symbol
271 `sound'. The rest of the list is a property list which may
272 contain the following key/value pairs:
273
274 - `:file FILE'
275
276 FILE is the sound file to play. If it isn't an absolute name,
277 it's searched under `data-directory'.
278
279 - `:data DATA'
280
281 DATA is a string containing sound data. Either :file or :data
282 may be present, but not both.
283
284 - `:device DEVICE'
285
286 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
287 If not specified, a default device is used.
288
289 - `:volume VOL'
290
291 VOL must be an integer in the range [0, 100], or a float in the
292 range [0, 1]. */
293
294 static int
295 parse_sound (sound, attrs)
296 Lisp_Object sound;
297 Lisp_Object *attrs;
298 {
299 /* SOUND must be a list starting with the symbol `sound'. */
300 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
301 return 0;
302
303 sound = XCDR (sound);
304 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
305 attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
306 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
307 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
308
309 /* File name or data must be specified. */
310 if (!STRINGP (attrs[SOUND_FILE])
311 && !STRINGP (attrs[SOUND_DATA]))
312 return 0;
313
314 /* Volume must be in the range 0..100 or unspecified. */
315 if (!NILP (attrs[SOUND_VOLUME]))
316 {
317 if (INTEGERP (attrs[SOUND_VOLUME]))
318 {
319 if (XINT (attrs[SOUND_VOLUME]) < 0
320 || XINT (attrs[SOUND_VOLUME]) > 100)
321 return 0;
322 }
323 else if (FLOATP (attrs[SOUND_VOLUME]))
324 {
325 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
326 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
327 return 0;
328 }
329 else
330 return 0;
331 }
332
333 /* Device must be a string or unspecified. */
334 if (!NILP (attrs[SOUND_DEVICE])
335 && !STRINGP (attrs[SOUND_DEVICE]))
336 return 0;
337
338 return 1;
339 }
340
341
342 /* Find out the type of the sound file whose file descriptor is FD.
343 S is the sound file structure to fill in. */
344
345 static void
346 find_sound_type (s)
347 struct sound *s;
348 {
349 if (!wav_init (s) && !au_init (s))
350 error ("Unknown sound format");
351 }
352
353
354 /* Function installed by play-sound with record_unwind_protect. */
355
356 static Lisp_Object
357 sound_cleanup (arg)
358 Lisp_Object arg;
359 {
360 if (current_sound_device)
361 {
362 if (current_sound_device->close)
363 current_sound_device->close (current_sound_device);
364 if (current_sound->fd > 0)
365 emacs_close (current_sound->fd);
366 }
367
368 return Qnil;
369 }
370
371
372 DEFUN ("play-sound", Fplay_sound, Splay_sound, 1, 1, 0,
373 "Play sound SOUND.\n\
374 SOUND is a list of the form `(sound KEYWORD VALUE...)'.\n\
375 The following keywords are recognized:\n\
376 \n\
377 :file FILE.- read sound data from FILE. If FILE isn't an\n\
378 absolute file name, it is searched in `data-directory'.\n\
379 \n\
380 :data DATA - read sound data from string DATA.\n\
381 \n\
382 Exactly one of :file or :data must be present.\n\
383 \n\
384 :volume VOL - set volume to VOL. VOL must an integer in the\n\
385 range 0..100 or a float in the range 0..1.0. If not specified,\n\
386 don't change the volume setting of the sound device.\n\
387 \n\
388 :device DEVICE - play sound on DEVICE. If not specified,\n\
389 a system-dependent default device name is used.")
390 (sound)
391 Lisp_Object sound;
392 {
393 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
394 Lisp_Object file;
395 struct gcpro gcpro1, gcpro2;
396 struct sound_device sd;
397 struct sound s;
398 Lisp_Object args[2];
399 int count = specpdl_ptr - specpdl;
400
401 file = Qnil;
402 GCPRO2 (sound, file);
403 bzero (&sd, sizeof sd);
404 bzero (&s, sizeof s);
405 current_sound_device = &sd;
406 current_sound = &s;
407 record_unwind_protect (sound_cleanup, Qnil);
408 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
409
410 /* Parse the sound specification. Give up if it is invalid. */
411 if (!parse_sound (sound, attrs))
412 error ("Invalid sound specification");
413
414 if (STRINGP (attrs[SOUND_FILE]))
415 {
416 /* Open the sound file. */
417 s.fd = openp (Fcons (Vdata_directory, Qnil),
418 attrs[SOUND_FILE], Qnil, &file, 0);
419 if (s.fd < 0)
420 sound_perror ("Open sound file");
421
422 /* Read the first bytes from the file. */
423 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
424 if (s.header_size < 0)
425 sound_perror ("Reading sound file header");
426 }
427 else
428 {
429 s.data = attrs[SOUND_DATA];
430 bcopy (XSTRING (s.data)->data, s.header,
431 min (MAX_SOUND_HEADER_BYTES, STRING_BYTES (XSTRING (s.data))));
432 }
433
434 /* Find out the type of sound. Give up if we can't tell. */
435 find_sound_type (&s);
436
437 /* Set up a device. */
438 if (STRINGP (attrs[SOUND_DEVICE]))
439 {
440 int len = XSTRING (attrs[SOUND_DEVICE])->size;
441 sd.file = (char *) alloca (len + 1);
442 strcpy (sd.file, XSTRING (attrs[SOUND_DEVICE])->data);
443 }
444
445 if (INTEGERP (attrs[SOUND_VOLUME]))
446 sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
447 else if (FLOATP (attrs[SOUND_VOLUME]))
448 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
449
450 args[0] = Qplay_sound_functions;
451 args[1] = sound;
452 Frun_hook_with_args (2, args);
453
454 /* There is only one type of device we currently support, the VOX
455 sound driver. Set up the device interface functions for that
456 device. */
457 vox_init (&sd);
458
459 /* Open the device. */
460 sd.open (&sd);
461
462 /* Play the sound. */
463 s.play (&s, &sd);
464
465 /* Close the input file, if any. */
466 if (!STRINGP (s.data))
467 {
468 emacs_close (s.fd);
469 s.fd = -1;
470 }
471
472 /* Close the device. */
473 sd.close (&sd);
474
475 /* Clean up. */
476 current_sound_device = NULL;
477 current_sound = NULL;
478 UNGCPRO;
479 unbind_to (count, Qnil);
480 return Qnil;
481 }
482
483 \f
484 /***********************************************************************
485 Byte-order Conversion
486 ***********************************************************************/
487
488 /* Convert 32-bit value VALUE which is in little-endian byte-order
489 to host byte-order. */
490
491 static u_int32_t
492 le2hl (value)
493 u_int32_t value;
494 {
495 #ifdef WORDS_BIG_ENDIAN
496 unsigned char *p = (unsigned char *) &value;
497 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
498 #endif
499 return value;
500 }
501
502
503 /* Convert 16-bit value VALUE which is in little-endian byte-order
504 to host byte-order. */
505
506 static u_int16_t
507 le2hs (value)
508 u_int16_t value;
509 {
510 #ifdef WORDS_BIG_ENDIAN
511 unsigned char *p = (unsigned char *) &value;
512 value = p[0] + (p[1] << 8);
513 #endif
514 return value;
515 }
516
517
518 /* Convert 32-bit value VALUE which is in big-endian byte-order
519 to host byte-order. */
520
521 static u_int32_t
522 be2hl (value)
523 u_int32_t value;
524 {
525 #ifndef WORDS_BIG_ENDIAN
526 unsigned char *p = (unsigned char *) &value;
527 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
528 #endif
529 return value;
530 }
531
532
533 #if 0 /* Currently not used. */
534
535 /* Convert 16-bit value VALUE which is in big-endian byte-order
536 to host byte-order. */
537
538 static u_int16_t
539 be2hs (value)
540 u_int16_t value;
541 {
542 #ifndef WORDS_BIG_ENDIAN
543 unsigned char *p = (unsigned char *) &value;
544 value = p[1] + (p[0] << 8);
545 #endif
546 return value;
547 }
548
549 #endif /* 0 */
550
551 \f
552 /***********************************************************************
553 RIFF-WAVE (*.wav)
554 ***********************************************************************/
555
556 /* Try to initialize sound file S from S->header. S->header
557 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
558 sound file. If the file is a WAV-format file, set up interface
559 functions in S and convert header fields to host byte-order.
560 Value is non-zero if the file is a WAV file. */
561
562 static int
563 wav_init (s)
564 struct sound *s;
565 {
566 struct wav_header *header = (struct wav_header *) s->header;
567
568 if (s->header_size < sizeof *header
569 || bcmp (s->header, "RIFF", 4) != 0)
570 return 0;
571
572 /* WAV files are in little-endian order. Convert the header
573 if on a big-endian machine. */
574 header->magic = le2hl (header->magic);
575 header->length = le2hl (header->length);
576 header->chunk_type = le2hl (header->chunk_type);
577 header->chunk_format = le2hl (header->chunk_format);
578 header->chunk_length = le2hl (header->chunk_length);
579 header->format = le2hs (header->format);
580 header->channels = le2hs (header->channels);
581 header->sample_rate = le2hl (header->sample_rate);
582 header->bytes_per_second = le2hl (header->bytes_per_second);
583 header->sample_size = le2hs (header->sample_size);
584 header->precision = le2hs (header->precision);
585 header->chunk_data = le2hl (header->chunk_data);
586 header->data_length = le2hl (header->data_length);
587
588 /* Set up the interface functions for WAV. */
589 s->type = RIFF;
590 s->play = wav_play;
591
592 return 1;
593 }
594
595
596 /* Play RIFF-WAVE audio file S on sound device SD. */
597
598 static void
599 wav_play (s, sd)
600 struct sound *s;
601 struct sound_device *sd;
602 {
603 struct wav_header *header = (struct wav_header *) s->header;
604
605 /* Let the device choose a suitable device-dependent format
606 for the file. */
607 sd->choose_format (sd, s);
608
609 /* Configure the device. */
610 sd->sample_size = header->sample_size;
611 sd->sample_rate = header->sample_rate;
612 sd->bps = header->bytes_per_second;
613 sd->channels = header->channels;
614 sd->configure (sd);
615
616 /* Copy sound data to the device. The WAV file specification is
617 actually more complex. This simple scheme worked with all WAV
618 files I found so far. If someone feels inclined to implement the
619 whole RIFF-WAVE spec, please do. */
620 if (STRINGP (s->data))
621 sd->write (sd, XSTRING (s->data)->data + sizeof *header,
622 STRING_BYTES (XSTRING (s->data)) - sizeof *header);
623 else
624 {
625 char *buffer;
626 int nbytes;
627 int blksize = 2048;
628
629 buffer = (char *) alloca (blksize);
630 lseek (s->fd, sizeof *header, SEEK_SET);
631
632 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
633 sd->write (sd, buffer, nbytes);
634
635 if (nbytes < 0)
636 sound_perror ("Reading sound file");
637 }
638 }
639
640
641 \f
642 /***********************************************************************
643 Sun Audio (*.au)
644 ***********************************************************************/
645
646 /* Sun audio file encodings. */
647
648 enum au_encoding
649 {
650 AU_ENCODING_ULAW_8 = 1,
651 AU_ENCODING_8,
652 AU_ENCODING_16,
653 AU_ENCODING_24,
654 AU_ENCODING_32,
655 AU_ENCODING_IEEE32,
656 AU_ENCODING_IEEE64,
657 AU_COMPRESSED = 23
658 };
659
660
661 /* Try to initialize sound file S from S->header. S->header
662 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
663 sound file. If the file is a AU-format file, set up interface
664 functions in S and convert header fields to host byte-order.
665 Value is non-zero if the file is an AU file. */
666
667 static int
668 au_init (s)
669 struct sound *s;
670 {
671 struct au_header *header = (struct au_header *) s->header;
672
673 if (s->header_size < sizeof *header
674 || bcmp (s->header, ".snd", 4) != 0)
675 return 0;
676
677 header->magic_number = be2hl (header->magic_number);
678 header->data_offset = be2hl (header->data_offset);
679 header->data_size = be2hl (header->data_size);
680 header->encoding = be2hl (header->encoding);
681 header->sample_rate = be2hl (header->sample_rate);
682 header->channels = be2hl (header->channels);
683
684 /* Set up the interface functions for AU. */
685 s->type = SUN_AUDIO;
686 s->play = au_play;
687
688 return 1;
689 }
690
691
692 /* Play Sun audio file S on sound device SD. */
693
694 static void
695 au_play (s, sd)
696 struct sound *s;
697 struct sound_device *sd;
698 {
699 struct au_header *header = (struct au_header *) s->header;
700
701 sd->sample_size = 0;
702 sd->sample_rate = header->sample_rate;
703 sd->bps = 0;
704 sd->channels = header->channels;
705 sd->choose_format (sd, s);
706 sd->configure (sd);
707
708 if (STRINGP (s->data))
709 sd->write (sd, XSTRING (s->data)->data + header->data_offset,
710 STRING_BYTES (XSTRING (s->data)) - header->data_offset);
711 else
712 {
713 int blksize = 2048;
714 char *buffer;
715 int nbytes;
716
717 /* Seek */
718 lseek (s->fd, header->data_offset, SEEK_SET);
719
720 /* Copy sound data to the device. */
721 buffer = (char *) alloca (blksize);
722 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
723 sd->write (sd, buffer, nbytes);
724
725 if (nbytes < 0)
726 sound_perror ("Reading sound file");
727 }
728 }
729
730
731 \f
732 /***********************************************************************
733 Voxware Driver Interface
734 ***********************************************************************/
735
736 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
737 has a compatible own driver aka Luigi's driver. */
738
739
740 /* Open device SD. If SD->file is non-null, open that device,
741 otherwise use a default device name. */
742
743 static void
744 vox_open (sd)
745 struct sound_device *sd;
746 {
747 char *file;
748
749 /* Open the sound device. Default is /dev/dsp. */
750 if (sd->file)
751 file = sd->file;
752 else
753 file = DEFAULT_SOUND_DEVICE;
754
755 sd->fd = emacs_open (file, O_WRONLY, 0);
756 if (sd->fd < 0)
757 sound_perror (file);
758 }
759
760
761 /* Configure device SD from parameters in it. */
762
763 static void
764 vox_configure (sd)
765 struct sound_device *sd;
766 {
767 int val;
768
769 xassert (sd->fd >= 0);
770
771 /* On GNU/Linux, it seems that the device driver doesn't like to be
772 interrupted by a signal. Block the ones we know to cause
773 troubles. */
774 turn_on_atimers (0);
775 #ifdef SIGIO
776 sigblock (sigmask (SIGIO));
777 #endif
778
779 val = sd->format;
780 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
781 || val != sd->format)
782 sound_perror ("Set sound format");
783
784 val = sd->channels != 1;
785 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
786 || val != (sd->channels != 1))
787 sound_perror ("Set stereo/mono");
788
789 /* I think bps and sampling_rate are the same, but who knows.
790 Check this. and use SND_DSP_SPEED for both. */
791 if (sd->sample_rate > 0)
792 {
793 val = sd->sample_rate;
794 if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0
795 || val != sd->sample_rate)
796 sound_perror ("Set sound speed");
797 }
798
799 if (sd->volume > 0)
800 {
801 int volume = sd->volume & 0xff;
802 volume |= volume << 8;
803 /* This may fail if there is no mixer. Ignore the failure. */
804 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
805 }
806
807 turn_on_atimers (1);
808 #ifdef SIGIO
809 sigunblock (sigmask (SIGIO));
810 #endif
811 }
812
813
814 /* Close device SD if it is open. */
815
816 static void
817 vox_close (sd)
818 struct sound_device *sd;
819 {
820 if (sd->fd >= 0)
821 {
822 /* On GNU/Linux, it seems that the device driver doesn't like to
823 be interrupted by a signal. Block the ones we know to cause
824 troubles. */
825 #ifdef SIGIO
826 sigblock (sigmask (SIGIO));
827 #endif
828 turn_on_atimers (0);
829
830 /* Flush sound data, and reset the device. */
831 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
832
833 turn_on_atimers (1);
834 #ifdef SIGIO
835 sigunblock (sigmask (SIGIO));
836 #endif
837
838 /* Close the device. */
839 emacs_close (sd->fd);
840 sd->fd = -1;
841 }
842 }
843
844
845 /* Choose device-dependent format for device SD from sound file S. */
846
847 static void
848 vox_choose_format (sd, s)
849 struct sound_device *sd;
850 struct sound *s;
851 {
852 if (s->type == RIFF)
853 {
854 struct wav_header *h = (struct wav_header *) s->header;
855 if (h->precision == 8)
856 sd->format = AFMT_U8;
857 else if (h->precision == 16)
858 sd->format = AFMT_S16_LE;
859 else
860 error ("Unsupported WAV file format");
861 }
862 else if (s->type == SUN_AUDIO)
863 {
864 struct au_header *header = (struct au_header *) s->header;
865 switch (header->encoding)
866 {
867 case AU_ENCODING_ULAW_8:
868 case AU_ENCODING_IEEE32:
869 case AU_ENCODING_IEEE64:
870 sd->format = AFMT_MU_LAW;
871 break;
872
873 case AU_ENCODING_8:
874 case AU_ENCODING_16:
875 case AU_ENCODING_24:
876 case AU_ENCODING_32:
877 sd->format = AFMT_S16_LE;
878 break;
879
880 default:
881 error ("Unsupported AU file format");
882 }
883 }
884 else
885 abort ();
886 }
887
888
889 /* Initialize device SD. Set up the interface functions in the device
890 structure. */
891
892 static void
893 vox_init (sd)
894 struct sound_device *sd;
895 {
896 sd->fd = -1;
897 sd->open = vox_open;
898 sd->close = vox_close;
899 sd->configure = vox_configure;
900 sd->choose_format = vox_choose_format;
901 sd->write = vox_write;
902 }
903
904
905 /* Write NBYTES bytes from BUFFER to device SD. */
906
907 static void
908 vox_write (sd, buffer, nbytes)
909 struct sound_device *sd;
910 char *buffer;
911 int nbytes;
912 {
913 int nwritten = emacs_write (sd->fd, buffer, nbytes);
914 if (nwritten < 0)
915 sound_perror ("Writing to sound device");
916 }
917
918
919 \f
920 /***********************************************************************
921 Initialization
922 ***********************************************************************/
923
924 void
925 syms_of_sound ()
926 {
927 QCdevice = intern (":device");
928 staticpro (&QCdevice);
929 QCvolume = intern (":volume");
930 staticpro (&QCvolume);
931 Qsound = intern ("sound");
932 staticpro (&Qsound);
933 Qplay_sound_functions = intern ("play-sound-functions");
934 staticpro (&Qplay_sound_functions);
935
936 defsubr (&Splay_sound);
937 }
938
939
940 void
941 init_sound ()
942 {
943 }
944
945 #endif /* HAVE_SOUND */