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