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