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