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 | ||
361358ea | 118 | #ifdef HAVE_ALSA |
f57e2426 | 119 | static void alsa_sound_perror (char *, int) NO_RETURN; |
361358ea | 120 | #endif |
f57e2426 J |
121 | static void sound_perror (char *) NO_RETURN; |
122 | static void sound_warning (char *); | |
123 | static int parse_sound (Lisp_Object, Lisp_Object *); | |
f60ae425 BK |
124 | |
125 | /* END: Common Definitions */ | |
126 | ||
127 | /* BEGIN: Non Windows Definitions */ | |
128 | #ifndef WINDOWSNT | |
80fcd514 KR |
129 | |
130 | #ifndef DEFAULT_SOUND_DEVICE | |
131 | #define DEFAULT_SOUND_DEVICE "/dev/dsp" | |
132 | #endif | |
2d2643f6 JD |
133 | #ifndef DEFAULT_ALSA_SOUND_DEVICE |
134 | #define DEFAULT_ALSA_SOUND_DEVICE "default" | |
135 | #endif | |
7840ced1 | 136 | |
7840ced1 GM |
137 | |
138 | /* Structure forward declarations. */ | |
139 | ||
d1299cde | 140 | struct sound; |
7840ced1 GM |
141 | struct sound_device; |
142 | ||
143 | /* The file header of RIFF-WAVE files (*.wav). Files are always in | |
144 | little-endian byte-order. */ | |
145 | ||
146 | struct wav_header | |
147 | { | |
148 | u_int32_t magic; | |
149 | u_int32_t length; | |
150 | u_int32_t chunk_type; | |
151 | u_int32_t chunk_format; | |
152 | u_int32_t chunk_length; | |
153 | u_int16_t format; | |
154 | u_int16_t channels; | |
155 | u_int32_t sample_rate; | |
156 | u_int32_t bytes_per_second; | |
157 | u_int16_t sample_size; | |
158 | u_int16_t precision; | |
159 | u_int32_t chunk_data; | |
160 | u_int32_t data_length; | |
161 | }; | |
162 | ||
163 | /* The file header of Sun adio files (*.au). Files are always in | |
164 | big-endian byte-order. */ | |
165 | ||
166 | struct au_header | |
167 | { | |
168 | /* ASCII ".snd" */ | |
169 | u_int32_t magic_number; | |
a4ff5d67 | 170 | |
7840ced1 GM |
171 | /* Offset of data part from start of file. Minimum value is 24. */ |
172 | u_int32_t data_offset; | |
a4ff5d67 | 173 | |
7840ced1 GM |
174 | /* Size of data part, 0xffffffff if unknown. */ |
175 | u_int32_t data_size; | |
176 | ||
177 | /* Data encoding format. | |
178 | 1 8-bit ISDN u-law | |
179 | 2 8-bit linear PCM (REF-PCM) | |
180 | 3 16-bit linear PCM | |
181 | 4 24-bit linear PCM | |
182 | 5 32-bit linear PCM | |
183 | 6 32-bit IEEE floating-point | |
184 | 7 64-bit IEEE floating-point | |
185 | 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data | |
186 | encoding scheme. */ | |
187 | u_int32_t encoding; | |
188 | ||
189 | /* Number of samples per second. */ | |
190 | u_int32_t sample_rate; | |
191 | ||
192 | /* Number of interleaved channels. */ | |
193 | u_int32_t channels; | |
194 | }; | |
195 | ||
196 | /* Maximum of all sound file headers sizes. */ | |
197 | ||
198 | #define MAX_SOUND_HEADER_BYTES \ | |
199 | max (sizeof (struct wav_header), sizeof (struct au_header)) | |
200 | ||
201 | /* Interface structure for sound devices. */ | |
202 | ||
203 | struct sound_device | |
204 | { | |
205 | /* The name of the device or null meaning use a default device name. */ | |
206 | char *file; | |
207 | ||
208 | /* File descriptor of the device. */ | |
209 | int fd; | |
210 | ||
211 | /* Device-dependent format. */ | |
212 | int format; | |
213 | ||
214 | /* Volume (0..100). Zero means unspecified. */ | |
215 | int volume; | |
216 | ||
217 | /* Sample size. */ | |
218 | int sample_size; | |
219 | ||
220 | /* Sample rate. */ | |
221 | int sample_rate; | |
222 | ||
223 | /* Bytes per second. */ | |
224 | int bps; | |
225 | ||
226 | /* 1 = mono, 2 = stereo, 0 = don't set. */ | |
227 | int channels; | |
a4ff5d67 | 228 | |
7840ced1 | 229 | /* Open device SD. */ |
f57e2426 | 230 | void (* open) (struct sound_device *sd); |
7840ced1 GM |
231 | |
232 | /* Close device SD. */ | |
f57e2426 | 233 | void (* close) (struct sound_device *sd); |
7840ced1 GM |
234 | |
235 | /* Configure SD accoring to device-dependent parameters. */ | |
f57e2426 | 236 | void (* configure) (struct sound_device *device); |
a4ff5d67 | 237 | |
d1299cde | 238 | /* Choose a device-dependent format for outputting sound S. */ |
f57e2426 J |
239 | void (* choose_format) (struct sound_device *sd, |
240 | struct sound *s); | |
7840ced1 | 241 | |
2d2643f6 JD |
242 | /* Return a preferred data size in bytes to be sent to write (below) |
243 | each time. 2048 is used if this is NULL. */ | |
f57e2426 | 244 | int (* period_size) (struct sound_device *sd); |
2d2643f6 | 245 | |
7840ced1 | 246 | /* Write NYBTES bytes from BUFFER to device SD. */ |
f57e2426 J |
247 | void (* write) (struct sound_device *sd, const char *buffer, |
248 | int nbytes); | |
7840ced1 GM |
249 | |
250 | /* A place for devices to store additional data. */ | |
251 | void *data; | |
252 | }; | |
253 | ||
254 | /* An enumerator for each supported sound file type. */ | |
255 | ||
256 | enum sound_type | |
257 | { | |
258 | RIFF, | |
259 | SUN_AUDIO | |
260 | }; | |
261 | ||
262 | /* Interface structure for sound files. */ | |
263 | ||
d1299cde | 264 | struct sound |
7840ced1 GM |
265 | { |
266 | /* The type of the file. */ | |
267 | enum sound_type type; | |
268 | ||
d1299cde | 269 | /* File descriptor of a sound file. */ |
7840ced1 GM |
270 | int fd; |
271 | ||
d1299cde GM |
272 | /* Pointer to sound file header. This contains header_size bytes |
273 | read from the start of a sound file. */ | |
7840ced1 GM |
274 | char *header; |
275 | ||
d1299cde GM |
276 | /* Number of bytes raed from sound file. This is always <= |
277 | MAX_SOUND_HEADER_BYTES. */ | |
278 | int header_size; | |
279 | ||
280 | /* Sound data, if a string. */ | |
281 | Lisp_Object data; | |
282 | ||
283 | /* Play sound file S on device SD. */ | |
f57e2426 | 284 | void (* play) (struct sound *s, struct sound_device *sd); |
7840ced1 GM |
285 | }; |
286 | ||
14e415e7 | 287 | /* These are set during `play-sound-internal' so that sound_cleanup has |
7840ced1 GM |
288 | access to them. */ |
289 | ||
d1299cde GM |
290 | struct sound_device *current_sound_device; |
291 | struct sound *current_sound; | |
7840ced1 GM |
292 | |
293 | /* Function prototypes. */ | |
294 | ||
f57e2426 J |
295 | static void vox_open (struct sound_device *); |
296 | static void vox_configure (struct sound_device *); | |
297 | static void vox_close (struct sound_device *sd); | |
298 | static void vox_choose_format (struct sound_device *, struct sound *); | |
299 | static int vox_init (struct sound_device *); | |
300 | static void vox_write (struct sound_device *, const char *, int); | |
301 | static void find_sound_type (struct sound *); | |
302 | static u_int32_t le2hl (u_int32_t); | |
303 | static u_int16_t le2hs (u_int16_t); | |
304 | static u_int32_t be2hl (u_int32_t); | |
305 | static int wav_init (struct sound *); | |
306 | static void wav_play (struct sound *, struct sound_device *); | |
307 | static int au_init (struct sound *); | |
308 | static void au_play (struct sound *, struct sound_device *); | |
7840ced1 | 309 | |
9680b9d0 | 310 | #if 0 /* Currently not used. */ |
f57e2426 | 311 | static u_int16_t be2hs (u_int16_t); |
9680b9d0 GM |
312 | #endif |
313 | ||
f60ae425 BK |
314 | /* END: Non Windows Definitions */ |
315 | #else /* WINDOWSNT */ | |
316 | ||
317 | /* BEGIN: Windows Specific Definitions */ | |
f57e2426 | 318 | static int do_play_sound (const char *, unsigned long); |
f60ae425 BK |
319 | /* |
320 | END: Windows Specific Definitions */ | |
321 | #endif /* WINDOWSNT */ | |
7840ced1 GM |
322 | |
323 | \f | |
324 | /*********************************************************************** | |
325 | General | |
326 | ***********************************************************************/ | |
327 | ||
f60ae425 BK |
328 | /* BEGIN: Common functions */ |
329 | ||
7840ced1 GM |
330 | /* Like perror, but signals an error. */ |
331 | ||
332 | static void | |
971de7fb | 333 | sound_perror (char *msg) |
7840ced1 | 334 | { |
3297e2a1 AS |
335 | int saved_errno = errno; |
336 | ||
62725a92 GM |
337 | turn_on_atimers (1); |
338 | #ifdef SIGIO | |
339 | sigunblock (sigmask (SIGIO)); | |
340 | #endif | |
3297e2a1 AS |
341 | if (saved_errno != 0) |
342 | error ("%s: %s", msg, strerror (saved_errno)); | |
62725a92 GM |
343 | else |
344 | error ("%s", msg); | |
345 | } | |
346 | ||
347 | ||
348 | /* Display a warning message. */ | |
349 | ||
350 | static void | |
971de7fb | 351 | sound_warning (char *msg) |
62725a92 GM |
352 | { |
353 | message (msg); | |
7840ced1 GM |
354 | } |
355 | ||
356 | ||
357 | /* Parse sound specification SOUND, and fill ATTRS with what is | |
358 | found. Value is non-zero if SOUND Is a valid sound specification. | |
359 | A valid sound specification is a list starting with the symbol | |
360 | `sound'. The rest of the list is a property list which may | |
361 | contain the following key/value pairs: | |
362 | ||
363 | - `:file FILE' | |
364 | ||
365 | FILE is the sound file to play. If it isn't an absolute name, | |
366 | it's searched under `data-directory'. | |
367 | ||
d1299cde GM |
368 | - `:data DATA' |
369 | ||
370 | DATA is a string containing sound data. Either :file or :data | |
371 | may be present, but not both. | |
372 | ||
7840ced1 GM |
373 | - `:device DEVICE' |
374 | ||
375 | DEVICE is the name of the device to play on, e.g. "/dev/dsp2". | |
376 | If not specified, a default device is used. | |
377 | ||
378 | - `:volume VOL' | |
379 | ||
2a53558d GM |
380 | VOL must be an integer in the range [0, 100], or a float in the |
381 | range [0, 1]. */ | |
7840ced1 GM |
382 | |
383 | static int | |
971de7fb | 384 | parse_sound (Lisp_Object sound, Lisp_Object *attrs) |
7840ced1 GM |
385 | { |
386 | /* SOUND must be a list starting with the symbol `sound'. */ | |
387 | if (!CONSP (sound) || !EQ (XCAR (sound), Qsound)) | |
388 | return 0; | |
389 | ||
390 | sound = XCDR (sound); | |
391 | attrs[SOUND_FILE] = Fplist_get (sound, QCfile); | |
d1299cde | 392 | attrs[SOUND_DATA] = Fplist_get (sound, QCdata); |
7840ced1 GM |
393 | attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice); |
394 | attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume); | |
395 | ||
f60ae425 | 396 | #ifndef WINDOWSNT |
d1299cde GM |
397 | /* File name or data must be specified. */ |
398 | if (!STRINGP (attrs[SOUND_FILE]) | |
399 | && !STRINGP (attrs[SOUND_DATA])) | |
7840ced1 | 400 | return 0; |
f60ae425 BK |
401 | #else /* WINDOWSNT */ |
402 | /* | |
403 | Data is not supported in Windows. Therefore a | |
404 | File name MUST be supplied. | |
405 | */ | |
406 | if (!STRINGP (attrs[SOUND_FILE])) | |
407 | { | |
408 | return 0; | |
409 | } | |
410 | #endif /* WINDOWSNT */ | |
7840ced1 GM |
411 | |
412 | /* Volume must be in the range 0..100 or unspecified. */ | |
413 | if (!NILP (attrs[SOUND_VOLUME])) | |
414 | { | |
2a53558d GM |
415 | if (INTEGERP (attrs[SOUND_VOLUME])) |
416 | { | |
417 | if (XINT (attrs[SOUND_VOLUME]) < 0 | |
418 | || XINT (attrs[SOUND_VOLUME]) > 100) | |
419 | return 0; | |
420 | } | |
421 | else if (FLOATP (attrs[SOUND_VOLUME])) | |
422 | { | |
423 | if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0 | |
424 | || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1) | |
425 | return 0; | |
426 | } | |
427 | else | |
7840ced1 GM |
428 | return 0; |
429 | } | |
430 | ||
f60ae425 | 431 | #ifndef WINDOWSNT |
7840ced1 GM |
432 | /* Device must be a string or unspecified. */ |
433 | if (!NILP (attrs[SOUND_DEVICE]) | |
434 | && !STRINGP (attrs[SOUND_DEVICE])) | |
435 | return 0; | |
f60ae425 BK |
436 | #endif /* WINDOWSNT */ |
437 | /* | |
438 | Since device is ignored in Windows, it does not matter | |
439 | what it is. | |
440 | */ | |
7840ced1 GM |
441 | return 1; |
442 | } | |
443 | ||
f60ae425 BK |
444 | /* END: Common functions */ |
445 | ||
446 | /* BEGIN: Non Windows functions */ | |
447 | #ifndef WINDOWSNT | |
7840ced1 GM |
448 | |
449 | /* Find out the type of the sound file whose file descriptor is FD. | |
d1299cde | 450 | S is the sound file structure to fill in. */ |
7840ced1 GM |
451 | |
452 | static void | |
971de7fb | 453 | find_sound_type (struct sound *s) |
7840ced1 | 454 | { |
d1299cde GM |
455 | if (!wav_init (s) && !au_init (s)) |
456 | error ("Unknown sound format"); | |
7840ced1 GM |
457 | } |
458 | ||
459 | ||
14e415e7 | 460 | /* Function installed by play-sound-internal with record_unwind_protect. */ |
7840ced1 GM |
461 | |
462 | static Lisp_Object | |
971de7fb | 463 | sound_cleanup (Lisp_Object arg) |
7840ced1 | 464 | { |
8e6ec322 RS |
465 | if (current_sound_device->close) |
466 | current_sound_device->close (current_sound_device); | |
467 | if (current_sound->fd > 0) | |
468 | emacs_close (current_sound->fd); | |
469 | free (current_sound_device); | |
470 | free (current_sound); | |
3887b449 GM |
471 | |
472 | return Qnil; | |
7840ced1 GM |
473 | } |
474 | ||
7840ced1 GM |
475 | /*********************************************************************** |
476 | Byte-order Conversion | |
477 | ***********************************************************************/ | |
478 | ||
479 | /* Convert 32-bit value VALUE which is in little-endian byte-order | |
480 | to host byte-order. */ | |
481 | ||
482 | static u_int32_t | |
971de7fb | 483 | le2hl (u_int32_t value) |
7840ced1 GM |
484 | { |
485 | #ifdef WORDS_BIG_ENDIAN | |
486 | unsigned char *p = (unsigned char *) &value; | |
487 | value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); | |
488 | #endif | |
489 | return value; | |
490 | } | |
491 | ||
492 | ||
493 | /* Convert 16-bit value VALUE which is in little-endian byte-order | |
494 | to host byte-order. */ | |
495 | ||
496 | static u_int16_t | |
971de7fb | 497 | le2hs (u_int16_t value) |
7840ced1 GM |
498 | { |
499 | #ifdef WORDS_BIG_ENDIAN | |
500 | unsigned char *p = (unsigned char *) &value; | |
501 | value = p[0] + (p[1] << 8); | |
502 | #endif | |
503 | return value; | |
504 | } | |
505 | ||
506 | ||
507 | /* Convert 32-bit value VALUE which is in big-endian byte-order | |
508 | to host byte-order. */ | |
509 | ||
510 | static u_int32_t | |
971de7fb | 511 | be2hl (u_int32_t value) |
7840ced1 GM |
512 | { |
513 | #ifndef WORDS_BIG_ENDIAN | |
514 | unsigned char *p = (unsigned char *) &value; | |
515 | value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24); | |
516 | #endif | |
517 | return value; | |
518 | } | |
519 | ||
520 | ||
9680b9d0 GM |
521 | #if 0 /* Currently not used. */ |
522 | ||
7840ced1 GM |
523 | /* Convert 16-bit value VALUE which is in big-endian byte-order |
524 | to host byte-order. */ | |
525 | ||
526 | static u_int16_t | |
7c3320d8 | 527 | be2hs (u_int16_t value) |
7840ced1 GM |
528 | { |
529 | #ifndef WORDS_BIG_ENDIAN | |
530 | unsigned char *p = (unsigned char *) &value; | |
531 | value = p[1] + (p[0] << 8); | |
532 | #endif | |
533 | return value; | |
534 | } | |
535 | ||
9680b9d0 | 536 | #endif /* 0 */ |
7840ced1 | 537 | |
7840ced1 GM |
538 | /*********************************************************************** |
539 | RIFF-WAVE (*.wav) | |
540 | ***********************************************************************/ | |
541 | ||
d1299cde | 542 | /* Try to initialize sound file S from S->header. S->header |
7840ced1 GM |
543 | contains the first MAX_SOUND_HEADER_BYTES number of bytes from the |
544 | sound file. If the file is a WAV-format file, set up interface | |
d1299cde | 545 | functions in S and convert header fields to host byte-order. |
7840ced1 GM |
546 | Value is non-zero if the file is a WAV file. */ |
547 | ||
548 | static int | |
971de7fb | 549 | wav_init (struct sound *s) |
7840ced1 | 550 | { |
d1299cde GM |
551 | struct wav_header *header = (struct wav_header *) s->header; |
552 | ||
553 | if (s->header_size < sizeof *header | |
72af86bd | 554 | || memcmp (s->header, "RIFF", 4) != 0) |
7840ced1 GM |
555 | return 0; |
556 | ||
557 | /* WAV files are in little-endian order. Convert the header | |
558 | if on a big-endian machine. */ | |
559 | header->magic = le2hl (header->magic); | |
560 | header->length = le2hl (header->length); | |
561 | header->chunk_type = le2hl (header->chunk_type); | |
562 | header->chunk_format = le2hl (header->chunk_format); | |
563 | header->chunk_length = le2hl (header->chunk_length); | |
564 | header->format = le2hs (header->format); | |
565 | header->channels = le2hs (header->channels); | |
566 | header->sample_rate = le2hl (header->sample_rate); | |
567 | header->bytes_per_second = le2hl (header->bytes_per_second); | |
568 | header->sample_size = le2hs (header->sample_size); | |
569 | header->precision = le2hs (header->precision); | |
570 | header->chunk_data = le2hl (header->chunk_data); | |
571 | header->data_length = le2hl (header->data_length); | |
572 | ||
573 | /* Set up the interface functions for WAV. */ | |
d1299cde GM |
574 | s->type = RIFF; |
575 | s->play = wav_play; | |
7840ced1 GM |
576 | |
577 | return 1; | |
a4ff5d67 | 578 | } |
7840ced1 GM |
579 | |
580 | ||
d1299cde | 581 | /* Play RIFF-WAVE audio file S on sound device SD. */ |
7840ced1 GM |
582 | |
583 | static void | |
971de7fb | 584 | wav_play (struct sound *s, struct sound_device *sd) |
7840ced1 | 585 | { |
d1299cde | 586 | struct wav_header *header = (struct wav_header *) s->header; |
7840ced1 GM |
587 | |
588 | /* Let the device choose a suitable device-dependent format | |
589 | for the file. */ | |
d1299cde | 590 | sd->choose_format (sd, s); |
a4ff5d67 | 591 | |
7840ced1 GM |
592 | /* Configure the device. */ |
593 | sd->sample_size = header->sample_size; | |
594 | sd->sample_rate = header->sample_rate; | |
595 | sd->bps = header->bytes_per_second; | |
596 | sd->channels = header->channels; | |
597 | sd->configure (sd); | |
598 | ||
599 | /* Copy sound data to the device. The WAV file specification is | |
600 | actually more complex. This simple scheme worked with all WAV | |
601 | files I found so far. If someone feels inclined to implement the | |
602 | whole RIFF-WAVE spec, please do. */ | |
d1299cde | 603 | if (STRINGP (s->data)) |
d5db4077 KR |
604 | sd->write (sd, SDATA (s->data) + sizeof *header, |
605 | SBYTES (s->data) - sizeof *header); | |
d1299cde GM |
606 | else |
607 | { | |
608 | char *buffer; | |
609 | int nbytes; | |
2d2643f6 | 610 | int blksize = sd->period_size ? sd->period_size (sd) : 2048; |
a28de257 | 611 | int data_left = header->data_length; |
a4ff5d67 | 612 | |
d1299cde GM |
613 | buffer = (char *) alloca (blksize); |
614 | lseek (s->fd, sizeof *header, SEEK_SET); | |
a28de257 JD |
615 | while (data_left > 0 |
616 | && (nbytes = emacs_read (s->fd, buffer, blksize)) > 0) | |
617 | { | |
618 | /* Don't play possible garbage at the end of file */ | |
619 | if (data_left < nbytes) nbytes = data_left; | |
620 | data_left -= nbytes; | |
621 | sd->write (sd, buffer, nbytes); | |
622 | } | |
7840ced1 | 623 | |
d1299cde | 624 | if (nbytes < 0) |
62725a92 | 625 | sound_perror ("Error reading sound file"); |
d1299cde | 626 | } |
7840ced1 GM |
627 | } |
628 | ||
629 | ||
7840ced1 GM |
630 | /*********************************************************************** |
631 | Sun Audio (*.au) | |
632 | ***********************************************************************/ | |
633 | ||
a4ff5d67 | 634 | /* Sun audio file encodings. */ |
7840ced1 GM |
635 | |
636 | enum au_encoding | |
637 | { | |
638 | AU_ENCODING_ULAW_8 = 1, | |
639 | AU_ENCODING_8, | |
640 | AU_ENCODING_16, | |
641 | AU_ENCODING_24, | |
642 | AU_ENCODING_32, | |
643 | AU_ENCODING_IEEE32, | |
644 | AU_ENCODING_IEEE64, | |
2d2643f6 JD |
645 | AU_COMPRESSED = 23, |
646 | AU_ENCODING_ALAW_8 = 27 | |
7840ced1 GM |
647 | }; |
648 | ||
649 | ||
d1299cde | 650 | /* Try to initialize sound file S from S->header. S->header |
7840ced1 GM |
651 | contains the first MAX_SOUND_HEADER_BYTES number of bytes from the |
652 | sound file. If the file is a AU-format file, set up interface | |
d1299cde | 653 | functions in S and convert header fields to host byte-order. |
7840ced1 GM |
654 | Value is non-zero if the file is an AU file. */ |
655 | ||
656 | static int | |
971de7fb | 657 | au_init (struct sound *s) |
7840ced1 | 658 | { |
d1299cde | 659 | struct au_header *header = (struct au_header *) s->header; |
a4ff5d67 | 660 | |
d1299cde | 661 | if (s->header_size < sizeof *header |
72af86bd | 662 | || memcmp (s->header, ".snd", 4) != 0) |
7840ced1 | 663 | return 0; |
a4ff5d67 | 664 | |
7840ced1 GM |
665 | header->magic_number = be2hl (header->magic_number); |
666 | header->data_offset = be2hl (header->data_offset); | |
667 | header->data_size = be2hl (header->data_size); | |
668 | header->encoding = be2hl (header->encoding); | |
669 | header->sample_rate = be2hl (header->sample_rate); | |
670 | header->channels = be2hl (header->channels); | |
a4ff5d67 | 671 | |
7840ced1 | 672 | /* Set up the interface functions for AU. */ |
d1299cde GM |
673 | s->type = SUN_AUDIO; |
674 | s->play = au_play; | |
7840ced1 GM |
675 | |
676 | return 1; | |
677 | } | |
678 | ||
679 | ||
d1299cde | 680 | /* Play Sun audio file S on sound device SD. */ |
7840ced1 GM |
681 | |
682 | static void | |
971de7fb | 683 | au_play (struct sound *s, struct sound_device *sd) |
7840ced1 | 684 | { |
d1299cde | 685 | struct au_header *header = (struct au_header *) s->header; |
7840ced1 GM |
686 | |
687 | sd->sample_size = 0; | |
688 | sd->sample_rate = header->sample_rate; | |
689 | sd->bps = 0; | |
690 | sd->channels = header->channels; | |
d1299cde | 691 | sd->choose_format (sd, s); |
7840ced1 | 692 | sd->configure (sd); |
d1299cde GM |
693 | |
694 | if (STRINGP (s->data)) | |
d5db4077 KR |
695 | sd->write (sd, SDATA (s->data) + header->data_offset, |
696 | SBYTES (s->data) - header->data_offset); | |
d1299cde GM |
697 | else |
698 | { | |
2d2643f6 | 699 | int blksize = sd->period_size ? sd->period_size (sd) : 2048; |
d1299cde GM |
700 | char *buffer; |
701 | int nbytes; | |
a4ff5d67 | 702 | |
d1299cde GM |
703 | /* Seek */ |
704 | lseek (s->fd, header->data_offset, SEEK_SET); | |
a4ff5d67 | 705 | |
d1299cde GM |
706 | /* Copy sound data to the device. */ |
707 | buffer = (char *) alloca (blksize); | |
708 | while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0) | |
709 | sd->write (sd, buffer, nbytes); | |
a4ff5d67 | 710 | |
d1299cde | 711 | if (nbytes < 0) |
62725a92 | 712 | sound_perror ("Error reading sound file"); |
d1299cde | 713 | } |
7840ced1 GM |
714 | } |
715 | ||
716 | ||
7840ced1 GM |
717 | /*********************************************************************** |
718 | Voxware Driver Interface | |
719 | ***********************************************************************/ | |
720 | ||
721 | /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD | |
722 | has a compatible own driver aka Luigi's driver. */ | |
723 | ||
724 | ||
725 | /* Open device SD. If SD->file is non-null, open that device, | |
726 | otherwise use a default device name. */ | |
727 | ||
728 | static void | |
971de7fb | 729 | vox_open (struct sound_device *sd) |
7840ced1 GM |
730 | { |
731 | char *file; | |
a4ff5d67 | 732 | |
7840ced1 GM |
733 | /* Open the sound device. Default is /dev/dsp. */ |
734 | if (sd->file) | |
735 | file = sd->file; | |
736 | else | |
80fcd514 | 737 | file = DEFAULT_SOUND_DEVICE; |
a4ff5d67 | 738 | |
68c45bf0 | 739 | sd->fd = emacs_open (file, O_WRONLY, 0); |
7840ced1 GM |
740 | if (sd->fd < 0) |
741 | sound_perror (file); | |
742 | } | |
743 | ||
744 | ||
745 | /* Configure device SD from parameters in it. */ | |
746 | ||
747 | static void | |
971de7fb | 748 | vox_configure (struct sound_device *sd) |
7840ced1 | 749 | { |
28fcb7dc | 750 | int val; |
a4ff5d67 | 751 | |
7840ced1 GM |
752 | xassert (sd->fd >= 0); |
753 | ||
bb6e8cee GM |
754 | /* On GNU/Linux, it seems that the device driver doesn't like to be |
755 | interrupted by a signal. Block the ones we know to cause | |
756 | troubles. */ | |
b5cb1ada | 757 | turn_on_atimers (0); |
bb6e8cee GM |
758 | #ifdef SIGIO |
759 | sigblock (sigmask (SIGIO)); | |
760 | #endif | |
b5cb1ada | 761 | |
28fcb7dc GM |
762 | val = sd->format; |
763 | if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0 | |
764 | || val != sd->format) | |
62725a92 | 765 | sound_perror ("Could not set sound format"); |
7840ced1 | 766 | |
28fcb7dc GM |
767 | val = sd->channels != 1; |
768 | if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0 | |
769 | || val != (sd->channels != 1)) | |
62725a92 | 770 | sound_perror ("Could not set stereo/mono"); |
7840ced1 | 771 | |
28fcb7dc GM |
772 | /* I think bps and sampling_rate are the same, but who knows. |
773 | Check this. and use SND_DSP_SPEED for both. */ | |
774 | if (sd->sample_rate > 0) | |
775 | { | |
776 | val = sd->sample_rate; | |
62725a92 GM |
777 | if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0) |
778 | sound_perror ("Could not set sound speed"); | |
779 | else if (val != sd->sample_rate) | |
780 | sound_warning ("Could not set sample rate"); | |
28fcb7dc | 781 | } |
7840ced1 | 782 | |
3887b449 GM |
783 | if (sd->volume > 0) |
784 | { | |
785 | int volume = sd->volume & 0xff; | |
786 | volume |= volume << 8; | |
28fcb7dc GM |
787 | /* This may fail if there is no mixer. Ignore the failure. */ |
788 | ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume); | |
3887b449 | 789 | } |
a4ff5d67 | 790 | |
b5cb1ada | 791 | turn_on_atimers (1); |
bb6e8cee GM |
792 | #ifdef SIGIO |
793 | sigunblock (sigmask (SIGIO)); | |
794 | #endif | |
7840ced1 GM |
795 | } |
796 | ||
797 | ||
798 | /* Close device SD if it is open. */ | |
799 | ||
800 | static void | |
971de7fb | 801 | vox_close (struct sound_device *sd) |
7840ced1 GM |
802 | { |
803 | if (sd->fd >= 0) | |
804 | { | |
bb6e8cee GM |
805 | /* On GNU/Linux, it seems that the device driver doesn't like to |
806 | be interrupted by a signal. Block the ones we know to cause | |
807 | troubles. */ | |
808 | #ifdef SIGIO | |
809 | sigblock (sigmask (SIGIO)); | |
810 | #endif | |
b5cb1ada | 811 | turn_on_atimers (0); |
a4ff5d67 | 812 | |
bb6e8cee | 813 | /* Flush sound data, and reset the device. */ |
7840ced1 | 814 | ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL); |
a4ff5d67 | 815 | |
b5cb1ada | 816 | turn_on_atimers (1); |
bb6e8cee GM |
817 | #ifdef SIGIO |
818 | sigunblock (sigmask (SIGIO)); | |
819 | #endif | |
7840ced1 GM |
820 | |
821 | /* Close the device. */ | |
68c45bf0 | 822 | emacs_close (sd->fd); |
7840ced1 GM |
823 | sd->fd = -1; |
824 | } | |
825 | } | |
826 | ||
827 | ||
d1299cde | 828 | /* Choose device-dependent format for device SD from sound file S. */ |
7840ced1 GM |
829 | |
830 | static void | |
971de7fb | 831 | vox_choose_format (struct sound_device *sd, struct sound *s) |
7840ced1 | 832 | { |
d1299cde | 833 | if (s->type == RIFF) |
7840ced1 | 834 | { |
d1299cde | 835 | struct wav_header *h = (struct wav_header *) s->header; |
7840ced1 GM |
836 | if (h->precision == 8) |
837 | sd->format = AFMT_U8; | |
838 | else if (h->precision == 16) | |
839 | sd->format = AFMT_S16_LE; | |
840 | else | |
841 | error ("Unsupported WAV file format"); | |
842 | } | |
d1299cde | 843 | else if (s->type == SUN_AUDIO) |
7840ced1 | 844 | { |
d1299cde | 845 | struct au_header *header = (struct au_header *) s->header; |
7840ced1 GM |
846 | switch (header->encoding) |
847 | { | |
848 | case AU_ENCODING_ULAW_8: | |
849 | case AU_ENCODING_IEEE32: | |
850 | case AU_ENCODING_IEEE64: | |
851 | sd->format = AFMT_MU_LAW; | |
852 | break; | |
a4ff5d67 | 853 | |
7840ced1 GM |
854 | case AU_ENCODING_8: |
855 | case AU_ENCODING_16: | |
856 | case AU_ENCODING_24: | |
857 | case AU_ENCODING_32: | |
858 | sd->format = AFMT_S16_LE; | |
859 | break; | |
860 | ||
861 | default: | |
862 | error ("Unsupported AU file format"); | |
863 | } | |
864 | } | |
865 | else | |
866 | abort (); | |
867 | } | |
868 | ||
869 | ||
870 | /* Initialize device SD. Set up the interface functions in the device | |
871 | structure. */ | |
872 | ||
2d2643f6 | 873 | static int |
971de7fb | 874 | vox_init (struct sound_device *sd) |
7840ced1 | 875 | { |
2d2643f6 JD |
876 | char *file; |
877 | int fd; | |
878 | ||
879 | /* Open the sound device. Default is /dev/dsp. */ | |
880 | if (sd->file) | |
881 | file = sd->file; | |
882 | else | |
883 | file = DEFAULT_SOUND_DEVICE; | |
884 | fd = emacs_open (file, O_WRONLY, 0); | |
885 | if (fd >= 0) | |
886 | emacs_close (fd); | |
887 | else | |
888 | return 0; | |
889 | ||
7840ced1 GM |
890 | sd->fd = -1; |
891 | sd->open = vox_open; | |
892 | sd->close = vox_close; | |
893 | sd->configure = vox_configure; | |
894 | sd->choose_format = vox_choose_format; | |
895 | sd->write = vox_write; | |
2d2643f6 JD |
896 | sd->period_size = NULL; |
897 | ||
898 | return 1; | |
7840ced1 GM |
899 | } |
900 | ||
7840ced1 GM |
901 | /* Write NBYTES bytes from BUFFER to device SD. */ |
902 | ||
903 | static void | |
971de7fb | 904 | vox_write (struct sound_device *sd, const char *buffer, int nbytes) |
7840ced1 | 905 | { |
68c45bf0 | 906 | int nwritten = emacs_write (sd->fd, buffer, nbytes); |
7840ced1 | 907 | if (nwritten < 0) |
62725a92 | 908 | sound_perror ("Error writing to sound device"); |
7840ced1 GM |
909 | } |
910 | ||
2d2643f6 JD |
911 | #ifdef HAVE_ALSA |
912 | /*********************************************************************** | |
913 | ALSA Driver Interface | |
914 | ***********************************************************************/ | |
915 | ||
916 | /* This driver is available on GNU/Linux. */ | |
917 | ||
918 | static void | |
971de7fb | 919 | alsa_sound_perror (char *msg, int err) |
2d2643f6 JD |
920 | { |
921 | error ("%s: %s", msg, snd_strerror (err)); | |
922 | } | |
923 | ||
924 | struct alsa_params | |
925 | { | |
926 | snd_pcm_t *handle; | |
927 | snd_pcm_hw_params_t *hwparams; | |
928 | snd_pcm_sw_params_t *swparams; | |
929 | snd_pcm_uframes_t period_size; | |
930 | }; | |
931 | ||
932 | /* Open device SD. If SD->file is non-null, open that device, | |
933 | otherwise use a default device name. */ | |
934 | ||
935 | static void | |
971de7fb | 936 | alsa_open (struct sound_device *sd) |
2d2643f6 JD |
937 | { |
938 | char *file; | |
939 | struct alsa_params *p; | |
940 | int err; | |
941 | ||
942 | /* Open the sound device. Default is "default". */ | |
943 | if (sd->file) | |
944 | file = sd->file; | |
945 | else | |
946 | file = DEFAULT_ALSA_SOUND_DEVICE; | |
947 | ||
948 | p = xmalloc (sizeof (*p)); | |
949 | p->handle = NULL; | |
950 | p->hwparams = NULL; | |
951 | p->swparams = NULL; | |
952 | ||
953 | sd->fd = -1; | |
954 | sd->data = p; | |
955 | ||
956 | ||
3fc7a865 JD |
957 | err = snd_pcm_open (&p->handle, file, SND_PCM_STREAM_PLAYBACK, 0); |
958 | if (err < 0) | |
2d2643f6 JD |
959 | alsa_sound_perror (file, err); |
960 | } | |
961 | ||
962 | static int | |
971de7fb | 963 | alsa_period_size (struct sound_device *sd) |
2d2643f6 JD |
964 | { |
965 | struct alsa_params *p = (struct alsa_params *) sd->data; | |
a28de257 JD |
966 | int fact = snd_pcm_format_size (sd->format, 1) * sd->channels; |
967 | return p->period_size * (fact > 0 ? fact : 1); | |
2d2643f6 JD |
968 | } |
969 | ||
970 | static void | |
971de7fb | 971 | alsa_configure (struct sound_device *sd) |
2d2643f6 JD |
972 | { |
973 | int val, err, dir; | |
dcc88121 | 974 | unsigned uval; |
2d2643f6 JD |
975 | struct alsa_params *p = (struct alsa_params *) sd->data; |
976 | snd_pcm_uframes_t buffer_size; | |
977 | ||
978 | xassert (p->handle != 0); | |
979 | ||
3fc7a865 JD |
980 | err = snd_pcm_hw_params_malloc (&p->hwparams); |
981 | if (err < 0) | |
2d2643f6 JD |
982 | alsa_sound_perror ("Could not allocate hardware parameter structure", err); |
983 | ||
3fc7a865 JD |
984 | err = snd_pcm_sw_params_malloc (&p->swparams); |
985 | if (err < 0) | |
2d2643f6 JD |
986 | alsa_sound_perror ("Could not allocate software parameter structure", err); |
987 | ||
3fc7a865 JD |
988 | err = snd_pcm_hw_params_any (p->handle, p->hwparams); |
989 | if (err < 0) | |
2d2643f6 JD |
990 | alsa_sound_perror ("Could not initialize hardware parameter structure", err); |
991 | ||
3fc7a865 JD |
992 | err = snd_pcm_hw_params_set_access (p->handle, p->hwparams, |
993 | SND_PCM_ACCESS_RW_INTERLEAVED); | |
994 | if (err < 0) | |
2d2643f6 JD |
995 | alsa_sound_perror ("Could not set access type", err); |
996 | ||
997 | val = sd->format; | |
3fc7a865 | 998 | err = snd_pcm_hw_params_set_format (p->handle, p->hwparams, val); |
ed1f0a45 | 999 | if (err < 0) |
2d2643f6 JD |
1000 | alsa_sound_perror ("Could not set sound format", err); |
1001 | ||
dcc88121 JD |
1002 | uval = sd->sample_rate; |
1003 | err = snd_pcm_hw_params_set_rate_near (p->handle, p->hwparams, &uval, 0); | |
3fc7a865 | 1004 | if (err < 0) |
2d2643f6 | 1005 | alsa_sound_perror ("Could not set sample rate", err); |
ed1f0a45 | 1006 | |
2d2643f6 | 1007 | val = sd->channels; |
3fc7a865 JD |
1008 | err = snd_pcm_hw_params_set_channels (p->handle, p->hwparams, val); |
1009 | if (err < 0) | |
2d2643f6 JD |
1010 | alsa_sound_perror ("Could not set channel count", err); |
1011 | ||
3fc7a865 JD |
1012 | err = snd_pcm_hw_params (p->handle, p->hwparams); |
1013 | if (err < 0) | |
07a7837c JD |
1014 | alsa_sound_perror ("Could not set parameters", err); |
1015 | ||
2d2643f6 JD |
1016 | |
1017 | err = snd_pcm_hw_params_get_period_size (p->hwparams, &p->period_size, &dir); | |
1018 | if (err < 0) | |
1019 | alsa_sound_perror ("Unable to get period size for playback", err); | |
1020 | ||
1021 | err = snd_pcm_hw_params_get_buffer_size (p->hwparams, &buffer_size); | |
1022 | if (err < 0) | |
1023 | alsa_sound_perror("Unable to get buffer size for playback", err); | |
1024 | ||
2d2643f6 JD |
1025 | err = snd_pcm_sw_params_current (p->handle, p->swparams); |
1026 | if (err < 0) | |
1027 | alsa_sound_perror ("Unable to determine current swparams for playback", | |
1028 | err); | |
1029 | ||
1030 | /* Start the transfer when the buffer is almost full */ | |
1031 | err = snd_pcm_sw_params_set_start_threshold (p->handle, p->swparams, | |
1032 | (buffer_size / p->period_size) | |
1033 | * p->period_size); | |
1034 | if (err < 0) | |
1035 | alsa_sound_perror ("Unable to set start threshold mode for playback", err); | |
1036 | ||
1037 | /* Allow the transfer when at least period_size samples can be processed */ | |
1038 | err = snd_pcm_sw_params_set_avail_min (p->handle, p->swparams, p->period_size); | |
1039 | if (err < 0) | |
1040 | alsa_sound_perror ("Unable to set avail min for playback", err); | |
1041 | ||
2d2643f6 JD |
1042 | err = snd_pcm_sw_params (p->handle, p->swparams); |
1043 | if (err < 0) | |
1044 | alsa_sound_perror ("Unable to set sw params for playback\n", err); | |
1045 | ||
1046 | snd_pcm_hw_params_free (p->hwparams); | |
1047 | p->hwparams = NULL; | |
1048 | snd_pcm_sw_params_free (p->swparams); | |
1049 | p->swparams = NULL; | |
ed1f0a45 | 1050 | |
3fc7a865 JD |
1051 | err = snd_pcm_prepare (p->handle); |
1052 | if (err < 0) | |
2d2643f6 | 1053 | alsa_sound_perror ("Could not prepare audio interface for use", err); |
ed1f0a45 | 1054 | |
2d2643f6 JD |
1055 | if (sd->volume > 0) |
1056 | { | |
1057 | int chn; | |
1058 | snd_mixer_t *handle; | |
1059 | snd_mixer_elem_t *e; | |
1060 | char *file = sd->file ? sd->file : DEFAULT_ALSA_SOUND_DEVICE; | |
1061 | ||
1062 | if (snd_mixer_open (&handle, 0) >= 0) | |
1063 | { | |
1064 | if (snd_mixer_attach (handle, file) >= 0 | |
1065 | && snd_mixer_load (handle) >= 0 | |
1066 | && snd_mixer_selem_register (handle, NULL, NULL) >= 0) | |
1067 | for (e = snd_mixer_first_elem (handle); | |
1068 | e; | |
1069 | e = snd_mixer_elem_next (e)) | |
1070 | { | |
1071 | if (snd_mixer_selem_has_playback_volume (e)) | |
1072 | { | |
646d0d12 | 1073 | long pmin, pmax, vol; |
2d2643f6 | 1074 | snd_mixer_selem_get_playback_volume_range (e, &pmin, &pmax); |
646d0d12 | 1075 | vol = pmin + (sd->volume * (pmax - pmin)) / 100; |
ed1f0a45 | 1076 | |
2d2643f6 JD |
1077 | for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++) |
1078 | snd_mixer_selem_set_playback_volume (e, chn, vol); | |
1079 | } | |
1080 | } | |
1081 | snd_mixer_close(handle); | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | ||
1087 | /* Close device SD if it is open. */ | |
1088 | ||
1089 | static void | |
971de7fb | 1090 | alsa_close (struct sound_device *sd) |
2d2643f6 JD |
1091 | { |
1092 | struct alsa_params *p = (struct alsa_params *) sd->data; | |
1093 | if (p) | |
1094 | { | |
1095 | if (p->hwparams) | |
1096 | snd_pcm_hw_params_free (p->hwparams); | |
1097 | if (p->swparams) | |
1098 | snd_pcm_sw_params_free (p->swparams); | |
1099 | if (p->handle) | |
1100 | { | |
dcc88121 | 1101 | snd_pcm_drain (p->handle); |
2d2643f6 JD |
1102 | snd_pcm_close (p->handle); |
1103 | } | |
1104 | free (p); | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | /* Choose device-dependent format for device SD from sound file S. */ | |
1109 | ||
1110 | static void | |
971de7fb | 1111 | alsa_choose_format (struct sound_device *sd, struct sound *s) |
2d2643f6 JD |
1112 | { |
1113 | struct alsa_params *p = (struct alsa_params *) sd->data; | |
1114 | if (s->type == RIFF) | |
1115 | { | |
1116 | struct wav_header *h = (struct wav_header *) s->header; | |
1117 | if (h->precision == 8) | |
1118 | sd->format = SND_PCM_FORMAT_U8; | |
1119 | else if (h->precision == 16) | |
1120 | sd->format = SND_PCM_FORMAT_S16_LE; | |
1121 | else | |
1122 | error ("Unsupported WAV file format"); | |
1123 | } | |
1124 | else if (s->type == SUN_AUDIO) | |
1125 | { | |
1126 | struct au_header *header = (struct au_header *) s->header; | |
1127 | switch (header->encoding) | |
1128 | { | |
1129 | case AU_ENCODING_ULAW_8: | |
1130 | sd->format = SND_PCM_FORMAT_MU_LAW; | |
1131 | break; | |
1132 | case AU_ENCODING_ALAW_8: | |
1133 | sd->format = SND_PCM_FORMAT_A_LAW; | |
1134 | break; | |
1135 | case AU_ENCODING_IEEE32: | |
1136 | sd->format = SND_PCM_FORMAT_FLOAT_BE; | |
1137 | break; | |
1138 | case AU_ENCODING_IEEE64: | |
1139 | sd->format = SND_PCM_FORMAT_FLOAT64_BE; | |
1140 | break; | |
1141 | case AU_ENCODING_8: | |
1142 | sd->format = SND_PCM_FORMAT_S8; | |
1143 | break; | |
1144 | case AU_ENCODING_16: | |
1145 | sd->format = SND_PCM_FORMAT_S16_BE; | |
1146 | break; | |
1147 | case AU_ENCODING_24: | |
1148 | sd->format = SND_PCM_FORMAT_S24_BE; | |
1149 | break; | |
1150 | case AU_ENCODING_32: | |
1151 | sd->format = SND_PCM_FORMAT_S32_BE; | |
1152 | break; | |
1153 | ||
1154 | default: | |
1155 | error ("Unsupported AU file format"); | |
1156 | } | |
1157 | } | |
1158 | else | |
1159 | abort (); | |
1160 | } | |
1161 | ||
1162 | ||
1163 | /* Write NBYTES bytes from BUFFER to device SD. */ | |
1164 | ||
1165 | static void | |
971de7fb | 1166 | alsa_write (struct sound_device *sd, const char *buffer, int nbytes) |
2d2643f6 JD |
1167 | { |
1168 | struct alsa_params *p = (struct alsa_params *) sd->data; | |
1169 | ||
1170 | /* The the third parameter to snd_pcm_writei is frames, not bytes. */ | |
1171 | int fact = snd_pcm_format_size (sd->format, 1) * sd->channels; | |
1172 | int nwritten = 0; | |
1173 | int err; | |
1174 | ||
1175 | while (nwritten < nbytes) | |
1176 | { | |
a28de257 JD |
1177 | snd_pcm_uframes_t frames = (nbytes - nwritten)/fact; |
1178 | if (frames == 0) break; | |
24f01470 | 1179 | |
a28de257 | 1180 | err = snd_pcm_writei (p->handle, buffer + nwritten, frames); |
3fc7a865 | 1181 | if (err < 0) |
2d2643f6 | 1182 | { |
2d2643f6 JD |
1183 | if (err == -EPIPE) |
1184 | { /* under-run */ | |
1185 | err = snd_pcm_prepare (p->handle); | |
1186 | if (err < 0) | |
1187 | alsa_sound_perror ("Can't recover from underrun, prepare failed", | |
1188 | err); | |
1189 | } | |
1190 | else if (err == -ESTRPIPE) | |
1191 | { | |
1192 | while ((err = snd_pcm_resume (p->handle)) == -EAGAIN) | |
1193 | sleep(1); /* wait until the suspend flag is released */ | |
1194 | if (err < 0) | |
1195 | { | |
1196 | err = snd_pcm_prepare (p->handle); | |
1197 | if (err < 0) | |
1198 | alsa_sound_perror ("Can't recover from suspend, " | |
1199 | "prepare failed", | |
1200 | err); | |
1201 | } | |
1202 | } | |
ed1f0a45 | 1203 | else |
2d2643f6 | 1204 | alsa_sound_perror ("Error writing to sound device", err); |
ed1f0a45 | 1205 | |
2d2643f6 JD |
1206 | } |
1207 | else | |
1208 | nwritten += err * fact; | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | static void | |
7c3320d8 JB |
1213 | snd_error_quiet (const char *file, int line, const char *function, int err, |
1214 | const char *fmt) | |
2d2643f6 JD |
1215 | { |
1216 | } | |
1217 | ||
1218 | /* Initialize device SD. Set up the interface functions in the device | |
1219 | structure. */ | |
1220 | ||
1221 | static int | |
971de7fb | 1222 | alsa_init (struct sound_device *sd) |
2d2643f6 JD |
1223 | { |
1224 | char *file; | |
1225 | snd_pcm_t *handle; | |
1226 | int err; | |
1227 | ||
1228 | /* Open the sound device. Default is "default". */ | |
1229 | if (sd->file) | |
1230 | file = sd->file; | |
1231 | else | |
1232 | file = DEFAULT_ALSA_SOUND_DEVICE; | |
1233 | ||
1234 | snd_lib_error_set_handler ((snd_lib_error_handler_t) snd_error_quiet); | |
1235 | err = snd_pcm_open (&handle, file, SND_PCM_STREAM_PLAYBACK, 0); | |
1236 | snd_lib_error_set_handler (NULL); | |
1237 | if (err < 0) | |
dcc88121 JD |
1238 | return 0; |
1239 | snd_pcm_close (handle); | |
2d2643f6 JD |
1240 | |
1241 | sd->fd = -1; | |
1242 | sd->open = alsa_open; | |
1243 | sd->close = alsa_close; | |
1244 | sd->configure = alsa_configure; | |
1245 | sd->choose_format = alsa_choose_format; | |
1246 | sd->write = alsa_write; | |
1247 | sd->period_size = alsa_period_size; | |
1248 | ||
1249 | return 1; | |
1250 | } | |
1251 | ||
1252 | #endif /* HAVE_ALSA */ | |
1253 | ||
1254 | ||
f60ae425 BK |
1255 | /* END: Non Windows functions */ |
1256 | #else /* WINDOWSNT */ | |
1257 | ||
1258 | /* BEGIN: Windows specific functions */ | |
1259 | ||
8db52afe JB |
1260 | #define SOUND_WARNING(fun, error, text) \ |
1261 | { \ | |
1262 | char buf[1024]; \ | |
1263 | char err_string[MAXERRORLENGTH]; \ | |
24f01470 | 1264 | fun (error, err_string, sizeof (err_string)); \ |
8db52afe JB |
1265 | _snprintf (buf, sizeof (buf), "%s\nError: %s", \ |
1266 | text, err_string); \ | |
1267 | sound_warning (buf); \ | |
24f01470 JB |
1268 | } |
1269 | ||
f60ae425 | 1270 | static int |
7c3320d8 | 1271 | do_play_sound (const char *psz_file, 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) */ |