Mercurial > emacs
annotate src/sound.c @ 25709:ba4e2a641663
(SXHASH_COMBINE): Add missing parentheses.
(Fchar_table_range, Fset_char_table_default, mapcar1,
Fyes_or_no_p, sweep_weak_hash_tables): Remove unused variable(s).
| author | Gerd Moellmann <gerd@gnu.org> |
|---|---|
| date | Tue, 14 Sep 1999 13:09:25 +0000 |
| parents | df6548ca33fd |
| children | 2be6ced279d3 |
| rev | line source |
|---|---|
| 25003 | 1 /* sound.c -- sound support. |
| 2 Copyright (C) 1998 Free Software Foundation. | |
| 3 | |
| 4 This file is part of GNU Emacs. | |
| 5 | |
| 6 GNU Emacs is free software; you can redistribute it and/or modify | |
| 7 it under the terms of the GNU General Public License as published by | |
| 8 the Free Software Foundation; either version 2, or (at your option) | |
| 9 any later version. | |
| 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 | |
| 17 along with GNU Emacs; see the file COPYING. If not, write to | |
| 18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
| 19 Boston, MA 02111-1307, USA. */ | |
| 20 | |
| 21 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's | |
| 22 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */ | |
| 23 | |
| 24 #include <config.h> | |
| 25 | |
| 26 #if defined HAVE_SOUND | |
| 27 | |
| 28 #include <lisp.h> | |
| 29 #include <fcntl.h> | |
| 30 #include <unistd.h> | |
| 31 #include <sys/types.h> | |
| 32 #include <dispextern.h> | |
| 33 #include <errno.h> | |
| 34 | |
| 35 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention | |
| 36 sys/soundcard.h. So, let's try whatever's there. */ | |
| 37 | |
| 38 #ifdef HAVE_MACHINE_SOUNDCARD_H | |
| 39 #include <machine/soundcard.h> | |
| 40 #endif | |
| 41 #ifdef HAVE_SYS_SOUNDCARD_H | |
| 42 #include <sys/soundcard.h> | |
| 43 #endif | |
| 44 | |
| 45 #define max(X, Y) ((X) > (Y) ? (X) : (Y)) | |
| 46 #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
| 47 #define abs(X) ((X) < 0 ? -(X) : (X)) | |
| 48 | |
| 49 /* Structure forward declarations. */ | |
| 50 | |
| 51 struct sound_file; | |
| 52 struct sound_device; | |
| 53 | |
| 54 /* The file header of RIFF-WAVE files (*.wav). Files are always in | |
| 55 little-endian byte-order. */ | |
| 56 | |
| 57 struct wav_header | |
| 58 { | |
| 59 u_int32_t magic; | |
| 60 u_int32_t length; | |
| 61 u_int32_t chunk_type; | |
| 62 u_int32_t chunk_format; | |
| 63 u_int32_t chunk_length; | |
| 64 u_int16_t format; | |
| 65 u_int16_t channels; | |
| 66 u_int32_t sample_rate; | |
| 67 u_int32_t bytes_per_second; | |
| 68 u_int16_t sample_size; | |
| 69 u_int16_t precision; | |
| 70 u_int32_t chunk_data; | |
| 71 u_int32_t data_length; | |
| 72 }; | |
| 73 | |
| 74 /* The file header of Sun adio files (*.au). Files are always in | |
| 75 big-endian byte-order. */ | |
| 76 | |
| 77 struct au_header | |
| 78 { | |
| 79 /* ASCII ".snd" */ | |
| 80 u_int32_t magic_number; | |
| 81 | |
| 82 /* Offset of data part from start of file. Minimum value is 24. */ | |
| 83 u_int32_t data_offset; | |
| 84 | |
| 85 /* Size of data part, 0xffffffff if unknown. */ | |
| 86 u_int32_t data_size; | |
| 87 | |
| 88 /* Data encoding format. | |
| 89 1 8-bit ISDN u-law | |
| 90 2 8-bit linear PCM (REF-PCM) | |
| 91 3 16-bit linear PCM | |
| 92 4 24-bit linear PCM | |
| 93 5 32-bit linear PCM | |
| 94 6 32-bit IEEE floating-point | |
| 95 7 64-bit IEEE floating-point | |
| 96 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data | |
| 97 encoding scheme. */ | |
| 98 u_int32_t encoding; | |
| 99 | |
| 100 /* Number of samples per second. */ | |
| 101 u_int32_t sample_rate; | |
| 102 | |
| 103 /* Number of interleaved channels. */ | |
| 104 u_int32_t channels; | |
| 105 }; | |
| 106 | |
| 107 /* Maximum of all sound file headers sizes. */ | |
| 108 | |
| 109 #define MAX_SOUND_HEADER_BYTES \ | |
| 110 max (sizeof (struct wav_header), sizeof (struct au_header)) | |
| 111 | |
| 112 /* Interface structure for sound devices. */ | |
| 113 | |
| 114 struct sound_device | |
| 115 { | |
| 116 /* The name of the device or null meaning use a default device name. */ | |
| 117 char *file; | |
| 118 | |
| 119 /* File descriptor of the device. */ | |
| 120 int fd; | |
| 121 | |
| 122 /* Device-dependent format. */ | |
| 123 int format; | |
| 124 | |
| 125 /* Volume (0..100). Zero means unspecified. */ | |
| 126 int volume; | |
| 127 | |
| 128 /* Sample size. */ | |
| 129 int sample_size; | |
| 130 | |
| 131 /* Sample rate. */ | |
| 132 int sample_rate; | |
| 133 | |
| 134 /* Bytes per second. */ | |
| 135 int bps; | |
| 136 | |
| 137 /* 1 = mono, 2 = stereo, 0 = don't set. */ | |
| 138 int channels; | |
| 139 | |
| 140 /* Open device SD. */ | |
| 141 void (* open) P_ ((struct sound_device *sd)); | |
| 142 | |
| 143 /* Close device SD. */ | |
| 144 void (* close) P_ ((struct sound_device *sd)); | |
| 145 | |
| 146 /* Configure SD accoring to device-dependent parameters. */ | |
| 147 void (* configure) P_ ((struct sound_device *device)); | |
| 148 | |
| 149 /* Choose a device-dependent format for outputting sound file SF. */ | |
| 150 void (* choose_format) P_ ((struct sound_device *sd, | |
| 151 struct sound_file *sf)); | |
| 152 | |
| 153 /* Write NYBTES bytes from BUFFER to device SD. */ | |
| 154 void (* write) P_ ((struct sound_device *sd, char *buffer, int nbytes)); | |
| 155 | |
| 156 /* A place for devices to store additional data. */ | |
| 157 void *data; | |
| 158 }; | |
| 159 | |
| 160 /* An enumerator for each supported sound file type. */ | |
| 161 | |
| 162 enum sound_type | |
| 163 { | |
| 164 RIFF, | |
| 165 SUN_AUDIO | |
| 166 }; | |
| 167 | |
| 168 /* Interface structure for sound files. */ | |
| 169 | |
| 170 struct sound_file | |
| 171 { | |
| 172 /* The type of the file. */ | |
| 173 enum sound_type type; | |
| 174 | |
| 175 /* File descriptor of the file. */ | |
| 176 int fd; | |
| 177 | |
| 178 /* Pointer to sound file header. This contains the first | |
| 179 MAX_SOUND_HEADER_BYTES read from the file. */ | |
| 180 char *header; | |
| 181 | |
| 182 /* Play sound file SF on device SD. */ | |
| 183 void (* play) P_ ((struct sound_file *sf, struct sound_device *sd)); | |
| 184 }; | |
| 185 | |
| 186 /* Indices of attributes in a sound attributes vector. */ | |
| 187 | |
| 188 enum sound_attr | |
| 189 { | |
| 190 SOUND_FILE, | |
| 191 SOUND_DEVICE, | |
| 192 SOUND_VOLUME, | |
| 193 SOUND_ATTR_SENTINEL | |
| 194 }; | |
| 195 | |
| 196 /* Symbols. */ | |
| 197 | |
| 198 extern Lisp_Object QCfile; | |
| 199 Lisp_Object QCvolume, QCdevice; | |
| 200 Lisp_Object Qsound; | |
|
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
201 Lisp_Object Qplay_sound_functions; |
| 25003 | 202 |
| 203 /* These are set during `play-sound' so that sound_cleanup has | |
| 204 access to them. */ | |
| 205 | |
| 206 struct sound_device *sound_device; | |
| 207 struct sound_file *sound_file; | |
| 208 | |
| 209 /* Function prototypes. */ | |
| 210 | |
| 211 static void vox_open P_ ((struct sound_device *)); | |
| 212 static void vox_configure P_ ((struct sound_device *)); | |
| 213 static void vox_close P_ ((struct sound_device *sd)); | |
| 214 static void vox_choose_format P_ ((struct sound_device *, struct sound_file *)); | |
| 215 static void vox_init P_ ((struct sound_device *)); | |
| 216 static void vox_write P_ ((struct sound_device *, char *, int)); | |
| 217 static void sound_perror P_ ((char *)); | |
| 218 static int parse_sound P_ ((Lisp_Object, Lisp_Object *)); | |
| 219 static void find_sound_file_type P_ ((struct sound_file *)); | |
| 220 static u_int32_t le2hl P_ ((u_int32_t)); | |
| 221 static u_int16_t le2hs P_ ((u_int16_t)); | |
| 222 static u_int32_t be2hl P_ ((u_int32_t)); | |
| 223 static u_int16_t be2hs P_ ((u_int16_t)); | |
| 224 static int wav_init P_ ((struct sound_file *)); | |
| 225 static void wav_play P_ ((struct sound_file *, struct sound_device *)); | |
| 226 static int au_init P_ ((struct sound_file *)); | |
| 227 static void au_play P_ ((struct sound_file *, struct sound_device *)); | |
| 228 | |
| 229 | |
| 230 | |
| 231 /*********************************************************************** | |
| 232 General | |
| 233 ***********************************************************************/ | |
| 234 | |
| 235 /* Like perror, but signals an error. */ | |
| 236 | |
| 237 static void | |
| 238 sound_perror (msg) | |
| 239 char *msg; | |
| 240 { | |
| 241 error ("%s: %s", msg, strerror (errno)); | |
| 242 } | |
| 243 | |
| 244 | |
| 245 /* Parse sound specification SOUND, and fill ATTRS with what is | |
| 246 found. Value is non-zero if SOUND Is a valid sound specification. | |
| 247 A valid sound specification is a list starting with the symbol | |
| 248 `sound'. The rest of the list is a property list which may | |
| 249 contain the following key/value pairs: | |
| 250 | |
| 251 - `:file FILE' | |
| 252 | |
| 253 FILE is the sound file to play. If it isn't an absolute name, | |
| 254 it's searched under `data-directory'. | |
| 255 | |
| 256 - `:device DEVICE' | |
| 257 | |
| 258 DEVICE is the name of the device to play on, e.g. "/dev/dsp2". | |
| 259 If not specified, a default device is used. | |
| 260 | |
| 261 - `:volume VOL' | |
| 262 | |
|
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
263 VOL must be an integer in the range [0, 100], or a float in the |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
264 range [0, 1]. */ |
| 25003 | 265 |
| 266 static int | |
| 267 parse_sound (sound, attrs) | |
| 268 Lisp_Object sound; | |
| 269 Lisp_Object *attrs; | |
| 270 { | |
| 271 /* SOUND must be a list starting with the symbol `sound'. */ | |
| 272 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound)) | |
| 273 return 0; | |
| 274 | |
| 275 sound = XCDR (sound); | |
| 276 attrs[SOUND_FILE] = Fplist_get (sound, QCfile); | |
| 277 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice); | |
| 278 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume); | |
| 279 | |
| 280 /* File name must be specified. */ | |
| 281 if (!STRINGP (attrs[SOUND_FILE])) | |
| 282 return 0; | |
| 283 | |
| 284 /* Volume must be in the range 0..100 or unspecified. */ | |
| 285 if (!NILP (attrs[SOUND_VOLUME])) | |
| 286 { | |
|
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
287 if (INTEGERP (attrs[SOUND_VOLUME])) |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
288 { |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
289 if (XINT (attrs[SOUND_VOLUME]) < 0 |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
290 || XINT (attrs[SOUND_VOLUME]) > 100) |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
291 return 0; |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
292 } |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
293 else if (FLOATP (attrs[SOUND_VOLUME])) |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
294 { |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
295 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0 |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
296 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1) |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
297 return 0; |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
298 } |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
299 else |
| 25003 | 300 return 0; |
| 301 } | |
| 302 | |
| 303 /* Device must be a string or unspecified. */ | |
| 304 if (!NILP (attrs[SOUND_DEVICE]) | |
| 305 && !STRINGP (attrs[SOUND_DEVICE])) | |
| 306 return 0; | |
| 307 | |
| 308 return 1; | |
| 309 } | |
| 310 | |
| 311 | |
| 312 /* Find out the type of the sound file whose file descriptor is FD. | |
| 313 SF is the sound file structure to fill in. */ | |
| 314 | |
| 315 static void | |
| 316 find_sound_file_type (sf) | |
| 317 struct sound_file *sf; | |
| 318 { | |
| 319 if (!wav_init (sf) | |
| 320 && !au_init (sf)) | |
| 321 error ("Unknown sound file format"); | |
| 322 } | |
| 323 | |
| 324 | |
| 325 /* Function installed by play-sound with record_unwind_protect. */ | |
| 326 | |
| 327 static Lisp_Object | |
| 328 sound_cleanup (arg) | |
| 329 Lisp_Object arg; | |
| 330 { | |
| 331 if (sound_device) | |
| 332 { | |
| 333 sound_device->close (sound_device); | |
| 334 if (sound_file->fd > 0) | |
| 335 close (sound_file->fd); | |
| 336 } | |
| 337 } | |
| 338 | |
| 339 | |
| 340 DEFUN ("play-sound", Fplay_sound, Splay_sound, 1, 1, 0, | |
| 341 "Play sound SOUND.") | |
| 342 (sound) | |
| 343 Lisp_Object sound; | |
| 344 { | |
| 345 Lisp_Object attrs[SOUND_ATTR_SENTINEL]; | |
| 346 char *header; | |
| 347 Lisp_Object file; | |
| 348 struct gcpro gcpro1, gcpro2; | |
| 349 int nbytes; | |
| 350 char *msg; | |
| 351 struct sound_device sd; | |
| 352 struct sound_file sf; | |
| 353 Lisp_Object args[2]; | |
| 354 int count = specpdl_ptr - specpdl; | |
| 355 | |
| 356 file = Qnil; | |
| 357 GCPRO2 (sound, file); | |
| 358 bzero (&sd, sizeof sd); | |
| 359 bzero (&sf, sizeof sf); | |
| 360 sf.header = (char *) alloca (MAX_SOUND_HEADER_BYTES); | |
| 361 | |
| 362 sound_device = &sd; | |
| 363 sound_file = &sf; | |
| 364 record_unwind_protect (sound_cleanup, Qnil); | |
| 365 | |
| 366 /* Parse the sound specification. Give up if it is invalid. */ | |
| 367 if (!parse_sound (sound, attrs)) | |
| 368 { | |
| 369 UNGCPRO; | |
| 370 error ("Invalid sound specification"); | |
| 371 } | |
| 372 | |
| 373 /* Open the sound file. */ | |
| 374 sf.fd = openp (Fcons (Vdata_directory, Qnil), | |
| 375 attrs[SOUND_FILE], "", &file, 0); | |
| 376 if (sf.fd < 0) | |
| 377 sound_perror ("Open sound file"); | |
| 378 | |
| 379 /* Read the first bytes from the file. */ | |
| 380 nbytes = read (sf.fd, sf.header, MAX_SOUND_HEADER_BYTES); | |
| 381 if (nbytes < 0) | |
| 382 sound_perror ("Reading sound file header"); | |
| 383 | |
| 384 /* Find out the type of sound file. Give up if we can't tell. */ | |
| 385 find_sound_file_type (&sf); | |
| 386 | |
| 387 /* Set up a device. */ | |
| 388 if (STRINGP (attrs[SOUND_DEVICE])) | |
| 389 { | |
| 390 int len = XSTRING (attrs[SOUND_DEVICE])->size; | |
| 391 sd.file = (char *) alloca (len + 1); | |
| 392 strcpy (sd.file, XSTRING (attrs[SOUND_DEVICE])->data); | |
| 393 } | |
| 394 if (INTEGERP (attrs[SOUND_VOLUME])) | |
| 395 sd.volume = XFASTINT (attrs[SOUND_VOLUME]); | |
|
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
396 else if (FLOATP (attrs[SOUND_VOLUME])) |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
397 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100; |
| 25003 | 398 |
|
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
399 args[0] = Qplay_sound_functions; |
| 25003 | 400 args[1] = sound; |
| 401 Frun_hook_with_args (make_number (2), args); | |
| 402 | |
| 403 vox_init (&sd); | |
| 404 sd.open (&sd); | |
| 405 | |
| 406 sf.play (&sf, &sd); | |
| 407 close (sf.fd); | |
| 408 sf.fd = -1; | |
| 409 sd.close (&sd); | |
| 410 sound_device = NULL; | |
| 411 sound_file = NULL; | |
| 412 UNGCPRO; | |
| 413 unbind_to (count, Qnil); | |
| 414 return Qnil; | |
| 415 } | |
| 416 | |
| 417 | |
| 418 /*********************************************************************** | |
| 419 Byte-order Conversion | |
| 420 ***********************************************************************/ | |
| 421 | |
| 422 /* Convert 32-bit value VALUE which is in little-endian byte-order | |
| 423 to host byte-order. */ | |
| 424 | |
| 425 static u_int32_t | |
| 426 le2hl (value) | |
| 427 u_int32_t value; | |
| 428 { | |
| 429 #ifdef WORDS_BIG_ENDIAN | |
| 430 unsigned char *p = (unsigned char *) &value; | |
| 431 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); | |
| 432 #endif | |
| 433 return value; | |
| 434 } | |
| 435 | |
| 436 | |
| 437 /* Convert 16-bit value VALUE which is in little-endian byte-order | |
| 438 to host byte-order. */ | |
| 439 | |
| 440 static u_int16_t | |
| 441 le2hs (value) | |
| 442 u_int16_t value; | |
| 443 { | |
| 444 #ifdef WORDS_BIG_ENDIAN | |
| 445 unsigned char *p = (unsigned char *) &value; | |
| 446 value = p[0] + (p[1] << 8); | |
| 447 #endif | |
| 448 return value; | |
| 449 } | |
| 450 | |
| 451 | |
| 452 /* Convert 32-bit value VALUE which is in big-endian byte-order | |
| 453 to host byte-order. */ | |
| 454 | |
| 455 static u_int32_t | |
| 456 be2hl (value) | |
| 457 u_int32_t value; | |
| 458 { | |
| 459 #ifndef WORDS_BIG_ENDIAN | |
| 460 unsigned char *p = (unsigned char *) &value; | |
| 461 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24); | |
| 462 #endif | |
| 463 return value; | |
| 464 } | |
| 465 | |
| 466 | |
| 467 /* Convert 16-bit value VALUE which is in big-endian byte-order | |
| 468 to host byte-order. */ | |
| 469 | |
| 470 static u_int16_t | |
| 471 be2hs (value) | |
| 472 u_int16_t value; | |
| 473 { | |
| 474 #ifndef WORDS_BIG_ENDIAN | |
| 475 unsigned char *p = (unsigned char *) &value; | |
| 476 value = p[1] + (p[0] << 8); | |
| 477 #endif | |
| 478 return value; | |
| 479 } | |
| 480 | |
| 481 | |
| 482 | |
| 483 /*********************************************************************** | |
| 484 RIFF-WAVE (*.wav) | |
| 485 ***********************************************************************/ | |
| 486 | |
| 487 /* Try to initialize sound file SF from SF->header. SF->header | |
| 488 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the | |
| 489 sound file. If the file is a WAV-format file, set up interface | |
| 490 functions in SF and convert header fields to host byte-order. | |
| 491 Value is non-zero if the file is a WAV file. */ | |
| 492 | |
| 493 static int | |
| 494 wav_init (sf) | |
| 495 struct sound_file *sf; | |
| 496 { | |
| 497 struct wav_header *header = (struct wav_header *) sf->header; | |
| 498 | |
| 499 if (bcmp (sf->header, "RIFF", 4) != 0) | |
| 500 return 0; | |
| 501 | |
| 502 /* WAV files are in little-endian order. Convert the header | |
| 503 if on a big-endian machine. */ | |
| 504 header->magic = le2hl (header->magic); | |
| 505 header->length = le2hl (header->length); | |
| 506 header->chunk_type = le2hl (header->chunk_type); | |
| 507 header->chunk_format = le2hl (header->chunk_format); | |
| 508 header->chunk_length = le2hl (header->chunk_length); | |
| 509 header->format = le2hs (header->format); | |
| 510 header->channels = le2hs (header->channels); | |
| 511 header->sample_rate = le2hl (header->sample_rate); | |
| 512 header->bytes_per_second = le2hl (header->bytes_per_second); | |
| 513 header->sample_size = le2hs (header->sample_size); | |
| 514 header->precision = le2hs (header->precision); | |
| 515 header->chunk_data = le2hl (header->chunk_data); | |
| 516 header->data_length = le2hl (header->data_length); | |
| 517 | |
| 518 /* Set up the interface functions for WAV. */ | |
| 519 sf->type = RIFF; | |
| 520 sf->play = wav_play; | |
| 521 | |
| 522 return 1; | |
| 523 } | |
| 524 | |
| 525 | |
| 526 /* Play RIFF-WAVE audio file SF on sound device SD. */ | |
| 527 | |
| 528 static void | |
| 529 wav_play (sf, sd) | |
| 530 struct sound_file *sf; | |
| 531 struct sound_device *sd; | |
| 532 { | |
| 533 struct wav_header *header = (struct wav_header *) sf->header; | |
| 534 char *buffer; | |
| 535 int nbytes; | |
| 536 int blksize = 2048; | |
| 537 | |
| 538 /* Let the device choose a suitable device-dependent format | |
| 539 for the file. */ | |
| 540 sd->choose_format (sd, sf); | |
| 541 | |
| 542 /* Configure the device. */ | |
| 543 sd->sample_size = header->sample_size; | |
| 544 sd->sample_rate = header->sample_rate; | |
| 545 sd->bps = header->bytes_per_second; | |
| 546 sd->channels = header->channels; | |
| 547 sd->configure (sd); | |
| 548 | |
| 549 /* Copy sound data to the device. The WAV file specification is | |
| 550 actually more complex. This simple scheme worked with all WAV | |
| 551 files I found so far. If someone feels inclined to implement the | |
| 552 whole RIFF-WAVE spec, please do. */ | |
| 553 buffer = (char *) alloca (blksize); | |
| 554 lseek (sf->fd, sizeof *header, SEEK_SET); | |
| 555 | |
| 556 while ((nbytes = read (sf->fd, buffer, blksize)) > 0) | |
| 557 sd->write (sd, buffer, nbytes); | |
| 558 | |
| 559 if (nbytes < 0) | |
| 560 sound_perror ("Reading sound file"); | |
| 561 } | |
| 562 | |
| 563 | |
| 564 | |
| 565 /*********************************************************************** | |
| 566 Sun Audio (*.au) | |
| 567 ***********************************************************************/ | |
| 568 | |
| 569 /* Sun audio file encodings. */ | |
| 570 | |
| 571 enum au_encoding | |
| 572 { | |
| 573 AU_ENCODING_ULAW_8 = 1, | |
| 574 AU_ENCODING_8, | |
| 575 AU_ENCODING_16, | |
| 576 AU_ENCODING_24, | |
| 577 AU_ENCODING_32, | |
| 578 AU_ENCODING_IEEE32, | |
| 579 AU_ENCODING_IEEE64, | |
| 580 AU_COMPRESSED = 23 | |
| 581 }; | |
| 582 | |
| 583 | |
| 584 /* Try to initialize sound file SF from SF->header. SF->header | |
| 585 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the | |
| 586 sound file. If the file is a AU-format file, set up interface | |
| 587 functions in SF and convert header fields to host byte-order. | |
| 588 Value is non-zero if the file is an AU file. */ | |
| 589 | |
| 590 static int | |
| 591 au_init (sf) | |
| 592 struct sound_file *sf; | |
| 593 { | |
| 594 struct au_header *header = (struct au_header *) sf->header; | |
| 595 | |
| 596 if (bcmp (sf->header, ".snd", 4) != 0) | |
| 597 return 0; | |
| 598 | |
| 599 header->magic_number = be2hl (header->magic_number); | |
| 600 header->data_offset = be2hl (header->data_offset); | |
| 601 header->data_size = be2hl (header->data_size); | |
| 602 header->encoding = be2hl (header->encoding); | |
| 603 header->sample_rate = be2hl (header->sample_rate); | |
| 604 header->channels = be2hl (header->channels); | |
| 605 | |
| 606 /* Set up the interface functions for AU. */ | |
| 607 sf->type = SUN_AUDIO; | |
| 608 sf->play = au_play; | |
| 609 | |
| 610 return 1; | |
| 611 } | |
| 612 | |
| 613 | |
| 614 /* Play Sun audio file SF on sound device SD. */ | |
| 615 | |
| 616 static void | |
| 617 au_play (sf, sd) | |
| 618 struct sound_file *sf; | |
| 619 struct sound_device *sd; | |
| 620 { | |
| 621 struct au_header *header = (struct au_header *) sf->header; | |
| 622 int blksize = 2048; | |
| 623 char *buffer; | |
| 624 int nbytes; | |
| 625 | |
| 626 sd->sample_size = 0; | |
| 627 sd->sample_rate = header->sample_rate; | |
| 628 sd->bps = 0; | |
| 629 sd->channels = header->channels; | |
| 630 sd->choose_format (sd, sf); | |
| 631 sd->configure (sd); | |
| 632 | |
| 633 /* Seek */ | |
| 634 lseek (sf->fd, header->data_offset, SEEK_SET); | |
| 635 | |
| 636 /* Copy sound data to the device. */ | |
| 637 buffer = (char *) alloca (blksize); | |
| 638 while ((nbytes = read (sf->fd, buffer, blksize)) > 0) | |
| 639 sd->write (sd, buffer, nbytes); | |
| 640 | |
| 641 if (nbytes < 0) | |
| 642 sound_perror ("Reading sound file"); | |
| 643 } | |
| 644 | |
| 645 | |
| 646 | |
| 647 /*********************************************************************** | |
| 648 Voxware Driver Interface | |
| 649 ***********************************************************************/ | |
| 650 | |
| 651 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD | |
| 652 has a compatible own driver aka Luigi's driver. */ | |
| 653 | |
| 654 | |
| 655 /* Open device SD. If SD->file is non-null, open that device, | |
| 656 otherwise use a default device name. */ | |
| 657 | |
| 658 static void | |
| 659 vox_open (sd) | |
| 660 struct sound_device *sd; | |
| 661 { | |
| 662 char *file; | |
| 663 | |
| 664 /* Open the sound device. Default is /dev/dsp. */ | |
| 665 if (sd->file) | |
| 666 file = sd->file; | |
| 667 else | |
| 668 file = "/dev/dsp"; | |
| 669 | |
| 670 sd->fd = open (file, O_WRONLY); | |
| 671 if (sd->fd < 0) | |
| 672 sound_perror (file); | |
| 673 } | |
| 674 | |
| 675 | |
| 676 /* Configure device SD from parameters in it. */ | |
| 677 | |
| 678 static void | |
| 679 vox_configure (sd) | |
| 680 struct sound_device *sd; | |
| 681 { | |
| 682 int requested; | |
| 683 | |
| 684 xassert (sd->fd >= 0); | |
| 685 | |
| 686 /* Device parameters apparently depend on each other in undocumented | |
| 687 ways (not to imply that there is any real documentation). Be | |
| 688 careful when reordering the calls below. */ | |
| 689 if (sd->sample_size > 0 | |
| 690 && ioctl (sd->fd, SNDCTL_DSP_SAMPLESIZE, &sd->sample_size) < 0) | |
| 691 sound_perror ("Setting sample size"); | |
| 692 | |
| 693 if (sd->bps > 0 | |
| 694 && ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->bps) < 0) | |
| 695 sound_perror ("Setting speed"); | |
| 696 | |
| 697 if (sd->sample_rate > 0 | |
| 698 && ioctl (sd->fd, SOUND_PCM_WRITE_RATE, &sd->sample_rate) < 0) | |
| 699 sound_perror ("Setting sample rate"); | |
| 700 | |
| 701 requested = sd->format; | |
| 702 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0) | |
| 703 sound_perror ("Setting format"); | |
| 704 else if (requested != sd->format) | |
| 705 error ("Setting format"); | |
| 706 | |
| 707 if (sd->channels > 1 | |
| 708 && ioctl (sd->fd, SNDCTL_DSP_STEREO, &sd->channels) < 0) | |
| 709 sound_perror ("Setting channels"); | |
| 710 | |
| 711 if (sd->volume > 0 | |
| 712 && ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &sd->volume) < 0) | |
| 713 sound_perror ("Setting volume"); | |
| 714 } | |
| 715 | |
| 716 | |
| 717 /* Close device SD if it is open. */ | |
| 718 | |
| 719 static void | |
| 720 vox_close (sd) | |
| 721 struct sound_device *sd; | |
| 722 { | |
| 723 if (sd->fd >= 0) | |
| 724 { | |
| 725 /* Flush sound data, and reset the device. */ | |
| 726 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL); | |
| 727 ioctl (sd->fd, SNDCTL_DSP_RESET, NULL); | |
| 728 | |
| 729 /* Close the device. */ | |
| 730 close (sd->fd); | |
| 731 sd->fd = -1; | |
| 732 } | |
| 733 } | |
| 734 | |
| 735 | |
| 736 /* Choose device-dependent format for device SD from sound file SF. */ | |
| 737 | |
| 738 static void | |
| 739 vox_choose_format (sd, sf) | |
| 740 struct sound_device *sd; | |
| 741 struct sound_file *sf; | |
| 742 { | |
| 743 if (sf->type == RIFF) | |
| 744 { | |
| 745 struct wav_header *h = (struct wav_header *) sf->header; | |
| 746 if (h->precision == 8) | |
| 747 sd->format = AFMT_U8; | |
| 748 else if (h->precision == 16) | |
| 749 sd->format = AFMT_S16_LE; | |
| 750 else | |
| 751 error ("Unsupported WAV file format"); | |
| 752 } | |
| 753 else if (sf->type == SUN_AUDIO) | |
| 754 { | |
| 755 struct au_header *header = (struct au_header *) sf->header; | |
| 756 switch (header->encoding) | |
| 757 { | |
| 758 case AU_ENCODING_ULAW_8: | |
| 759 case AU_ENCODING_IEEE32: | |
| 760 case AU_ENCODING_IEEE64: | |
| 761 sd->format = AFMT_MU_LAW; | |
| 762 break; | |
| 763 | |
| 764 case AU_ENCODING_8: | |
| 765 case AU_ENCODING_16: | |
| 766 case AU_ENCODING_24: | |
| 767 case AU_ENCODING_32: | |
| 768 sd->format = AFMT_S16_LE; | |
| 769 break; | |
| 770 | |
| 771 default: | |
| 772 error ("Unsupported AU file format"); | |
| 773 } | |
| 774 } | |
| 775 else | |
| 776 abort (); | |
| 777 } | |
| 778 | |
| 779 | |
| 780 /* Initialize device SD. Set up the interface functions in the device | |
| 781 structure. */ | |
| 782 | |
| 783 static void | |
| 784 vox_init (sd) | |
| 785 struct sound_device *sd; | |
| 786 { | |
| 787 sd->fd = -1; | |
| 788 sd->open = vox_open; | |
| 789 sd->close = vox_close; | |
| 790 sd->configure = vox_configure; | |
| 791 sd->choose_format = vox_choose_format; | |
| 792 sd->write = vox_write; | |
| 793 } | |
| 794 | |
| 795 | |
| 796 /* Write NBYTES bytes from BUFFER to device SD. */ | |
| 797 | |
| 798 static void | |
| 799 vox_write (sd, buffer, nbytes) | |
| 800 struct sound_device *sd; | |
| 801 char *buffer; | |
| 802 int nbytes; | |
| 803 { | |
| 804 int nwritten = write (sd->fd, buffer, nbytes); | |
| 805 if (nwritten < 0) | |
| 806 sound_perror ("Writing to sound device"); | |
| 807 } | |
| 808 | |
| 809 | |
| 810 | |
| 811 /*********************************************************************** | |
| 812 Initialization | |
| 813 ***********************************************************************/ | |
| 814 | |
| 815 void | |
| 816 syms_of_sound () | |
| 817 { | |
| 818 QCdevice = intern (":device"); | |
| 819 staticpro (&QCdevice); | |
| 820 QCvolume = intern (":volume"); | |
| 821 staticpro (&QCvolume); | |
| 822 Qsound = intern ("sound"); | |
| 823 staticpro (&Qsound); | |
|
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
824 Qplay_sound_functions = intern ("play-sound-functions"); |
|
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
825 staticpro (&Qplay_sound_functions); |
| 25003 | 826 |
| 827 defsubr (&Splay_sound); | |
| 828 } | |
| 829 | |
| 830 | |
| 831 void | |
| 832 init_sound () | |
| 833 { | |
| 834 } | |
| 835 | |
| 836 #endif /* HAVE_SOUND */ |
