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