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