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