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