428
|
1 /* nas.c --- XEmacs support for the Network Audio System server.
|
|
2 *
|
|
3 * Author: Richard Caley <R.Caley@ed.ac.uk>
|
|
4 *
|
|
5 * Copyright 1994 Free Software Foundation, Inc.
|
|
6 * Copyright 1993 Network Computing Devices, Inc.
|
|
7 *
|
|
8 * Permission to use, copy, modify, distribute, and sell this software and
|
|
9 * its documentation for any purpose is hereby granted without fee, provided
|
|
10 * that the above copyright notice appear in all copies and that both that
|
|
11 * copyright notice and this permission notice appear in supporting
|
|
12 * documentation, and that the name Network Computing Devices, Inc. not be
|
2108
|
13 * used in advertising or publicity pertaining to distribution of this
|
428
|
14 * software without specific, written prior permission.
|
2108
|
15 *
|
428
|
16 * THIS SOFTWARE IS PROVIDED 'AS-IS'. NETWORK COMPUTING DEVICES, INC.,
|
|
17 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
|
|
18 * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
19 * PARTICULAR PURPOSE, OR NONINFRINGEMENT. IN NO EVENT SHALL NETWORK
|
|
20 * COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
|
|
21 * SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
|
|
22 * OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
|
|
23 * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
|
|
24 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
25 */
|
|
26
|
|
27 /* Synched up with: Not in FSF. */
|
|
28
|
563
|
29 /* This file Mule-ized by Ben Wing, 5-15-01. */
|
|
30
|
428
|
31 /* There are four compile-time options.
|
|
32 *
|
|
33 * XTOOLKIT This will be part of an Xt program.
|
2108
|
34 *
|
428
|
35 * XTEVENTS The playing will be supervised asynchronously by the Xt event
|
|
36 * loop. If not set, playing will be completed within the call
|
2108
|
37 * to play_file etc.
|
428
|
38 *
|
|
39 * ROBUST_PLAY Causes errors in nas to be caught. This means that the
|
|
40 * program will attempt not to die if the nas server does.
|
|
41 *
|
|
42 * CACHE_SOUNDS Causes the sounds to be played in buckets in the NAS
|
|
43 * server. They are named by their comment field, or if that is
|
|
44 * empty by the filename, or for play_sound_data by a name made up
|
|
45 * from the sample itself.
|
|
46 */
|
|
47
|
|
48 /* CHANGES:
|
|
49 * 10/8/94, rjc Changed names from netaudio to nas
|
|
50 * Added back asynchronous play if nas library has
|
|
51 * correct error facilities.
|
|
52 * 4/11/94, rjc Added wait_for_sounds to be called when user wants to
|
|
53 * be sure all play has finished.
|
|
54 * 1998-10-01 rlt Added support for WAVE files.
|
1097
|
55 * 2002-10-16 Jon Trulson modifed this to work with NAS releases
|
|
56 * 1.5f and higher. We were using the private variable
|
|
57 * SoundFileInfo that doesn't exist anymore. But preserve
|
|
58 * backward compatibility. This will not work for some
|
|
59 * versions of NAS around 1.5b to 1.5f or so. Known to
|
|
60 * work on 1.2p5 and 1.6.
|
428
|
61 */
|
|
62
|
|
63 #include <config.h>
|
|
64 #include "lisp.h"
|
563
|
65
|
|
66 #include "sound.h"
|
|
67
|
428
|
68 #include "sysdep.h"
|
|
69 #include "syssignal.h"
|
|
70
|
442
|
71 /* NAS <= 1.2p5 defines {BIG,LITTLE}_ENDIAN in <audio/fileutil.h>,
|
|
72 conflicting with GNU libc (at least); newer versions avoid this
|
|
73 name space pollution.
|
428
|
74
|
442
|
75 DO NOT USE THOSE MACROS in this file. Use NAS_{BIG,LITTLE}_ENDIAN.
|
|
76
|
|
77 It would be slightly more reliable to do this via configure, but that
|
|
78 seems unnecessarily complex.
|
|
79 */
|
428
|
80 #undef LITTLE_ENDIAN
|
|
81 #undef BIG_ENDIAN
|
442
|
82
|
2119
|
83 BEGIN_C_DECLS
|
428
|
84 #include <audio/audiolib.h>
|
|
85 #include <audio/soundlib.h>
|
|
86 #include <audio/snd.h>
|
|
87 #include <audio/wave.h>
|
|
88 #include <audio/fileutil.h>
|
2119
|
89 END_C_DECLS
|
2110
|
90
|
428
|
91
|
442
|
92 /* NAS <= 1.2p5 <audio/fileutil.h> doesn't define the NAS_ versions */
|
|
93 #ifndef NAS_LITTLE_ENDIAN
|
613
|
94 # define NAS_LITTLE_ENDIAN LITTLE_ENDIAN
|
|
95 # define NAS_BIG_ENDIAN BIG_ENDIAN
|
442
|
96 #endif
|
|
97
|
613
|
98 #define XTOOLKIT
|
|
99 #define XTEVENTS
|
|
100 #define ROBUST_PLAY
|
|
101 #define CACHE_SOUNDS
|
428
|
102
|
|
103 /*
|
|
104 * For old NAS libraries, force playing to be synchronous
|
|
105 * and declare the long jump point locally.
|
|
106 */
|
|
107
|
613
|
108 #if defined (NAS_NO_ERROR_JUMP)
|
|
109 # undef XTEVENTS
|
|
110 # include <setjmp.h>
|
|
111 jmp_buf AuXtErrorJump;
|
|
112 #endif
|
428
|
113
|
|
114 #ifdef XTOOLKIT
|
613
|
115 # include <X11/Intrinsic.h>
|
|
116 # include <audio/Xtutil.h>
|
428
|
117 #endif
|
|
118
|
1746
|
119 /* audiolib <= some version doesn't define major and minor versions */
|
|
120 /* Do NOT use AudioLibraryVersion in this file. */
|
|
121 #ifndef AudioLibraryVersion
|
|
122 #define AudioLibraryVersion 1
|
|
123 #endif
|
|
124 #ifndef AudioLibraryVersionMajor
|
|
125 #define AudioLibraryVersionMajor AudioLibraryVersion
|
|
126 #endif
|
|
127 #ifndef AudioLibraryVersionMinor
|
|
128 #define AudioLibraryVersionMinor 0
|
|
129 #endif
|
|
130
|
428
|
131 #if defined (ROBUST_PLAY)
|
|
132 static AuBool CatchIoErrorAndJump (AuServer *aud);
|
|
133 static AuBool CatchErrorAndJump (AuServer *aud, AuErrorEvent *event);
|
|
134 SIGTYPE sigpipe_handle (int signo);
|
|
135 #endif
|
|
136
|
|
137 extern Lisp_Object Vsynchronous_sounds;
|
|
138
|
2367
|
139 static Sound SoundOpenDataForReading (Binbyte *data, int length);
|
428
|
140
|
|
141 static AuServer *aud;
|
|
142
|
|
143 /* count of sounds currently being played. */
|
|
144 static int sounds_in_play;
|
|
145
|
|
146
|
|
147 #ifdef XTOOLKIT
|
|
148 static Display *aud_server;
|
|
149 static XtInputId input_id;
|
|
150 #else
|
563
|
151 static Extbyte *aud_server;
|
428
|
152 #endif /* XTOOLKIT */
|
|
153
|
563
|
154 Extbyte *
|
613
|
155 nas_init_play (
|
428
|
156 #ifdef XTOOLKIT
|
|
157 Display *display
|
|
158 #else
|
563
|
159 Extbyte *server
|
428
|
160 #endif
|
432
|
161 );
|
563
|
162 Extbyte *
|
613
|
163 nas_init_play (
|
432
|
164 #ifdef XTOOLKIT
|
|
165 Display *display
|
|
166 #else
|
563
|
167 Extbyte *server
|
432
|
168 #endif
|
428
|
169 )
|
|
170 {
|
563
|
171 Extbyte *err_message;
|
432
|
172 SIGTYPE (*old_sigpipe) (int);
|
428
|
173
|
|
174 #ifdef XTOOLKIT
|
563
|
175 Extbyte * server = DisplayString (display);
|
428
|
176 XtAppContext app_context = XtDisplayToApplicationContext (display);
|
|
177
|
|
178 aud_server = display;
|
|
179 #else
|
|
180
|
|
181 aud_server = server;
|
|
182 #endif
|
|
183
|
|
184 #ifdef ROBUST_PLAY
|
613
|
185 old_sigpipe = EMACS_SIGNAL (SIGPIPE, sigpipe_handle);
|
428
|
186 if (setjmp (AuXtErrorJump))
|
|
187 {
|
613
|
188 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
189 start_interrupts ();
|
|
190 return "error in NAS";
|
|
191 }
|
|
192 #endif
|
|
193
|
|
194 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
|
|
195 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
|
|
196 AuDefaultErrorHandler = CatchErrorAndJump;
|
|
197 #endif
|
|
198
|
|
199 stop_interrupts ();
|
|
200 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
|
|
201 start_interrupts ();
|
|
202 if (!aud)
|
|
203 {
|
|
204 #ifdef ROBUST_PLAY
|
613
|
205 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
206 #endif
|
|
207 if (err_message == NULL)
|
|
208 return "Can't connect to audio server";
|
|
209 else
|
|
210 return err_message;
|
|
211 }
|
|
212
|
|
213 #if defined (ROBUST_PLAY)
|
|
214 # if defined (NAS_NO_ERROR_JUMP)
|
|
215 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
|
|
216 aud->funcs.error_handler = CatchErrorAndJump;
|
|
217 # else /* !NAS_NO_ERROR_JUMP */
|
|
218 AuDefaultIOErrorHandler = NULL;
|
|
219 AuDefaultErrorHandler = NULL;
|
|
220 # endif
|
|
221 #endif
|
|
222
|
|
223 #ifdef XTEVENTS
|
2108
|
224 input_id = AuXtAppAddAudioHandler (app_context, aud);
|
428
|
225 #endif
|
|
226
|
|
227 #ifdef CACHE_SOUNDS
|
|
228 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
|
|
229 #endif
|
|
230
|
|
231 #ifdef ROBUST_PLAY
|
613
|
232 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
233 #endif
|
|
234
|
|
235 sounds_in_play = 0;
|
|
236
|
|
237 return NULL;
|
|
238 }
|
|
239
|
432
|
240 static void
|
613
|
241 nas_close_down_play (void)
|
428
|
242
|
|
243 {
|
|
244 AuCloseServer (aud);
|
563
|
245 sound_warn ("disconnected from audio server");
|
428
|
246 }
|
|
247
|
|
248 /********************************************************************\
|
|
249 * *
|
|
250 * Callback which is run when the sound finishes playing. *
|
|
251 * *
|
|
252 \********************************************************************/
|
|
253
|
|
254 static void
|
2286
|
255 doneCB (AuServer *UNUSED (auserver),
|
|
256 AuEventHandlerRec *UNUSED (handler),
|
|
257 AuEvent *UNUSED (ev),
|
428
|
258 AuPointer data)
|
|
259 {
|
|
260 int *in_play_p = (int *) data;
|
|
261
|
|
262 (*in_play_p) --;
|
|
263 }
|
|
264
|
|
265 #ifdef CACHE_SOUNDS
|
|
266
|
|
267 /********************************************************************\
|
|
268 * *
|
|
269 * Play a sound by playing the relevant bucket, if any or *
|
|
270 * downloading it if not. *
|
|
271 * *
|
|
272 \********************************************************************/
|
|
273
|
|
274 static void
|
|
275 do_caching_play (Sound s,
|
|
276 int volume,
|
2367
|
277 Binbyte *buf)
|
428
|
278
|
|
279 {
|
|
280 AuBucketAttributes *list, b;
|
|
281 AuBucketID id;
|
|
282 int n;
|
|
283
|
|
284 AuSetString (AuBucketDescription (&b),
|
|
285 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
|
|
286
|
|
287 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
|
|
288
|
|
289 if (list == NULL)
|
|
290 {
|
432
|
291 AuPointer my_buf;
|
428
|
292
|
|
293 if (buf==NULL)
|
|
294 {
|
432
|
295 if ((my_buf= (AuPointer) malloc (SoundNumBytes (s)))==NULL)
|
428
|
296 {
|
|
297 return;
|
|
298 }
|
|
299
|
563
|
300 if (SoundReadFile ((Extbyte *) my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
|
428
|
301 {
|
|
302 free (my_buf);
|
|
303 return;
|
|
304 }
|
|
305 }
|
|
306 else
|
432
|
307 my_buf = (AuPointer) buf;
|
428
|
308
|
2108
|
309 id = AuSoundCreateBucketFromData (aud,
|
428
|
310 s,
|
|
311 my_buf,
|
2108
|
312 AuAccessAllMasks,
|
428
|
313 NULL,
|
|
314 NULL);
|
|
315 if (buf == NULL)
|
|
316 free (my_buf);
|
|
317 }
|
|
318 else /* found cached sound */
|
|
319 {
|
|
320 id = AuBucketIdentifier (list);
|
|
321 AuFreeBucketAttributes (aud, n, list);
|
|
322 }
|
|
323
|
|
324 sounds_in_play++;
|
|
325
|
2108
|
326 AuSoundPlayFromBucket (aud,
|
|
327 id,
|
428
|
328 AuNone,
|
2108
|
329 AuFixedPointFromFraction (volume, 100),
|
428
|
330 doneCB, (AuPointer) &sounds_in_play,
|
|
331 1,
|
|
332 NULL, NULL,
|
|
333 NULL, NULL);
|
|
334
|
|
335 }
|
|
336 #endif /* CACHE_SOUNDS */
|
|
337
|
|
338
|
613
|
339 void nas_wait_for_sounds (void);
|
2108
|
340 void
|
613
|
341 nas_wait_for_sounds (void)
|
428
|
342
|
|
343 {
|
|
344 AuEvent ev;
|
|
345
|
|
346 while (sounds_in_play>0)
|
|
347 {
|
|
348 AuNextEvent (aud, AuTrue, &ev);
|
|
349 AuDispatchEvent (aud, &ev);
|
|
350 }
|
|
351 }
|
|
352
|
613
|
353 int nas_play_sound_file (Extbyte *sound_file, int volume);
|
428
|
354 int
|
613
|
355 nas_play_sound_file (Extbyte *sound_file,
|
|
356 int volume)
|
428
|
357 {
|
432
|
358 SIGTYPE (*old_sigpipe) (int);
|
428
|
359
|
|
360 #ifdef ROBUST_PLAY
|
613
|
361 old_sigpipe = EMACS_SIGNAL (SIGPIPE, sigpipe_handle);
|
428
|
362 if (setjmp (AuXtErrorJump))
|
|
363 {
|
613
|
364 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
365 return 0;
|
|
366 }
|
|
367 #endif
|
|
368
|
613
|
369 if (aud==NULL)
|
|
370 {
|
|
371 if (aud_server != NULL)
|
|
372 {
|
|
373 Extbyte *m;
|
|
374 /* attempt to reconect */
|
|
375 if ((m = nas_init_play (aud_server)) != NULL)
|
|
376 {
|
2108
|
377
|
428
|
378 #ifdef ROBUST_PLAY
|
613
|
379 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
380 #endif
|
613
|
381 return 0;
|
|
382 }
|
|
383 }
|
|
384 else
|
|
385 {
|
|
386 sound_warn ("Attempt to play with no audio init\n");
|
428
|
387 #ifdef ROBUST_PLAY
|
613
|
388 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
389 #endif
|
613
|
390 return 0;
|
|
391 }
|
|
392 }
|
428
|
393
|
|
394 #ifndef CACHE_SOUNDS
|
|
395 sounds_in_play++;
|
|
396 AuSoundPlayFromFile (aud,
|
|
397 sound_file,
|
|
398 AuNone,
|
|
399 AuFixedPointFromFraction (volume,100),
|
|
400 doneCB, (AuPointer) &sounds_in_play,
|
|
401 NULL,
|
|
402 NULL,
|
|
403 NULL,
|
|
404 NULL);
|
|
405 #else
|
|
406 /* Cache the sounds in buckets on the server */
|
|
407
|
|
408 {
|
|
409 Sound s;
|
|
410
|
|
411 if ((s = SoundOpenFileForReading (sound_file))==NULL)
|
|
412 {
|
|
413 #ifdef ROBUST_PLAY
|
613
|
414 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
415 #endif
|
|
416 return 0;
|
|
417 }
|
|
418
|
|
419 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
|
|
420 {
|
|
421 SoundComment (s) = FileCommentFromFilename (sound_file);
|
|
422 }
|
|
423
|
|
424 do_caching_play (s, volume, NULL);
|
|
425
|
|
426 SoundCloseFile (s);
|
|
427
|
|
428 }
|
|
429 #endif /* CACHE_SOUNDS */
|
|
430
|
|
431 #ifndef XTEVENTS
|
613
|
432 nas_wait_for_sounds ();
|
428
|
433 #else
|
|
434 if (!NILP (Vsynchronous_sounds))
|
613
|
435 nas_wait_for_sounds ();
|
428
|
436 #endif
|
|
437
|
|
438 #ifdef ROBUST_PLAY
|
613
|
439 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
440 #endif
|
|
441
|
|
442 return 1;
|
|
443 }
|
|
444
|
2367
|
445 int nas_play_sound_data (Binbyte *data, int length, int volume);
|
428
|
446 int
|
2367
|
447 nas_play_sound_data (Binbyte *data, int length, int volume)
|
428
|
448 {
|
|
449 Sound s;
|
|
450 int offset;
|
432
|
451 SIGTYPE (*old_sigpipe) (int);
|
428
|
452
|
|
453 #if !defined (XTEVENTS)
|
|
454 AuEvent ev;
|
|
455 #endif
|
|
456
|
|
457 #ifdef ROBUST_PLAY
|
613
|
458 old_sigpipe = EMACS_SIGNAL (SIGPIPE, sigpipe_handle);
|
|
459 if (setjmp (AuXtErrorJump) != 0)
|
428
|
460 {
|
613
|
461 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
462 return 0;
|
|
463 }
|
|
464 #endif
|
|
465
|
|
466
|
|
467 if (aud == NULL) {
|
|
468 if (aud_server != NULL)
|
|
469 {
|
563
|
470 Extbyte *m;
|
428
|
471 /* attempt to reconect */
|
613
|
472 if ((m = nas_init_play (aud_server)) != NULL)
|
428
|
473 {
|
|
474 #ifdef ROBUST_PLAY
|
613
|
475 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
476 #endif
|
|
477 return 0;
|
|
478 }
|
|
479 }
|
|
480 else
|
|
481 {
|
563
|
482 sound_warn ("Attempt to play with no audio init\n");
|
428
|
483 #ifdef ROBUST_PLAY
|
613
|
484 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
485 #endif
|
|
486 return 0;
|
|
487 }
|
|
488 }
|
|
489
|
|
490 if ((s=SoundOpenDataForReading (data, length))==NULL)
|
|
491 {
|
563
|
492 sound_warn ("unknown sound type");
|
428
|
493 #ifdef ROBUST_PLAY
|
613
|
494 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
495 #endif
|
|
496 return 0;
|
|
497 }
|
|
498
|
|
499 if (SoundFileFormat (s) == SoundFileFormatSnd)
|
|
500 {
|
|
501 /* hack, hack */
|
|
502 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
|
|
503 }
|
|
504 else if (SoundFileFormat (s) == SoundFileFormatWave)
|
|
505 {
|
|
506 offset = ((WaveInfo *) (s->formatInfo))->dataOffset;
|
|
507 }
|
|
508 else
|
|
509 {
|
563
|
510 sound_warn ("only understand snd and wave files at the moment");
|
428
|
511 SoundCloseFile (s);
|
|
512 #ifdef ROBUST_PLAY
|
613
|
513 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
514 #endif
|
|
515 return 0;
|
|
516 }
|
|
517
|
|
518 #ifndef CACHE_SOUNDS
|
|
519 sounds_in_play++;
|
|
520 AuSoundPlayFromData (aud,
|
|
521 s,
|
|
522 data+offset,
|
|
523 AuNone,
|
|
524 AuFixedPointFromFraction (volume,100),
|
|
525 doneCB, (AuPointer) &sounds_in_play,
|
|
526 NULL,
|
|
527 NULL,
|
|
528 NULL,
|
|
529 NULL);
|
|
530 #else
|
|
531 /* Cache the sounds in buckets on the server */
|
|
532
|
613
|
533 do_caching_play (s, volume, data+offset);
|
428
|
534 #endif /* CACHE_SOUNDS */
|
|
535
|
|
536
|
|
537 #ifndef XTEVENTS
|
613
|
538 nas_wait_for_sounds ();
|
428
|
539 #else
|
|
540 if (!NILP (Vsynchronous_sounds))
|
613
|
541 nas_wait_for_sounds ();
|
428
|
542 #endif
|
|
543
|
2108
|
544 SoundCloseFile (s);
|
428
|
545
|
|
546 #ifdef ROBUST_PLAY
|
613
|
547 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
548 #endif
|
|
549
|
|
550 return 1;
|
|
551 }
|
|
552
|
|
553 #if defined (ROBUST_PLAY)
|
|
554
|
|
555 /********************************************************************\
|
|
556 * *
|
|
557 * Code to protect the client from server shutdowns. *
|
|
558 * *
|
|
559 * This is unbelievably horrible. *
|
|
560 * *
|
|
561 \********************************************************************/
|
|
562
|
|
563 static AuBool
|
|
564 CatchIoErrorAndJump (AuServer *old_aud)
|
|
565 {
|
|
566 if (old_aud)
|
2108
|
567 sound_warn ("Audio Server connection broken");
|
428
|
568 else
|
563
|
569 sound_warn ("Audio Server connection broken because of signal");
|
428
|
570
|
|
571 #ifdef XTEVENTS
|
|
572 #ifdef XTOOLKIT
|
2108
|
573 AuXtAppRemoveAudioHandler (aud, input_id);
|
428
|
574 #endif
|
|
575
|
|
576 if (aud)
|
|
577 AuCloseServer (aud);
|
|
578 aud = NULL;
|
|
579 sounds_in_play = 0;
|
|
580
|
|
581 longjmp (AuXtErrorJump, 1);
|
|
582
|
|
583 #else /* not XTEVENTS */
|
|
584
|
|
585 if (aud)
|
|
586 AuCloseServer (aud);
|
|
587 aud = NULL;
|
|
588 sounds_in_play = 0;
|
|
589 longjmp (AuXtErrorJump, 1);
|
2108
|
590
|
428
|
591 #endif /* XTEVENTS */
|
|
592 return 0;
|
|
593 }
|
|
594
|
|
595 SIGTYPE
|
2286
|
596 sigpipe_handle (int UNUSED (signo))
|
428
|
597 {
|
|
598 CatchIoErrorAndJump (NULL);
|
|
599 }
|
|
600
|
|
601 static AuBool
|
|
602 CatchErrorAndJump (AuServer *old_aud,
|
2286
|
603 AuErrorEvent *UNUSED (event))
|
428
|
604 {
|
|
605 return CatchIoErrorAndJump (old_aud);
|
|
606 }
|
|
607
|
|
608 #endif /* ROBUST_PLAY */
|
|
609
|
|
610 /********************************************************************\
|
|
611 * *
|
|
612 * This code is here because the nas Sound library doesn't *
|
|
613 * support playing from a file buffered in memory. It's a fairly *
|
|
614 * direct translation of the file-based equivalent. *
|
|
615 * *
|
|
616 * Since we don't have a filename, samples with no comment field *
|
|
617 * are named by a section of their content. *
|
|
618 * *
|
|
619 \********************************************************************/
|
|
620
|
|
621 /* Create a name from the sound. */
|
|
622
|
563
|
623 static Extbyte *
|
2367
|
624 NameFromData (const CBinbyte *buf,
|
428
|
625 int len)
|
|
626
|
|
627 {
|
563
|
628 Extbyte name[9];
|
428
|
629 int i;
|
563
|
630 Extbyte *s;
|
428
|
631
|
|
632 buf+=len/2;
|
|
633 len -= len/2;
|
|
634
|
|
635 i=0;
|
|
636 while (i<8 && len >0)
|
|
637 {
|
|
638 while (*buf < 32 && len>0)
|
|
639 {
|
|
640 buf++;
|
|
641 len--;
|
|
642 }
|
|
643 name[i]= *buf;
|
|
644 i++;
|
|
645 buf++;
|
|
646 len--;
|
|
647 }
|
|
648
|
|
649 name[i]='\0';
|
|
650
|
|
651 if (i==8)
|
|
652 {
|
563
|
653 strcpy (s = (Extbyte *) malloc (10), name);
|
428
|
654 }
|
2108
|
655 else
|
428
|
656 {
|
563
|
657 strcpy (s = (Extbyte *) malloc (15), "short sound");
|
428
|
658 }
|
|
659
|
|
660 return s;
|
|
661 }
|
|
662
|
|
663 /* Code to do a pseudo-open on a data buffer. Only for snd files at the
|
2108
|
664 moment.
|
428
|
665 */
|
|
666
|
|
667 static SndInfo *
|
2367
|
668 SndOpenDataForReading (const CBinbyte *data,
|
428
|
669 int length)
|
|
670
|
|
671 {
|
|
672 SndInfo *si;
|
|
673 int size;
|
|
674
|
|
675 if (!(si = (SndInfo *) malloc (sizeof (SndInfo))))
|
|
676 return NULL;
|
|
677
|
|
678 si->comment = NULL;
|
|
679 si->writing = 0;
|
|
680
|
|
681 memcpy (&si->h, data, sizeof (SndHeader));
|
|
682
|
442
|
683 if (NAS_LITTLE_ENDIAN)
|
428
|
684 {
|
2367
|
685 CBinbyte n;
|
2108
|
686
|
428
|
687 swapl (&si->h.magic, n);
|
|
688 swapl (&si->h.dataOffset, n);
|
|
689 swapl (&si->h.dataSize, n);
|
|
690 swapl (&si->h.format, n);
|
|
691 swapl (&si->h.sampleRate, n);
|
|
692 swapl (&si->h.tracks, n);
|
|
693 }
|
|
694
|
|
695 if (si->h.magic != SND_MAGIC_NUM)
|
|
696 {
|
|
697 free (si);
|
|
698 return NULL;
|
|
699 }
|
|
700
|
|
701 size = si->h.dataOffset - sizeof (SndHeader);
|
|
702
|
|
703 if (size)
|
|
704 {
|
563
|
705 if (!(si->comment = (Extbyte *) malloc (size + 1)))
|
428
|
706 {
|
|
707 free (si);
|
|
708 return NULL;
|
|
709 }
|
|
710
|
|
711 memcpy (si->comment, data+sizeof (SndHeader), size);
|
|
712
|
|
713 *(si->comment + size) = 0;
|
|
714 if (*si->comment == '\0')
|
|
715 si->comment =
|
|
716 NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
|
|
717 }
|
|
718 else
|
|
719 si->comment = NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
|
|
720
|
|
721 si->h.dataSize = length-si->h.dataOffset;
|
|
722
|
|
723 si->fp=NULL;
|
|
724
|
|
725 return si;
|
|
726 }
|
|
727
|
|
728 /* Stuff taken from wave.c from NAS. Just like snd files, NAS can't
|
|
729 read wave data from memory, so these functions do that for us. */
|
|
730
|
3462
|
731 #define Err() { free(wi); return NULL; }
|
428
|
732 #define readFourcc(_f) dread(_f, sizeof(RIFF_FOURCC), 1)
|
|
733 #define cmpID(_x, _y) \
|
2367
|
734 strncmp((CBinbyte *) (_x), (CBinbyte *) (_y), sizeof(RIFF_FOURCC))
|
428
|
735 #define PAD2(_x) (((_x) + 1) & ~1)
|
|
736
|
|
737 /* These functions here are for faking file I/O from buffer. */
|
|
738
|
|
739 /* The "file" position */
|
432
|
740 static size_t file_posn;
|
428
|
741 /* The length of the "file" */
|
432
|
742 static size_t file_len;
|
428
|
743 /* The actual "file" data. */
|
442
|
744 static const void* file_data;
|
428
|
745
|
|
746 /* Like fopen, but for a buffer in memory */
|
|
747 static void
|
442
|
748 dopen (const void* data, size_t length)
|
428
|
749 {
|
|
750 file_data = data;
|
|
751 file_len = length;
|
|
752 file_posn = 0;
|
|
753 }
|
|
754
|
|
755 /* Like fread, but for a buffer in memory */
|
|
756 static int
|
432
|
757 dread (void* buf, size_t size, size_t nitems)
|
428
|
758 {
|
432
|
759 size_t nread = size * nitems;
|
2108
|
760
|
428
|
761 if (file_posn + nread <= file_len)
|
|
762 {
|
2367
|
763 memcpy(buf, (CBinbyte *) file_data + file_posn, size * nitems);
|
428
|
764 file_posn += nread;
|
|
765 return nitems;
|
|
766 }
|
|
767 else
|
|
768 {
|
|
769 return EOF;
|
|
770 }
|
|
771 }
|
|
772
|
|
773 /* Like fgetc, but for a buffer in memory */
|
|
774 static int
|
432
|
775 dgetc (void)
|
428
|
776 {
|
|
777 if (file_posn < file_len)
|
2367
|
778 return ((CBinbyte *)file_data)[file_posn++];
|
428
|
779 else
|
|
780 return -1;
|
|
781 }
|
|
782
|
|
783 /* Like fseek, but for a buffer in memory */
|
|
784 static int
|
432
|
785 dseek (long offset, int from)
|
428
|
786 {
|
|
787 if (from == 0)
|
|
788 file_posn = offset;
|
|
789 else if (from == 1)
|
|
790 file_posn += offset;
|
|
791 else if (from == 2)
|
|
792 file_posn = file_len + offset;
|
|
793
|
|
794 return 0;
|
|
795 }
|
|
796
|
|
797 /* Like ftell, but for a buffer in memory */
|
432
|
798 static long
|
440
|
799 dtell (void)
|
428
|
800 {
|
|
801 return file_posn;
|
|
802 }
|
|
803
|
|
804 /* Data buffer analogs for FileReadS and FileReadL in NAS. */
|
|
805
|
|
806 static unsigned short
|
440
|
807 DataReadS (int swapit)
|
428
|
808 {
|
613
|
809 unsigned short us;
|
428
|
810
|
613
|
811 dread(&us, 2, 1);
|
|
812 if (swapit)
|
|
813 us = FileSwapS(us);
|
|
814 return us;
|
428
|
815 }
|
|
816
|
|
817 static AuUint32
|
440
|
818 DataReadL (int swapit)
|
428
|
819 {
|
613
|
820 AuUint32 ul;
|
428
|
821
|
613
|
822 dread(&ul, 4, 1);
|
|
823 if (swapit)
|
|
824 ul = FileSwapL(ul);
|
|
825 return ul;
|
428
|
826 }
|
|
827
|
|
828 static int
|
440
|
829 readChunk (RiffChunk *c)
|
428
|
830 {
|
613
|
831 int status;
|
2367
|
832 CBinbyte n;
|
428
|
833
|
613
|
834 if ((status = dread(c, sizeof(RiffChunk), 1)))
|
|
835 if (NAS_BIG_ENDIAN)
|
|
836 swapl(&c->ckSize, n);
|
428
|
837
|
613
|
838 return status;
|
428
|
839 }
|
|
840
|
|
841 /* A very straight-forward translation of WaveOpenFileForReading to
|
|
842 read the wave data from a buffer in memory. */
|
|
843
|
|
844 static WaveInfo *
|
2367
|
845 WaveOpenDataForReading (const CBinbyte *data,
|
440
|
846 int length)
|
428
|
847 {
|
613
|
848 RiffChunk ck;
|
|
849 RIFF_FOURCC fourcc;
|
|
850 AuInt32 fileSize;
|
|
851 WaveInfo *wi;
|
428
|
852
|
2108
|
853
|
613
|
854 if (!(wi = (WaveInfo *) malloc(sizeof(WaveInfo))))
|
|
855 return NULL;
|
428
|
856
|
613
|
857 wi->comment = NULL;
|
|
858 wi->dataOffset = wi->format = wi->writing = 0;
|
428
|
859
|
613
|
860 dopen(data, length);
|
2108
|
861
|
613
|
862 if (!readChunk(&ck) ||
|
|
863 cmpID(&ck.ckID, RIFF_RiffID) ||
|
|
864 !readFourcc(&fourcc) ||
|
|
865 cmpID(&fourcc, RIFF_WaveID))
|
|
866 Err();
|
|
867
|
|
868 fileSize = PAD2(ck.ckSize) - sizeof(RIFF_FOURCC);
|
|
869
|
1346
|
870 while (fileSize >= (AuInt32) sizeof(RiffChunk))
|
613
|
871 {
|
|
872 if (!readChunk(&ck))
|
428
|
873 Err();
|
|
874
|
613
|
875 fileSize -= sizeof(RiffChunk) + PAD2(ck.ckSize);
|
428
|
876
|
613
|
877 /* LIST chunk */
|
|
878 if (!cmpID(&ck.ckID, RIFF_ListID))
|
|
879 {
|
|
880 if (!readFourcc(&fourcc))
|
428
|
881 Err();
|
|
882
|
613
|
883 /* INFO chunk */
|
|
884 if (!cmpID(&fourcc, RIFF_ListInfoID))
|
|
885 {
|
|
886 ck.ckSize -= sizeof(RIFF_FOURCC);
|
428
|
887
|
613
|
888 while (ck.ckSize)
|
|
889 {
|
|
890 RiffChunk c;
|
428
|
891
|
613
|
892 if (!readChunk(&c))
|
|
893 Err();
|
428
|
894
|
613
|
895 /* ICMT chunk */
|
|
896 if (!cmpID(&c.ckID, RIFF_InfoIcmtID))
|
|
897 {
|
|
898 if (!(wi->comment = (Extbyte *) malloc(c.ckSize)) ||
|
|
899 !dread(wi->comment, c.ckSize, 1))
|
428
|
900 Err();
|
|
901
|
613
|
902 if (c.ckSize & 1)
|
|
903 dgetc(); /* eat the pad byte */
|
|
904 }
|
|
905 else
|
|
906 /* skip unknown chunk */
|
|
907 dseek(PAD2(c.ckSize), 1);
|
428
|
908
|
613
|
909 ck.ckSize -= sizeof(RiffChunk) + PAD2(c.ckSize);
|
428
|
910 }
|
|
911 }
|
613
|
912 else
|
|
913 /* skip unknown chunk */
|
|
914 dseek(PAD2(ck.ckSize) - sizeof(RIFF_FOURCC), 1);
|
428
|
915 }
|
613
|
916 /* wave format chunk */
|
|
917 else if (!cmpID(&ck.ckID, RIFF_WaveFmtID) && !wi->format)
|
428
|
918 {
|
613
|
919 AuInt32 dummy;
|
428
|
920
|
613
|
921 wi->format = DataReadS(NAS_BIG_ENDIAN);
|
|
922 wi->channels = DataReadS(NAS_BIG_ENDIAN);
|
|
923 wi->sampleRate = DataReadL(NAS_BIG_ENDIAN);
|
428
|
924
|
613
|
925 /* we don't care about the next two fields */
|
|
926 dummy = DataReadL(NAS_BIG_ENDIAN);
|
|
927 dummy = DataReadS(NAS_BIG_ENDIAN);
|
428
|
928
|
613
|
929 if (wi->format != RIFF_WAVE_FORMAT_PCM)
|
|
930 Err();
|
428
|
931
|
613
|
932 wi->bitsPerSample = DataReadS(NAS_BIG_ENDIAN);
|
428
|
933
|
613
|
934 /* skip any other format specific fields */
|
|
935 dseek(PAD2(ck.ckSize - 16), 1);
|
428
|
936 }
|
613
|
937 /* wave data chunk */
|
|
938 else if (!cmpID(&ck.ckID, RIFF_WaveDataID) && !wi->dataOffset)
|
428
|
939 {
|
613
|
940 long endOfFile;
|
428
|
941
|
613
|
942 wi->dataOffset = dtell();
|
|
943 wi->dataSize = ck.ckSize;
|
|
944 dseek(0, 2);
|
|
945 endOfFile = dtell();
|
428
|
946
|
613
|
947 /* seek past the data */
|
|
948 if (dseek(wi->dataOffset + PAD2(ck.ckSize), 0) ||
|
|
949 dtell() > endOfFile)
|
428
|
950 {
|
613
|
951 /* the seek failed, assume the size is bogus */
|
|
952 dseek(0, 2);
|
|
953 wi->dataSize = dtell() - wi->dataOffset;
|
428
|
954 }
|
|
955
|
613
|
956 wi->dataOffset -= sizeof(long);
|
428
|
957 }
|
613
|
958 else
|
|
959 /* skip unknown chunk */
|
|
960 dseek(PAD2(ck.ckSize), 1);
|
428
|
961 }
|
|
962
|
613
|
963 if (!wi->dataOffset)
|
|
964 Err();
|
428
|
965
|
613
|
966 wi->numSamples = wi->dataSize / wi->channels / (wi->bitsPerSample >> 3);
|
428
|
967
|
613
|
968 if (!wi->comment)
|
|
969 wi->comment = NameFromData (data + wi->dataOffset,
|
|
970 length - wi->dataOffset);
|
428
|
971
|
613
|
972 wi->fp = NULL;
|
2108
|
973
|
613
|
974 return wi;
|
428
|
975 }
|
|
976
|
|
977
|
|
978 static Sound
|
2367
|
979 SoundOpenDataForReading (Binbyte *data,
|
428
|
980 int length)
|
|
981
|
|
982 {
|
|
983 Sound s;
|
1097
|
984 #if (AudioLibraryVersionMajor >= 2 ) && (AudioLibraryVersionMinor >= 3)
|
|
985 SoundFileInfoProc toProc;
|
|
986 #endif
|
428
|
987
|
|
988 if (!(s = (Sound) malloc (sizeof (SoundRec))))
|
|
989 return NULL;
|
|
990
|
2367
|
991 if ((s->formatInfo = SndOpenDataForReading ((CBinbyte *) data, length)) != NULL)
|
428
|
992 {
|
1097
|
993 #if (AudioLibraryVersionMajor >= 2 ) && (AudioLibraryVersionMinor >= 3)
|
2108
|
994 if ((toProc = SoundFileGetProc(SoundFileFormatSnd,
|
1097
|
995 SoundFileInfoProcTo)) == NULL)
|
|
996 {
|
|
997 SndCloseFile ((SndInfo *) (s->formatInfo));
|
|
998 free (s);
|
|
999
|
|
1000 return NULL;
|
|
1001 }
|
|
1002 if (!((*toProc)(s)))
|
|
1003 #else
|
432
|
1004 if (!((int(*)(Sound))(SoundFileInfo[SoundFileFormatSnd].toSound)) (s))
|
1097
|
1005 #endif
|
428
|
1006 {
|
432
|
1007 SndCloseFile ((SndInfo *) (s->formatInfo));
|
428
|
1008 free (s);
|
|
1009 return NULL;
|
|
1010 }
|
|
1011 }
|
2367
|
1012 else if ((s->formatInfo = WaveOpenDataForReading ((CBinbyte *) data, length)) != NULL)
|
428
|
1013 {
|
1097
|
1014 #if (AudioLibraryVersionMajor >= 2 ) && (AudioLibraryVersionMinor >= 3)
|
2108
|
1015 if ((toProc = SoundFileGetProc(SoundFileFormatWave,
|
1097
|
1016 SoundFileInfoProcTo)) == NULL)
|
|
1017 {
|
|
1018 WaveCloseFile ((WaveInfo *) (s->formatInfo));
|
|
1019 free (s);
|
|
1020
|
|
1021 return NULL;
|
|
1022 }
|
|
1023 if (!((*toProc)(s)))
|
|
1024 #else
|
432
|
1025 if (!((int(*)(Sound))(SoundFileInfo[SoundFileFormatWave].toSound)) (s))
|
1097
|
1026 #endif
|
428
|
1027 {
|
432
|
1028 WaveCloseFile ((WaveInfo *) (s->formatInfo));
|
428
|
1029 free (s);
|
|
1030 return NULL;
|
|
1031 }
|
|
1032 }
|
|
1033
|
|
1034 return s;
|
|
1035 }
|