(openp): Initialized encoded_fn before GCPRO it.
[bpt/emacs.git] / src / sound.c
CommitLineData
7840ced1 1/* sound.c -- sound support.
0b5538bd
TTN
2 Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
3 2005 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
f60ae425
BK
76/* END: Non Windows Includes */
77
78#else /* WINDOWSNT */
79
80/* BEGIN: Windows Specific Includes */
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <limits.h>
85#include <windows.h>
86#include <mmsystem.h>
87/* END: Windows Specific Includes */
88
89#endif /* WINDOWSNT */
90
91/* BEGIN: Common Definitions */
92#define abs(X) ((X) < 0 ? -(X) : (X))
93
94/* Symbols. */
95
96extern Lisp_Object QCfile, QCdata;
97Lisp_Object QCvolume, QCdevice;
98Lisp_Object Qsound;
99Lisp_Object Qplay_sound_functions;
100
101/* Indices of attributes in a sound attributes vector. */
102
103enum sound_attr
104{
105 SOUND_FILE,
106 SOUND_DATA,
107 SOUND_DEVICE,
108 SOUND_VOLUME,
109 SOUND_ATTR_SENTINEL
110};
111
112static void sound_perror P_ ((char *));
113static void sound_warning P_ ((char *));
114static int parse_sound P_ ((Lisp_Object, Lisp_Object *));
115
116/* END: Common Definitions */
117
118/* BEGIN: Non Windows Definitions */
119#ifndef WINDOWSNT
80fcd514
KR
120
121#ifndef DEFAULT_SOUND_DEVICE
122#define DEFAULT_SOUND_DEVICE "/dev/dsp"
123#endif
7840ced1 124
7840ced1
GM
125
126/* Structure forward declarations. */
127
d1299cde 128struct sound;
7840ced1
GM
129struct sound_device;
130
131/* The file header of RIFF-WAVE files (*.wav). Files are always in
132 little-endian byte-order. */
133
134struct wav_header
135{
136 u_int32_t magic;
137 u_int32_t length;
138 u_int32_t chunk_type;
139 u_int32_t chunk_format;
140 u_int32_t chunk_length;
141 u_int16_t format;
142 u_int16_t channels;
143 u_int32_t sample_rate;
144 u_int32_t bytes_per_second;
145 u_int16_t sample_size;
146 u_int16_t precision;
147 u_int32_t chunk_data;
148 u_int32_t data_length;
149};
150
151/* The file header of Sun adio files (*.au). Files are always in
152 big-endian byte-order. */
153
154struct au_header
155{
156 /* ASCII ".snd" */
157 u_int32_t magic_number;
a4ff5d67 158
7840ced1
GM
159 /* Offset of data part from start of file. Minimum value is 24. */
160 u_int32_t data_offset;
a4ff5d67 161
7840ced1
GM
162 /* Size of data part, 0xffffffff if unknown. */
163 u_int32_t data_size;
164
165 /* Data encoding format.
166 1 8-bit ISDN u-law
167 2 8-bit linear PCM (REF-PCM)
168 3 16-bit linear PCM
169 4 24-bit linear PCM
170 5 32-bit linear PCM
171 6 32-bit IEEE floating-point
172 7 64-bit IEEE floating-point
173 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data
174 encoding scheme. */
175 u_int32_t encoding;
176
177 /* Number of samples per second. */
178 u_int32_t sample_rate;
179
180 /* Number of interleaved channels. */
181 u_int32_t channels;
182};
183
184/* Maximum of all sound file headers sizes. */
185
186#define MAX_SOUND_HEADER_BYTES \
187 max (sizeof (struct wav_header), sizeof (struct au_header))
188
189/* Interface structure for sound devices. */
190
191struct sound_device
192{
193 /* The name of the device or null meaning use a default device name. */
194 char *file;
195
196 /* File descriptor of the device. */
197 int fd;
198
199 /* Device-dependent format. */
200 int format;
201
202 /* Volume (0..100). Zero means unspecified. */
203 int volume;
204
205 /* Sample size. */
206 int sample_size;
207
208 /* Sample rate. */
209 int sample_rate;
210
211 /* Bytes per second. */
212 int bps;
213
214 /* 1 = mono, 2 = stereo, 0 = don't set. */
215 int channels;
a4ff5d67 216
7840ced1
GM
217 /* Open device SD. */
218 void (* open) P_ ((struct sound_device *sd));
219
220 /* Close device SD. */
221 void (* close) P_ ((struct sound_device *sd));
222
223 /* Configure SD accoring to device-dependent parameters. */
224 void (* configure) P_ ((struct sound_device *device));
a4ff5d67 225
d1299cde 226 /* Choose a device-dependent format for outputting sound S. */
7840ced1 227 void (* choose_format) P_ ((struct sound_device *sd,
d1299cde 228 struct sound *s));
7840ced1
GM
229
230 /* Write NYBTES bytes from BUFFER to device SD. */
dca0fc1c
KR
231 void (* write) P_ ((struct sound_device *sd, const char *buffer,
232 int nbytes));
7840ced1
GM
233
234 /* A place for devices to store additional data. */
235 void *data;
236};
237
238/* An enumerator for each supported sound file type. */
239
240enum sound_type
241{
242 RIFF,
243 SUN_AUDIO
244};
245
246/* Interface structure for sound files. */
247
d1299cde 248struct sound
7840ced1
GM
249{
250 /* The type of the file. */
251 enum sound_type type;
252
d1299cde 253 /* File descriptor of a sound file. */
7840ced1
GM
254 int fd;
255
d1299cde
GM
256 /* Pointer to sound file header. This contains header_size bytes
257 read from the start of a sound file. */
7840ced1
GM
258 char *header;
259
d1299cde
GM
260 /* Number of bytes raed from sound file. This is always <=
261 MAX_SOUND_HEADER_BYTES. */
262 int header_size;
263
264 /* Sound data, if a string. */
265 Lisp_Object data;
266
267 /* Play sound file S on device SD. */
a4ff5d67 268 void (* play) P_ ((struct sound *s, struct sound_device *sd));
7840ced1
GM
269};
270
14e415e7 271/* These are set during `play-sound-internal' so that sound_cleanup has
7840ced1
GM
272 access to them. */
273
d1299cde
GM
274struct sound_device *current_sound_device;
275struct sound *current_sound;
7840ced1
GM
276
277/* Function prototypes. */
278
279static void vox_open P_ ((struct sound_device *));
280static void vox_configure P_ ((struct sound_device *));
281static void vox_close P_ ((struct sound_device *sd));
d1299cde 282static void vox_choose_format P_ ((struct sound_device *, struct sound *));
7840ced1 283static void vox_init P_ ((struct sound_device *));
dca0fc1c 284static void vox_write P_ ((struct sound_device *, const char *, int));
d1299cde 285static void find_sound_type P_ ((struct sound *));
7840ced1
GM
286static u_int32_t le2hl P_ ((u_int32_t));
287static u_int16_t le2hs P_ ((u_int16_t));
288static u_int32_t be2hl P_ ((u_int32_t));
d1299cde
GM
289static int wav_init P_ ((struct sound *));
290static void wav_play P_ ((struct sound *, struct sound_device *));
291static int au_init P_ ((struct sound *));
292static void au_play P_ ((struct sound *, struct sound_device *));
7840ced1 293
9680b9d0
GM
294#if 0 /* Currently not used. */
295static u_int16_t be2hs P_ ((u_int16_t));
296#endif
297
f60ae425
BK
298/* END: Non Windows Definitions */
299#else /* WINDOWSNT */
300
301/* BEGIN: Windows Specific Definitions */
302static int do_play_sound P_ ((const char *, unsigned long));
303/*
304 END: Windows Specific Definitions */
305#endif /* WINDOWSNT */
7840ced1
GM
306
307\f
308/***********************************************************************
309 General
310 ***********************************************************************/
311
f60ae425
BK
312/* BEGIN: Common functions */
313
7840ced1
GM
314/* Like perror, but signals an error. */
315
316static void
317sound_perror (msg)
318 char *msg;
319{
3297e2a1
AS
320 int saved_errno = errno;
321
62725a92
GM
322 turn_on_atimers (1);
323#ifdef SIGIO
324 sigunblock (sigmask (SIGIO));
325#endif
3297e2a1
AS
326 if (saved_errno != 0)
327 error ("%s: %s", msg, strerror (saved_errno));
62725a92
GM
328 else
329 error ("%s", msg);
330}
331
332
333/* Display a warning message. */
334
335static void
336sound_warning (msg)
337 char *msg;
338{
339 message (msg);
7840ced1
GM
340}
341
342
343/* Parse sound specification SOUND, and fill ATTRS with what is
344 found. Value is non-zero if SOUND Is a valid sound specification.
345 A valid sound specification is a list starting with the symbol
346 `sound'. The rest of the list is a property list which may
347 contain the following key/value pairs:
348
349 - `:file FILE'
350
351 FILE is the sound file to play. If it isn't an absolute name,
352 it's searched under `data-directory'.
353
d1299cde
GM
354 - `:data DATA'
355
356 DATA is a string containing sound data. Either :file or :data
357 may be present, but not both.
358
7840ced1
GM
359 - `:device DEVICE'
360
361 DEVICE is the name of the device to play on, e.g. "/dev/dsp2".
362 If not specified, a default device is used.
363
364 - `:volume VOL'
365
2a53558d
GM
366 VOL must be an integer in the range [0, 100], or a float in the
367 range [0, 1]. */
7840ced1
GM
368
369static int
370parse_sound (sound, attrs)
371 Lisp_Object sound;
372 Lisp_Object *attrs;
373{
374 /* SOUND must be a list starting with the symbol `sound'. */
375 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound))
376 return 0;
377
378 sound = XCDR (sound);
379 attrs[SOUND_FILE] = Fplist_get (sound, QCfile);
d1299cde 380 attrs[SOUND_DATA] = Fplist_get (sound, QCdata);
7840ced1
GM
381 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice);
382 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume);
383
f60ae425 384#ifndef WINDOWSNT
d1299cde
GM
385 /* File name or data must be specified. */
386 if (!STRINGP (attrs[SOUND_FILE])
387 && !STRINGP (attrs[SOUND_DATA]))
7840ced1 388 return 0;
f60ae425
BK
389#else /* WINDOWSNT */
390 /*
391 Data is not supported in Windows. Therefore a
392 File name MUST be supplied.
393 */
394 if (!STRINGP (attrs[SOUND_FILE]))
395 {
396 return 0;
397 }
398#endif /* WINDOWSNT */
7840ced1
GM
399
400 /* Volume must be in the range 0..100 or unspecified. */
401 if (!NILP (attrs[SOUND_VOLUME]))
402 {
2a53558d
GM
403 if (INTEGERP (attrs[SOUND_VOLUME]))
404 {
405 if (XINT (attrs[SOUND_VOLUME]) < 0
406 || XINT (attrs[SOUND_VOLUME]) > 100)
407 return 0;
408 }
409 else if (FLOATP (attrs[SOUND_VOLUME]))
410 {
411 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0
412 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1)
413 return 0;
414 }
415 else
7840ced1
GM
416 return 0;
417 }
418
f60ae425 419#ifndef WINDOWSNT
7840ced1
GM
420 /* Device must be a string or unspecified. */
421 if (!NILP (attrs[SOUND_DEVICE])
422 && !STRINGP (attrs[SOUND_DEVICE]))
423 return 0;
f60ae425
BK
424#endif /* WINDOWSNT */
425 /*
426 Since device is ignored in Windows, it does not matter
427 what it is.
428 */
7840ced1
GM
429 return 1;
430}
431
f60ae425
BK
432/* END: Common functions */
433
434/* BEGIN: Non Windows functions */
435#ifndef WINDOWSNT
7840ced1
GM
436
437/* Find out the type of the sound file whose file descriptor is FD.
d1299cde 438 S is the sound file structure to fill in. */
7840ced1
GM
439
440static void
d1299cde
GM
441find_sound_type (s)
442 struct sound *s;
7840ced1 443{
d1299cde
GM
444 if (!wav_init (s) && !au_init (s))
445 error ("Unknown sound format");
7840ced1
GM
446}
447
448
14e415e7 449/* Function installed by play-sound-internal with record_unwind_protect. */
7840ced1
GM
450
451static Lisp_Object
452sound_cleanup (arg)
453 Lisp_Object arg;
454{
d1299cde 455 if (current_sound_device)
7840ced1 456 {
66e4690f
KR
457 if (current_sound_device->close)
458 current_sound_device->close (current_sound_device);
d1299cde
GM
459 if (current_sound->fd > 0)
460 emacs_close (current_sound->fd);
7840ced1 461 }
3887b449
GM
462
463 return Qnil;
7840ced1
GM
464}
465
7840ced1
GM
466/***********************************************************************
467 Byte-order Conversion
468 ***********************************************************************/
469
470/* Convert 32-bit value VALUE which is in little-endian byte-order
471 to host byte-order. */
472
473static u_int32_t
474le2hl (value)
475 u_int32_t value;
476{
477#ifdef WORDS_BIG_ENDIAN
478 unsigned char *p = (unsigned char *) &value;
479 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
480#endif
481 return value;
482}
483
484
485/* Convert 16-bit value VALUE which is in little-endian byte-order
486 to host byte-order. */
487
488static u_int16_t
489le2hs (value)
490 u_int16_t value;
491{
492#ifdef WORDS_BIG_ENDIAN
493 unsigned char *p = (unsigned char *) &value;
494 value = p[0] + (p[1] << 8);
495#endif
496 return value;
497}
498
499
500/* Convert 32-bit value VALUE which is in big-endian byte-order
501 to host byte-order. */
502
503static u_int32_t
504be2hl (value)
505 u_int32_t value;
506{
507#ifndef WORDS_BIG_ENDIAN
508 unsigned char *p = (unsigned char *) &value;
509 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24);
510#endif
511 return value;
512}
513
514
9680b9d0
GM
515#if 0 /* Currently not used. */
516
7840ced1
GM
517/* Convert 16-bit value VALUE which is in big-endian byte-order
518 to host byte-order. */
519
520static u_int16_t
521be2hs (value)
522 u_int16_t value;
523{
524#ifndef WORDS_BIG_ENDIAN
525 unsigned char *p = (unsigned char *) &value;
526 value = p[1] + (p[0] << 8);
527#endif
528 return value;
529}
530
9680b9d0 531#endif /* 0 */
7840ced1 532
7840ced1
GM
533/***********************************************************************
534 RIFF-WAVE (*.wav)
535 ***********************************************************************/
536
d1299cde 537/* Try to initialize sound file S from S->header. S->header
7840ced1
GM
538 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
539 sound file. If the file is a WAV-format file, set up interface
d1299cde 540 functions in S and convert header fields to host byte-order.
7840ced1
GM
541 Value is non-zero if the file is a WAV file. */
542
543static int
d1299cde
GM
544wav_init (s)
545 struct sound *s;
7840ced1 546{
d1299cde
GM
547 struct wav_header *header = (struct wav_header *) s->header;
548
549 if (s->header_size < sizeof *header
550 || bcmp (s->header, "RIFF", 4) != 0)
7840ced1
GM
551 return 0;
552
553 /* WAV files are in little-endian order. Convert the header
554 if on a big-endian machine. */
555 header->magic = le2hl (header->magic);
556 header->length = le2hl (header->length);
557 header->chunk_type = le2hl (header->chunk_type);
558 header->chunk_format = le2hl (header->chunk_format);
559 header->chunk_length = le2hl (header->chunk_length);
560 header->format = le2hs (header->format);
561 header->channels = le2hs (header->channels);
562 header->sample_rate = le2hl (header->sample_rate);
563 header->bytes_per_second = le2hl (header->bytes_per_second);
564 header->sample_size = le2hs (header->sample_size);
565 header->precision = le2hs (header->precision);
566 header->chunk_data = le2hl (header->chunk_data);
567 header->data_length = le2hl (header->data_length);
568
569 /* Set up the interface functions for WAV. */
d1299cde
GM
570 s->type = RIFF;
571 s->play = wav_play;
7840ced1
GM
572
573 return 1;
a4ff5d67 574}
7840ced1
GM
575
576
d1299cde 577/* Play RIFF-WAVE audio file S on sound device SD. */
7840ced1
GM
578
579static void
d1299cde
GM
580wav_play (s, sd)
581 struct sound *s;
7840ced1
GM
582 struct sound_device *sd;
583{
d1299cde 584 struct wav_header *header = (struct wav_header *) s->header;
7840ced1
GM
585
586 /* Let the device choose a suitable device-dependent format
587 for the file. */
d1299cde 588 sd->choose_format (sd, s);
a4ff5d67 589
7840ced1
GM
590 /* Configure the device. */
591 sd->sample_size = header->sample_size;
592 sd->sample_rate = header->sample_rate;
593 sd->bps = header->bytes_per_second;
594 sd->channels = header->channels;
595 sd->configure (sd);
596
597 /* Copy sound data to the device. The WAV file specification is
598 actually more complex. This simple scheme worked with all WAV
599 files I found so far. If someone feels inclined to implement the
600 whole RIFF-WAVE spec, please do. */
d1299cde 601 if (STRINGP (s->data))
d5db4077
KR
602 sd->write (sd, SDATA (s->data) + sizeof *header,
603 SBYTES (s->data) - sizeof *header);
d1299cde
GM
604 else
605 {
606 char *buffer;
607 int nbytes;
608 int blksize = 2048;
a4ff5d67 609
d1299cde
GM
610 buffer = (char *) alloca (blksize);
611 lseek (s->fd, sizeof *header, SEEK_SET);
a4ff5d67 612
d1299cde
GM
613 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
614 sd->write (sd, buffer, nbytes);
7840ced1 615
d1299cde 616 if (nbytes < 0)
62725a92 617 sound_perror ("Error reading sound file");
d1299cde 618 }
7840ced1
GM
619}
620
621
7840ced1
GM
622/***********************************************************************
623 Sun Audio (*.au)
624 ***********************************************************************/
625
a4ff5d67 626/* Sun audio file encodings. */
7840ced1
GM
627
628enum au_encoding
629{
630 AU_ENCODING_ULAW_8 = 1,
631 AU_ENCODING_8,
632 AU_ENCODING_16,
633 AU_ENCODING_24,
634 AU_ENCODING_32,
635 AU_ENCODING_IEEE32,
636 AU_ENCODING_IEEE64,
637 AU_COMPRESSED = 23
638};
639
640
d1299cde 641/* Try to initialize sound file S from S->header. S->header
7840ced1
GM
642 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the
643 sound file. If the file is a AU-format file, set up interface
d1299cde 644 functions in S and convert header fields to host byte-order.
7840ced1
GM
645 Value is non-zero if the file is an AU file. */
646
647static int
d1299cde
GM
648au_init (s)
649 struct sound *s;
7840ced1 650{
d1299cde 651 struct au_header *header = (struct au_header *) s->header;
a4ff5d67 652
d1299cde
GM
653 if (s->header_size < sizeof *header
654 || bcmp (s->header, ".snd", 4) != 0)
7840ced1 655 return 0;
a4ff5d67 656
7840ced1
GM
657 header->magic_number = be2hl (header->magic_number);
658 header->data_offset = be2hl (header->data_offset);
659 header->data_size = be2hl (header->data_size);
660 header->encoding = be2hl (header->encoding);
661 header->sample_rate = be2hl (header->sample_rate);
662 header->channels = be2hl (header->channels);
a4ff5d67 663
7840ced1 664 /* Set up the interface functions for AU. */
d1299cde
GM
665 s->type = SUN_AUDIO;
666 s->play = au_play;
7840ced1
GM
667
668 return 1;
669}
670
671
d1299cde 672/* Play Sun audio file S on sound device SD. */
7840ced1
GM
673
674static void
d1299cde
GM
675au_play (s, sd)
676 struct sound *s;
7840ced1
GM
677 struct sound_device *sd;
678{
d1299cde 679 struct au_header *header = (struct au_header *) s->header;
7840ced1
GM
680
681 sd->sample_size = 0;
682 sd->sample_rate = header->sample_rate;
683 sd->bps = 0;
684 sd->channels = header->channels;
d1299cde 685 sd->choose_format (sd, s);
7840ced1 686 sd->configure (sd);
d1299cde
GM
687
688 if (STRINGP (s->data))
d5db4077
KR
689 sd->write (sd, SDATA (s->data) + header->data_offset,
690 SBYTES (s->data) - header->data_offset);
d1299cde
GM
691 else
692 {
693 int blksize = 2048;
694 char *buffer;
695 int nbytes;
a4ff5d67 696
d1299cde
GM
697 /* Seek */
698 lseek (s->fd, header->data_offset, SEEK_SET);
a4ff5d67 699
d1299cde
GM
700 /* Copy sound data to the device. */
701 buffer = (char *) alloca (blksize);
702 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0)
703 sd->write (sd, buffer, nbytes);
a4ff5d67 704
d1299cde 705 if (nbytes < 0)
62725a92 706 sound_perror ("Error reading sound file");
d1299cde 707 }
7840ced1
GM
708}
709
710
7840ced1
GM
711/***********************************************************************
712 Voxware Driver Interface
713 ***********************************************************************/
714
715/* This driver is available on GNU/Linux, and the free BSDs. FreeBSD
716 has a compatible own driver aka Luigi's driver. */
717
718
719/* Open device SD. If SD->file is non-null, open that device,
720 otherwise use a default device name. */
721
722static void
723vox_open (sd)
724 struct sound_device *sd;
725{
726 char *file;
a4ff5d67 727
7840ced1
GM
728 /* Open the sound device. Default is /dev/dsp. */
729 if (sd->file)
730 file = sd->file;
731 else
80fcd514 732 file = DEFAULT_SOUND_DEVICE;
a4ff5d67 733
68c45bf0 734 sd->fd = emacs_open (file, O_WRONLY, 0);
7840ced1
GM
735 if (sd->fd < 0)
736 sound_perror (file);
737}
738
739
740/* Configure device SD from parameters in it. */
741
742static void
743vox_configure (sd)
744 struct sound_device *sd;
745{
28fcb7dc 746 int val;
a4ff5d67 747
7840ced1
GM
748 xassert (sd->fd >= 0);
749
bb6e8cee
GM
750 /* On GNU/Linux, it seems that the device driver doesn't like to be
751 interrupted by a signal. Block the ones we know to cause
752 troubles. */
b5cb1ada 753 turn_on_atimers (0);
bb6e8cee
GM
754#ifdef SIGIO
755 sigblock (sigmask (SIGIO));
756#endif
b5cb1ada 757
28fcb7dc
GM
758 val = sd->format;
759 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0
760 || val != sd->format)
62725a92 761 sound_perror ("Could not set sound format");
7840ced1 762
28fcb7dc
GM
763 val = sd->channels != 1;
764 if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0
765 || val != (sd->channels != 1))
62725a92 766 sound_perror ("Could not set stereo/mono");
7840ced1 767
28fcb7dc
GM
768 /* I think bps and sampling_rate are the same, but who knows.
769 Check this. and use SND_DSP_SPEED for both. */
770 if (sd->sample_rate > 0)
771 {
772 val = sd->sample_rate;
62725a92
GM
773 if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0)
774 sound_perror ("Could not set sound speed");
775 else if (val != sd->sample_rate)
776 sound_warning ("Could not set sample rate");
28fcb7dc 777 }
7840ced1 778
3887b449
GM
779 if (sd->volume > 0)
780 {
781 int volume = sd->volume & 0xff;
782 volume |= volume << 8;
28fcb7dc
GM
783 /* This may fail if there is no mixer. Ignore the failure. */
784 ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume);
3887b449 785 }
a4ff5d67 786
b5cb1ada 787 turn_on_atimers (1);
bb6e8cee
GM
788#ifdef SIGIO
789 sigunblock (sigmask (SIGIO));
790#endif
7840ced1
GM
791}
792
793
794/* Close device SD if it is open. */
795
796static void
797vox_close (sd)
798 struct sound_device *sd;
799{
800 if (sd->fd >= 0)
801 {
bb6e8cee
GM
802 /* On GNU/Linux, it seems that the device driver doesn't like to
803 be interrupted by a signal. Block the ones we know to cause
804 troubles. */
805#ifdef SIGIO
806 sigblock (sigmask (SIGIO));
807#endif
b5cb1ada 808 turn_on_atimers (0);
a4ff5d67 809
bb6e8cee 810 /* Flush sound data, and reset the device. */
7840ced1 811 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL);
a4ff5d67 812
b5cb1ada 813 turn_on_atimers (1);
bb6e8cee
GM
814#ifdef SIGIO
815 sigunblock (sigmask (SIGIO));
816#endif
7840ced1
GM
817
818 /* Close the device. */
68c45bf0 819 emacs_close (sd->fd);
7840ced1
GM
820 sd->fd = -1;
821 }
822}
823
824
d1299cde 825/* Choose device-dependent format for device SD from sound file S. */
7840ced1
GM
826
827static void
d1299cde 828vox_choose_format (sd, s)
7840ced1 829 struct sound_device *sd;
d1299cde 830 struct sound *s;
7840ced1 831{
d1299cde 832 if (s->type == RIFF)
7840ced1 833 {
d1299cde 834 struct wav_header *h = (struct wav_header *) s->header;
7840ced1
GM
835 if (h->precision == 8)
836 sd->format = AFMT_U8;
837 else if (h->precision == 16)
838 sd->format = AFMT_S16_LE;
839 else
840 error ("Unsupported WAV file format");
841 }
d1299cde 842 else if (s->type == SUN_AUDIO)
7840ced1 843 {
d1299cde 844 struct au_header *header = (struct au_header *) s->header;
7840ced1
GM
845 switch (header->encoding)
846 {
847 case AU_ENCODING_ULAW_8:
848 case AU_ENCODING_IEEE32:
849 case AU_ENCODING_IEEE64:
850 sd->format = AFMT_MU_LAW;
851 break;
a4ff5d67 852
7840ced1
GM
853 case AU_ENCODING_8:
854 case AU_ENCODING_16:
855 case AU_ENCODING_24:
856 case AU_ENCODING_32:
857 sd->format = AFMT_S16_LE;
858 break;
859
860 default:
861 error ("Unsupported AU file format");
862 }
863 }
864 else
865 abort ();
866}
867
868
869/* Initialize device SD. Set up the interface functions in the device
870 structure. */
871
872static void
873vox_init (sd)
874 struct sound_device *sd;
875{
876 sd->fd = -1;
877 sd->open = vox_open;
878 sd->close = vox_close;
879 sd->configure = vox_configure;
880 sd->choose_format = vox_choose_format;
881 sd->write = vox_write;
882}
883
7840ced1
GM
884/* Write NBYTES bytes from BUFFER to device SD. */
885
886static void
887vox_write (sd, buffer, nbytes)
888 struct sound_device *sd;
dca0fc1c 889 const char *buffer;
7840ced1
GM
890 int nbytes;
891{
68c45bf0 892 int nwritten = emacs_write (sd->fd, buffer, nbytes);
7840ced1 893 if (nwritten < 0)
62725a92 894 sound_perror ("Error writing to sound device");
7840ced1
GM
895}
896
f60ae425
BK
897/* END: Non Windows functions */
898#else /* WINDOWSNT */
899
900/* BEGIN: Windows specific functions */
901
902static int
903do_play_sound (psz_file, ui_volume)
06c3eeed
JB
904 const char *psz_file;
905 unsigned long ui_volume;
f60ae425 906{
06c3eeed
JB
907 int i_result = 0;
908 MCIERROR mci_error = 0;
909 char sz_cmd_buf[520] = {0};
910 char sz_ret_buf[520] = {0};
911 MMRESULT mm_result = MMSYSERR_NOERROR;
912 unsigned long ui_volume_org = 0;
913 BOOL b_reset_volume = FALSE;
914
f60ae425
BK
915 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
916 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
06c3eeed
JB
917 sprintf (sz_cmd_buf,
918 "open \"%s\" alias GNUEmacs_PlaySound_Device wait",
919 psz_file);
920 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
f60ae425
BK
921 if (mci_error != 0)
922 {
06c3eeed
JB
923 sound_warning ("The open mciSendString command failed to open\n"
924 "the specified sound file");
925 i_result = (int) mci_error;
f60ae425
BK
926 return i_result;
927 }
928 if ((ui_volume > 0) && (ui_volume != UINT_MAX))
929 {
06c3eeed 930 mm_result = waveOutGetVolume ((HWAVEOUT) WAVE_MAPPER, &ui_volume_org);
f60ae425
BK
931 if (mm_result == MMSYSERR_NOERROR)
932 {
06c3eeed
JB
933 b_reset_volume = TRUE;
934 mm_result = waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume);
f60ae425
BK
935 if ( mm_result != MMSYSERR_NOERROR)
936 {
06c3eeed
JB
937 sound_warning ("waveOutSetVolume failed to set the volume level\n"
938 "of the WAVE_MAPPER device.\n"
939 "As a result, the user selected volume level will\n"
940 "not be used.");
f60ae425
BK
941 }
942 }
943 else
944 {
06c3eeed
JB
945 sound_warning ("waveOutGetVolume failed to obtain the original\n"
946 "volume level of the WAVE_MAPPER device.\n"
947 "As a result, the user selected volume level will\n"
948 "not be used.");
f60ae425
BK
949 }
950 }
951 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
952 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
953 strcpy (sz_cmd_buf, "play GNUEmacs_PlaySound_Device wait");
06c3eeed 954 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
f60ae425
BK
955 if (mci_error != 0)
956 {
06c3eeed
JB
957 sound_warning ("The play mciSendString command failed to play the\n"
958 "opened sound file.");
959 i_result = (int) mci_error;
f60ae425
BK
960 }
961 memset (sz_cmd_buf, 0, sizeof(sz_cmd_buf));
962 memset (sz_ret_buf, 0, sizeof(sz_ret_buf));
963 strcpy (sz_cmd_buf, "close GNUEmacs_PlaySound_Device wait");
06c3eeed 964 mci_error = mciSendString (sz_cmd_buf, sz_ret_buf, 520, NULL);
f60ae425
BK
965 if (b_reset_volume == TRUE)
966 {
06c3eeed 967 mm_result=waveOutSetVolume ((HWAVEOUT) WAVE_MAPPER, ui_volume_org);
f60ae425
BK
968 if (mm_result != MMSYSERR_NOERROR)
969 {
06c3eeed
JB
970 sound_warning ("waveOutSetVolume failed to reset the original volume\n"
971 "level of the WAVE_MAPPER device.");
f60ae425
BK
972 }
973 }
974 return i_result;
975}
976
977/* END: Windows specific functions */
978
979#endif /* WINDOWSNT */
980
f60ae425
BK
981DEFUN ("play-sound-internal", Fplay_sound_internal, Splay_sound_internal, 1, 1, 0,
982 doc: /* Play sound SOUND.
983
984Internal use only, use `play-sound' instead.\n */)
985 (sound)
986 Lisp_Object sound;
987{
988 Lisp_Object attrs[SOUND_ATTR_SENTINEL];
989 int count = SPECPDL_INDEX ();
990
991#ifndef WINDOWSNT
992 Lisp_Object file;
993 struct gcpro gcpro1, gcpro2;
994 struct sound_device sd;
995 struct sound s;
996 Lisp_Object args[2];
997#else /* WINDOWSNT */
06c3eeed
JB
998 int len = 0;
999 Lisp_Object lo_file = {0};
1000 char * psz_file = NULL;
1001 unsigned long ui_volume_tmp = UINT_MAX;
1002 unsigned long ui_volume = UINT_MAX;
1003 int i_result = 0;
f60ae425
BK
1004#endif /* WINDOWSNT */
1005
1006 /* Parse the sound specification. Give up if it is invalid. */
1007 if (!parse_sound (sound, attrs))
1008 error ("Invalid sound specification");
1009
1010#ifndef WINDOWSNT
1011 file = Qnil;
1012 GCPRO2 (sound, file);
1013 bzero (&sd, sizeof sd);
1014 bzero (&s, sizeof s);
1015 current_sound_device = &sd;
1016 current_sound = &s;
1017 record_unwind_protect (sound_cleanup, Qnil);
1018 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES);
1019
1020 if (STRINGP (attrs[SOUND_FILE]))
1021 {
1022 /* Open the sound file. */
1023 s.fd = openp (Fcons (Vdata_directory, Qnil),
1024 attrs[SOUND_FILE], Qnil, &file, Qnil);
1025 if (s.fd < 0)
1026 sound_perror ("Could not open sound file");
1027
1028 /* Read the first bytes from the file. */
1029 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES);
1030 if (s.header_size < 0)
1031 sound_perror ("Invalid sound file header");
1032 }
1033 else
1034 {
1035 s.data = attrs[SOUND_DATA];
1036 s.header_size = min (MAX_SOUND_HEADER_BYTES, SBYTES (s.data));
1037 bcopy (SDATA (s.data), s.header, s.header_size);
1038 }
1039
1040 /* Find out the type of sound. Give up if we can't tell. */
1041 find_sound_type (&s);
1042
1043 /* Set up a device. */
1044 if (STRINGP (attrs[SOUND_DEVICE]))
1045 {
1046 int len = SCHARS (attrs[SOUND_DEVICE]);
1047 sd.file = (char *) alloca (len + 1);
1048 strcpy (sd.file, SDATA (attrs[SOUND_DEVICE]));
1049 }
1050
1051 if (INTEGERP (attrs[SOUND_VOLUME]))
1052 sd.volume = XFASTINT (attrs[SOUND_VOLUME]);
1053 else if (FLOATP (attrs[SOUND_VOLUME]))
1054 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
7840ced1 1055
f60ae425
BK
1056 args[0] = Qplay_sound_functions;
1057 args[1] = sound;
1058 Frun_hook_with_args (2, args);
1059
1060 /* There is only one type of device we currently support, the VOX
1061 sound driver. Set up the device interface functions for that
1062 device. */
1063 vox_init (&sd);
1064
1065 /* Open the device. */
1066 sd.open (&sd);
1067
1068 /* Play the sound. */
1069 s.play (&s, &sd);
1070
1071 /* Close the input file, if any. */
1072 if (!STRINGP (s.data))
1073 {
1074 emacs_close (s.fd);
1075 s.fd = -1;
1076 }
1077
1078 /* Close the device. */
1079 sd.close (&sd);
1080
1081 /* Clean up. */
1082 current_sound_device = NULL;
1083 current_sound = NULL;
1084 UNGCPRO;
06c3eeed 1085
f60ae425 1086#else /* WINDOWSNT */
06c3eeed
JB
1087
1088 lo_file = Fexpand_file_name (attrs[SOUND_FILE], Qnil);
1089 len = XSTRING (lo_file)->size;
1090 psz_file = (char *) alloca (len + 1);
f60ae425
BK
1091 strcpy (psz_file, XSTRING (lo_file)->data);
1092 if (INTEGERP (attrs[SOUND_VOLUME]))
1093 {
06c3eeed 1094 ui_volume_tmp = XFASTINT (attrs[SOUND_VOLUME]);
f60ae425
BK
1095 }
1096 else if (FLOATP (attrs[SOUND_VOLUME]))
1097 {
06c3eeed 1098 ui_volume_tmp = (unsigned long) XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100;
f60ae425
BK
1099 }
1100 /*
1101 Based on some experiments I have conducted, a value of 100 or less
1102 for the sound volume is much too low. You cannot even hear it.
1103 A value of UINT_MAX indicates that you wish for the sound to played
1104 at the maximum possible volume. A value of UINT_MAX/2 plays the
1105 sound at 50% maximum volume. Therefore the value passed to do_play_sound
06c3eeed 1106 (and thus to waveOutSetVolume) must be some fraction of UINT_MAX.
f60ae425 1107 The following code adjusts the user specified volume level appropriately.
06c3eeed 1108 */
f60ae425
BK
1109 if ((ui_volume_tmp > 0) && (ui_volume_tmp <= 100))
1110 {
06c3eeed 1111 ui_volume = ui_volume_tmp * (UINT_MAX / 100);
f60ae425 1112 }
06c3eeed
JB
1113 i_result = do_play_sound (psz_file, ui_volume);
1114
f60ae425 1115#endif /* WINDOWSNT */
06c3eeed 1116
f60ae425
BK
1117 unbind_to (count, Qnil);
1118 return Qnil;
1119}
7840ced1
GM
1120\f
1121/***********************************************************************
1122 Initialization
1123 ***********************************************************************/
1124
1125void
1126syms_of_sound ()
1127{
1128 QCdevice = intern (":device");
1129 staticpro (&QCdevice);
1130 QCvolume = intern (":volume");
1131 staticpro (&QCvolume);
1132 Qsound = intern ("sound");
1133 staticpro (&Qsound);
2a53558d
GM
1134 Qplay_sound_functions = intern ("play-sound-functions");
1135 staticpro (&Qplay_sound_functions);
7840ced1 1136
14e415e7 1137 defsubr (&Splay_sound_internal);
7840ced1
GM
1138}
1139
1140
1141void
1142init_sound ()
1143{
1144}
1145
1146#endif /* HAVE_SOUND */
ab5796a9
MB
1147
1148/* arch-tag: dd850ad8-0433-4e2c-9cba-b7aeeccc0dbd
1149 (do not change this comment) */