]> code.delx.au - gnu-emacs/blob - src/sound.c
(Version, mh-version): Update for release 8.0.
[gnu-emacs] / src / sound.c
1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
23 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
24
25 /*
26 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
27 implementation of the play-sound specification for Windows.
28
29 Notes:
30 In the Windows implementation of play-sound-internal only the
31 :file and :volume keywords are supported. The :device keyword,
32 if present, is ignored. The :data keyword, if present, will
33 cause an error to be generated.
34
35 The Windows implementation of play-sound is implemented via the
36 Win32 API functions mciSendString, waveOutGetVolume, and
37 waveOutSetVolume which are exported by Winmm.dll.
38 */
39
40 #include <config.h>
41
42 #if defined HAVE_SOUND
43
44 /* BEGIN: Common Includes */
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <errno.h>
49 #include "lisp.h"
50 #include "dispextern.h"
51 #include "atimer.h"
52 #include <signal.h>
53 #include "syssignal.h"
54 /* END: Common Includes */
55
56
57 /* BEGIN: Non Windows Includes */
58 #ifndef WINDOWSNT
59
60 #ifndef MSDOS
61 #include <sys/ioctl.h>
62 #endif
63
64 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
65 sys/soundcard.h. So, let's try whatever's there. */
66
67 #ifdef HAVE_MACHINE_SOUNDCARD_H
68 #include <machine/soundcard.h>
69 #endif
70 #ifdef HAVE_SYS_SOUNDCARD_H
71 #include <sys/soundcard.h>
72 #endif
73 #ifdef HAVE_SOUNDCARD_H
74 #include <soundcard.h>
75 #endif
76 /* END: Non Windows Includes */
77
78 #else /* WINDOWSNT */
79
80 /* BEGIN: Windows Specific Includes */
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <limits.h>
85 #include <windows.h>
86 #include <mmsystem.h>
87 /* END: Windows Specific Includes */
88
89 #endif /* WINDOWSNT */
90
91 /* BEGIN: Common Definitions */
92 #define abs(X) ((X) < 0 ? -(X) : (X))
93
94 /* Symbols. */
95
96 extern Lisp_Object QCfile, QCdata;
97 Lisp_Object QCvolume, QCdevice;
98 Lisp_Object Qsound;
99 Lisp_Object Qplay_sound_functions;
100
101 /* Indices of attributes in a sound attributes vector. */
102
103 enum sound_attr
104 {
105 SOUND_FILE,
106 SOUND_DATA,
107 SOUND_DEVICE,
108 SOUND_VOLUME,
109 SOUND_ATTR_SENTINEL
110 };
111
112 static void sound_perror P_ ((char *));
113 static void sound_warning P_ ((char *));
114 static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
115
116 /* END: Common Definitions */
117
118 /* BEGIN: Non Windows Definitions */
119 #ifndef WINDOWSNT
120
121 #ifndef DEFAULT_SOUND_DEVICE
122 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
123 #endif
124
125
126 /* Structure forward declarations. */
127
128 struct sound;
129 struct sound_device;
130
131 /* The file header of RIFF-WAVE files (*.wav). Files are always in
132 little-endian byte-order. */
133
134 struct wav_header
135 {
136 u_int32_t magic;
137 u_int32_t length;
138 u_int32_t chunk_type;
139 u_int32_t chunk_format;
140 u_int32_t chunk_length;
141 u_int16_t format;
142 u_int16_t channels;
143 u_int32_t sample_rate;
144 u_int32_t bytes_per_second;
145 u_int16_t sample_size;
146 u_int16_t precision;
147 u_int32_t chunk_data;
148 u_int32_t data_length;
149 };
150
151 /* The file header of Sun adio files (*.au). Files are always in
152 big-endian byte-order. */
153
154 struct au_header
155 {
156 /* ASCII ".snd" */
157 u_int32_t magic_number;
158
159 /* Offset of data part from start of file. Minimum value is 24. */
160 u_int32_t data_offset;
161
162 /* Size of data part, 0xffffffff if unknown. */
163 u_int32_t data_size;
164
165 /* Data encoding format.
166 1 8-bit ISDN u-law
167 2 8-bit linear PCM (REF-PCM)
168 3 16-bit linear PCM
169 4 24-bit linear PCM
170 5 32-bit linear PCM
171 6 32-bit IEEE floating-point
172 7 64-bit IEEE floating-point
173 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
174 encoding scheme. */
175 u_int32_t encoding;
176
177 /* Number of samples per second. */
178 u_int32_t sample_rate;
179
180 /* Number of interleaved channels. */
181 u_int32_t channels;
182 };
183
184 /* Maximum of all sound file headers sizes. */
185
186 #define MAX_SOUND_HEADER_BYTES \
187 max (sizeof (struct wav_header), sizeof (struct au_header))
188
189 /* Interface structure for sound devices. */
190
191 struct sound_device
192 {
193 /* The name of the device or null meaning use a default device name. */
194 char *file;
195
196 /* File descriptor of the device. */
197 int fd;
198
199 /* Device-dependent format. */
200 int format;
201
202 /* Volume (0..100). Zero means unspecified. */
203 int volume;
204
205 /* Sample size. */
206 int sample_size;
207
208 /* Sample rate. */
209 int sample_rate;
210
211 /* Bytes per second. */
212 int bps;
213
214 /* 1 = mono, 2 = stereo, 0 = don't set. */
215 int channels;
216
217 /* Open device SD. */
218 void (* open) P_ ((struct sound_device *sd));
219
220 /* Close device SD. */
221 void (* close) P_ ((struct sound_device *sd));
222
223 /* Configure SD accoring to device-dependent parameters. */
224 void (* configure) P_ ((struct sound_device *device));
225
226 /* Choose a device-dependent format for outputting sound S. */
227 void (* choose_format) P_ ((struct sound_device *sd,
228 struct sound *s));
229
230 /* Write NYBTES bytes from BUFFER to device SD. */
231 void (* write) P_ ((struct sound_device *sd, const char *buffer,
232 int nbytes));
233
234 /* A place for devices to store additional data. */
235 void *data;
236 };
237
238 /* An enumerator for each supported sound file type. */
239
240 enum sound_type
241 {
242 RIFF,
243 SUN_AUDIO
244 };
245
246 /* Interface structure for sound files. */
247
248 struct sound
249 {
250 /* The type of the file. */
251 enum sound_type type;
252
253 /* File descriptor of a sound file. */
254 int fd;
255
256 /* Pointer to sound file header. This contains header_size bytes
257 read from the start of a sound file. */
258 char *header;
259
260 /* Number of bytes raed from sound file. This is always <=
261 MAX_SOUND_HEADER_BYTES. */
262 int header_size;
263
264 /* Sound data, if a string. */
265 Lisp_Object data;
266
267 /* Play sound file S on device SD. */
268 void (* play) P_ ((struct sound *s, struct sound_device *sd));
269 };
270
271 /* These are set during `play-sound-internal' so that sound_cleanup has
272 access to them. */
273
274 struct sound_device *current_sound_device;
275 struct sound *current_sound;
276
277 /* Function prototypes. */
278
279 static void vox_open P_ ((struct sound_device *));
280 static void vox_configure P_ ((struct sound_device *));
281 static void vox_close P_ ((struct sound_device *sd));
282 static void vox_choose_format P_ ((struct sound_device *, struct sound *));
283 static void vox_init P_ ((struct sound_device *));
284 static void vox_write P_ ((struct sound_device *, const char *, int));
285 static void find_sound_type P_ ((struct sound *));
286 static u_int32_t le2hl P_ ((u_int32_t));
287 static u_int16_t le2hs P_ ((u_int16_t));
288 static u_int32_t be2hl P_ ((u_int32_t));
289 static int wav_init P_ ((struct sound *));
290 static void wav_play P_ ((struct sound *, struct sound_device *));
291 static int au_init P_ ((struct sound *));
292 static void au_play P_ ((struct sound *, struct sound_device *));
293
294 #if 0 /* Currently not used. */
295 static u_int16_t be2hs P_ ((u_int16_t));
296 #endif
297
298 /* END: Non Windows Definitions */
299 #else /* WINDOWSNT */
300
301 /* BEGIN: Windows Specific Definitions */
302 static int do_play_sound P_ ((const char *, unsigned long));
303 /*
304 END: Windows Specific Definitions */
305 #endif /* WINDOWSNT */
306
307 \f
308 /***********************************************************************
309 General
310 ***********************************************************************/
311
312 /* BEGIN: Common functions */
313
314 /* Like perror, but signals an error. */
315
316 static void
317 sound_perror (msg)
318 char *msg;
319 {
320 int saved_errno = errno;
321
322 turn_on_atimers (1);
323 #ifdef SIGIO
324 sigunblock (sigmask (SIGIO));
325 #endif
326 if (saved_errno != 0)
327 error ("%s: %s", msg, strerror (saved_errno));
328 else
329 error ("%s", msg);
330 }
331
332
333 /* Display a warning message. */
334
335 static void
336 sound_warning (msg)
337 char *msg;
338 {
339 message (msg);
340 }
341
342
343 /* Parse sound specification SOUND, and fill ATTRS with what is
344 found. Value is non-zero if SOUND Is a valid sound specification.
345 A valid sound specification is a list starting with the symbol
346 `sound'. The rest of the list is a property list which may
347 contain the following key/value pairs:
348
349 - `:file FILE'
350
351 FILE is the sound file to play. If it isn't an absolute name,
352 it's searched under `data-directory'.
353
354 - `:data DATA'
355
356 DATA is a string containing sound data. Either :file or :data
357 may be present, but not both.
358
359 - `:device DEVICE'
360
361 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
362 If not specified, a default device is used.
363
364 - `:volume VOL'
365
366 VOL must be an integer in the range [0, 100], or a float in the
367 range [0, 1]. */
368
369 static int
370 parse_sound (sound, attrs)
371 Lisp_Object sound;
372 Lisp_Object *attrs;
373 {
374 /* SOUND must be a list starting with the symbol `sound'. */
375 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
376 return 0;
377
378 sound = XCDR (sound);
379 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
380 attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
381 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
382 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
383
384 #ifndef WINDOWSNT
385 /* File name or data must be specified. */
386 if (!STRINGP (attrs[SOUND_FILE])
387 && !STRINGP (attrs[SOUND_DATA]))
388 return 0;
389 #else /* WINDOWSNT */
390 /*
391 Data is not supported in Windows. Therefore a
392 File name MUST be supplied.
393 */
394 if (!STRINGP (attrs[SOUND_FILE]))
395 {
396 return 0;
397 }
398 #endif /* WINDOWSNT */
399
400 /* Volume must be in the range 0..100 or unspecified. */
401 if (!NILP (attrs[SOUND_VOLUME]))
402 {
403 if (INTEGERP (attrs[SOUND_VOLUME]))
404 {
405 if (XINT (attrs[SOUND_VOLUME]) < 0
406 || XINT (attrs[SOUND_VOLUME]) > 100)
407 return 0;
408 }
409 else if (FLOATP (attrs[SOUND_VOLUME]))
410 {
411 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
412 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
413 return 0;
414 }
415 else
416 return 0;
417 }
418
419 #ifndef WINDOWSNT
420 /* Device must be a string or unspecified. */
421 if (!NILP (attrs[SOUND_DEVICE])
422 && !STRINGP (attrs[SOUND_DEVICE]))
423 return 0;
424 #endif /* WINDOWSNT */
425 /*
426 Since device is ignored in Windows, it does not matter
427 what it is.
428 */
429 return 1;
430 }
431
432 /* END: Common functions */
433
434 /* BEGIN: Non Windows functions */
435 #ifndef WINDOWSNT
436
437 /* Find out the type of the sound file whose file descriptor is FD.
438 S is the sound file structure to fill in. */
439
440 static void
441 find_sound_type (s)
442 struct sound *s;
443 {
444 if (!wav_init (s) && !au_init (s))
445 error ("Unknown sound format");
446 }
447
448
449 /* Function installed by play-sound-internal with record_unwind_protect. */
450
451 static Lisp_Object
452 sound_cleanup (arg)
453 Lisp_Object arg;
454 {
455 if (current_sound_device->close)
456 current_sound_device->close (current_sound_device);
457 if (current_sound->fd > 0)
458 emacs_close (current_sound->fd);
459 free (current_sound_device);
460 free (current_sound);
461
462 return Qnil;
463 }
464
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
472 static u_int32_t
473 le2hl (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
487 static u_int16_t
488 le2hs (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
502 static u_int32_t
503 be2hl (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
514 #if 0 /* Currently not used. */
515
516 /* Convert 16-bit value VALUE which is in big-endian byte-order
517 to host byte-order. */
518
519 static u_int16_t
520 be2hs (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
530 #endif /* 0 */
531
532 /***********************************************************************
533 RIFF-WAVE (*.wav)
534 ***********************************************************************/
535
536 /* Try to initialize sound file S from S->header. S->header
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
539 functions in S and convert header fields to host byte-order.
540 Value is non-zero if the file is a WAV file. */
541
542 static int
543 wav_init (s)
544 struct sound *s;
545 {
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)
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. */
569 s->type = RIFF;
570 s->play = wav_play;
571
572 return 1;
573 }
574
575
576 /* Play RIFF-WAVE audio file S on sound device SD. */
577
578 static void
579 wav_play (s, sd)
580 struct sound *s;
581 struct sound_device *sd;
582 {
583 struct wav_header *header = (struct wav_header *) s->header;
584
585 /* Let the device choose a suitable device-dependent format
586 for the file. */
587 sd->choose_format (sd, s);
588
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. */
600 if (STRINGP (s->data))
601 sd->write (sd, SDATA (s->data) + sizeof *header,
602 SBYTES (s->data) - sizeof *header);
603 else
604 {
605 char *buffer;
606 int nbytes;
607 int blksize = 2048;
608
609 buffer = (char *) alloca (blksize);
610 lseek (s->fd, sizeof *header, SEEK_SET);
611
612 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
613 sd->write (sd, buffer, nbytes);
614
615 if (nbytes < 0)
616 sound_perror ("Error reading sound file");
617 }
618 }
619
620
621 /***********************************************************************
622 Sun Audio (*.au)
623 ***********************************************************************/
624
625 /* Sun audio file encodings. */
626
627 enum 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
640 /* Try to initialize sound file S from S->header. S->header
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
643 functions in S and convert header fields to host byte-order.
644 Value is non-zero if the file is an AU file. */
645
646 static int
647 au_init (s)
648 struct sound *s;
649 {
650 struct au_header *header = (struct au_header *) s->header;
651
652 if (s->header_size < sizeof *header
653 || bcmp (s->header, ".snd", 4) != 0)
654 return 0;
655
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);
662
663 /* Set up the interface functions for AU. */
664 s->type = SUN_AUDIO;
665 s->play = au_play;
666
667 return 1;
668 }
669
670
671 /* Play Sun audio file S on sound device SD. */
672
673 static void
674 au_play (s, sd)
675 struct sound *s;
676 struct sound_device *sd;
677 {
678 struct au_header *header = (struct au_header *) s->header;
679
680 sd->sample_size = 0;
681 sd->sample_rate = header->sample_rate;
682 sd->bps = 0;
683 sd->channels = header->channels;
684 sd->choose_format (sd, s);
685 sd->configure (sd);
686
687 if (STRINGP (s->data))
688 sd->write (sd, SDATA (s->data) + header->data_offset,
689 SBYTES (s->data) - header->data_offset);
690 else
691 {
692 int blksize = 2048;
693 char *buffer;
694 int nbytes;
695
696 /* Seek */
697 lseek (s->fd, header->data_offset, SEEK_SET);
698
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);
703
704 if (nbytes < 0)
705 sound_perror ("Error reading sound file");
706 }
707 }
708
709
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
721 static void
722 vox_open (sd)
723 struct sound_device *sd;
724 {
725 char *file;
726
727 /* Open the sound device. Default is /dev/dsp. */
728 if (sd->file)
729 file = sd->file;
730 else
731 file = DEFAULT_SOUND_DEVICE;
732
733 sd->fd = emacs_open (file, O_WRONLY, 0);
734 if (sd->fd < 0)
735 sound_perror (file);
736 }
737
738
739 /* Configure device SD from parameters in it. */
740
741 static void
742 vox_configure (sd)
743 struct sound_device *sd;
744 {
745 int val;
746
747 xassert (sd->fd >= 0);
748
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. */
752 turn_on_atimers (0);
753 #ifdef SIGIO
754 sigblock (sigmask (SIGIO));
755 #endif
756
757 val = sd->format;
758 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
759 || val != sd->format)
760 sound_perror ("Could not set sound format");
761
762 val = sd->channels != 1;
763 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
764 || val != (sd->channels != 1))
765 sound_perror ("Could not set stereo/mono");
766
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;
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");
776 }
777
778 if (sd->volume > 0)
779 {
780 int volume = sd->volume & 0xff;
781 volume |= volume << 8;
782 /* This may fail if there is no mixer. Ignore the failure. */
783 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
784 }
785
786 turn_on_atimers (1);
787 #ifdef SIGIO
788 sigunblock (sigmask (SIGIO));
789 #endif
790 }
791
792
793 /* Close device SD if it is open. */
794
795 static void
796 vox_close (sd)
797 struct sound_device *sd;
798 {
799 if (sd->fd >= 0)
800 {
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
807 turn_on_atimers (0);
808
809 /* Flush sound data, and reset the device. */
810 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
811
812 turn_on_atimers (1);
813 #ifdef SIGIO
814 sigunblock (sigmask (SIGIO));
815 #endif
816
817 /* Close the device. */
818 emacs_close (sd->fd);
819 sd->fd = -1;
820 }
821 }
822
823
824 /* Choose device-dependent format for device SD from sound file S. */
825
826 static void
827 vox_choose_format (sd, s)
828 struct sound_device *sd;
829 struct sound *s;
830 {
831 if (s->type == RIFF)
832 {
833 struct wav_header *h = (struct wav_header *) s->header;
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 }
841 else if (s->type == SUN_AUDIO)
842 {
843 struct au_header *header = (struct au_header *) s->header;
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;
851
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
871 static void
872 vox_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
883 /* Write NBYTES bytes from BUFFER to device SD. */
884
885 static void
886 vox_write (sd, buffer, nbytes)
887 struct sound_device *sd;
888 const char *buffer;
889 int nbytes;
890 {
891 int nwritten = emacs_write (sd->fd, buffer, nbytes);
892 if (nwritten < 0)
893 sound_perror ("Error writing to sound device");
894 }
895
896 /* END: Non Windows functions */
897 #else /* WINDOWSNT */
898
899 /* BEGIN: Windows specific functions */
900
901 static int
902 do_play_sound (psz_file, ui_volume)
903 const char *psz_file;
904 unsigned long ui_volume;
905 {
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
914 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
915 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
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);
920 if (mci_error != 0)
921 {
922 sound_warning ("The open mciSendString command failed to open\n"
923 "the specified sound file");
924 i_result = (int) mci_error;
925 return i_result;
926 }
927 if ((ui_volume > 0) && (ui_volume != UINT_MAX))
928 {
929 mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
930 if (mm_result == MMSYSERR_NOERROR)
931 {
932 b_reset_volume = TRUE;
933 mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
934 if ( mm_result != MMSYSERR_NOERROR)
935 {
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.");
940 }
941 }
942 else
943 {
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.");
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");
953 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
954 if (mci_error != 0)
955 {
956 sound_warning ("The play mciSendString command failed to play the\n"
957 "opened sound file.");
958 i_result = (int) mci_error;
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");
963 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
964 if (b_reset_volume == TRUE)
965 {
966 mm_result=waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
967 if (mm_result != MMSYSERR_NOERROR)
968 {
969 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
970 "level of the WAVE_MAPPER device.");
971 }
972 }
973 return i_result;
974 }
975
976 /* END: Windows specific functions */
977
978 #endif /* WINDOWSNT */
979
980 DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
981 doc: /* Play sound SOUND.
982
983 Internal 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 Lisp_Object args[2];
994 #else /* WINDOWSNT */
995 int len = 0;
996 Lisp_Object lo_file = {0};
997 char * psz_file = NULL;
998 unsigned long ui_volume_tmp = UINT_MAX;
999 unsigned long ui_volume = UINT_MAX;
1000 int i_result = 0;
1001 #endif /* WINDOWSNT */
1002
1003 /* Parse the sound specification. Give up if it is invalid. */
1004 if (!parse_sound (sound, attrs))
1005 error ("Invalid sound specification");
1006
1007 #ifndef WINDOWSNT
1008 file = Qnil;
1009 GCPRO2 (sound, file);
1010 current_sound_device = (struct sound_device *) xmalloc (sizeof (struct sound_device));
1011 bzero (current_sound_device, sizeof (struct sound_device));
1012 current_sound = (struct sound *) xmalloc (sizeof (struct sound));
1013 bzero (current_sound, sizeof (struct sound));
1014 record_unwind_protect (sound_cleanup, Qnil);
1015 current_sound->header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
1016
1017 if (STRINGP (attrs[SOUND_FILE]))
1018 {
1019 /* Open the sound file. */
1020 current_sound->fd = openp (Fcons (Vdata_directory, Qnil),
1021 attrs[SOUND_FILE], Qnil, &file, Qnil);
1022 if (current_sound->fd < 0)
1023 sound_perror ("Could not open sound file");
1024
1025 /* Read the first bytes from the file. */
1026 current_sound->header_size
1027 = emacs_read (current_sound->fd, current_sound->header,
1028 MAX_SOUND_HEADER_BYTES);
1029 if (current_sound->header_size < 0)
1030 sound_perror ("Invalid sound file header");
1031 }
1032 else
1033 {
1034 current_sound->data = attrs[SOUND_DATA];
1035 current_sound->header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (current_sound->data));
1036 bcopy (SDATA (current_sound->data), current_sound->header, current_sound->header_size);
1037 }
1038
1039 /* Find out the type of sound. Give up if we can't tell. */
1040 find_sound_type (current_sound);
1041
1042 /* Set up a device. */
1043 if (STRINGP (attrs[SOUND_DEVICE]))
1044 {
1045 int len = SCHARS (attrs[SOUND_DEVICE]);
1046 current_sound_device->file = (char *) alloca (len + 1);
1047 strcpy (current_sound_device->file, SDATA (attrs[SOUND_DEVICE]));
1048 }
1049
1050 if (INTEGERP (attrs[SOUND_VOLUME]))
1051 current_sound_device->volume = XFASTINT (attrs[SOUND_VOLUME]);
1052 else if (FLOATP (attrs[SOUND_VOLUME]))
1053 current_sound_device->volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
1054
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 (current_sound_device);
1063
1064 /* Open the device. */
1065 current_sound_device->open (current_sound_device);
1066
1067 /* Play the sound. */
1068 current_sound->play (current_sound, current_sound_device);
1069
1070 /* Clean up. */
1071 UNGCPRO;
1072
1073 #else /* WINDOWSNT */
1074
1075 lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil);
1076 len = XSTRING (lo_file)->size;
1077 psz_file = (char *) alloca (len + 1);
1078 strcpy (psz_file, XSTRING (lo_file)->data);
1079 if (INTEGERP (attrs[SOUND_VOLUME]))
1080 {
1081 ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
1082 }
1083 else if (FLOATP (attrs[SOUND_VOLUME]))
1084 {
1085 ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
1086 }
1087 /*
1088 Based on some experiments I have conducted, a value of 100 or less
1089 for the sound volume is much too low. You cannot even hear it.
1090 A value of UINT_MAX indicates that you wish for the sound to played
1091 at the maximum possible volume. A value of UINT_MAX/2 plays the
1092 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1093 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1094 The following code adjusts the user specified volume level appropriately.
1095 */
1096 if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
1097 {
1098 ui_volume = ui_volume_tmp * (UINT_MAX / 100);
1099 }
1100 i_result = do_play_sound (psz_file, ui_volume);
1101
1102 #endif /* WINDOWSNT */
1103
1104 unbind_to (count, Qnil);
1105 return Qnil;
1106 }
1107 \f
1108 /***********************************************************************
1109 Initialization
1110 ***********************************************************************/
1111
1112 void
1113 syms_of_sound ()
1114 {
1115 QCdevice = intern (":device");
1116 staticpro (&QCdevice);
1117 QCvolume = intern (":volume");
1118 staticpro (&QCvolume);
1119 Qsound = intern ("sound");
1120 staticpro (&Qsound);
1121 Qplay_sound_functions = intern ("play-sound-functions");
1122 staticpro (&Qplay_sound_functions);
1123
1124 defsubr (&Splay_sound_internal);
1125 }
1126
1127
1128 void
1129 init_sound ()
1130 {
1131 }
1132
1133 #endif /* HAVE_SOUND */
1134
1135 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1136 (do not change this comment) */