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