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