*** empty log message ***
[bpt/emacs.git] / src / sound.c
CommitLineData
7840ced1 1/* sound.c -- sound support.
53fab6e2 2 Copyright (C) 1998, 1999, 2001 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
f60ae425
BK
24/*
25 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
26 implementation of the play-sound specification for Windows.
27
28 Notes:
29 In the Windows implementation of play-sound-internal only the
30 :file and :volume keywords are supported. The :device keyword,
31 if present, is ignored. The :data keyword, if present, will
32 cause an error to be generated.
33
34 The Windows implementation of play-sound is implemented via the
35 Win32 API functions mciSendString, waveOutGetVolume, and
06c3eeed 36 waveOutSetVolume which are exported by Winmm.dll.
f60ae425
BK
37*/
38
7840ced1
GM
39#include <config.h>
40
41#if defined HAVE_SOUND
42
f60ae425 43/* BEGIN: Common Includes */
7840ced1
GM
44#include <fcntl.h>
45#include <unistd.h>
46#include <sys/types.h>
7840ced1 47#include <errno.h>
53fab6e2
GM
48#include "lisp.h"
49#include "dispextern.h"
50#include "atimer.h"
bb6e8cee
GM
51#include <signal.h>
52#include "syssignal.h"
f60ae425
BK
53/* END: Common Includes */
54
55
56/* BEGIN: Non Windows Includes */
57#ifndef WINDOWSNT
7840ced1 58
502150e5
PJ
59#ifndef MSDOS
60#include <sys/ioctl.h>
61#endif
62
7840ced1
GM
63/* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
64 sys/soundcard.h. So, let's try whatever's there. */
65
66#ifdef HAVE_MACHINE_SOUNDCARD_H
67#include <machine/soundcard.h>
68#endif
69#ifdef HAVE_SYS_SOUNDCARD_H
70#include <sys/soundcard.h>
71#endif
80fcd514 72#ifdef HAVE_SOUNDCARD_H
80fcd514
KR
73#include <soundcard.h>
74#endif
f60ae425
BK
75/* END: Non Windows Includes */
76
77#else /* WINDOWSNT */
78
79/* BEGIN: Windows Specific Includes */
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <limits.h>
84#include <windows.h>
85#include <mmsystem.h>
86/* END: Windows Specific Includes */
87
88#endif /* WINDOWSNT */
89
90/* BEGIN: Common Definitions */
91#define abs(X) ((X) < 0 ? -(X) : (X))
92
93/* Symbols. */
94
95extern Lisp_Object QCfile, QCdata;
96Lisp_Object QCvolume, QCdevice;
97Lisp_Object Qsound;
98Lisp_Object Qplay_sound_functions;
99
100/* Indices of attributes in a sound attributes vector. */
101
102enum sound_attr
103{
104 SOUND_FILE,
105 SOUND_DATA,
106 SOUND_DEVICE,
107 SOUND_VOLUME,
108 SOUND_ATTR_SENTINEL
109};
110
111static void sound_perror P_ ((char *));
112static void sound_warning P_ ((char *));
113static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
114
115/* END: Common Definitions */
116
117/* BEGIN: Non Windows Definitions */
118#ifndef WINDOWSNT
80fcd514
KR
119
120#ifndef DEFAULT_SOUND_DEVICE
121#define DEFAULT_SOUND_DEVICE "/dev/dsp"
122#endif
7840ced1 123
7840ced1
GM
124
125/* Structure forward declarations. */
126
d1299cde 127struct sound;
7840ced1
GM
128struct sound_device;
129
130/* The file header of RIFF-WAVE files (*.wav). Files are always in
131 little-endian byte-order. */
132
133struct wav_header
134{
135 u_int32_t magic;
136 u_int32_t length;
137 u_int32_t chunk_type;
138 u_int32_t chunk_format;
139 u_int32_t chunk_length;
140 u_int16_t format;
141 u_int16_t channels;
142 u_int32_t sample_rate;
143 u_int32_t bytes_per_second;
144 u_int16_t sample_size;
145 u_int16_t precision;
146 u_int32_t chunk_data;
147 u_int32_t data_length;
148};
149
150/* The file header of Sun adio files (*.au). Files are always in
151 big-endian byte-order. */
152
153struct au_header
154{
155 /* ASCII ".snd" */
156 u_int32_t magic_number;
a4ff5d67 157
7840ced1
GM
158 /* Offset of data part from start of file. Minimum value is 24. */
159 u_int32_t data_offset;
a4ff5d67 160
7840ced1
GM
161 /* Size of data part, 0xffffffff if unknown. */
162 u_int32_t data_size;
163
164 /* Data encoding format.
165 1 8-bit ISDN u-law
166 2 8-bit linear PCM (REF-PCM)
167 3 16-bit linear PCM
168 4 24-bit linear PCM
169 5 32-bit linear PCM
170 6 32-bit IEEE floating-point
171 7 64-bit IEEE floating-point
172 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
173 encoding scheme. */
174 u_int32_t encoding;
175
176 /* Number of samples per second. */
177 u_int32_t sample_rate;
178
179 /* Number of interleaved channels. */
180 u_int32_t channels;
181};
182
183/* Maximum of all sound file headers sizes. */
184
185#define MAX_SOUND_HEADER_BYTES \
186 max (sizeof (struct wav_header), sizeof (struct au_header))
187
188/* Interface structure for sound devices. */
189
190struct sound_device
191{
192 /* The name of the device or null meaning use a default device name. */
193 char *file;
194
195 /* File descriptor of the device. */
196 int fd;
197
198 /* Device-dependent format. */
199 int format;
200
201 /* Volume (0..100). Zero means unspecified. */
202 int volume;
203
204 /* Sample size. */
205 int sample_size;
206
207 /* Sample rate. */
208 int sample_rate;
209
210 /* Bytes per second. */
211 int bps;
212
213 /* 1 = mono, 2 = stereo, 0 = don't set. */
214 int channels;
a4ff5d67 215
7840ced1
GM
216 /* Open device SD. */
217 void (* open) P_ ((struct sound_device *sd));
218
219 /* Close device SD. */
220 void (* close) P_ ((struct sound_device *sd));
221
222 /* Configure SD accoring to device-dependent parameters. */
223 void (* configure) P_ ((struct sound_device *device));
a4ff5d67 224
d1299cde 225 /* Choose a device-dependent format for outputting sound S. */
7840ced1 226 void (* choose_format) P_ ((struct sound_device *sd,
d1299cde 227 struct sound *s));
7840ced1
GM
228
229 /* Write NYBTES bytes from BUFFER to device SD. */
dca0fc1c
KR
230 void (* write) P_ ((struct sound_device *sd, const char *buffer,
231 int nbytes));
7840ced1
GM
232
233 /* A place for devices to store additional data. */
234 void *data;
235};
236
237/* An enumerator for each supported sound file type. */
238
239enum sound_type
240{
241 RIFF,
242 SUN_AUDIO
243};
244
245/* Interface structure for sound files. */
246
d1299cde 247struct sound
7840ced1
GM
248{
249 /* The type of the file. */
250 enum sound_type type;
251
d1299cde 252 /* File descriptor of a sound file. */
7840ced1
GM
253 int fd;
254
d1299cde
GM
255 /* Pointer to sound file header. This contains header_size bytes
256 read from the start of a sound file. */
7840ced1
GM
257 char *header;
258
d1299cde
GM
259 /* Number of bytes raed from sound file. This is always <=
260 MAX_SOUND_HEADER_BYTES. */
261 int header_size;
262
263 /* Sound data, if a string. */
264 Lisp_Object data;
265
266 /* Play sound file S on device SD. */
a4ff5d67 267 void (* play) P_ ((struct sound *s, struct sound_device *sd));
7840ced1
GM
268};
269
14e415e7 270/* These are set during `play-sound-internal' so that sound_cleanup has
7840ced1
GM
271 access to them. */
272
d1299cde
GM
273struct sound_device *current_sound_device;
274struct sound *current_sound;
7840ced1
GM
275
276/* Function prototypes. */
277
278static void vox_open P_ ((struct sound_device *));
279static void vox_configure P_ ((struct sound_device *));
280static void vox_close P_ ((struct sound_device *sd));
d1299cde 281static void vox_choose_format P_ ((struct sound_device *, struct sound *));
7840ced1 282static void vox_init P_ ((struct sound_device *));
dca0fc1c 283static void vox_write P_ ((struct sound_device *, const char *, int));
d1299cde 284static void find_sound_type P_ ((struct sound *));
7840ced1
GM
285static u_int32_t le2hl P_ ((u_int32_t));
286static u_int16_t le2hs P_ ((u_int16_t));
287static u_int32_t be2hl P_ ((u_int32_t));
d1299cde
GM
288static int wav_init P_ ((struct sound *));
289static void wav_play P_ ((struct sound *, struct sound_device *));
290static int au_init P_ ((struct sound *));
291static void au_play P_ ((struct sound *, struct sound_device *));
7840ced1 292
9680b9d0
GM
293#if 0 /* Currently not used. */
294static u_int16_t be2hs P_ ((u_int16_t));
295#endif
296
f60ae425
BK
297/* END: Non Windows Definitions */
298#else /* WINDOWSNT */
299
300/* BEGIN: Windows Specific Definitions */
301static int do_play_sound P_ ((const char *, unsigned long));
302/*
303 END: Windows Specific Definitions */
304#endif /* WINDOWSNT */
7840ced1
GM
305
306\f
307/***********************************************************************
308 General
309 ***********************************************************************/
310
f60ae425
BK
311/* BEGIN: Common functions */
312
7840ced1
GM
313/* Like perror, but signals an error. */
314
315static void
316sound_perror (msg)
317 char *msg;
318{
3297e2a1
AS
319 int saved_errno = errno;
320
62725a92
GM
321 turn_on_atimers (1);
322#ifdef SIGIO
323 sigunblock (sigmask (SIGIO));
324#endif
3297e2a1
AS
325 if (saved_errno != 0)
326 error ("%s: %s", msg, strerror (saved_errno));
62725a92
GM
327 else
328 error ("%s", msg);
329}
330
331
332/* Display a warning message. */
333
334static void
335sound_warning (msg)
336 char *msg;
337{
338 message (msg);
7840ced1
GM
339}
340
341
342/* Parse sound specification SOUND, and fill ATTRS with what is
343 found. Value is non-zero if SOUND Is a valid sound specification.
344 A valid sound specification is a list starting with the symbol
345 `sound'. The rest of the list is a property list which may
346 contain the following key/value pairs:
347
348 - `:file FILE'
349
350 FILE is the sound file to play. If it isn't an absolute name,
351 it's searched under `data-directory'.
352
d1299cde
GM
353 - `:data DATA'
354
355 DATA is a string containing sound data. Either :file or :data
356 may be present, but not both.
357
7840ced1
GM
358 - `:device DEVICE'
359
360 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
361 If not specified, a default device is used.
362
363 - `:volume VOL'
364
2a53558d
GM
365 VOL must be an integer in the range [0, 100], or a float in the
366 range [0, 1]. */
7840ced1
GM
367
368static int
369parse_sound (sound, attrs)
370 Lisp_Object sound;
371 Lisp_Object *attrs;
372{
373 /* SOUND must be a list starting with the symbol `sound'. */
374 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
375 return 0;
376
377 sound = XCDR (sound);
378 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
d1299cde 379 attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
7840ced1
GM
380 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
381 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
382
f60ae425 383#ifndef WINDOWSNT
d1299cde
GM
384 /* File name or data must be specified. */
385 if (!STRINGP (attrs[SOUND_FILE])
386 && !STRINGP (attrs[SOUND_DATA]))
7840ced1 387 return 0;
f60ae425
BK
388#else /* WINDOWSNT */
389 /*
390 Data is not supported in Windows. Therefore a
391 File name MUST be supplied.
392 */
393 if (!STRINGP (attrs[SOUND_FILE]))
394 {
395 return 0;
396 }
397#endif /* WINDOWSNT */
7840ced1
GM
398
399 /* Volume must be in the range 0..100 or unspecified. */
400 if (!NILP (attrs[SOUND_VOLUME]))
401 {
2a53558d
GM
402 if (INTEGERP (attrs[SOUND_VOLUME]))
403 {
404 if (XINT (attrs[SOUND_VOLUME]) < 0
405 || XINT (attrs[SOUND_VOLUME]) > 100)
406 return 0;
407 }
408 else if (FLOATP (attrs[SOUND_VOLUME]))
409 {
410 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
411 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
412 return 0;
413 }
414 else
7840ced1
GM
415 return 0;
416 }
417
f60ae425 418#ifndef WINDOWSNT
7840ced1
GM
419 /* Device must be a string or unspecified. */
420 if (!NILP (attrs[SOUND_DEVICE])
421 && !STRINGP (attrs[SOUND_DEVICE]))
422 return 0;
f60ae425
BK
423#endif /* WINDOWSNT */
424 /*
425 Since device is ignored in Windows, it does not matter
426 what it is.
427 */
7840ced1
GM
428 return 1;
429}
430
f60ae425
BK
431/* END: Common functions */
432
433/* BEGIN: Non Windows functions */
434#ifndef WINDOWSNT
7840ced1
GM
435
436/* Find out the type of the sound file whose file descriptor is FD.
d1299cde 437 S is the sound file structure to fill in. */
7840ced1
GM
438
439static void
d1299cde
GM
440find_sound_type (s)
441 struct sound *s;
7840ced1 442{
d1299cde
GM
443 if (!wav_init (s) && !au_init (s))
444 error ("Unknown sound format");
7840ced1
GM
445}
446
447
14e415e7 448/* Function installed by play-sound-internal with record_unwind_protect. */
7840ced1
GM
449
450static Lisp_Object
451sound_cleanup (arg)
452 Lisp_Object arg;
453{
d1299cde 454 if (current_sound_device)
7840ced1 455 {
66e4690f
KR
456 if (current_sound_device->close)
457 current_sound_device->close (current_sound_device);
d1299cde
GM
458 if (current_sound->fd > 0)
459 emacs_close (current_sound->fd);
7840ced1 460 }
3887b449
GM
461
462 return Qnil;
7840ced1
GM
463}
464
7840ced1
GM
465/***********************************************************************
466 Byte-order Conversion
467 ***********************************************************************/
468
469/* Convert 32-bit value VALUE which is in little-endian byte-order
470 to host byte-order. */
471
472static u_int32_t
473le2hl (value)
474 u_int32_t value;
475{
476#ifdef WORDS_BIG_ENDIAN
477 unsigned char *p = (unsigned char *) &value;
478 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
479#endif
480 return value;
481}
482
483
484/* Convert 16-bit value VALUE which is in little-endian byte-order
485 to host byte-order. */
486
487static u_int16_t
488le2hs (value)
489 u_int16_t value;
490{
491#ifdef WORDS_BIG_ENDIAN
492 unsigned char *p = (unsigned char *) &value;
493 value = p[0] + (p[1] << 8);
494#endif
495 return value;
496}
497
498
499/* Convert 32-bit value VALUE which is in big-endian byte-order
500 to host byte-order. */
501
502static u_int32_t
503be2hl (value)
504 u_int32_t value;
505{
506#ifndef WORDS_BIG_ENDIAN
507 unsigned char *p = (unsigned char *) &value;
508 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
509#endif
510 return value;
511}
512
513
9680b9d0
GM
514#if 0 /* Currently not used. */
515
7840ced1
GM
516/* Convert 16-bit value VALUE which is in big-endian byte-order
517 to host byte-order. */
518
519static u_int16_t
520be2hs (value)
521 u_int16_t value;
522{
523#ifndef WORDS_BIG_ENDIAN
524 unsigned char *p = (unsigned char *) &value;
525 value = p[1] + (p[0] << 8);
526#endif
527 return value;
528}
529
9680b9d0 530#endif /* 0 */
7840ced1 531
7840ced1
GM
532/***********************************************************************
533 RIFF-WAVE (*.wav)
534 ***********************************************************************/
535
d1299cde 536/* Try to initialize sound file S from S->header. S->header
7840ced1
GM
537 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
538 sound file. If the file is a WAV-format file, set up interface
d1299cde 539 functions in S and convert header fields to host byte-order.
7840ced1
GM
540 Value is non-zero if the file is a WAV file. */
541
542static int
d1299cde
GM
543wav_init (s)
544 struct sound *s;
7840ced1 545{
d1299cde
GM
546 struct wav_header *header = (struct wav_header *) s->header;
547
548 if (s->header_size < sizeof *header
549 || bcmp (s->header, "RIFF", 4) != 0)
7840ced1
GM
550 return 0;
551
552 /* WAV files are in little-endian order. Convert the header
553 if on a big-endian machine. */
554 header->magic = le2hl (header->magic);
555 header->length = le2hl (header->length);
556 header->chunk_type = le2hl (header->chunk_type);
557 header->chunk_format = le2hl (header->chunk_format);
558 header->chunk_length = le2hl (header->chunk_length);
559 header->format = le2hs (header->format);
560 header->channels = le2hs (header->channels);
561 header->sample_rate = le2hl (header->sample_rate);
562 header->bytes_per_second = le2hl (header->bytes_per_second);
563 header->sample_size = le2hs (header->sample_size);
564 header->precision = le2hs (header->precision);
565 header->chunk_data = le2hl (header->chunk_data);
566 header->data_length = le2hl (header->data_length);
567
568 /* Set up the interface functions for WAV. */
d1299cde
GM
569 s->type = RIFF;
570 s->play = wav_play;
7840ced1
GM
571
572 return 1;
a4ff5d67 573}
7840ced1
GM
574
575
d1299cde 576/* Play RIFF-WAVE audio file S on sound device SD. */
7840ced1
GM
577
578static void
d1299cde
GM
579wav_play (s, sd)
580 struct sound *s;
7840ced1
GM
581 struct sound_device *sd;
582{
d1299cde 583 struct wav_header *header = (struct wav_header *) s->header;
7840ced1
GM
584
585 /* Let the device choose a suitable device-dependent format
586 for the file. */
d1299cde 587 sd->choose_format (sd, s);
a4ff5d67 588
7840ced1
GM
589 /* Configure the device. */
590 sd->sample_size = header->sample_size;
591 sd->sample_rate = header->sample_rate;
592 sd->bps = header->bytes_per_second;
593 sd->channels = header->channels;
594 sd->configure (sd);
595
596 /* Copy sound data to the device. The WAV file specification is
597 actually more complex. This simple scheme worked with all WAV
598 files I found so far. If someone feels inclined to implement the
599 whole RIFF-WAVE spec, please do. */
d1299cde 600 if (STRINGP (s->data))
d5db4077
KR
601 sd->write (sd, SDATA (s->data) + sizeof *header,
602 SBYTES (s->data) - sizeof *header);
d1299cde
GM
603 else
604 {
605 char *buffer;
606 int nbytes;
607 int blksize = 2048;
a4ff5d67 608
d1299cde
GM
609 buffer = (char *) alloca (blksize);
610 lseek (s->fd, sizeof *header, SEEK_SET);
a4ff5d67 611
d1299cde
GM
612 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
613 sd->write (sd, buffer, nbytes);
7840ced1 614
d1299cde 615 if (nbytes < 0)
62725a92 616 sound_perror ("Error reading sound file");
d1299cde 617 }
7840ced1
GM
618}
619
620
7840ced1
GM
621/***********************************************************************
622 Sun Audio (*.au)
623 ***********************************************************************/
624
a4ff5d67 625/* Sun audio file encodings. */
7840ced1
GM
626
627enum au_encoding
628{
629 AU_ENCODING_ULAW_8 = 1,
630 AU_ENCODING_8,
631 AU_ENCODING_16,
632 AU_ENCODING_24,
633 AU_ENCODING_32,
634 AU_ENCODING_IEEE32,
635 AU_ENCODING_IEEE64,
636 AU_COMPRESSED = 23
637};
638
639
d1299cde 640/* Try to initialize sound file S from S->header. S->header
7840ced1
GM
641 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
642 sound file. If the file is a AU-format file, set up interface
d1299cde 643 functions in S and convert header fields to host byte-order.
7840ced1
GM
644 Value is non-zero if the file is an AU file. */
645
646static int
d1299cde
GM
647au_init (s)
648 struct sound *s;
7840ced1 649{
d1299cde 650 struct au_header *header = (struct au_header *) s->header;
a4ff5d67 651
d1299cde
GM
652 if (s->header_size < sizeof *header
653 || bcmp (s->header, ".snd", 4) != 0)
7840ced1 654 return 0;
a4ff5d67 655
7840ced1
GM
656 header->magic_number = be2hl (header->magic_number);
657 header->data_offset = be2hl (header->data_offset);
658 header->data_size = be2hl (header->data_size);
659 header->encoding = be2hl (header->encoding);
660 header->sample_rate = be2hl (header->sample_rate);
661 header->channels = be2hl (header->channels);
a4ff5d67 662
7840ced1 663 /* Set up the interface functions for AU. */
d1299cde
GM
664 s->type = SUN_AUDIO;
665 s->play = au_play;
7840ced1
GM
666
667 return 1;
668}
669
670
d1299cde 671/* Play Sun audio file S on sound device SD. */
7840ced1
GM
672
673static void
d1299cde
GM
674au_play (s, sd)
675 struct sound *s;
7840ced1
GM
676 struct sound_device *sd;
677{
d1299cde 678 struct au_header *header = (struct au_header *) s->header;
7840ced1
GM
679
680 sd->sample_size = 0;
681 sd->sample_rate = header->sample_rate;
682 sd->bps = 0;
683 sd->channels = header->channels;
d1299cde 684 sd->choose_format (sd, s);
7840ced1 685 sd->configure (sd);
d1299cde
GM
686
687 if (STRINGP (s->data))
d5db4077
KR
688 sd->write (sd, SDATA (s->data) + header->data_offset,
689 SBYTES (s->data) - header->data_offset);
d1299cde
GM
690 else
691 {
692 int blksize = 2048;
693 char *buffer;
694 int nbytes;
a4ff5d67 695
d1299cde
GM
696 /* Seek */
697 lseek (s->fd, header->data_offset, SEEK_SET);
a4ff5d67 698
d1299cde
GM
699 /* Copy sound data to the device. */
700 buffer = (char *) alloca (blksize);
701 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
702 sd->write (sd, buffer, nbytes);
a4ff5d67 703
d1299cde 704 if (nbytes < 0)
62725a92 705 sound_perror ("Error reading sound file");
d1299cde 706 }
7840ced1
GM
707}
708
709
7840ced1
GM
710/***********************************************************************
711 Voxware Driver Interface
712 ***********************************************************************/
713
714/* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
715 has a compatible own driver aka Luigi's driver. */
716
717
718/* Open device SD. If SD->file is non-null, open that device,
719 otherwise use a default device name. */
720
721static void
722vox_open (sd)
723 struct sound_device *sd;
724{
725 char *file;
a4ff5d67 726
7840ced1
GM
727 /* Open the sound device. Default is /dev/dsp. */
728 if (sd->file)
729 file = sd->file;
730 else
80fcd514 731 file = DEFAULT_SOUND_DEVICE;
a4ff5d67 732
68c45bf0 733 sd->fd = emacs_open (file, O_WRONLY, 0);
7840ced1
GM
734 if (sd->fd < 0)
735 sound_perror (file);
736}
737
738
739/* Configure device SD from parameters in it. */
740
741static void
742vox_configure (sd)
743 struct sound_device *sd;
744{
28fcb7dc 745 int val;
a4ff5d67 746
7840ced1
GM
747 xassert (sd->fd >= 0);
748
bb6e8cee
GM
749 /* On GNU/Linux, it seems that the device driver doesn't like to be
750 interrupted by a signal. Block the ones we know to cause
751 troubles. */
b5cb1ada 752 turn_on_atimers (0);
bb6e8cee
GM
753#ifdef SIGIO
754 sigblock (sigmask (SIGIO));
755#endif
b5cb1ada 756
28fcb7dc
GM
757 val = sd->format;
758 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
759 || val != sd->format)
62725a92 760 sound_perror ("Could not set sound format");
7840ced1 761
28fcb7dc
GM
762 val = sd->channels != 1;
763 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
764 || val != (sd->channels != 1))
62725a92 765 sound_perror ("Could not set stereo/mono");
7840ced1 766
28fcb7dc
GM
767 /* I think bps and sampling_rate are the same, but who knows.
768 Check this. and use SND_DSP_SPEED for both. */
769 if (sd->sample_rate > 0)
770 {
771 val = sd->sample_rate;
62725a92
GM
772 if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0)
773 sound_perror ("Could not set sound speed");
774 else if (val != sd->sample_rate)
775 sound_warning ("Could not set sample rate");
28fcb7dc 776 }
7840ced1 777
3887b449
GM
778 if (sd->volume > 0)
779 {
780 int volume = sd->volume & 0xff;
781 volume |= volume << 8;
28fcb7dc
GM
782 /* This may fail if there is no mixer. Ignore the failure. */
783 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
3887b449 784 }
a4ff5d67 785
b5cb1ada 786 turn_on_atimers (1);
bb6e8cee
GM
787#ifdef SIGIO
788 sigunblock (sigmask (SIGIO));
789#endif
7840ced1
GM
790}
791
792
793/* Close device SD if it is open. */
794
795static void
796vox_close (sd)
797 struct sound_device *sd;
798{
799 if (sd->fd >= 0)
800 {
bb6e8cee
GM
801 /* On GNU/Linux, it seems that the device driver doesn't like to
802 be interrupted by a signal. Block the ones we know to cause
803 troubles. */
804#ifdef SIGIO
805 sigblock (sigmask (SIGIO));
806#endif
b5cb1ada 807 turn_on_atimers (0);
a4ff5d67 808
bb6e8cee 809 /* Flush sound data, and reset the device. */
7840ced1 810 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
a4ff5d67 811
b5cb1ada 812 turn_on_atimers (1);
bb6e8cee
GM
813#ifdef SIGIO
814 sigunblock (sigmask (SIGIO));
815#endif
7840ced1
GM
816
817 /* Close the device. */
68c45bf0 818 emacs_close (sd->fd);
7840ced1
GM
819 sd->fd = -1;
820 }
821}
822
823
d1299cde 824/* Choose device-dependent format for device SD from sound file S. */
7840ced1
GM
825
826static void
d1299cde 827vox_choose_format (sd, s)
7840ced1 828 struct sound_device *sd;
d1299cde 829 struct sound *s;
7840ced1 830{
d1299cde 831 if (s->type == RIFF)
7840ced1 832 {
d1299cde 833 struct wav_header *h = (struct wav_header *) s->header;
7840ced1
GM
834 if (h->precision == 8)
835 sd->format = AFMT_U8;
836 else if (h->precision == 16)
837 sd->format = AFMT_S16_LE;
838 else
839 error ("Unsupported WAV file format");
840 }
d1299cde 841 else if (s->type == SUN_AUDIO)
7840ced1 842 {
d1299cde 843 struct au_header *header = (struct au_header *) s->header;
7840ced1
GM
844 switch (header->encoding)
845 {
846 case AU_ENCODING_ULAW_8:
847 case AU_ENCODING_IEEE32:
848 case AU_ENCODING_IEEE64:
849 sd->format = AFMT_MU_LAW;
850 break;
a4ff5d67 851
7840ced1
GM
852 case AU_ENCODING_8:
853 case AU_ENCODING_16:
854 case AU_ENCODING_24:
855 case AU_ENCODING_32:
856 sd->format = AFMT_S16_LE;
857 break;
858
859 default:
860 error ("Unsupported AU file format");
861 }
862 }
863 else
864 abort ();
865}
866
867
868/* Initialize device SD. Set up the interface functions in the device
869 structure. */
870
871static void
872vox_init (sd)
873 struct sound_device *sd;
874{
875 sd->fd = -1;
876 sd->open = vox_open;
877 sd->close = vox_close;
878 sd->configure = vox_configure;
879 sd->choose_format = vox_choose_format;
880 sd->write = vox_write;
881}
882
7840ced1
GM
883/* Write NBYTES bytes from BUFFER to device SD. */
884
885static void
886vox_write (sd, buffer, nbytes)
887 struct sound_device *sd;
dca0fc1c 888 const char *buffer;
7840ced1
GM
889 int nbytes;
890{
68c45bf0 891 int nwritten = emacs_write (sd->fd, buffer, nbytes);
7840ced1 892 if (nwritten < 0)
62725a92 893 sound_perror ("Error writing to sound device");
7840ced1
GM
894}
895
f60ae425
BK
896/* END: Non Windows functions */
897#else /* WINDOWSNT */
898
899/* BEGIN: Windows specific functions */
900
901static int
902do_play_sound (psz_file, ui_volume)
06c3eeed
JB
903 const char *psz_file;
904 unsigned long ui_volume;
f60ae425 905{
06c3eeed
JB
906 int i_result = 0;
907 MCIERROR mci_error = 0;
908 char sz_cmd_buf[520] = {0};
909 char sz_ret_buf[520] = {0};
910 MMRESULT mm_result = MMSYSERR_NOERROR;
911 unsigned long ui_volume_org = 0;
912 BOOL b_reset_volume = FALSE;
913
f60ae425
BK
914 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
915 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
06c3eeed
JB
916 sprintf (sz_cmd_buf,
917 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
918 psz_file);
919 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
f60ae425
BK
920 if (mci_error != 0)
921 {
06c3eeed
JB
922 sound_warning ("The open mciSendString command failed to open\n"
923 "the specified sound file");
924 i_result = (int) mci_error;
f60ae425
BK
925 return i_result;
926 }
927 if ((ui_volume > 0) && (ui_volume != UINT_MAX))
928 {
06c3eeed 929 mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
f60ae425
BK
930 if (mm_result == MMSYSERR_NOERROR)
931 {
06c3eeed
JB
932 b_reset_volume = TRUE;
933 mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
f60ae425
BK
934 if ( mm_result != MMSYSERR_NOERROR)
935 {
06c3eeed
JB
936 sound_warning ("waveOutSetVolume failed to set the volume level\n"
937 "of the WAVE_MAPPER device.\n"
938 "As a result, the user selected volume level will\n"
939 "not be used.");
f60ae425
BK
940 }
941 }
942 else
943 {
06c3eeed
JB
944 sound_warning ("waveOutGetVolume failed to obtain the original\n"
945 "volume level of the WAVE_MAPPER device.\n"
946 "As a result, the user selected volume level will\n"
947 "not be used.");
f60ae425
BK
948 }
949 }
950 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
951 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
952 strcpy (sz_cmd_buf, "play GNUEmacs_PlaySound_Device wait");
06c3eeed 953 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
f60ae425
BK
954 if (mci_error != 0)
955 {
06c3eeed
JB
956 sound_warning ("The play mciSendString command failed to play the\n"
957 "opened sound file.");
958 i_result = (int) mci_error;
f60ae425
BK
959 }
960 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
961 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
962 strcpy (sz_cmd_buf, "close GNUEmacs_PlaySound_Device wait");
06c3eeed 963 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
f60ae425
BK
964 if (b_reset_volume == TRUE)
965 {
06c3eeed 966 mm_result=waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
f60ae425
BK
967 if (mm_result != MMSYSERR_NOERROR)
968 {
06c3eeed
JB
969 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
970 "level of the WAVE_MAPPER device.");
f60ae425
BK
971 }
972 }
973 return i_result;
974}
975
976/* END: Windows specific functions */
977
978#endif /* WINDOWSNT */
979
f60ae425
BK
980DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
981 doc: /* Play sound SOUND.
982
983Internal use only, use `play-sound' instead.\n */)
984 (sound)
985 Lisp_Object sound;
986{
987 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
988 int count = SPECPDL_INDEX ();
989
990#ifndef WINDOWSNT
991 Lisp_Object file;
992 struct gcpro gcpro1, gcpro2;
993 struct sound_device sd;
994 struct sound s;
995 Lisp_Object args[2];
996#else /* WINDOWSNT */
06c3eeed
JB
997 int len = 0;
998 Lisp_Object lo_file = {0};
999 char * psz_file = NULL;
1000 unsigned long ui_volume_tmp = UINT_MAX;
1001 unsigned long ui_volume = UINT_MAX;
1002 int i_result = 0;
f60ae425
BK
1003#endif /* WINDOWSNT */
1004
1005 /* Parse the sound specification. Give up if it is invalid. */
1006 if (!parse_sound (sound, attrs))
1007 error ("Invalid sound specification");
1008
1009#ifndef WINDOWSNT
1010 file = Qnil;
1011 GCPRO2 (sound, file);
1012 bzero (&sd, sizeof sd);
1013 bzero (&s, sizeof s);
1014 current_sound_device = &sd;
1015 current_sound = &s;
1016 record_unwind_protect (sound_cleanup, Qnil);
1017 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
1018
1019 if (STRINGP (attrs[SOUND_FILE]))
1020 {
1021 /* Open the sound file. */
1022 s.fd = openp (Fcons (Vdata_directory, Qnil),
1023 attrs[SOUND_FILE], Qnil, &file, Qnil);
1024 if (s.fd < 0)
1025 sound_perror ("Could not open sound file");
1026
1027 /* Read the first bytes from the file. */
1028 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
1029 if (s.header_size < 0)
1030 sound_perror ("Invalid sound file header");
1031 }
1032 else
1033 {
1034 s.data = attrs[SOUND_DATA];
1035 s.header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (s.data));
1036 bcopy (SDATA (s.data), s.header, s.header_size);
1037 }
1038
1039 /* Find out the type of sound. Give up if we can't tell. */
1040 find_sound_type (&s);
1041
1042 /* Set up a device. */
1043 if (STRINGP (attrs[SOUND_DEVICE]))
1044 {
1045 int len = SCHARS (attrs[SOUND_DEVICE]);
1046 sd.file = (char *) alloca (len + 1);
1047 strcpy (sd.file, SDATA (attrs[SOUND_DEVICE]));
1048 }
1049
1050 if (INTEGERP (attrs[SOUND_VOLUME]))
1051 sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
1052 else if (FLOATP (attrs[SOUND_VOLUME]))
1053 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
7840ced1 1054
f60ae425
BK
1055 args[0] = Qplay_sound_functions;
1056 args[1] = sound;
1057 Frun_hook_with_args (2, args);
1058
1059 /* There is only one type of device we currently support, the VOX
1060 sound driver. Set up the device interface functions for that
1061 device. */
1062 vox_init (&sd);
1063
1064 /* Open the device. */
1065 sd.open (&sd);
1066
1067 /* Play the sound. */
1068 s.play (&s, &sd);
1069
1070 /* Close the input file, if any. */
1071 if (!STRINGP (s.data))
1072 {
1073 emacs_close (s.fd);
1074 s.fd = -1;
1075 }
1076
1077 /* Close the device. */
1078 sd.close (&sd);
1079
1080 /* Clean up. */
1081 current_sound_device = NULL;
1082 current_sound = NULL;
1083 UNGCPRO;
06c3eeed 1084
f60ae425 1085#else /* WINDOWSNT */
06c3eeed
JB
1086
1087 lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil);
1088 len = XSTRING (lo_file)->size;
1089 psz_file = (char *) alloca (len + 1);
f60ae425
BK
1090 strcpy (psz_file, XSTRING (lo_file)->data);
1091 if (INTEGERP (attrs[SOUND_VOLUME]))
1092 {
06c3eeed 1093 ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
f60ae425
BK
1094 }
1095 else if (FLOATP (attrs[SOUND_VOLUME]))
1096 {
06c3eeed 1097 ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
f60ae425
BK
1098 }
1099 /*
1100 Based on some experiments I have conducted, a value of 100 or less
1101 for the sound volume is much too low. You cannot even hear it.
1102 A value of UINT_MAX indicates that you wish for the sound to played
1103 at the maximum possible volume. A value of UINT_MAX/2 plays the
1104 sound at 50% maximum volume. Therefore the value passed to do_play_sound
06c3eeed 1105 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
f60ae425 1106 The following code adjusts the user specified volume level appropriately.
06c3eeed 1107 */
f60ae425
BK
1108 if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
1109 {
06c3eeed 1110 ui_volume = ui_volume_tmp * (UINT_MAX / 100);
f60ae425 1111 }
06c3eeed
JB
1112 i_result = do_play_sound (psz_file, ui_volume);
1113
f60ae425 1114#endif /* WINDOWSNT */
06c3eeed 1115
f60ae425
BK
1116 unbind_to (count, Qnil);
1117 return Qnil;
1118}
7840ced1
GM
1119\f
1120/***********************************************************************
1121 Initialization
1122 ***********************************************************************/
1123
1124void
1125syms_of_sound ()
1126{
1127 QCdevice = intern (":device");
1128 staticpro (&QCdevice);
1129 QCvolume = intern (":volume");
1130 staticpro (&QCvolume);
1131 Qsound = intern ("sound");
1132 staticpro (&Qsound);
2a53558d
GM
1133 Qplay_sound_functions = intern ("play-sound-functions");
1134 staticpro (&Qplay_sound_functions);
7840ced1 1135
14e415e7 1136 defsubr (&Splay_sound_internal);
7840ced1
GM
1137}
1138
1139
1140void
1141init_sound ()
1142{
1143}
1144
1145#endif /* HAVE_SOUND */