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