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