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)
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.
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.
42 #if defined HAVE_SOUND
44 /* BEGIN: Common Includes */
47 #include <sys/types.h>
50 #include "dispextern.h"
53 #include "syssignal.h"
54 /* END: Common Includes */
57 /* BEGIN: Non Windows Includes */
61 #include <sys/ioctl.h>
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>
70 #ifdef HAVE_SYS_SOUNDCARD_H
71 #include <sys/soundcard.h>
73 #ifdef HAVE_SOUNDCARD_H
74 #include <soundcard.h>
76 /* END: Non Windows Includes */
80 /* BEGIN: Windows Specific Includes */
87 /* END: Windows Specific Includes */
89 #endif /* WINDOWSNT */
91 /* BEGIN: Common Definitions */
92 #define abs(X) ((X) < 0 ? -(X) : (X))
96 extern Lisp_Object QCfile
, QCdata
;
97 Lisp_Object QCvolume
, QCdevice
;
99 Lisp_Object Qplay_sound_functions
;
101 /* Indices of attributes in a sound attributes vector. */
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 */
121 #ifndef DEFAULT_SOUND_DEVICE
122 #define DEFAULT_SOUND_DEVICE "/dev/dsp"
126 /* Structure forward declarations. */
131 /* The file header of RIFF-WAVE files (*.wav). Files are always in
132 little-endian byte-order. */
138 u_int32_t chunk_type
;
139 u_int32_t chunk_format
;
140 u_int32_t chunk_length
;
143 u_int32_t sample_rate
;
144 u_int32_t bytes_per_second
;
145 u_int16_t sample_size
;
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. */
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. */
165 /* Data encoding format.
167 2 8-bit linear PCM (REF-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
177 /* Number of samples per second. */
178 u_int32_t sample_rate
;
180 /* Number of interleaved 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. */
193 /* The name of the device or null meaning use a default device name. */
196 /* File descriptor of the device. */
199 /* Device-dependent format. */
202 /* Volume (0..100). Zero means unspecified. */
211 /* Bytes per second. */
214 /* 1 = mono, 2 = stereo, 0 = don't set. */
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
,
230 /* Write NYBTES bytes from BUFFER to device SD. */
231 void (* write
) P_ ((struct sound_device
*sd
, const char *buffer
,
234 /* A place for devices to store additional data. */
238 /* An enumerator for each supported sound file type. */
246 /* Interface structure for sound files. */
250 /* The type of the file. */
251 enum sound_type type
;
253 /* File descriptor of a sound file. */
256 /* Pointer to sound file header. This contains header_size bytes
257 read from the start of a sound file. */
260 /* Number of bytes raed from sound file. This is always <=
261 MAX_SOUND_HEADER_BYTES. */
264 /* Sound data, if a string. */
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
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
));
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 /***********************************************************************
310 ***********************************************************************/
312 /* BEGIN: Common functions */
314 /* Like perror, but signals an error. */
320 int saved_errno
= errno
;
324 sigunblock (sigmask (SIGIO
));
326 if (saved_errno
!= 0)
327 error ("%s: %s", msg
, strerror (saved_errno
));
333 /* Display a warning message. */
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:
351 FILE is the sound file to play. If it isn't an absolute name,
352 it's searched under `data-directory'.
356 DATA is a string containing sound data. Either :file or :data
357 may be present, but not both.
361 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
362 If not specified, a default device is used.
366 VOL must be an integer in the range [0, 100], or a float in the
370 parse_sound (sound
, attrs
)
374 /* SOUND must be a list starting with the symbol `sound'. */
375 if (!CONSP (sound
) || !EQ (XCAR (sound
), Qsound
))
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
);
385 /* File name or data must be specified. */
386 if (!STRINGP (attrs
[SOUND_FILE
])
387 && !STRINGP (attrs
[SOUND_DATA
]))
389 #else /* WINDOWSNT */
391 Data is not supported in Windows. Therefore a
392 File name MUST be supplied.
394 if (!STRINGP (attrs
[SOUND_FILE
]))
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)
409 else if (FLOATP (attrs
[SOUND_VOLUME
]))
411 if (XFLOAT_DATA (attrs
[SOUND_VOLUME
]) < 0
412 || XFLOAT_DATA (attrs
[SOUND_VOLUME
]) > 1)
420 /* Device must be a string or unspecified. */
421 if (!NILP (attrs
[SOUND_DEVICE
])
422 && !STRINGP (attrs
[SOUND_DEVICE
]))
424 #endif /* WINDOWSNT */
426 Since device is ignored in Windows, it does not matter
432 /* END: Common functions */
434 /* BEGIN: Non Windows functions */
437 /* Find out the type of the sound file whose file descriptor is FD.
438 S is the sound file structure to fill in. */
444 if (!wav_init (s
) && !au_init (s
))
445 error ("Unknown sound format");
449 /* Function installed by play-sound-internal with record_unwind_protect. */
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
);
466 /***********************************************************************
467 Byte-order Conversion
468 ***********************************************************************/
470 /* Convert 32-bit value VALUE which is in little-endian byte-order
471 to host byte-order. */
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);
485 /* Convert 16-bit value VALUE which is in little-endian byte-order
486 to host byte-order. */
492 #ifdef WORDS_BIG_ENDIAN
493 unsigned char *p
= (unsigned char *) &value
;
494 value
= p
[0] + (p
[1] << 8);
500 /* Convert 32-bit value VALUE which is in big-endian byte-order
501 to host byte-order. */
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);
515 #if 0 /* Currently not used. */
517 /* Convert 16-bit value VALUE which is in big-endian byte-order
518 to host byte-order. */
524 #ifndef WORDS_BIG_ENDIAN
525 unsigned char *p
= (unsigned char *) &value
;
526 value
= p
[1] + (p
[0] << 8);
533 /***********************************************************************
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. */
547 struct wav_header
*header
= (struct wav_header
*) s
->header
;
549 if (s
->header_size
< sizeof *header
550 || bcmp (s
->header
, "RIFF", 4) != 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. */
577 /* Play RIFF-WAVE audio file S on sound device SD. */
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
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
;
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
);
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
);
617 sound_perror ("Error reading sound file");
622 /***********************************************************************
624 ***********************************************************************/
626 /* Sun audio file encodings. */
630 AU_ENCODING_ULAW_8
= 1,
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. */
651 struct au_header
*header
= (struct au_header
*) s
->header
;
653 if (s
->header_size
< sizeof *header
654 || bcmp (s
->header
, ".snd", 4) != 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. */
672 /* Play Sun audio file S on sound device SD. */
677 struct sound_device
*sd
;
679 struct au_header
*header
= (struct au_header
*) s
->header
;
682 sd
->sample_rate
= header
->sample_rate
;
684 sd
->channels
= header
->channels
;
685 sd
->choose_format (sd
, s
);
688 if (STRINGP (s
->data
))
689 sd
->write (sd
, SDATA (s
->data
) + header
->data_offset
,
690 SBYTES (s
->data
) - header
->data_offset
);
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
);
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. */
724 struct sound_device
*sd
;
728 /* Open the sound device. Default is /dev/dsp. */
732 file
= DEFAULT_SOUND_DEVICE
;
734 sd
->fd
= emacs_open (file
, O_WRONLY
, 0);
740 /* Configure device SD from parameters in it. */
744 struct sound_device
*sd
;
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
755 sigblock (sigmask (SIGIO
));
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");
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
);
789 sigunblock (sigmask (SIGIO
));
794 /* Close device SD if it is open. */
798 struct sound_device
*sd
;
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
806 sigblock (sigmask (SIGIO
));
810 /* Flush sound data, and reset the device. */
811 ioctl (sd
->fd
, SNDCTL_DSP_SYNC
, NULL
);
815 sigunblock (sigmask (SIGIO
));
818 /* Close the device. */
819 emacs_close (sd
->fd
);
825 /* Choose device-dependent format for device SD from sound file S. */
828 vox_choose_format (sd
, s
)
829 struct sound_device
*sd
;
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
;
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
;
857 sd
->format
= AFMT_S16_LE
;
861 error ("Unsupported AU file format");
869 /* Initialize device SD. Set up the interface functions in the device
874 struct sound_device
*sd
;
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. */
887 vox_write (sd
, buffer
, nbytes
)
888 struct sound_device
*sd
;
892 int nwritten
= emacs_write (sd
->fd
, buffer
, nbytes
);
894 sound_perror ("Error writing to sound device");
897 /* END: Non Windows functions */
898 #else /* WINDOWSNT */
900 /* BEGIN: Windows specific functions */
903 do_play_sound (psz_file
, ui_volume
)
904 const char *psz_file
;
905 unsigned long ui_volume
;
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
));
918 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
920 mci_error
= mciSendString (sz_cmd_buf
, sz_ret_buf
, 520, NULL
);
923 sound_warning ("The open mciSendString command failed to open\n"
924 "the specified sound file");
925 i_result
= (int) mci_error
;
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"
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"
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
);
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.");
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 */)
988 Lisp_Object attrs
[SOUND_ATTR_SENTINEL
];
989 int count
= SPECPDL_INDEX ();
993 struct gcpro gcpro1
, gcpro2
;
994 struct sound_device sd
;
997 #else /* WINDOWSNT */
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
;
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");
1012 GCPRO2 (sound
, file
);
1013 bzero (&sd
, sizeof sd
);
1014 bzero (&s
, sizeof s
);
1015 current_sound_device
= &sd
;
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
);
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");
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
;
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
1065 /* Open the device. */
1068 /* Play the sound. */
1071 /* Close the input file, if any. */
1072 if (!STRINGP (s
.data
))
1078 /* Close the device. */
1082 current_sound_device
= NULL
;
1083 current_sound
= NULL
;
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
);
1121 /***********************************************************************
1123 ***********************************************************************/
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
);
1146 #endif /* HAVE_SOUND */
1148 /* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1149 (do not change this comment) */