]> code.delx.au - gnu-emacs/blob - src/sound.c
Merge from origin/emacs-24
[gnu-emacs] / src / sound.c
1 /* sound.c -- sound support.
2
3 Copyright (C) 1998-1999, 2001-2015 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's
21 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */
22
23 /*
24 Modified by Ben Key <Bkey1@tampabay.rr.com> to add a partial
25 implementation of the play-sound specification for Windows.
26
27 Notes:
28 In the Windows implementation of play-sound-internal only the
29 :file and :volume keywords are supported. The :device keyword,
30 if present, is ignored. The :data keyword, if present, will
31 cause an error to be generated.
32
33 The Windows implementation of play-sound is implemented via the
34 Windows API functions mciSendString, waveOutGetVolume, and
35 waveOutSetVolume which are exported by Winmm.dll.
36 */
37
38 #include <config.h>
39
40 #if defined HAVE_SOUND
41
42 /* BEGIN: Common Includes */
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <errno.h>
47
48 #include "lisp.h"
49 #include "dispextern.h"
50 #include "atimer.h"
51 #include "syssignal.h"
52 /* END: Common Includes */
53
54
55 /* BEGIN: Non Windows Includes */
56 #ifndef WINDOWSNT
57
58 #include <byteswap.h>
59
60 #include <sys/ioctl.h>
61
62 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention
63 sys/soundcard.h. So, let's try whatever's there. */
64
65 #ifdef HAVE_MACHINE_SOUNDCARD_H
66 #include <machine/soundcard.h>
67 #endif
68 #ifdef HAVE_SYS_SOUNDCARD_H
69 #include <sys/soundcard.h>
70 #endif
71 #ifdef HAVE_SOUNDCARD_H
72 #include <soundcard.h>
73 #endif
74 #ifdef HAVE_ALSA
75 #ifdef ALSA_SUBDIR_INCLUDE
76 #include <alsa/asoundlib.h>
77 #else
78 #include <asoundlib.h>
79 #endif /* ALSA_SUBDIR_INCLUDE */
80 #endif /* HAVE_ALSA */
81
82 /* END: Non Windows Includes */
83
84 #else /* WINDOWSNT */
85
86 /* BEGIN: Windows Specific Includes */
87 #include <stdio.h>
88 #include <limits.h>
89 #include <mbstring.h>
90 #include <windows.h>
91 #include <mmsystem.h>
92
93 #include "coding.h"
94 #include "w32common.h"
95 #include "w32.h"
96 /* END: Windows Specific Includes */
97
98 #endif /* WINDOWSNT */
99
100 /* BEGIN: Common Definitions */
101
102 /* Indices of attributes in a sound attributes vector. */
103
104 enum sound_attr
105 {
106 SOUND_FILE,
107 SOUND_DATA,
108 SOUND_DEVICE,
109 SOUND_VOLUME,
110 SOUND_ATTR_SENTINEL
111 };
112
113 /* END: Common Definitions */
114
115 /* BEGIN: Non Windows Definitions */
116 #ifndef WINDOWSNT
117
118 /* Structure forward declarations. */
119
120 struct sound;
121 struct sound_device;
122
123 /* The file header of RIFF-WAVE files (*.wav). Files are always in
124 little-endian byte-order. */
125
126 struct wav_header
127 {
128 u_int32_t magic;
129 u_int32_t length;
130 u_int32_t chunk_type;
131 u_int32_t chunk_format;
132 u_int32_t chunk_length;
133 u_int16_t format;
134 u_int16_t channels;
135 u_int32_t sample_rate;
136 u_int32_t bytes_per_second;
137 u_int16_t sample_size;
138 u_int16_t precision;
139 u_int32_t chunk_data;
140 u_int32_t data_length;
141 };
142
143 /* The file header of Sun adio files (*.au). Files are always in
144 big-endian byte-order. */
145
146 struct au_header
147 {
148 /* ASCII ".snd" */
149 u_int32_t magic_number;
150
151 /* Offset of data part from start of file. Minimum value is 24. */
152 u_int32_t data_offset;
153
154 /* Size of data part, 0xffffffff if unknown. */
155 u_int32_t data_size;
156
157 /* Data encoding format.
158 1 8-bit ISDN u-law
159 2 8-bit linear PCM (REF-PCM)
160 3 16-bit linear PCM
161 4 24-bit linear PCM
162 5 32-bit linear PCM
163 6 32-bit IEEE floating-point
164 7 64-bit IEEE floating-point
165 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
166 encoding scheme. */
167 u_int32_t encoding;
168
169 /* Number of samples per second. */
170 u_int32_t sample_rate;
171
172 /* Number of interleaved channels. */
173 u_int32_t channels;
174 };
175
176 /* Maximum of all sound file headers sizes. */
177
178 #define MAX_SOUND_HEADER_BYTES \
179 max (sizeof (struct wav_header), sizeof (struct au_header))
180
181 /* Interface structure for sound devices. */
182
183 struct sound_device
184 {
185 /* If a string, the name of the device; otherwise use a default. */
186 Lisp_Object file;
187
188 /* File descriptor of the device. */
189 int fd;
190
191 /* Device-dependent format. */
192 int format;
193
194 /* Volume (0..100). Zero means unspecified. */
195 int volume;
196
197 /* Sample size. */
198 int sample_size;
199
200 /* Sample rate. */
201 int sample_rate;
202
203 /* Bytes per second. */
204 int bps;
205
206 /* 1 = mono, 2 = stereo, 0 = don't set. */
207 int channels;
208
209 /* Open device SD. */
210 void (* open) (struct sound_device *sd);
211
212 /* Close device SD. */
213 void (* close) (struct sound_device *sd);
214
215 /* Configure SD according to device-dependent parameters. */
216 void (* configure) (struct sound_device *device);
217
218 /* Choose a device-dependent format for outputting sound S. */
219 void (* choose_format) (struct sound_device *sd,
220 struct sound *s);
221
222 /* Return a preferred data size in bytes to be sent to write (below)
223 each time. 2048 is used if this is NULL. */
224 ptrdiff_t (* period_size) (struct sound_device *sd);
225
226 /* Write NYBTES bytes from BUFFER to device SD. */
227 void (* write) (struct sound_device *sd, const char *buffer,
228 ptrdiff_t nbytes);
229
230 /* A place for devices to store additional data. */
231 void *data;
232 };
233
234 /* An enumerator for each supported sound file type. */
235
236 enum sound_type
237 {
238 RIFF,
239 SUN_AUDIO
240 };
241
242 /* Interface structure for sound files. */
243
244 struct sound
245 {
246 /* The type of the file. */
247 enum sound_type type;
248
249 /* File descriptor of a sound file. */
250 int fd;
251
252 /* Pointer to sound file header. This contains header_size bytes
253 read from the start of a sound file. */
254 char *header;
255
256 /* Number of bytes read from sound file. This is always <=
257 MAX_SOUND_HEADER_BYTES. */
258 int header_size;
259
260 /* Sound data, if a string. */
261 Lisp_Object data;
262
263 /* Play sound file S on device SD. */
264 void (* play) (struct sound *s, struct sound_device *sd);
265 };
266
267 /* These are set during `play-sound-internal' so that sound_cleanup has
268 access to them. */
269
270 static struct sound_device *current_sound_device;
271 static struct sound *current_sound;
272
273 /* Function prototypes. */
274
275 static void vox_write (struct sound_device *, const char *, ptrdiff_t);
276 static bool wav_init (struct sound *);
277 static void wav_play (struct sound *, struct sound_device *);
278 static bool au_init (struct sound *);
279 static void au_play (struct sound *, struct sound_device *);
280
281 /* END: Non Windows Definitions */
282 #else /* WINDOWSNT */
283
284 /* BEGIN: Windows Specific Definitions */
285 static int do_play_sound (const char *, unsigned long);
286 /*
287 END: Windows Specific Definitions */
288 #endif /* WINDOWSNT */
289
290 \f
291 /***********************************************************************
292 General
293 ***********************************************************************/
294
295 /* BEGIN: Common functions */
296
297 /* Like perror, but signals an error. */
298
299 static _Noreturn void
300 sound_perror (const char *msg)
301 {
302 int saved_errno = errno;
303
304 turn_on_atimers (1);
305 #ifdef USABLE_SIGIO
306 {
307 sigset_t unblocked;
308 sigemptyset (&unblocked);
309 sigaddset (&unblocked, SIGIO);
310 pthread_sigmask (SIG_UNBLOCK, &unblocked, 0);
311 }
312 #endif
313 if (saved_errno != 0)
314 error ("%s: %s", msg, strerror (saved_errno));
315 else
316 error ("%s", msg);
317 }
318
319
320 /* Display a warning message. */
321
322 static void
323 sound_warning (const char *msg)
324 {
325 message1 (msg);
326 }
327
328
329 /* Parse sound specification SOUND, and fill ATTRS with what is
330 found. Value is non-zero if SOUND Is a valid sound specification.
331 A valid sound specification is a list starting with the symbol
332 `sound'. The rest of the list is a property list which may
333 contain the following key/value pairs:
334
335 - `:file FILE'
336
337 FILE is the sound file to play. If it isn't an absolute name,
338 it's searched under `data-directory'.
339
340 - `:data DATA'
341
342 DATA is a string containing sound data. Either :file or :data
343 may be present, but not both.
344
345 - `:device DEVICE'
346
347 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
348 If not specified, a default device is used.
349
350 - `:volume VOL'
351
352 VOL must be an integer in the range [0, 100], or a float in the
353 range [0, 1]. */
354
355 static bool
356 parse_sound (Lisp_Object sound, Lisp_Object *attrs)
357 {
358 /* SOUND must be a list starting with the symbol `sound'. */
359 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
360 return 0;
361
362 sound = XCDR (sound);
363 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
364 attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
365 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
366 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
367
368 #ifndef WINDOWSNT
369 /* File name or data must be specified. */
370 if (!STRINGP (attrs[SOUND_FILE])
371 && !STRINGP (attrs[SOUND_DATA]))
372 return 0;
373 #else /* WINDOWSNT */
374 /*
375 Data is not supported in Windows. Therefore a
376 File name MUST be supplied.
377 */
378 if (!STRINGP (attrs[SOUND_FILE]))
379 {
380 return 0;
381 }
382 #endif /* WINDOWSNT */
383
384 /* Volume must be in the range 0..100 or unspecified. */
385 if (!NILP (attrs[SOUND_VOLUME]))
386 {
387 if (INTEGERP (attrs[SOUND_VOLUME]))
388 {
389 if (XINT (attrs[SOUND_VOLUME]) < 0
390 || XINT (attrs[SOUND_VOLUME]) > 100)
391 return 0;
392 }
393 else if (FLOATP (attrs[SOUND_VOLUME]))
394 {
395 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
396 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
397 return 0;
398 }
399 else
400 return 0;
401 }
402
403 #ifndef WINDOWSNT
404 /* Device must be a string or unspecified. */
405 if (!NILP (attrs[SOUND_DEVICE])
406 && !STRINGP (attrs[SOUND_DEVICE]))
407 return 0;
408 #endif /* WINDOWSNT */
409 /*
410 Since device is ignored in Windows, it does not matter
411 what it is.
412 */
413 return 1;
414 }
415
416 /* END: Common functions */
417
418 /* BEGIN: Non Windows functions */
419 #ifndef WINDOWSNT
420
421 /* Return S's value as a string if S is a string, otherwise DEFAULT_VALUE. */
422
423 static char const *
424 string_default (Lisp_Object s, char const *default_value)
425 {
426 return STRINGP (s) ? SSDATA (s) : default_value;
427 }
428
429
430 /* Find out the type of the sound file whose file descriptor is FD.
431 S is the sound file structure to fill in. */
432
433 static void
434 find_sound_type (struct sound *s)
435 {
436 if (!wav_init (s) && !au_init (s))
437 error ("Unknown sound format");
438 }
439
440
441 /* Function installed by play-sound-internal with record_unwind_protect_void. */
442
443 static void
444 sound_cleanup (void)
445 {
446 if (current_sound_device->close)
447 current_sound_device->close (current_sound_device);
448 if (current_sound->fd > 0)
449 emacs_close (current_sound->fd);
450 xfree (current_sound_device);
451 xfree (current_sound);
452 }
453
454 /***********************************************************************
455 Byte-order Conversion
456 ***********************************************************************/
457
458 /* Convert 32-bit value VALUE which is in little-endian byte-order
459 to host byte-order. */
460
461 static u_int32_t
462 le2hl (u_int32_t value)
463 {
464 #ifdef WORDS_BIGENDIAN
465 value = bswap_32 (value);
466 #endif
467 return value;
468 }
469
470
471 /* Convert 16-bit value VALUE which is in little-endian byte-order
472 to host byte-order. */
473
474 static u_int16_t
475 le2hs (u_int16_t value)
476 {
477 #ifdef WORDS_BIGENDIAN
478 value = bswap_16 (value);
479 #endif
480 return value;
481 }
482
483
484 /* Convert 32-bit value VALUE which is in big-endian byte-order
485 to host byte-order. */
486
487 static u_int32_t
488 be2hl (u_int32_t value)
489 {
490 #ifndef WORDS_BIGENDIAN
491 value = bswap_32 (value);
492 #endif
493 return value;
494 }
495
496 /***********************************************************************
497 RIFF-WAVE (*.wav)
498 ***********************************************************************/
499
500 /* Try to initialize sound file S from S->header. S->header
501 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
502 sound file. If the file is a WAV-format file, set up interface
503 functions in S and convert header fields to host byte-order.
504 Value is true if the file is a WAV file. */
505
506 static bool
507 wav_init (struct sound *s)
508 {
509 struct wav_header *header = (struct wav_header *) s->header;
510
511 if (s->header_size < sizeof *header
512 || memcmp (s->header, "RIFF", 4) != 0)
513 return 0;
514
515 /* WAV files are in little-endian order. Convert the header
516 if on a big-endian machine. */
517 header->magic = le2hl (header->magic);
518 header->length = le2hl (header->length);
519 header->chunk_type = le2hl (header->chunk_type);
520 header->chunk_format = le2hl (header->chunk_format);
521 header->chunk_length = le2hl (header->chunk_length);
522 header->format = le2hs (header->format);
523 header->channels = le2hs (header->channels);
524 header->sample_rate = le2hl (header->sample_rate);
525 header->bytes_per_second = le2hl (header->bytes_per_second);
526 header->sample_size = le2hs (header->sample_size);
527 header->precision = le2hs (header->precision);
528 header->chunk_data = le2hl (header->chunk_data);
529 header->data_length = le2hl (header->data_length);
530
531 /* Set up the interface functions for WAV. */
532 s->type = RIFF;
533 s->play = wav_play;
534
535 return 1;
536 }
537
538
539 /* Play RIFF-WAVE audio file S on sound device SD. */
540
541 static void
542 wav_play (struct sound *s, struct sound_device *sd)
543 {
544 struct wav_header *header = (struct wav_header *) s->header;
545
546 /* Let the device choose a suitable device-dependent format
547 for the file. */
548 sd->choose_format (sd, s);
549
550 /* Configure the device. */
551 sd->sample_size = header->sample_size;
552 sd->sample_rate = header->sample_rate;
553 sd->bps = header->bytes_per_second;
554 sd->channels = header->channels;
555 sd->configure (sd);
556
557 /* Copy sound data to the device. The WAV file specification is
558 actually more complex. This simple scheme worked with all WAV
559 files I found so far. If someone feels inclined to implement the
560 whole RIFF-WAVE spec, please do. */
561 if (STRINGP (s->data))
562 sd->write (sd, SSDATA (s->data) + sizeof *header,
563 SBYTES (s->data) - sizeof *header);
564 else
565 {
566 ptrdiff_t nbytes = 0;
567 ptrdiff_t blksize = sd->period_size ? sd->period_size (sd) : 2048;
568 ptrdiff_t data_left = header->data_length;
569 USE_SAFE_ALLOCA;
570 char *buffer = SAFE_ALLOCA (blksize);
571 lseek (s->fd, sizeof *header, SEEK_SET);
572 while (data_left > 0
573 && (nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
574 {
575 /* Don't play possible garbage at the end of file */
576 if (data_left < nbytes) nbytes = data_left;
577 data_left -= nbytes;
578 sd->write (sd, buffer, nbytes);
579 }
580
581 if (nbytes < 0)
582 sound_perror ("Error reading sound file");
583 SAFE_FREE ();
584 }
585 }
586
587
588 /***********************************************************************
589 Sun Audio (*.au)
590 ***********************************************************************/
591
592 /* Sun audio file encodings. */
593
594 enum au_encoding
595 {
596 AU_ENCODING_ULAW_8 = 1,
597 AU_ENCODING_8,
598 AU_ENCODING_16,
599 AU_ENCODING_24,
600 AU_ENCODING_32,
601 AU_ENCODING_IEEE32,
602 AU_ENCODING_IEEE64,
603 AU_COMPRESSED = 23,
604 AU_ENCODING_ALAW_8 = 27
605 };
606
607
608 /* Try to initialize sound file S from S->header. S->header
609 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
610 sound file. If the file is a AU-format file, set up interface
611 functions in S and convert header fields to host byte-order.
612 Value is true if the file is an AU file. */
613
614 static bool
615 au_init (struct sound *s)
616 {
617 struct au_header *header = (struct au_header *) s->header;
618
619 if (s->header_size < sizeof *header
620 || memcmp (s->header, ".snd", 4) != 0)
621 return 0;
622
623 header->magic_number = be2hl (header->magic_number);
624 header->data_offset = be2hl (header->data_offset);
625 header->data_size = be2hl (header->data_size);
626 header->encoding = be2hl (header->encoding);
627 header->sample_rate = be2hl (header->sample_rate);
628 header->channels = be2hl (header->channels);
629
630 /* Set up the interface functions for AU. */
631 s->type = SUN_AUDIO;
632 s->play = au_play;
633
634 return 1;
635 }
636
637
638 /* Play Sun audio file S on sound device SD. */
639
640 static void
641 au_play (struct sound *s, struct sound_device *sd)
642 {
643 struct au_header *header = (struct au_header *) s->header;
644
645 sd->sample_size = 0;
646 sd->sample_rate = header->sample_rate;
647 sd->bps = 0;
648 sd->channels = header->channels;
649 sd->choose_format (sd, s);
650 sd->configure (sd);
651
652 if (STRINGP (s->data))
653 sd->write (sd, SSDATA (s->data) + header->data_offset,
654 SBYTES (s->data) - header->data_offset);
655 else
656 {
657 ptrdiff_t blksize = sd->period_size ? sd->period_size (sd) : 2048;
658 ptrdiff_t nbytes;
659
660 /* Seek */
661 lseek (s->fd, header->data_offset, SEEK_SET);
662
663 /* Copy sound data to the device. */
664 USE_SAFE_ALLOCA;
665 char *buffer = SAFE_ALLOCA (blksize);
666 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
667 sd->write (sd, buffer, nbytes);
668
669 if (nbytes < 0)
670 sound_perror ("Error reading sound file");
671 SAFE_FREE ();
672 }
673 }
674
675
676 /***********************************************************************
677 Voxware Driver Interface
678 ***********************************************************************/
679
680 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
681 has a compatible own driver aka Luigi's driver. */
682
683
684 /* Open device SD. If SD->file is a string, open that device,
685 otherwise use a default device name. */
686
687 static void
688 vox_open (struct sound_device *sd)
689 {
690 /* Open the sound device (eg /dev/dsp). */
691 char const *file = string_default (sd->file, DEFAULT_SOUND_DEVICE);
692 sd->fd = emacs_open (file, O_WRONLY, 0);
693 if (sd->fd < 0)
694 sound_perror (file);
695 }
696
697
698 /* Configure device SD from parameters in it. */
699
700 static void
701 vox_configure (struct sound_device *sd)
702 {
703 int val;
704 #ifdef USABLE_SIGIO
705 sigset_t oldset, blocked;
706 #endif
707
708 eassert (sd->fd >= 0);
709
710 /* On GNU/Linux, it seems that the device driver doesn't like to be
711 interrupted by a signal. Block the ones we know to cause
712 troubles. */
713 turn_on_atimers (0);
714 #ifdef USABLE_SIGIO
715 sigemptyset (&blocked);
716 sigaddset (&blocked, SIGIO);
717 pthread_sigmask (SIG_BLOCK, &blocked, &oldset);
718 #endif
719
720 val = sd->format;
721 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
722 || val != sd->format)
723 sound_perror ("Could not set sound format");
724
725 val = sd->channels != 1;
726 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
727 || val != (sd->channels != 1))
728 sound_perror ("Could not set stereo/mono");
729
730 /* I think bps and sampling_rate are the same, but who knows.
731 Check this. and use SND_DSP_SPEED for both. */
732 if (sd->sample_rate > 0)
733 {
734 val = sd->sample_rate;
735 if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0)
736 sound_perror ("Could not set sound speed");
737 else if (val != sd->sample_rate)
738 sound_warning ("Could not set sample rate");
739 }
740
741 if (sd->volume > 0)
742 {
743 int volume = sd->volume & 0xff;
744 volume |= volume << 8;
745 /* This may fail if there is no mixer. Ignore the failure. */
746 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
747 }
748
749 turn_on_atimers (1);
750 #ifdef USABLE_SIGIO
751 pthread_sigmask (SIG_SETMASK, &oldset, 0);
752 #endif
753 }
754
755
756 /* Close device SD if it is open. */
757
758 static void
759 vox_close (struct sound_device *sd)
760 {
761 if (sd->fd >= 0)
762 {
763 /* On GNU/Linux, it seems that the device driver doesn't like to
764 be interrupted by a signal. Block the ones we know to cause
765 troubles. */
766 #ifdef USABLE_SIGIO
767 sigset_t blocked, oldset;
768 sigemptyset (&blocked);
769 sigaddset (&blocked, SIGIO);
770 pthread_sigmask (SIG_BLOCK, &blocked, &oldset);
771 #endif
772 turn_on_atimers (0);
773
774 /* Flush sound data, and reset the device. */
775 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
776
777 turn_on_atimers (1);
778 #ifdef USABLE_SIGIO
779 pthread_sigmask (SIG_SETMASK, &oldset, 0);
780 #endif
781
782 /* Close the device. */
783 emacs_close (sd->fd);
784 sd->fd = -1;
785 }
786 }
787
788
789 /* Choose device-dependent format for device SD from sound file S. */
790
791 static void
792 vox_choose_format (struct sound_device *sd, struct sound *s)
793 {
794 if (s->type == RIFF)
795 {
796 struct wav_header *h = (struct wav_header *) s->header;
797 if (h->precision == 8)
798 sd->format = AFMT_U8;
799 else if (h->precision == 16)
800 sd->format = AFMT_S16_LE;
801 else
802 error ("Unsupported WAV file format");
803 }
804 else if (s->type == SUN_AUDIO)
805 {
806 struct au_header *header = (struct au_header *) s->header;
807 switch (header->encoding)
808 {
809 case AU_ENCODING_ULAW_8:
810 case AU_ENCODING_IEEE32:
811 case AU_ENCODING_IEEE64:
812 sd->format = AFMT_MU_LAW;
813 break;
814
815 case AU_ENCODING_8:
816 case AU_ENCODING_16:
817 case AU_ENCODING_24:
818 case AU_ENCODING_32:
819 sd->format = AFMT_S16_LE;
820 break;
821
822 default:
823 error ("Unsupported AU file format");
824 }
825 }
826 else
827 emacs_abort ();
828 }
829
830
831 /* Initialize device SD. Set up the interface functions in the device
832 structure. */
833
834 static bool
835 vox_init (struct sound_device *sd)
836 {
837 /* Open the sound device (eg /dev/dsp). */
838 char const *file = string_default (sd->file, DEFAULT_SOUND_DEVICE);
839 int fd = emacs_open (file, O_WRONLY, 0);
840 if (fd >= 0)
841 emacs_close (fd);
842 else
843 return 0;
844
845 sd->fd = -1;
846 sd->open = vox_open;
847 sd->close = vox_close;
848 sd->configure = vox_configure;
849 sd->choose_format = vox_choose_format;
850 sd->write = vox_write;
851 sd->period_size = NULL;
852
853 return 1;
854 }
855
856 /* Write NBYTES bytes from BUFFER to device SD. */
857
858 static void
859 vox_write (struct sound_device *sd, const char *buffer, ptrdiff_t nbytes)
860 {
861 if (emacs_write_sig (sd->fd, buffer, nbytes) != nbytes)
862 sound_perror ("Error writing to sound device");
863 }
864
865 #ifdef HAVE_ALSA
866 /***********************************************************************
867 ALSA Driver Interface
868 ***********************************************************************/
869
870 /* This driver is available on GNU/Linux. */
871
872 #ifndef DEFAULT_ALSA_SOUND_DEVICE
873 #define DEFAULT_ALSA_SOUND_DEVICE "default"
874 #endif
875
876 static _Noreturn void
877 alsa_sound_perror (const char *msg, int err)
878 {
879 error ("%s: %s", msg, snd_strerror (err));
880 }
881
882 struct alsa_params
883 {
884 snd_pcm_t *handle;
885 snd_pcm_hw_params_t *hwparams;
886 snd_pcm_sw_params_t *swparams;
887 snd_pcm_uframes_t period_size;
888 };
889
890 /* Open device SD. If SD->file is a string, open that device,
891 otherwise use a default device name. */
892
893 static void
894 alsa_open (struct sound_device *sd)
895 {
896 /* Open the sound device. Default is "default". */
897 struct alsa_params *p = xmalloc (sizeof *p);
898 char const *file = string_default (sd->file, DEFAULT_ALSA_SOUND_DEVICE);
899 int err;
900
901 p->handle = NULL;
902 p->hwparams = NULL;
903 p->swparams = NULL;
904
905 sd->fd = -1;
906 sd->data = p;
907
908
909 err = snd_pcm_open (&p->handle, file, SND_PCM_STREAM_PLAYBACK, 0);
910 if (err < 0)
911 alsa_sound_perror (file, err);
912 }
913
914 static ptrdiff_t
915 alsa_period_size (struct sound_device *sd)
916 {
917 struct alsa_params *p = (struct alsa_params *) sd->data;
918 int fact = snd_pcm_format_size (sd->format, 1) * sd->channels;
919 return p->period_size * (fact > 0 ? fact : 1);
920 }
921
922 static void
923 alsa_configure (struct sound_device *sd)
924 {
925 int val, err, dir;
926 unsigned uval;
927 struct alsa_params *p = (struct alsa_params *) sd->data;
928 snd_pcm_uframes_t buffer_size;
929
930 eassert (p->handle != 0);
931
932 err = snd_pcm_hw_params_malloc (&p->hwparams);
933 if (err < 0)
934 alsa_sound_perror ("Could not allocate hardware parameter structure", err);
935
936 err = snd_pcm_sw_params_malloc (&p->swparams);
937 if (err < 0)
938 alsa_sound_perror ("Could not allocate software parameter structure", err);
939
940 err = snd_pcm_hw_params_any (p->handle, p->hwparams);
941 if (err < 0)
942 alsa_sound_perror ("Could not initialize hardware parameter structure", err);
943
944 err = snd_pcm_hw_params_set_access (p->handle, p->hwparams,
945 SND_PCM_ACCESS_RW_INTERLEAVED);
946 if (err < 0)
947 alsa_sound_perror ("Could not set access type", err);
948
949 val = sd->format;
950 err = snd_pcm_hw_params_set_format (p->handle, p->hwparams, val);
951 if (err < 0)
952 alsa_sound_perror ("Could not set sound format", err);
953
954 uval = sd->sample_rate;
955 err = snd_pcm_hw_params_set_rate_near (p->handle, p->hwparams, &uval, 0);
956 if (err < 0)
957 alsa_sound_perror ("Could not set sample rate", err);
958
959 val = sd->channels;
960 err = snd_pcm_hw_params_set_channels (p->handle, p->hwparams, val);
961 if (err < 0)
962 alsa_sound_perror ("Could not set channel count", err);
963
964 err = snd_pcm_hw_params (p->handle, p->hwparams);
965 if (err < 0)
966 alsa_sound_perror ("Could not set parameters", err);
967
968
969 err = snd_pcm_hw_params_get_period_size (p->hwparams, &p->period_size, &dir);
970 if (err < 0)
971 alsa_sound_perror ("Unable to get period size for playback", err);
972
973 err = snd_pcm_hw_params_get_buffer_size (p->hwparams, &buffer_size);
974 if (err < 0)
975 alsa_sound_perror ("Unable to get buffer size for playback", err);
976
977 err = snd_pcm_sw_params_current (p->handle, p->swparams);
978 if (err < 0)
979 alsa_sound_perror ("Unable to determine current swparams for playback",
980 err);
981
982 /* Start the transfer when the buffer is almost full */
983 err = snd_pcm_sw_params_set_start_threshold (p->handle, p->swparams,
984 (buffer_size / p->period_size)
985 * p->period_size);
986 if (err < 0)
987 alsa_sound_perror ("Unable to set start threshold mode for playback", err);
988
989 /* Allow the transfer when at least period_size samples can be processed */
990 err = snd_pcm_sw_params_set_avail_min (p->handle, p->swparams, p->period_size);
991 if (err < 0)
992 alsa_sound_perror ("Unable to set avail min for playback", err);
993
994 err = snd_pcm_sw_params (p->handle, p->swparams);
995 if (err < 0)
996 alsa_sound_perror ("Unable to set sw params for playback\n", err);
997
998 snd_pcm_hw_params_free (p->hwparams);
999 p->hwparams = NULL;
1000 snd_pcm_sw_params_free (p->swparams);
1001 p->swparams = NULL;
1002
1003 err = snd_pcm_prepare (p->handle);
1004 if (err < 0)
1005 alsa_sound_perror ("Could not prepare audio interface for use", err);
1006
1007 if (sd->volume > 0)
1008 {
1009 int chn;
1010 snd_mixer_t *handle;
1011 snd_mixer_elem_t *e;
1012 if (snd_mixer_open (&handle, 0) >= 0)
1013 {
1014 char const *file = string_default (sd->file,
1015 DEFAULT_ALSA_SOUND_DEVICE);
1016 if (snd_mixer_attach (handle, file) >= 0
1017 && snd_mixer_load (handle) >= 0
1018 && snd_mixer_selem_register (handle, NULL, NULL) >= 0)
1019 for (e = snd_mixer_first_elem (handle);
1020 e;
1021 e = snd_mixer_elem_next (e))
1022 {
1023 if (snd_mixer_selem_has_playback_volume (e))
1024 {
1025 long pmin, pmax, vol;
1026 snd_mixer_selem_get_playback_volume_range (e, &pmin, &pmax);
1027 vol = pmin + (sd->volume * (pmax - pmin)) / 100;
1028
1029 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++)
1030 snd_mixer_selem_set_playback_volume (e, chn, vol);
1031 }
1032 }
1033 snd_mixer_close (handle);
1034 }
1035 }
1036 }
1037
1038
1039 /* Close device SD if it is open. */
1040
1041 static void
1042 alsa_close (struct sound_device *sd)
1043 {
1044 struct alsa_params *p = (struct alsa_params *) sd->data;
1045 if (p)
1046 {
1047 if (p->hwparams)
1048 snd_pcm_hw_params_free (p->hwparams);
1049 if (p->swparams)
1050 snd_pcm_sw_params_free (p->swparams);
1051 if (p->handle)
1052 {
1053 snd_pcm_drain (p->handle);
1054 snd_pcm_close (p->handle);
1055 }
1056 xfree (p);
1057 }
1058 }
1059
1060 /* Choose device-dependent format for device SD from sound file S. */
1061
1062 static void
1063 alsa_choose_format (struct sound_device *sd, struct sound *s)
1064 {
1065 if (s->type == RIFF)
1066 {
1067 struct wav_header *h = (struct wav_header *) s->header;
1068 if (h->precision == 8)
1069 sd->format = SND_PCM_FORMAT_U8;
1070 else if (h->precision == 16)
1071 sd->format = SND_PCM_FORMAT_S16_LE;
1072 else
1073 error ("Unsupported WAV file format");
1074 }
1075 else if (s->type == SUN_AUDIO)
1076 {
1077 struct au_header *header = (struct au_header *) s->header;
1078 switch (header->encoding)
1079 {
1080 case AU_ENCODING_ULAW_8:
1081 sd->format = SND_PCM_FORMAT_MU_LAW;
1082 break;
1083 case AU_ENCODING_ALAW_8:
1084 sd->format = SND_PCM_FORMAT_A_LAW;
1085 break;
1086 case AU_ENCODING_IEEE32:
1087 sd->format = SND_PCM_FORMAT_FLOAT_BE;
1088 break;
1089 case AU_ENCODING_IEEE64:
1090 sd->format = SND_PCM_FORMAT_FLOAT64_BE;
1091 break;
1092 case AU_ENCODING_8:
1093 sd->format = SND_PCM_FORMAT_S8;
1094 break;
1095 case AU_ENCODING_16:
1096 sd->format = SND_PCM_FORMAT_S16_BE;
1097 break;
1098 case AU_ENCODING_24:
1099 sd->format = SND_PCM_FORMAT_S24_BE;
1100 break;
1101 case AU_ENCODING_32:
1102 sd->format = SND_PCM_FORMAT_S32_BE;
1103 break;
1104
1105 default:
1106 error ("Unsupported AU file format");
1107 }
1108 }
1109 else
1110 emacs_abort ();
1111 }
1112
1113
1114 /* Write NBYTES bytes from BUFFER to device SD. */
1115
1116 static void
1117 alsa_write (struct sound_device *sd, const char *buffer, ptrdiff_t nbytes)
1118 {
1119 struct alsa_params *p = (struct alsa_params *) sd->data;
1120
1121 /* The the third parameter to snd_pcm_writei is frames, not bytes. */
1122 int fact = snd_pcm_format_size (sd->format, 1) * sd->channels;
1123 ptrdiff_t nwritten = 0;
1124 int err;
1125
1126 while (nwritten < nbytes)
1127 {
1128 snd_pcm_uframes_t frames = (nbytes - nwritten)/fact;
1129 if (frames == 0) break;
1130
1131 err = snd_pcm_writei (p->handle, buffer + nwritten, frames);
1132 if (err < 0)
1133 {
1134 if (err == -EPIPE)
1135 { /* under-run */
1136 err = snd_pcm_prepare (p->handle);
1137 if (err < 0)
1138 alsa_sound_perror ("Can't recover from underrun, prepare failed",
1139 err);
1140 }
1141 else if (err == -ESTRPIPE)
1142 {
1143 while ((err = snd_pcm_resume (p->handle)) == -EAGAIN)
1144 sleep (1); /* wait until the suspend flag is released */
1145 if (err < 0)
1146 {
1147 err = snd_pcm_prepare (p->handle);
1148 if (err < 0)
1149 alsa_sound_perror ("Can't recover from suspend, "
1150 "prepare failed",
1151 err);
1152 }
1153 }
1154 else
1155 alsa_sound_perror ("Error writing to sound device", err);
1156
1157 }
1158 else
1159 nwritten += err * fact;
1160 }
1161 }
1162
1163 static void
1164 snd_error_quiet (const char *file, int line, const char *function, int err,
1165 const char *fmt)
1166 {
1167 }
1168
1169 /* Initialize device SD. Set up the interface functions in the device
1170 structure. */
1171
1172 static bool
1173 alsa_init (struct sound_device *sd)
1174 {
1175 /* Open the sound device. Default is "default". */
1176 char const *file = string_default (sd->file, DEFAULT_ALSA_SOUND_DEVICE);
1177 snd_pcm_t *handle;
1178 int err;
1179
1180 snd_lib_error_set_handler ((snd_lib_error_handler_t) snd_error_quiet);
1181 err = snd_pcm_open (&handle, file, SND_PCM_STREAM_PLAYBACK, 0);
1182 snd_lib_error_set_handler (NULL);
1183 if (err < 0)
1184 return 0;
1185 snd_pcm_close (handle);
1186
1187 sd->fd = -1;
1188 sd->open = alsa_open;
1189 sd->close = alsa_close;
1190 sd->configure = alsa_configure;
1191 sd->choose_format = alsa_choose_format;
1192 sd->write = alsa_write;
1193 sd->period_size = alsa_period_size;
1194
1195 return 1;
1196 }
1197
1198 #endif /* HAVE_ALSA */
1199
1200
1201 /* END: Non Windows functions */
1202 #else /* WINDOWSNT */
1203
1204 /* BEGIN: Windows specific functions */
1205
1206 #define SOUND_WARNING(func, error, text) \
1207 do { \
1208 char buf[1024]; \
1209 char err_string[MAXERRORLENGTH]; \
1210 func (error, err_string, sizeof (err_string)); \
1211 _snprintf (buf, sizeof (buf), "%s\nMCI Error: %s", \
1212 text, err_string); \
1213 message_with_string ("%s", build_string (buf), 1); \
1214 } while (0)
1215
1216 static int
1217 do_play_sound (const char *psz_file, unsigned long ui_volume)
1218 {
1219 int i_result = 0;
1220 MCIERROR mci_error = 0;
1221 char sz_cmd_buf_a[520];
1222 char sz_ret_buf_a[520];
1223 MMRESULT mm_result = MMSYSERR_NOERROR;
1224 unsigned long ui_volume_org = 0;
1225 BOOL b_reset_volume = FALSE;
1226 char warn_text[560];
1227
1228 /* Since UNICOWS.DLL includes only a stub for mciSendStringW, we
1229 need to encode the file in the ANSI codepage on Windows 9X even
1230 if w32_unicode_filenames is non-zero. */
1231 if (w32_major_version <= 4 || !w32_unicode_filenames)
1232 {
1233 char fname_a[MAX_PATH], shortname[MAX_PATH], *fname_to_use;
1234
1235 filename_to_ansi (psz_file, fname_a);
1236 fname_to_use = fname_a;
1237 /* If the file name is not encodable in ANSI, try its short 8+3
1238 alias. This will only work if w32_unicode_filenames is
1239 non-zero. */
1240 if (_mbspbrk ((const unsigned char *)fname_a,
1241 (const unsigned char *)"?"))
1242 {
1243 if (w32_get_short_filename (psz_file, shortname, MAX_PATH))
1244 fname_to_use = shortname;
1245 else
1246 mci_error = MCIERR_FILE_NOT_FOUND;
1247 }
1248
1249 if (!mci_error)
1250 {
1251 memset (sz_cmd_buf_a, 0, sizeof (sz_cmd_buf_a));
1252 memset (sz_ret_buf_a, 0, sizeof (sz_ret_buf_a));
1253 sprintf (sz_cmd_buf_a,
1254 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
1255 fname_to_use);
1256 mci_error = mciSendStringA (sz_cmd_buf_a,
1257 sz_ret_buf_a, sizeof (sz_ret_buf_a), NULL);
1258 }
1259 }
1260 else
1261 {
1262 wchar_t sz_cmd_buf_w[520];
1263 wchar_t sz_ret_buf_w[520];
1264 wchar_t fname_w[MAX_PATH];
1265
1266 filename_to_utf16 (psz_file, fname_w);
1267 memset (sz_cmd_buf_w, 0, sizeof (sz_cmd_buf_w));
1268 memset (sz_ret_buf_w, 0, sizeof (sz_ret_buf_w));
1269 /* _swprintf is not available on Windows 9X, so we construct the
1270 UTF-16 command string by hand. */
1271 wcscpy (sz_cmd_buf_w, L"open \"");
1272 wcscat (sz_cmd_buf_w, fname_w);
1273 wcscat (sz_cmd_buf_w, L"\" alias GNUEmacs_PlaySound_Device wait");
1274 mci_error = mciSendStringW (sz_cmd_buf_w,
1275 sz_ret_buf_w, ARRAYELTS (sz_ret_buf_w) , NULL);
1276 }
1277 if (mci_error != 0)
1278 {
1279 strcpy (warn_text,
1280 "mciSendString: 'open' command failed to open sound file ");
1281 strcat (warn_text, psz_file);
1282 SOUND_WARNING (mciGetErrorString, mci_error, warn_text);
1283 i_result = (int) mci_error;
1284 return i_result;
1285 }
1286 if ((ui_volume > 0) && (ui_volume != UINT_MAX))
1287 {
1288 mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
1289 if (mm_result == MMSYSERR_NOERROR)
1290 {
1291 b_reset_volume = TRUE;
1292 mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
1293 if (mm_result != MMSYSERR_NOERROR)
1294 {
1295 SOUND_WARNING (waveOutGetErrorText, mm_result,
1296 "waveOutSetVolume: failed to set the volume level"
1297 " of the WAVE_MAPPER device.\n"
1298 "As a result, the user selected volume level will"
1299 " not be used.");
1300 }
1301 }
1302 else
1303 {
1304 SOUND_WARNING (waveOutGetErrorText, mm_result,
1305 "waveOutGetVolume: failed to obtain the original"
1306 " volume level of the WAVE_MAPPER device.\n"
1307 "As a result, the user selected volume level will"
1308 " not be used.");
1309 }
1310 }
1311 memset (sz_cmd_buf_a, 0, sizeof (sz_cmd_buf_a));
1312 memset (sz_ret_buf_a, 0, sizeof (sz_ret_buf_a));
1313 strcpy (sz_cmd_buf_a, "play GNUEmacs_PlaySound_Device wait");
1314 mci_error = mciSendStringA (sz_cmd_buf_a, sz_ret_buf_a, sizeof (sz_ret_buf_a),
1315 NULL);
1316 if (mci_error != 0)
1317 {
1318 strcpy (warn_text,
1319 "mciSendString: 'play' command failed to play sound file ");
1320 strcat (warn_text, psz_file);
1321 SOUND_WARNING (mciGetErrorString, mci_error, warn_text);
1322 i_result = (int) mci_error;
1323 }
1324 memset (sz_cmd_buf_a, 0, sizeof (sz_cmd_buf_a));
1325 memset (sz_ret_buf_a, 0, sizeof (sz_ret_buf_a));
1326 strcpy (sz_cmd_buf_a, "close GNUEmacs_PlaySound_Device wait");
1327 mci_error = mciSendStringA (sz_cmd_buf_a, sz_ret_buf_a, sizeof (sz_ret_buf_a),
1328 NULL);
1329 if (b_reset_volume == TRUE)
1330 {
1331 mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
1332 if (mm_result != MMSYSERR_NOERROR)
1333 {
1334 SOUND_WARNING (waveOutGetErrorText, mm_result,
1335 "waveOutSetVolume: failed to reset the original"
1336 " volume level of the WAVE_MAPPER device.");
1337 }
1338 }
1339 return i_result;
1340 }
1341
1342 /* END: Windows specific functions */
1343
1344 #endif /* WINDOWSNT */
1345
1346 DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
1347 doc: /* Play sound SOUND.
1348
1349 Internal use only, use `play-sound' instead. */)
1350 (Lisp_Object sound)
1351 {
1352 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
1353 ptrdiff_t count = SPECPDL_INDEX ();
1354 struct gcpro gcpro1, gcpro2;
1355
1356 #ifdef WINDOWSNT
1357 unsigned long ui_volume_tmp = UINT_MAX;
1358 unsigned long ui_volume = UINT_MAX;
1359 #endif /* WINDOWSNT */
1360
1361 /* Parse the sound specification. Give up if it is invalid. */
1362 if (!parse_sound (sound, attrs))
1363 error ("Invalid sound specification");
1364
1365 Lisp_Object file = Qnil;
1366 GCPRO2 (sound, file);
1367
1368 #ifndef WINDOWSNT
1369 current_sound_device = xzalloc (sizeof *current_sound_device);
1370 current_sound = xzalloc (sizeof *current_sound);
1371 record_unwind_protect_void (sound_cleanup);
1372 char headerbuf[MAX_SOUND_HEADER_BYTES];
1373 current_sound->header = headerbuf;
1374
1375 if (STRINGP (attrs[SOUND_FILE]))
1376 {
1377 /* Open the sound file. */
1378 current_sound->fd = openp (list1 (Vdata_directory),
1379 attrs[SOUND_FILE], Qnil, &file, Qnil, false);
1380 if (current_sound->fd < 0)
1381 sound_perror ("Could not open sound file");
1382
1383 /* Read the first bytes from the file. */
1384 current_sound->header_size
1385 = emacs_read (current_sound->fd, current_sound->header,
1386 MAX_SOUND_HEADER_BYTES);
1387 if (current_sound->header_size < 0)
1388 sound_perror ("Invalid sound file header");
1389 }
1390 else
1391 {
1392 current_sound->data = attrs[SOUND_DATA];
1393 current_sound->header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (current_sound->data));
1394 memcpy (current_sound->header, SDATA (current_sound->data),
1395 current_sound->header_size);
1396 }
1397
1398 /* Find out the type of sound. Give up if we can't tell. */
1399 find_sound_type (current_sound);
1400
1401 /* Set up a device. */
1402 current_sound_device->file = attrs[SOUND_DEVICE];
1403
1404 if (INTEGERP (attrs[SOUND_VOLUME]))
1405 current_sound_device->volume = XFASTINT (attrs[SOUND_VOLUME]);
1406 else if (FLOATP (attrs[SOUND_VOLUME]))
1407 current_sound_device->volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
1408
1409 CALLN (Frun_hook_with_args, Qplay_sound_functions, sound);
1410
1411 #ifdef HAVE_ALSA
1412 if (!alsa_init (current_sound_device))
1413 #endif
1414 if (!vox_init (current_sound_device))
1415 error ("No usable sound device driver found");
1416
1417 /* Open the device. */
1418 current_sound_device->open (current_sound_device);
1419
1420 /* Play the sound. */
1421 current_sound->play (current_sound, current_sound_device);
1422
1423 #else /* WINDOWSNT */
1424
1425 file = Fexpand_file_name (attrs[SOUND_FILE], Vdata_directory);
1426 file = ENCODE_FILE (file);
1427 if (INTEGERP (attrs[SOUND_VOLUME]))
1428 {
1429 ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
1430 }
1431 else if (FLOATP (attrs[SOUND_VOLUME]))
1432 {
1433 ui_volume_tmp = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
1434 }
1435
1436 CALLN (Frun_hook_with_args, Qplay_sound_functions, sound);
1437
1438 /*
1439 Based on some experiments I have conducted, a value of 100 or less
1440 for the sound volume is much too low. You cannot even hear it.
1441 A value of UINT_MAX indicates that you wish for the sound to played
1442 at the maximum possible volume. A value of UINT_MAX/2 plays the
1443 sound at 50% maximum volume. Therefore the value passed to do_play_sound
1444 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
1445 The following code adjusts the user specified volume level appropriately.
1446 */
1447 if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
1448 {
1449 ui_volume = ui_volume_tmp * (UINT_MAX / 100);
1450 }
1451 (void)do_play_sound (SSDATA (file), ui_volume);
1452
1453 #endif /* WINDOWSNT */
1454
1455 UNGCPRO;
1456 return unbind_to (count, Qnil);
1457 }
1458 \f
1459 /***********************************************************************
1460 Initialization
1461 ***********************************************************************/
1462
1463 void
1464 syms_of_sound (void)
1465 {
1466 DEFSYM (QCdevice, ":device");
1467 DEFSYM (QCvolume, ":volume");
1468 DEFSYM (Qsound, "sound");
1469 DEFSYM (Qplay_sound_functions, "play-sound-functions");
1470
1471 defsubr (&Splay_sound_internal);
1472 }
1473
1474 #endif /* HAVE_SOUND */