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