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