1 /* sound.c -- sound support.
2 Copyright (C) 1998, 1999, 2001 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
22 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
25 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
26 implementation of the play-sound specification for Windows.
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.
34 The Windows implementation of play-sound is implemented via the
35 Win32 API functions mciSendString, waveOutGetVolume, and
36 waveOutSetVolume which are exported by Winmm.dll.
41 #if defined HAVE_SOUND
43 /* BEGIN: Common Includes */
46 #include <sys/types.h>
49 #include "dispextern.h"
52 #include "syssignal.h"
53 /* END: Common Includes */
56 /* BEGIN: Non Windows Includes */
60 #include <sys/ioctl.h>
63 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
64 sys/soundcard.h. So, let's try whatever's there. */
66 #ifdef HAVE_MACHINE_SOUNDCARD_H
67 #include <machine/soundcard.h>
69 #ifdef HAVE_SYS_SOUNDCARD_H
70 #include <sys/soundcard.h>
72 #ifdef HAVE_SOUNDCARD_H
73 #include <soundcard.h>
75 /* END: Non Windows Includes */
79 /* BEGIN: Windows Specific Includes */
86 /* END: Windows Specific Includes */
88 #endif /* WINDOWSNT */
90 /* BEGIN: Common Definitions */
91 #define abs(X) ((X) < 0 ? -(X) : (X))
95 extern Lisp_Object QCfile
, QCdata
;
96 Lisp_Object QCvolume
, QCdevice
;
98 Lisp_Object Qplay_sound_functions
;
100 /* Indices of attributes in a sound attributes vector. */
111 static void sound_perror
P_ ((char *));
112 static void sound_warning
P_ ((char *));
113 static int parse_sound
P_ ((Lisp_Object
, Lisp_Object
*));
115 /* END: Common Definitions */
117 /* BEGIN: Non Windows Definitions */
120 #ifndef DEFAULT_SOUND_DEVICE
121 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
125 /* Structure forward declarations. */
130 /* The file header of RIFF-WAVE files (*.wav). Files are always in
131 little-endian byte-order. */
137 u_int32_t chunk_type
;
138 u_int32_t chunk_format
;
139 u_int32_t chunk_length
;
142 u_int32_t sample_rate
;
143 u_int32_t bytes_per_second
;
144 u_int16_t sample_size
;
146 u_int32_t chunk_data
;
147 u_int32_t data_length
;
150 /* The file header of Sun adio files (*.au). Files are always in
151 big-endian byte-order. */
156 u_int32_t magic_number
;
158 /* Offset of data part from start of file. Minimum value is 24. */
159 u_int32_t data_offset
;
161 /* Size of data part, 0xffffffff if unknown. */
164 /* Data encoding format.
166 2 8-bit linear PCM (REF-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
176 /* Number of samples per second. */
177 u_int32_t sample_rate
;
179 /* Number of interleaved channels. */
183 /* Maximum of all sound file headers sizes. */
185 #define MAX_SOUND_HEADER_BYTES \
186 max (sizeof (struct wav_header), sizeof (struct au_header))
188 /* Interface structure for sound devices. */
192 /* The name of the device or null meaning use a default device name. */
195 /* File descriptor of the device. */
198 /* Device-dependent format. */
201 /* Volume (0..100). Zero means unspecified. */
210 /* Bytes per second. */
213 /* 1 = mono, 2 = stereo, 0 = don't set. */
216 /* Open device SD. */
217 void (* open
) P_ ((struct sound_device
*sd
));
219 /* Close device SD. */
220 void (* close
) P_ ((struct sound_device
*sd
));
222 /* Configure SD accoring to device-dependent parameters. */
223 void (* configure
) P_ ((struct sound_device
*device
));
225 /* Choose a device-dependent format for outputting sound S. */
226 void (* choose_format
) P_ ((struct sound_device
*sd
,
229 /* Write NYBTES bytes from BUFFER to device SD. */
230 void (* write
) P_ ((struct sound_device
*sd
, const char *buffer
,
233 /* A place for devices to store additional data. */
237 /* An enumerator for each supported sound file type. */
245 /* Interface structure for sound files. */
249 /* The type of the file. */
250 enum sound_type type
;
252 /* File descriptor of a sound file. */
255 /* Pointer to sound file header. This contains header_size bytes
256 read from the start of a sound file. */
259 /* Number of bytes raed from sound file. This is always <=
260 MAX_SOUND_HEADER_BYTES. */
263 /* Sound data, if a string. */
266 /* Play sound file S on device SD. */
267 void (* play
) P_ ((struct sound
*s
, struct sound_device
*sd
));
270 /* These are set during `play-sound-internal' so that sound_cleanup has
273 struct sound_device
*current_sound_device
;
274 struct sound
*current_sound
;
276 /* Function prototypes. */
278 static void vox_open
P_ ((struct sound_device
*));
279 static void vox_configure
P_ ((struct sound_device
*));
280 static void vox_close
P_ ((struct sound_device
*sd
));
281 static void vox_choose_format
P_ ((struct sound_device
*, struct sound
*));
282 static void vox_init
P_ ((struct sound_device
*));
283 static void vox_write
P_ ((struct sound_device
*, const char *, int));
284 static void find_sound_type
P_ ((struct sound
*));
285 static u_int32_t le2hl
P_ ((u_int32_t
));
286 static u_int16_t le2hs
P_ ((u_int16_t
));
287 static u_int32_t be2hl
P_ ((u_int32_t
));
288 static int wav_init
P_ ((struct sound
*));
289 static void wav_play
P_ ((struct sound
*, struct sound_device
*));
290 static int au_init
P_ ((struct sound
*));
291 static void au_play
P_ ((struct sound
*, struct sound_device
*));
293 #if 0 /* Currently not used. */
294 static u_int16_t be2hs
P_ ((u_int16_t
));
297 /* END: Non Windows Definitions */
298 #else /* WINDOWSNT */
300 /* BEGIN: Windows Specific Definitions */
301 static int do_play_sound
P_ ((const char *, unsigned long));
303 END: Windows Specific Definitions */
304 #endif /* WINDOWSNT */
307 /***********************************************************************
309 ***********************************************************************/
311 /* BEGIN: Common functions */
313 /* Like perror, but signals an error. */
319 int saved_errno
= errno
;
323 sigunblock (sigmask (SIGIO
));
325 if (saved_errno
!= 0)
326 error ("%s: %s", msg
, strerror (saved_errno
));
332 /* Display a warning message. */
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:
350 FILE is the sound file to play. If it isn't an absolute name,
351 it's searched under `data-directory'.
355 DATA is a string containing sound data. Either :file or :data
356 may be present, but not both.
360 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
361 If not specified, a default device is used.
365 VOL must be an integer in the range [0, 100], or a float in the
369 parse_sound (sound
, attrs
)
373 /* SOUND must be a list starting with the symbol `sound'. */
374 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
377 sound
= XCDR (sound
);
378 attrs
[SOUND_FILE
] = Fplist_get (sound
, QCfile
);
379 attrs
[SOUND_DATA
] = Fplist_get (sound
, QCdata
);
380 attrs
[SOUND_DEVICE
] = Fplist_get (sound
, QCdevice
);
381 attrs
[SOUND_VOLUME
] = Fplist_get (sound
, QCvolume
);
384 /* File name or data must be specified. */
385 if (!STRINGP (attrs
[SOUND_FILE
])
386 && !STRINGP (attrs
[SOUND_DATA
]))
388 #else /* WINDOWSNT */
390 Data is not supported in Windows. Therefore a
391 File name MUST be supplied.
393 if (!STRINGP (attrs
[SOUND_FILE
]))
397 #endif /* WINDOWSNT */
399 /* Volume must be in the range 0..100 or unspecified. */
400 if (!NILP (attrs
[SOUND_VOLUME
]))
402 if (INTEGERP (attrs
[SOUND_VOLUME
]))
404 if (XINT (attrs
[SOUND_VOLUME
]) < 0
405 || XINT (attrs
[SOUND_VOLUME
]) > 100)
408 else if (FLOATP (attrs
[SOUND_VOLUME
]))
410 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
411 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
419 /* Device must be a string or unspecified. */
420 if (!NILP (attrs
[SOUND_DEVICE
])
421 && !STRINGP (attrs
[SOUND_DEVICE
]))
423 #endif /* WINDOWSNT */
425 Since device is ignored in Windows, it does not matter
431 /* END: Common functions */
433 /* BEGIN: Non Windows functions */
436 /* Find out the type of the sound file whose file descriptor is FD.
437 S is the sound file structure to fill in. */
443 if (!wav_init (s
) && !au_init (s
))
444 error ("Unknown sound format");
448 /* Function installed by play-sound-internal with record_unwind_protect. */
454 if (current_sound_device
)
456 if (current_sound_device
->close
)
457 current_sound_device
->close (current_sound_device
);
458 if (current_sound
->fd
> 0)
459 emacs_close (current_sound
->fd
);
465 /***********************************************************************
466 Byte-order Conversion
467 ***********************************************************************/
469 /* Convert 32-bit value VALUE which is in little-endian byte-order
470 to host byte-order. */
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);
484 /* Convert 16-bit value VALUE which is in little-endian byte-order
485 to host byte-order. */
491 #ifdef WORDS_BIG_ENDIAN
492 unsigned char *p
= (unsigned char *) &value
;
493 value
= p
[0] + (p
[1] << 8);
499 /* Convert 32-bit value VALUE which is in big-endian byte-order
500 to host byte-order. */
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);
514 #if 0 /* Currently not used. */
516 /* Convert 16-bit value VALUE which is in big-endian byte-order
517 to host byte-order. */
523 #ifndef WORDS_BIG_ENDIAN
524 unsigned char *p
= (unsigned char *) &value
;
525 value
= p
[1] + (p
[0] << 8);
532 /***********************************************************************
534 ***********************************************************************/
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. */
546 struct wav_header
*header
= (struct wav_header
*) s
->header
;
548 if (s
->header_size
< sizeof *header
549 || bcmp (s
->header
, "RIFF", 4) != 0)
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
);
568 /* Set up the interface functions for WAV. */
576 /* Play RIFF-WAVE audio file S on sound device SD. */
581 struct sound_device
*sd
;
583 struct wav_header
*header
= (struct wav_header
*) s
->header
;
585 /* Let the device choose a suitable device-dependent format
587 sd
->choose_format (sd
, s
);
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
;
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
);
609 buffer
= (char *) alloca (blksize
);
610 lseek (s
->fd
, sizeof *header
, SEEK_SET
);
612 while ((nbytes
= emacs_read (s
->fd
, buffer
, blksize
)) > 0)
613 sd
->write (sd
, buffer
, nbytes
);
616 sound_perror ("Error reading sound file");
621 /***********************************************************************
623 ***********************************************************************/
625 /* Sun audio file encodings. */
629 AU_ENCODING_ULAW_8
= 1,
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. */
650 struct au_header
*header
= (struct au_header
*) s
->header
;
652 if (s
->header_size
< sizeof *header
653 || bcmp (s
->header
, ".snd", 4) != 0)
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
);
663 /* Set up the interface functions for AU. */
671 /* Play Sun audio file S on sound device SD. */
676 struct sound_device
*sd
;
678 struct au_header
*header
= (struct au_header
*) s
->header
;
681 sd
->sample_rate
= header
->sample_rate
;
683 sd
->channels
= header
->channels
;
684 sd
->choose_format (sd
, s
);
687 if (STRINGP (s
->data
))
688 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
689 SBYTES (s
->data
) - header
->data_offset
);
697 lseek (s
->fd
, header
->data_offset
, SEEK_SET
);
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
);
705 sound_perror ("Error reading sound file");
710 /***********************************************************************
711 Voxware Driver Interface
712 ***********************************************************************/
714 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
715 has a compatible own driver aka Luigi's driver. */
718 /* Open device SD. If SD->file is non-null, open that device,
719 otherwise use a default device name. */
723 struct sound_device
*sd
;
727 /* Open the sound device. Default is /dev/dsp. */
731 file
= DEFAULT_SOUND_DEVICE
;
733 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
739 /* Configure device SD from parameters in it. */
743 struct sound_device
*sd
;
747 xassert (sd
->fd
>= 0);
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
754 sigblock (sigmask (SIGIO
));
758 if (ioctl (sd
->fd
, SNDCTL_DSP_SETFMT
, &sd
->format
) < 0
759 || val
!= sd
->format
)
760 sound_perror ("Could not set sound format");
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");
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)
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");
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
);
788 sigunblock (sigmask (SIGIO
));
793 /* Close device SD if it is open. */
797 struct sound_device
*sd
;
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
805 sigblock (sigmask (SIGIO
));
809 /* Flush sound data, and reset the device. */
810 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
814 sigunblock (sigmask (SIGIO
));
817 /* Close the device. */
818 emacs_close (sd
->fd
);
824 /* Choose device-dependent format for device SD from sound file S. */
827 vox_choose_format (sd
, s
)
828 struct sound_device
*sd
;
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
;
839 error ("Unsupported WAV file format");
841 else if (s
->type
== SUN_AUDIO
)
843 struct au_header
*header
= (struct au_header
*) s
->header
;
844 switch (header
->encoding
)
846 case AU_ENCODING_ULAW_8
:
847 case AU_ENCODING_IEEE32
:
848 case AU_ENCODING_IEEE64
:
849 sd
->format
= AFMT_MU_LAW
;
856 sd
->format
= AFMT_S16_LE
;
860 error ("Unsupported AU file format");
868 /* Initialize device SD. Set up the interface functions in the device
873 struct sound_device
*sd
;
877 sd
->close
= vox_close
;
878 sd
->configure
= vox_configure
;
879 sd
->choose_format
= vox_choose_format
;
880 sd
->write
= vox_write
;
883 /* Write NBYTES bytes from BUFFER to device SD. */
886 vox_write (sd
, buffer
, nbytes
)
887 struct sound_device
*sd
;
891 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
893 sound_perror ("Error writing to sound device");
896 /* END: Non Windows functions */
897 #else /* WINDOWSNT */
899 /* BEGIN: Windows specific functions */
902 do_play_sound (psz_file
, ui_volume
)
903 const char *psz_file
;
904 unsigned long ui_volume
;
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
;
914 memset (sz_cmd_buf
, 0, sizeof(sz_cmd_buf
));
915 memset (sz_ret_buf
, 0, sizeof(sz_ret_buf
));
917 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
919 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
922 sound_warning ("The open mciSendString command failed to open\n"
923 "the specified sound file");
924 i_result
= (int) mci_error
;
927 if ((ui_volume
> 0) && (ui_volume
!= UINT_MAX
))
929 mm_result
= waveOutGetVolume ((HWAVEOUT
) WAVE_MAPPER
, &ui_volume_org
);
930 if (mm_result
== MMSYSERR_NOERROR
)
932 b_reset_volume
= TRUE
;
933 mm_result
= waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume
);
934 if ( mm_result
!= MMSYSERR_NOERROR
)
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"
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"
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
);
956 sound_warning ("The play mciSendString command failed to play the\n"
957 "opened sound file.");
958 i_result
= (int) mci_error
;
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
)
966 mm_result
=waveOutSetVolume ((HWAVEOUT
) WAVE_MAPPER
, ui_volume_org
);
967 if (mm_result
!= MMSYSERR_NOERROR
)
969 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
970 "level of the WAVE_MAPPER device.");
976 /* END: Windows specific functions */
978 #endif /* WINDOWSNT */
980 DEFUN ("play-sound-internal", Fplay_sound_internal
, Splay_sound_internal
, 1, 1, 0,
981 doc
: /* Play sound SOUND.
983 Internal use only, use `play-sound' instead.\n */)
987 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
988 int count
= SPECPDL_INDEX ();
992 struct gcpro gcpro1
, gcpro2
;
993 struct sound_device sd
;
996 #else /* WINDOWSNT */
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
;
1003 #endif /* WINDOWSNT */
1005 /* Parse the sound specification. Give up if it is invalid. */
1006 if (!parse_sound (sound
, attrs
))
1007 error ("Invalid sound specification");
1011 GCPRO2 (sound
, file
);
1012 bzero (&sd
, sizeof sd
);
1013 bzero (&s
, sizeof s
);
1014 current_sound_device
= &sd
;
1016 record_unwind_protect (sound_cleanup
, Qnil
);
1017 s
.header
= (char *) alloca (MAX_SOUND_HEADER_BYTES
);
1019 if (STRINGP (attrs
[SOUND_FILE
]))
1021 /* Open the sound file. */
1022 s
.fd
= openp (Fcons (Vdata_directory
, Qnil
),
1023 attrs
[SOUND_FILE
], Qnil
, &file
, Qnil
);
1025 sound_perror ("Could not open sound file");
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");
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
);
1039 /* Find out the type of sound. Give up if we can't tell. */
1040 find_sound_type (&s
);
1042 /* Set up a device. */
1043 if (STRINGP (attrs
[SOUND_DEVICE
]))
1045 int len
= SCHARS (attrs
[SOUND_DEVICE
]);
1046 sd
.file
= (char *) alloca (len
+ 1);
1047 strcpy (sd
.file
, SDATA (attrs
[SOUND_DEVICE
]));
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;
1055 args
[0] = Qplay_sound_functions
;
1057 Frun_hook_with_args (2, args
);
1059 /* There is only one type of device we currently support, the VOX
1060 sound driver. Set up the device interface functions for that
1064 /* Open the device. */
1067 /* Play the sound. */
1070 /* Close the input file, if any. */
1071 if (!STRINGP (s
.data
))
1077 /* Close the device. */
1081 current_sound_device
= NULL
;
1082 current_sound
= NULL
;
1085 #else /* WINDOWSNT */
1087 lo_file
= Fexpand_file_name (attrs
[SOUND_FILE
], Qnil
);
1088 len
= XSTRING (lo_file
)->size
;
1089 psz_file
= (char *) alloca (len
+ 1);
1090 strcpy (psz_file
, XSTRING (lo_file
)->data
);
1091 if (INTEGERP (attrs
[SOUND_VOLUME
]))
1093 ui_volume_tmp
= XFASTINT (attrs
[SOUND_VOLUME
]);
1095 else if (FLOATP (attrs
[SOUND_VOLUME
]))
1097 ui_volume_tmp
= (unsigned long) XFLOAT_DATA (attrs
[SOUND_VOLUME
]) * 100;
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
1105 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1106 The following code adjusts the user specified volume level appropriately.
1108 if ((ui_volume_tmp
> 0) && (ui_volume_tmp
<= 100))
1110 ui_volume
= ui_volume_tmp
* (UINT_MAX
/ 100);
1112 i_result
= do_play_sound (psz_file
, ui_volume
);
1114 #endif /* WINDOWSNT */
1116 unbind_to (count
, Qnil
);
1120 /***********************************************************************
1122 ***********************************************************************/
1127 QCdevice
= intern (":device");
1128 staticpro (&QCdevice
);
1129 QCvolume
= intern (":volume");
1130 staticpro (&QCvolume
);
1131 Qsound
= intern ("sound");
1132 staticpro (&Qsound
);
1133 Qplay_sound_functions
= intern ("play-sound-functions");
1134 staticpro (&Qplay_sound_functions
);
1136 defsubr (&Splay_sound_internal
);
1145 #endif /* HAVE_SOUND */