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