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