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