(vc-svn-registered): Fix problem of visiting non-writable
[emacs.git] / src / sound.c
blob1e6a17d0275a0c8e5841bddc69013771a2178eb5
1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
22 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
23 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
26 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
27 implementation of the play-sound specification for Windows.
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.
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.
40 #include <config.h>
42 #if defined HAVE_SOUND
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 */
57 /* BEGIN: Non Windows Includes */
58 #ifndef WINDOWSNT
60 #ifndef MSDOS
61 #include <sys/ioctl.h>
62 #endif
64 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
65 sys/soundcard.h. So, let's try whatever's there. */
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 */
78 #else /* WINDOWSNT */
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 */
89 #endif /* WINDOWSNT */
91 /* BEGIN: Common Definitions */
92 #define abs(X) ((X) < 0 ? -(X) : (X))
94 /* Symbols. */
96 extern Lisp_Object QCfile, QCdata;
97 Lisp_Object QCvolume, QCdevice;
98 Lisp_Object Qsound;
99 Lisp_Object Qplay_sound_functions;
101 /* Indices of attributes in a sound attributes vector. */
103 enum sound_attr
105 SOUND_FILE,
106 SOUND_DATA,
107 SOUND_DEVICE,
108 SOUND_VOLUME,
109 SOUND_ATTR_SENTINEL
112 static void sound_perror P_ ((char *));
113 static void sound_warning P_ ((char *));
114 static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
116 /* END: Common Definitions */
118 /* BEGIN: Non Windows Definitions */
119 #ifndef WINDOWSNT
121 #ifndef DEFAULT_SOUND_DEVICE
122 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
123 #endif
126 /* Structure forward declarations. */
128 struct sound;
129 struct sound_device;
131 /* The file header of RIFF-WAVE files (*.wav). Files are always in
132 little-endian byte-order. */
134 struct wav_header
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;
151 /* The file header of Sun adio files (*.au). Files are always in
152 big-endian byte-order. */
154 struct au_header
156 /* ASCII ".snd" */
157 u_int32_t magic_number;
159 /* Offset of data part from start of file. Minimum value is 24. */
160 u_int32_t data_offset;
162 /* Size of data part, 0xffffffff if unknown. */
163 u_int32_t data_size;
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;
177 /* Number of samples per second. */
178 u_int32_t sample_rate;
180 /* Number of interleaved channels. */
181 u_int32_t channels;
184 /* Maximum of all sound file headers sizes. */
186 #define MAX_SOUND_HEADER_BYTES \
187 max (sizeof (struct wav_header), sizeof (struct au_header))
189 /* Interface structure for sound devices. */
191 struct sound_device
193 /* The name of the device or null meaning use a default device name. */
194 char *file;
196 /* File descriptor of the device. */
197 int fd;
199 /* Device-dependent format. */
200 int format;
202 /* Volume (0..100). Zero means unspecified. */
203 int volume;
205 /* Sample size. */
206 int sample_size;
208 /* Sample rate. */
209 int sample_rate;
211 /* Bytes per second. */
212 int bps;
214 /* 1 = mono, 2 = stereo, 0 = don't set. */
215 int channels;
217 /* Open device SD. */
218 void (* open) P_ ((struct sound_device *sd));
220 /* Close device SD. */
221 void (* close) P_ ((struct sound_device *sd));
223 /* Configure SD accoring to device-dependent parameters. */
224 void (* configure) P_ ((struct sound_device *device));
226 /* Choose a device-dependent format for outputting sound S. */
227 void (* choose_format) P_ ((struct sound_device *sd,
228 struct sound *s));
230 /* Write NYBTES bytes from BUFFER to device SD. */
231 void (* write) P_ ((struct sound_device *sd, const char *buffer,
232 int nbytes));
234 /* A place for devices to store additional data. */
235 void *data;
238 /* An enumerator for each supported sound file type. */
240 enum sound_type
242 RIFF,
243 SUN_AUDIO
246 /* Interface structure for sound files. */
248 struct sound
250 /* The type of the file. */
251 enum sound_type type;
253 /* File descriptor of a sound file. */
254 int fd;
256 /* Pointer to sound file header. This contains header_size bytes
257 read from the start of a sound file. */
258 char *header;
260 /* Number of bytes raed from sound file. This is always <=
261 MAX_SOUND_HEADER_BYTES. */
262 int header_size;
264 /* Sound data, if a string. */
265 Lisp_Object data;
267 /* Play sound file S on device SD. */
268 void (* play) P_ ((struct sound *s, struct sound_device *sd));
271 /* These are set during `play-sound-internal' so that sound_cleanup has
272 access to them. */
274 struct sound_device *current_sound_device;
275 struct sound *current_sound;
277 /* Function prototypes. */
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 *));
294 #if 0 /* Currently not used. */
295 static u_int16_t be2hs P_ ((u_int16_t));
296 #endif
298 /* END: Non Windows Definitions */
299 #else /* WINDOWSNT */
301 /* BEGIN: Windows Specific Definitions */
302 static int do_play_sound P_ ((const char *, unsigned long));
304 END: Windows Specific Definitions */
305 #endif /* WINDOWSNT */
308 /***********************************************************************
309 General
310 ***********************************************************************/
312 /* BEGIN: Common functions */
314 /* Like perror, but signals an error. */
316 static void
317 sound_perror (msg)
318 char *msg;
320 int saved_errno = errno;
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);
333 /* Display a warning message. */
335 static void
336 sound_warning (msg)
337 char *msg;
339 message (msg);
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:
349 - `:file FILE'
351 FILE is the sound file to play. If it isn't an absolute name,
352 it's searched under `data-directory'.
354 - `:data DATA'
356 DATA is a string containing sound data. Either :file or :data
357 may be present, but not both.
359 - `:device DEVICE'
361 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
362 If not specified, a default device is used.
364 - `:volume VOL'
366 VOL must be an integer in the range [0, 100], or a float in the
367 range [0, 1]. */
369 static int
370 parse_sound (sound, attrs)
371 Lisp_Object sound;
372 Lisp_Object *attrs;
374 /* SOUND must be a list starting with the symbol `sound'. */
375 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
376 return 0;
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);
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 */
391 Data is not supported in Windows. Therefore a
392 File name MUST be supplied.
394 if (!STRINGP (attrs[SOUND_FILE]))
396 return 0;
398 #endif /* WINDOWSNT */
400 /* Volume must be in the range 0..100 or unspecified. */
401 if (!NILP (attrs[SOUND_VOLUME]))
403 if (INTEGERP (attrs[SOUND_VOLUME]))
405 if (XINT (attrs[SOUND_VOLUME]) < 0
406 || XINT (attrs[SOUND_VOLUME]) > 100)
407 return 0;
409 else if (FLOATP (attrs[SOUND_VOLUME]))
411 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
412 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
413 return 0;
415 else
416 return 0;
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 */
426 Since device is ignored in Windows, it does not matter
427 what it is.
429 return 1;
432 /* END: Common functions */
434 /* BEGIN: Non Windows functions */
435 #ifndef WINDOWSNT
437 /* Find out the type of the sound file whose file descriptor is FD.
438 S is the sound file structure to fill in. */
440 static void
441 find_sound_type (s)
442 struct sound *s;
444 if (!wav_init (s) && !au_init (s))
445 error ("Unknown sound format");
449 /* Function installed by play-sound-internal with record_unwind_protect. */
451 static Lisp_Object
452 sound_cleanup (arg)
453 Lisp_Object arg;
455 if (current_sound_device)
457 if (current_sound_device->close)
458 current_sound_device->close (current_sound_device);
459 if (current_sound->fd > 0)
460 emacs_close (current_sound->fd);
463 return Qnil;
466 /***********************************************************************
467 Byte-order Conversion
468 ***********************************************************************/
470 /* Convert 32-bit value VALUE which is in little-endian byte-order
471 to host byte-order. */
473 static u_int32_t
474 le2hl (value)
475 u_int32_t value;
477 #ifdef WORDS_BIG_ENDIAN
478 unsigned char *p = (unsigned char *) &value;
479 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
480 #endif
481 return value;
485 /* Convert 16-bit value VALUE which is in little-endian byte-order
486 to host byte-order. */
488 static u_int16_t
489 le2hs (value)
490 u_int16_t value;
492 #ifdef WORDS_BIG_ENDIAN
493 unsigned char *p = (unsigned char *) &value;
494 value = p[0] + (p[1] << 8);
495 #endif
496 return value;
500 /* Convert 32-bit value VALUE which is in big-endian byte-order
501 to host byte-order. */
503 static u_int32_t
504 be2hl (value)
505 u_int32_t value;
507 #ifndef WORDS_BIG_ENDIAN
508 unsigned char *p = (unsigned char *) &value;
509 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
510 #endif
511 return value;
515 #if 0 /* Currently not used. */
517 /* Convert 16-bit value VALUE which is in big-endian byte-order
518 to host byte-order. */
520 static u_int16_t
521 be2hs (value)
522 u_int16_t value;
524 #ifndef WORDS_BIG_ENDIAN
525 unsigned char *p = (unsigned char *) &value;
526 value = p[1] + (p[0] << 8);
527 #endif
528 return value;
531 #endif /* 0 */
533 /***********************************************************************
534 RIFF-WAVE (*.wav)
535 ***********************************************************************/
537 /* Try to initialize sound file S from S->header. S->header
538 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
539 sound file. If the file is a WAV-format file, set up interface
540 functions in S and convert header fields to host byte-order.
541 Value is non-zero if the file is a WAV file. */
543 static int
544 wav_init (s)
545 struct sound *s;
547 struct wav_header *header = (struct wav_header *) s->header;
549 if (s->header_size < sizeof *header
550 || bcmp (s->header, "RIFF", 4) != 0)
551 return 0;
553 /* WAV files are in little-endian order. Convert the header
554 if on a big-endian machine. */
555 header->magic = le2hl (header->magic);
556 header->length = le2hl (header->length);
557 header->chunk_type = le2hl (header->chunk_type);
558 header->chunk_format = le2hl (header->chunk_format);
559 header->chunk_length = le2hl (header->chunk_length);
560 header->format = le2hs (header->format);
561 header->channels = le2hs (header->channels);
562 header->sample_rate = le2hl (header->sample_rate);
563 header->bytes_per_second = le2hl (header->bytes_per_second);
564 header->sample_size = le2hs (header->sample_size);
565 header->precision = le2hs (header->precision);
566 header->chunk_data = le2hl (header->chunk_data);
567 header->data_length = le2hl (header->data_length);
569 /* Set up the interface functions for WAV. */
570 s->type = RIFF;
571 s->play = wav_play;
573 return 1;
577 /* Play RIFF-WAVE audio file S on sound device SD. */
579 static void
580 wav_play (s, sd)
581 struct sound *s;
582 struct sound_device *sd;
584 struct wav_header *header = (struct wav_header *) s->header;
586 /* Let the device choose a suitable device-dependent format
587 for the file. */
588 sd->choose_format (sd, s);
590 /* Configure the device. */
591 sd->sample_size = header->sample_size;
592 sd->sample_rate = header->sample_rate;
593 sd->bps = header->bytes_per_second;
594 sd->channels = header->channels;
595 sd->configure (sd);
597 /* Copy sound data to the device. The WAV file specification is
598 actually more complex. This simple scheme worked with all WAV
599 files I found so far. If someone feels inclined to implement the
600 whole RIFF-WAVE spec, please do. */
601 if (STRINGP (s->data))
602 sd->write (sd, SDATA (s->data) + sizeof *header,
603 SBYTES (s->data) - sizeof *header);
604 else
606 char *buffer;
607 int nbytes;
608 int blksize = 2048;
610 buffer = (char *) alloca (blksize);
611 lseek (s->fd, sizeof *header, SEEK_SET);
613 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
614 sd->write (sd, buffer, nbytes);
616 if (nbytes < 0)
617 sound_perror ("Error reading sound file");
622 /***********************************************************************
623 Sun Audio (*.au)
624 ***********************************************************************/
626 /* Sun audio file encodings. */
628 enum au_encoding
630 AU_ENCODING_ULAW_8 = 1,
631 AU_ENCODING_8,
632 AU_ENCODING_16,
633 AU_ENCODING_24,
634 AU_ENCODING_32,
635 AU_ENCODING_IEEE32,
636 AU_ENCODING_IEEE64,
637 AU_COMPRESSED = 23
641 /* Try to initialize sound file S from S->header. S->header
642 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
643 sound file. If the file is a AU-format file, set up interface
644 functions in S and convert header fields to host byte-order.
645 Value is non-zero if the file is an AU file. */
647 static int
648 au_init (s)
649 struct sound *s;
651 struct au_header *header = (struct au_header *) s->header;
653 if (s->header_size < sizeof *header
654 || bcmp (s->header, ".snd", 4) != 0)
655 return 0;
657 header->magic_number = be2hl (header->magic_number);
658 header->data_offset = be2hl (header->data_offset);
659 header->data_size = be2hl (header->data_size);
660 header->encoding = be2hl (header->encoding);
661 header->sample_rate = be2hl (header->sample_rate);
662 header->channels = be2hl (header->channels);
664 /* Set up the interface functions for AU. */
665 s->type = SUN_AUDIO;
666 s->play = au_play;
668 return 1;
672 /* Play Sun audio file S on sound device SD. */
674 static void
675 au_play (s, sd)
676 struct sound *s;
677 struct sound_device *sd;
679 struct au_header *header = (struct au_header *) s->header;
681 sd->sample_size = 0;
682 sd->sample_rate = header->sample_rate;
683 sd->bps = 0;
684 sd->channels = header->channels;
685 sd->choose_format (sd, s);
686 sd->configure (sd);
688 if (STRINGP (s->data))
689 sd->write (sd, SDATA (s->data) + header->data_offset,
690 SBYTES (s->data) - header->data_offset);
691 else
693 int blksize = 2048;
694 char *buffer;
695 int nbytes;
697 /* Seek */
698 lseek (s->fd, header->data_offset, SEEK_SET);
700 /* Copy sound data to the device. */
701 buffer = (char *) alloca (blksize);
702 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
703 sd->write (sd, buffer, nbytes);
705 if (nbytes < 0)
706 sound_perror ("Error reading sound file");
711 /***********************************************************************
712 Voxware Driver Interface
713 ***********************************************************************/
715 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
716 has a compatible own driver aka Luigi's driver. */
719 /* Open device SD. If SD->file is non-null, open that device,
720 otherwise use a default device name. */
722 static void
723 vox_open (sd)
724 struct sound_device *sd;
726 char *file;
728 /* Open the sound device. Default is /dev/dsp. */
729 if (sd->file)
730 file = sd->file;
731 else
732 file = DEFAULT_SOUND_DEVICE;
734 sd->fd = emacs_open (file, O_WRONLY, 0);
735 if (sd->fd < 0)
736 sound_perror (file);
740 /* Configure device SD from parameters in it. */
742 static void
743 vox_configure (sd)
744 struct sound_device *sd;
746 int val;
748 xassert (sd->fd >= 0);
750 /* On GNU/Linux, it seems that the device driver doesn't like to be
751 interrupted by a signal. Block the ones we know to cause
752 troubles. */
753 turn_on_atimers (0);
754 #ifdef SIGIO
755 sigblock (sigmask (SIGIO));
756 #endif
758 val = sd->format;
759 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
760 || val != sd->format)
761 sound_perror ("Could not set sound format");
763 val = sd->channels != 1;
764 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
765 || val != (sd->channels != 1))
766 sound_perror ("Could not set stereo/mono");
768 /* I think bps and sampling_rate are the same, but who knows.
769 Check this. and use SND_DSP_SPEED for both. */
770 if (sd->sample_rate > 0)
772 val = sd->sample_rate;
773 if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0)
774 sound_perror ("Could not set sound speed");
775 else if (val != sd->sample_rate)
776 sound_warning ("Could not set sample rate");
779 if (sd->volume > 0)
781 int volume = sd->volume & 0xff;
782 volume |= volume << 8;
783 /* This may fail if there is no mixer. Ignore the failure. */
784 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
787 turn_on_atimers (1);
788 #ifdef SIGIO
789 sigunblock (sigmask (SIGIO));
790 #endif
794 /* Close device SD if it is open. */
796 static void
797 vox_close (sd)
798 struct sound_device *sd;
800 if (sd->fd >= 0)
802 /* On GNU/Linux, it seems that the device driver doesn't like to
803 be interrupted by a signal. Block the ones we know to cause
804 troubles. */
805 #ifdef SIGIO
806 sigblock (sigmask (SIGIO));
807 #endif
808 turn_on_atimers (0);
810 /* Flush sound data, and reset the device. */
811 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
813 turn_on_atimers (1);
814 #ifdef SIGIO
815 sigunblock (sigmask (SIGIO));
816 #endif
818 /* Close the device. */
819 emacs_close (sd->fd);
820 sd->fd = -1;
825 /* Choose device-dependent format for device SD from sound file S. */
827 static void
828 vox_choose_format (sd, s)
829 struct sound_device *sd;
830 struct sound *s;
832 if (s->type == RIFF)
834 struct wav_header *h = (struct wav_header *) s->header;
835 if (h->precision == 8)
836 sd->format = AFMT_U8;
837 else if (h->precision == 16)
838 sd->format = AFMT_S16_LE;
839 else
840 error ("Unsupported WAV file format");
842 else if (s->type == SUN_AUDIO)
844 struct au_header *header = (struct au_header *) s->header;
845 switch (header->encoding)
847 case AU_ENCODING_ULAW_8:
848 case AU_ENCODING_IEEE32:
849 case AU_ENCODING_IEEE64:
850 sd->format = AFMT_MU_LAW;
851 break;
853 case AU_ENCODING_8:
854 case AU_ENCODING_16:
855 case AU_ENCODING_24:
856 case AU_ENCODING_32:
857 sd->format = AFMT_S16_LE;
858 break;
860 default:
861 error ("Unsupported AU file format");
864 else
865 abort ();
869 /* Initialize device SD. Set up the interface functions in the device
870 structure. */
872 static void
873 vox_init (sd)
874 struct sound_device *sd;
876 sd->fd = -1;
877 sd->open = vox_open;
878 sd->close = vox_close;
879 sd->configure = vox_configure;
880 sd->choose_format = vox_choose_format;
881 sd->write = vox_write;
884 /* Write NBYTES bytes from BUFFER to device SD. */
886 static void
887 vox_write (sd, buffer, nbytes)
888 struct sound_device *sd;
889 const char *buffer;
890 int nbytes;
892 int nwritten = emacs_write (sd->fd, buffer, nbytes);
893 if (nwritten < 0)
894 sound_perror ("Error writing to sound device");
897 /* END: Non Windows functions */
898 #else /* WINDOWSNT */
900 /* BEGIN: Windows specific functions */
902 static int
903 do_play_sound (psz_file, ui_volume)
904 const char *psz_file;
905 unsigned long ui_volume;
907 int i_result = 0;
908 MCIERROR mci_error = 0;
909 char sz_cmd_buf[520] = {0};
910 char sz_ret_buf[520] = {0};
911 MMRESULT mm_result = MMSYSERR_NOERROR;
912 unsigned long ui_volume_org = 0;
913 BOOL b_reset_volume = FALSE;
915 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
916 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
917 sprintf (sz_cmd_buf,
918 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
919 psz_file);
920 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
921 if (mci_error != 0)
923 sound_warning ("The open mciSendString command failed to open\n"
924 "the specified sound file");
925 i_result = (int) mci_error;
926 return i_result;
928 if ((ui_volume > 0) && (ui_volume != UINT_MAX))
930 mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
931 if (mm_result == MMSYSERR_NOERROR)
933 b_reset_volume = TRUE;
934 mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
935 if ( mm_result != MMSYSERR_NOERROR)
937 sound_warning ("waveOutSetVolume failed to set the volume level\n"
938 "of the WAVE_MAPPER device.\n"
939 "As a result, the user selected volume level will\n"
940 "not be used.");
943 else
945 sound_warning ("waveOutGetVolume failed to obtain the original\n"
946 "volume level of the WAVE_MAPPER device.\n"
947 "As a result, the user selected volume level will\n"
948 "not be used.");
951 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
952 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
953 strcpy (sz_cmd_buf, "play GNUEmacs_PlaySound_Device wait");
954 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
955 if (mci_error != 0)
957 sound_warning ("The play mciSendString command failed to play the\n"
958 "opened sound file.");
959 i_result = (int) mci_error;
961 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
962 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
963 strcpy (sz_cmd_buf, "close GNUEmacs_PlaySound_Device wait");
964 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
965 if (b_reset_volume == TRUE)
967 mm_result=waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
968 if (mm_result != MMSYSERR_NOERROR)
970 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
971 "level of the WAVE_MAPPER device.");
974 return i_result;
977 /* END: Windows specific functions */
979 #endif /* WINDOWSNT */
981 DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
982 doc: /* Play sound SOUND.
984 Internal use only, use `play-sound' instead.\n */)
985 (sound)
986 Lisp_Object sound;
988 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
989 int count = SPECPDL_INDEX ();
991 #ifndef WINDOWSNT
992 Lisp_Object file;
993 struct gcpro gcpro1, gcpro2;
994 struct sound_device sd;
995 struct sound s;
996 Lisp_Object args[2];
997 #else /* WINDOWSNT */
998 int len = 0;
999 Lisp_Object lo_file = {0};
1000 char * psz_file = NULL;
1001 unsigned long ui_volume_tmp = UINT_MAX;
1002 unsigned long ui_volume = UINT_MAX;
1003 int i_result = 0;
1004 #endif /* WINDOWSNT */
1006 /* Parse the sound specification. Give up if it is invalid. */
1007 if (!parse_sound (sound, attrs))
1008 error ("Invalid sound specification");
1010 #ifndef WINDOWSNT
1011 file = Qnil;
1012 GCPRO2 (sound, file);
1013 bzero (&sd, sizeof sd);
1014 bzero (&s, sizeof s);
1015 current_sound_device = &sd;
1016 current_sound = &s;
1017 record_unwind_protect (sound_cleanup, Qnil);
1018 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
1020 if (STRINGP (attrs[SOUND_FILE]))
1022 /* Open the sound file. */
1023 s.fd = openp (Fcons (Vdata_directory, Qnil),
1024 attrs[SOUND_FILE], Qnil, &file, Qnil);
1025 if (s.fd < 0)
1026 sound_perror ("Could not open sound file");
1028 /* Read the first bytes from the file. */
1029 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
1030 if (s.header_size < 0)
1031 sound_perror ("Invalid sound file header");
1033 else
1035 s.data = attrs[SOUND_DATA];
1036 s.header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (s.data));
1037 bcopy (SDATA (s.data), s.header, s.header_size);
1040 /* Find out the type of sound. Give up if we can't tell. */
1041 find_sound_type (&s);
1043 /* Set up a device. */
1044 if (STRINGP (attrs[SOUND_DEVICE]))
1046 int len = SCHARS (attrs[SOUND_DEVICE]);
1047 sd.file = (char *) alloca (len + 1);
1048 strcpy (sd.file, SDATA (attrs[SOUND_DEVICE]));
1051 if (INTEGERP (attrs[SOUND_VOLUME]))
1052 sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
1053 else if (FLOATP (attrs[SOUND_VOLUME]))
1054 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
1056 args[0] = Qplay_sound_functions;
1057 args[1] = sound;
1058 Frun_hook_with_args (2, args);
1060 /* There is only one type of device we currently support, the VOX
1061 sound driver. Set up the device interface functions for that
1062 device. */
1063 vox_init (&sd);
1065 /* Open the device. */
1066 sd.open (&sd);
1068 /* Play the sound. */
1069 s.play (&s, &sd);
1071 /* Close the input file, if any. */
1072 if (!STRINGP (s.data))
1074 emacs_close (s.fd);
1075 s.fd = -1;
1078 /* Close the device. */
1079 sd.close (&sd);
1081 /* Clean up. */
1082 current_sound_device = NULL;
1083 current_sound = NULL;
1084 UNGCPRO;
1086 #else /* WINDOWSNT */
1088 lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil);
1089 len = XSTRING (lo_file)->size;
1090 psz_file = (char *) alloca (len + 1);
1091 strcpy (psz_file, XSTRING (lo_file)->data);
1092 if (INTEGERP (attrs[SOUND_VOLUME]))
1094 ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
1096 else if (FLOATP (attrs[SOUND_VOLUME]))
1098 ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
1101 Based on some experiments I have conducted, a value of 100 or less
1102 for the sound volume is much too low. You cannot even hear it.
1103 A value of UINT_MAX indicates that you wish for the sound to played
1104 at the maximum possible volume. A value of UINT_MAX/2 plays the
1105 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1106 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1107 The following code adjusts the user specified volume level appropriately.
1109 if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
1111 ui_volume = ui_volume_tmp * (UINT_MAX / 100);
1113 i_result = do_play_sound (psz_file, ui_volume);
1115 #endif /* WINDOWSNT */
1117 unbind_to (count, Qnil);
1118 return Qnil;
1121 /***********************************************************************
1122 Initialization
1123 ***********************************************************************/
1125 void
1126 syms_of_sound ()
1128 QCdevice = intern (":device");
1129 staticpro (&QCdevice);
1130 QCvolume = intern (":volume");
1131 staticpro (&QCvolume);
1132 Qsound = intern ("sound");
1133 staticpro (&Qsound);
1134 Qplay_sound_functions = intern ("play-sound-functions");
1135 staticpro (&Qplay_sound_functions);
1137 defsubr (&Splay_sound_internal);
1141 void
1142 init_sound ()
1146 #endif /* HAVE_SOUND */
1148 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1149 (do not change this comment) */