+/* Write NBYTES bytes from BUFFER to device SD. */
+
+static void
+alsa_write (sd, buffer, nbytes)
+ struct sound_device *sd;
+ const char *buffer;
+ int nbytes;
+{
+ struct alsa_params *p = (struct alsa_params *) sd->data;
+
+ /* The the third parameter to snd_pcm_writei is frames, not bytes. */
+ int fact = snd_pcm_format_size (sd->format, 1) * sd->channels;
+ int nwritten = 0;
+ int err;
+
+ while (nwritten < nbytes)
+ {
+ snd_pcm_uframes_t frames = (nbytes - nwritten)/fact;
+ if (frames == 0) break;
+
+ err = snd_pcm_writei (p->handle, buffer + nwritten, frames);
+ if (err < 0)
+ {
+ if (err == -EPIPE)
+ { /* under-run */
+ err = snd_pcm_prepare (p->handle);
+ if (err < 0)
+ alsa_sound_perror ("Can't recover from underrun, prepare failed",
+ err);
+ }
+ else if (err == -ESTRPIPE)
+ {
+ while ((err = snd_pcm_resume (p->handle)) == -EAGAIN)
+ sleep(1); /* wait until the suspend flag is released */
+ if (err < 0)
+ {
+ err = snd_pcm_prepare (p->handle);
+ if (err < 0)
+ alsa_sound_perror ("Can't recover from suspend, "
+ "prepare failed",
+ err);
+ }
+ }
+ else
+ alsa_sound_perror ("Error writing to sound device", err);
+
+ }
+ else
+ nwritten += err * fact;
+ }
+}
+
+static void
+snd_error_quiet (file, line, function, err, fmt)
+ const char *file;
+ int line;
+ const char *function;
+ int err;
+ const char *fmt;
+{
+}
+
+/* Initialize device SD. Set up the interface functions in the device
+ structure. */
+
+static int
+alsa_init (sd)
+ struct sound_device *sd;
+{
+ char *file;
+ snd_pcm_t *handle;
+ int err;
+
+ /* Open the sound device. Default is "default". */
+ if (sd->file)
+ file = sd->file;
+ else
+ file = DEFAULT_ALSA_SOUND_DEVICE;
+
+ snd_lib_error_set_handler ((snd_lib_error_handler_t) snd_error_quiet);
+ err = snd_pcm_open (&handle, file, SND_PCM_STREAM_PLAYBACK, 0);
+ snd_lib_error_set_handler (NULL);
+ if (err < 0)
+ return 0;
+ snd_pcm_close (handle);
+
+ sd->fd = -1;
+ sd->open = alsa_open;
+ sd->close = alsa_close;
+ sd->configure = alsa_configure;
+ sd->choose_format = alsa_choose_format;
+ sd->write = alsa_write;
+ sd->period_size = alsa_period_size;
+
+ return 1;
+}
+
+#endif /* HAVE_ALSA */
+
+
+/* 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. */)
+ (sound)
+ Lisp_Object sound;
+{
+ Lisp_Object attrs[SOUND_ATTR_SENTINEL];
+ int count = SPECPDL_INDEX ();
+
+#ifndef WINDOWSNT
+ Lisp_Object file;
+ struct gcpro gcpro1, gcpro2;
+ 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);
+ current_sound_device = (struct sound_device *) xmalloc (sizeof (struct sound_device));
+ bzero (current_sound_device, sizeof (struct sound_device));
+ current_sound = (struct sound *) xmalloc (sizeof (struct sound));
+ bzero (current_sound, sizeof (struct sound));
+ record_unwind_protect (sound_cleanup, Qnil);
+ current_sound->header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
+
+ if (STRINGP (attrs[SOUND_FILE]))
+ {
+ /* Open the sound file. */
+ current_sound->fd = openp (Fcons (Vdata_directory, Qnil),
+ attrs[SOUND_FILE], Qnil, &file, Qnil);
+ if (current_sound->fd < 0)
+ sound_perror ("Could not open sound file");
+
+ /* Read the first bytes from the file. */
+ current_sound->header_size
+ = emacs_read (current_sound->fd, current_sound->header,
+ MAX_SOUND_HEADER_BYTES);
+ if (current_sound->header_size < 0)
+ sound_perror ("Invalid sound file header");
+ }
+ else
+ {
+ current_sound->data = attrs[SOUND_DATA];
+ current_sound->header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (current_sound->data));
+ bcopy (SDATA (current_sound->data), current_sound->header, current_sound->header_size);
+ }
+
+ /* Find out the type of sound. Give up if we can't tell. */
+ find_sound_type (current_sound);
+
+ /* Set up a device. */
+ if (STRINGP (attrs[SOUND_DEVICE]))
+ {
+ int len = SCHARS (attrs[SOUND_DEVICE]);
+ current_sound_device->file = (char *) alloca (len + 1);
+ strcpy (current_sound_device->file, SDATA (attrs[SOUND_DEVICE]));
+ }
+
+ if (INTEGERP (attrs[SOUND_VOLUME]))
+ current_sound_device->volume = XFASTINT (attrs[SOUND_VOLUME]);
+ else if (FLOATP (attrs[SOUND_VOLUME]))
+ current_sound_device->volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
+
+ args[0] = Qplay_sound_functions;
+ args[1] = sound;
+ Frun_hook_with_args (2, args);
+
+#ifdef HAVE_ALSA
+ if (!alsa_init (current_sound_device))
+#endif
+ if (!vox_init (current_sound_device))
+ error ("No usable sound device driver found");
+
+ /* Open the device. */
+ current_sound_device->open (current_sound_device);
+
+ /* Play the sound. */
+ current_sound->play (current_sound, current_sound_device);
+
+ /* Clean up. */
+ 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);
+
+#endif /* WINDOWSNT */
+
+ unbind_to (count, Qnil);
+ return Qnil;
+}