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