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