+/* END: Non Windows functions */
+#else /* WINDOWSNT */
+
+/* BEGIN: Windows specific functions */
+
+static int
+do_play_sound (psz_file, ui_volume)
+ const char *psz_file;
+ unsigned long ui_volume;
+{
+ int i_result = 0;
+ MCIERROR mci_error = 0;
+ char sz_cmd_buf[520] = {0};
+ char sz_ret_buf[520] = {0};
+ MMRESULT mm_result = MMSYSERR_NOERROR;
+ unsigned long ui_volume_org = 0;
+ BOOL b_reset_volume = FALSE;
+
+ memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
+ memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
+ sprintf (sz_cmd_buf,
+ "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
+ psz_file);
+ mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
+ if (mci_error != 0)
+ {
+ sound_warning ("The open mciSendString command failed to open\n"
+ "the specified sound file");
+ i_result = (int) mci_error;
+ return i_result;
+ }
+ if ((ui_volume > 0) && (ui_volume != UINT_MAX))
+ {
+ mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
+ if (mm_result == MMSYSERR_NOERROR)
+ {
+ b_reset_volume = TRUE;
+ mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
+ if ( mm_result != MMSYSERR_NOERROR)
+ {
+ sound_warning ("waveOutSetVolume failed to set the volume level\n"
+ "of the WAVE_MAPPER device.\n"
+ "As a result, the user selected volume level will\n"
+ "not be used.");
+ }
+ }
+ else
+ {
+ sound_warning ("waveOutGetVolume failed to obtain the original\n"
+ "volume level of the WAVE_MAPPER device.\n"
+ "As a result, the user selected volume level will\n"
+ "not be used.");
+ }
+ }
+ memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
+ memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
+ strcpy (sz_cmd_buf, "play GNUEmacs_PlaySound_Device wait");
+ mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
+ if (mci_error != 0)
+ {
+ sound_warning ("The play mciSendString command failed to play the\n"
+ "opened sound file.");
+ i_result = (int) mci_error;
+ }
+ memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
+ memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
+ strcpy (sz_cmd_buf, "close GNUEmacs_PlaySound_Device wait");
+ mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
+ if (b_reset_volume == TRUE)
+ {
+ mm_result=waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
+ if (mm_result != MMSYSERR_NOERROR)
+ {
+ sound_warning ("waveOutSetVolume failed to reset the original volume\n"
+ "level of the WAVE_MAPPER device.");
+ }
+ }
+ return i_result;
+}
+
+/* END: Windows specific functions */
+
+#endif /* WINDOWSNT */
+
+DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
+ doc: /* Play sound SOUND.
+
+Internal use only, use `play-sound' instead.\n */)
+ (sound)
+ Lisp_Object sound;
+{
+ Lisp_Object attrs[SOUND_ATTR_SENTINEL];
+ int count = SPECPDL_INDEX ();
+
+#ifndef WINDOWSNT
+ Lisp_Object file;
+ struct gcpro gcpro1, gcpro2;
+ struct sound_device sd;
+ struct sound s;
+ Lisp_Object args[2];
+#else /* WINDOWSNT */
+ int len = 0;
+ Lisp_Object lo_file = {0};
+ char * psz_file = NULL;
+ unsigned long ui_volume_tmp = UINT_MAX;
+ unsigned long ui_volume = UINT_MAX;
+ int i_result = 0;
+#endif /* WINDOWSNT */
+
+ /* Parse the sound specification. Give up if it is invalid. */
+ if (!parse_sound (sound, attrs))
+ error ("Invalid sound specification");
+
+#ifndef WINDOWSNT
+ file = Qnil;
+ GCPRO2 (sound, file);
+ bzero (&sd, sizeof sd);
+ bzero (&s, sizeof s);
+ current_sound_device = &sd;
+ current_sound = &s;
+ record_unwind_protect (sound_cleanup, Qnil);
+ s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
+
+ if (STRINGP (attrs[SOUND_FILE]))
+ {
+ /* Open the sound file. */
+ s.fd = openp (Fcons (Vdata_directory, Qnil),
+ attrs[SOUND_FILE], Qnil, &file, Qnil);
+ if (s.fd < 0)
+ sound_perror ("Could not open sound file");
+
+ /* Read the first bytes from the file. */
+ s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
+ if (s.header_size < 0)
+ sound_perror ("Invalid sound file header");
+ }
+ else
+ {
+ s.data = attrs[SOUND_DATA];
+ s.header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (s.data));
+ bcopy (SDATA (s.data), s.header, s.header_size);
+ }
+
+ /* Find out the type of sound. Give up if we can't tell. */
+ find_sound_type (&s);
+
+ /* Set up a device. */
+ if (STRINGP (attrs[SOUND_DEVICE]))
+ {
+ int len = SCHARS (attrs[SOUND_DEVICE]);
+ sd.file = (char *) alloca (len + 1);
+ strcpy (sd.file, SDATA (attrs[SOUND_DEVICE]));
+ }
+
+ if (INTEGERP (attrs[SOUND_VOLUME]))
+ sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
+ else if (FLOATP (attrs[SOUND_VOLUME]))
+ sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
+
+ args[0] = Qplay_sound_functions;
+ args[1] = sound;
+ Frun_hook_with_args (2, args);
+
+ /* There is only one type of device we currently support, the VOX
+ sound driver. Set up the device interface functions for that
+ device. */
+ vox_init (&sd);
+
+ /* Open the device. */
+ sd.open (&sd);
+
+ /* Play the sound. */
+ s.play (&s, &sd);
+
+ /* Close the input file, if any. */
+ if (!STRINGP (s.data))
+ {
+ emacs_close (s.fd);
+ s.fd = -1;
+ }
+
+ /* Close the device. */
+ sd.close (&sd);
+
+ /* Clean up. */
+ current_sound_device = NULL;
+ current_sound = NULL;
+ UNGCPRO;
+
+#else /* WINDOWSNT */
+
+ lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil);
+ len = XSTRING (lo_file)->size;
+ psz_file = (char *) alloca (len + 1);
+ strcpy (psz_file, XSTRING (lo_file)->data);
+ if (INTEGERP (attrs[SOUND_VOLUME]))
+ {
+ ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
+ }
+ else if (FLOATP (attrs[SOUND_VOLUME]))
+ {
+ ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
+ }
+ /*
+ Based on some experiments I have conducted, a value of 100 or less
+ for the sound volume is much too low. You cannot even hear it.
+ A value of UINT_MAX indicates that you wish for the sound to played
+ at the maximum possible volume. A value of UINT_MAX/2 plays the
+ sound at 50% maximum volume. Therefore the value passed to do_play_sound
+ (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
+ The following code adjusts the user specified volume level appropriately.
+ */
+ if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
+ {
+ ui_volume = ui_volume_tmp * (UINT_MAX / 100);
+ }
+ i_result = do_play_sound (psz_file, ui_volume);