0
|
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
|
|
29 /* There are four compile-time options.
|
|
30 *
|
|
31 * XTOOLKIT This will be part of an Xt program.
|
|
32 *
|
|
33 * XTEVENTS The playing will be supervised asynchronously by the Xt event
|
|
34 * loop. If not set, playing will be completed within the call
|
|
35 * to play_file etc.
|
|
36 *
|
|
37 * ROBUST_PLAY Causes errors in nas to be caught. This means that the
|
|
38 * program will attempt not to die if the nas server does.
|
|
39 *
|
|
40 * CACHE_SOUNDS Causes the sounds to be played in buckets in the NAS
|
|
41 * server. They are named by their comment field, or if that is
|
|
42 * empty by the filename, or for play_sound_data by a name made up
|
|
43 * from the sample itself.
|
|
44 */
|
|
45
|
|
46 /* CHANGES:
|
|
47 * 10/8/94, rjc Changed names from netaudio to nas
|
|
48 * Added back asynchronous play if nas library has
|
|
49 * correct error facilities.
|
|
50 * 4/11/94, rjc Added wait_for_sounds to be called when user wants to
|
|
51 * be sure all play has finished.
|
|
52 */
|
|
53
|
|
54 #if __STDC__ || defined (STDC_HEADERS)
|
16
|
55 # include <stdlib.h>
|
|
56 # include <stdarg.h>
|
|
57 # include <string.h>
|
|
58 #endif
|
0
|
59
|
16
|
60 #ifdef HAVE_UNISTD_H
|
|
61 #include <unistd.h>
|
0
|
62 #endif
|
|
63
|
|
64 #include <stdio.h>
|
|
65 #include <config.h> /* for CONST in syssignal.h (neal@ctd.comsat.com) */
|
|
66 #include "syssignal.h"
|
|
67
|
|
68 #include <audio/audiolib.h>
|
|
69 #include <audio/soundlib.h>
|
|
70 #include <audio/snd.h>
|
|
71 #include <audio/fileutil.h>
|
|
72
|
|
73 #ifdef emacs
|
|
74
|
|
75 # include <config.h>
|
|
76 # include "lisp.h"
|
|
77
|
|
78 # define XTOOLKIT
|
|
79 # define XTEVENTS
|
|
80 # define ROBUST_PLAY
|
|
81 # define CACHE_SOUNDS
|
|
82
|
|
83 /*
|
|
84 * For old NAS libraries, force playing to be synchronous
|
|
85 * and declare the long jump point locally.
|
|
86 */
|
|
87
|
|
88 # if defined (NAS_NO_ERROR_JUMP)
|
|
89
|
|
90 # undef XTEVENTS
|
|
91
|
|
92 # include <setjmp.h>
|
|
93 jmp_buf AuXtErrorJump;
|
|
94 # endif
|
|
95
|
|
96 /* The GETTEXT is correct. --ben */
|
|
97 # define warn(str) warn_when_safe (Qnas, Qwarning, "nas: %s ", GETTEXT (str))
|
|
98
|
|
99 # define play_sound_file nas_play_sound_file
|
|
100 # define play_sound_data nas_play_sound_data
|
|
101 # define wait_for_sounds nas_wait_for_sounds
|
|
102 # define init_play nas_init_play
|
|
103 # define close_down_play nas_close_down_play
|
|
104
|
|
105 #else /* !emacs */
|
|
106 # define warn(str) fprintf (stderr, "%s\n", (str))
|
|
107 # define CONST const
|
|
108 #endif /* emacs */
|
|
109
|
|
110 #ifdef XTOOLKIT
|
|
111 # include <X11/Intrinsic.h>
|
|
112 # include <audio/Xtutil.h>
|
|
113 #endif
|
|
114
|
|
115 #if defined (ROBUST_PLAY)
|
|
116 static AuBool CatchIoErrorAndJump (AuServer *aud);
|
|
117 static AuBool CatchErrorAndJump (AuServer *aud, AuErrorEvent *event);
|
|
118 SIGTYPE sigpipe_handle (int signo);
|
|
119 #endif
|
|
120
|
|
121 extern Lisp_Object Vsynchronous_sounds;
|
|
122
|
|
123 static Sound SoundOpenDataForReading (unsigned char *data, int length);
|
|
124
|
|
125 static AuServer *aud;
|
|
126
|
|
127 /* count of sounds currently being played. */
|
|
128 static int sounds_in_play;
|
|
129
|
|
130
|
|
131 #ifdef XTOOLKIT
|
|
132 static Display *aud_server;
|
|
133 static XtInputId input_id;
|
|
134 #else
|
|
135 static char *aud_server;
|
|
136 #endif /* XTOOLKIT */
|
|
137
|
|
138 char *
|
|
139 init_play (
|
|
140 #ifdef XTOOLKIT
|
|
141 Display *display
|
|
142 #else
|
|
143 char *server
|
|
144 #endif
|
|
145 )
|
|
146 {
|
|
147 char *err_message;
|
|
148 SIGTYPE (*old_sigpipe) ();
|
|
149
|
|
150 #ifdef XTOOLKIT
|
|
151 char * server = DisplayString (display);
|
|
152 XtAppContext app_context = XtDisplayToApplicationContext (display);
|
|
153
|
|
154 aud_server = display;
|
|
155 #else
|
|
156
|
|
157 aud_server = server;
|
|
158 #endif
|
|
159
|
|
160 #ifdef ROBUST_PLAY
|
|
161 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
|
|
162 if (setjmp (AuXtErrorJump))
|
|
163 {
|
|
164 signal (SIGPIPE, old_sigpipe);
|
|
165 #ifdef emacs
|
|
166 start_interrupts ();
|
|
167 #endif
|
|
168 return "error in NAS";
|
|
169 }
|
|
170 #endif
|
|
171
|
|
172 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
|
|
173 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
|
|
174 AuDefaultErrorHandler = CatchErrorAndJump;
|
|
175 #endif
|
|
176
|
|
177 #ifdef emacs
|
|
178 stop_interrupts ();
|
|
179 #endif
|
|
180 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
|
|
181 #ifdef emacs
|
|
182 start_interrupts ();
|
|
183 #endif
|
|
184 if (!aud)
|
|
185 {
|
|
186 #ifdef ROBUST_PLAY
|
|
187 signal (SIGPIPE, old_sigpipe);
|
|
188 #endif
|
|
189 if (err_message == NULL)
|
|
190 return "Can't connect to audio server";
|
|
191 else
|
|
192 return err_message;
|
|
193 }
|
|
194
|
|
195 #if defined (ROBUST_PLAY)
|
|
196 # if defined (NAS_NO_ERROR_JUMP)
|
|
197 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
|
|
198 aud->funcs.error_handler = CatchErrorAndJump;
|
|
199 # else /* !NAS_NO_ERROR_JUMP */
|
|
200 AuDefaultIOErrorHandler = NULL;
|
|
201 AuDefaultErrorHandler = NULL;
|
|
202 # endif
|
|
203 #endif
|
|
204
|
|
205 #ifdef XTEVENTS
|
|
206 input_id = AuXtAppAddAudioHandler (app_context, aud);
|
|
207 #endif
|
|
208
|
|
209 #ifdef CACHE_SOUNDS
|
|
210 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
|
|
211 #endif
|
|
212
|
|
213 #ifdef ROBUST_PLAY
|
|
214 signal (SIGPIPE, old_sigpipe);
|
|
215 #endif
|
|
216
|
|
217 sounds_in_play = 0;
|
|
218
|
|
219 return NULL;
|
|
220 }
|
|
221
|
|
222 void
|
|
223 close_down_play (void)
|
|
224
|
|
225 {
|
|
226 AuCloseServer (aud);
|
|
227 warn ("disconnected from audio server");
|
|
228 }
|
|
229
|
|
230 /********************************************************************\
|
|
231 * *
|
|
232 * Callback which is run when the sound finishes playing. *
|
|
233 * *
|
|
234 \********************************************************************/
|
|
235
|
|
236 static void
|
|
237 doneCB (AuServer *aud,
|
|
238 AuEventHandlerRec *handler,
|
|
239 AuEvent *ev,
|
|
240 AuPointer data)
|
|
241 {
|
|
242 int *in_play_p = (int *) data;
|
|
243
|
|
244 (*in_play_p) --;
|
|
245 }
|
|
246
|
|
247 #ifdef CACHE_SOUNDS
|
|
248
|
|
249 /********************************************************************\
|
|
250 * *
|
|
251 * Play a sound by playing the relevant bucket, if any or *
|
|
252 * downloading it if not. *
|
|
253 * *
|
|
254 \********************************************************************/
|
|
255
|
|
256 static void
|
|
257 do_caching_play (Sound s,
|
|
258 int volume,
|
|
259 unsigned char *buf)
|
|
260
|
|
261 {
|
|
262 AuBucketAttributes *list, b;
|
|
263 AuBucketID id;
|
|
264 int n;
|
|
265
|
|
266 AuSetString (AuBucketDescription (&b),
|
|
267 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
|
|
268
|
|
269 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
|
|
270
|
|
271 if (list == NULL)
|
|
272 {
|
|
273 unsigned char *my_buf;
|
|
274
|
|
275 if (buf==NULL)
|
|
276 {
|
|
277 if ((my_buf=malloc (SoundNumBytes (s)))==NULL)
|
|
278 {
|
|
279 return;
|
|
280 }
|
|
281
|
|
282 if (SoundReadFile (my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
|
|
283 {
|
|
284 free (my_buf);
|
|
285 return;
|
|
286 }
|
|
287 }
|
|
288 else
|
|
289 my_buf=buf;
|
|
290
|
|
291 id = AuSoundCreateBucketFromData (aud,
|
|
292 s,
|
|
293 my_buf,
|
|
294 AuAccessAllMasks,
|
|
295 NULL,
|
|
296 NULL);
|
|
297 if (buf == NULL)
|
|
298 free (my_buf);
|
|
299 }
|
|
300 else /* found cached sound */
|
|
301 {
|
|
302 id = AuBucketIdentifier (list);
|
|
303 AuFreeBucketAttributes (aud, n, list);
|
|
304 }
|
|
305
|
|
306 sounds_in_play++;
|
|
307
|
|
308 AuSoundPlayFromBucket (aud,
|
|
309 id,
|
|
310 AuNone,
|
|
311 AuFixedPointFromFraction (volume, 100),
|
|
312 doneCB, (AuPointer) &sounds_in_play,
|
|
313 1,
|
|
314 NULL, NULL,
|
|
315 NULL, NULL);
|
|
316
|
|
317 }
|
|
318 #endif /* CACHE_SOUNDS */
|
|
319
|
|
320
|
|
321 void
|
|
322 wait_for_sounds (void)
|
|
323
|
|
324 {
|
|
325 AuEvent ev;
|
|
326
|
|
327 while (sounds_in_play>0)
|
|
328 {
|
|
329 AuNextEvent (aud, AuTrue, &ev);
|
|
330 AuDispatchEvent (aud, &ev);
|
|
331 }
|
|
332 }
|
|
333
|
|
334 int
|
|
335 play_sound_file (char *sound_file,
|
|
336 int volume)
|
|
337 {
|
|
338 SIGTYPE (*old_sigpipe) ();
|
|
339
|
|
340 #ifdef ROBUST_PLAY
|
|
341 old_sigpipe=signal (SIGPIPE, sigpipe_handle);
|
|
342 if (setjmp (AuXtErrorJump))
|
|
343 {
|
|
344 signal (SIGPIPE, old_sigpipe);
|
|
345 return 0;
|
|
346 }
|
|
347 #endif
|
|
348
|
|
349 if (aud==NULL)
|
|
350 if (aud_server != NULL)
|
|
351 {
|
|
352 char *m;
|
|
353 /* attempt to reconect */
|
|
354 if ((m=init_play (aud_server))!= NULL)
|
|
355 {
|
|
356
|
|
357 #ifdef ROBUST_PLAY
|
|
358 signal (SIGPIPE, old_sigpipe);
|
|
359 #endif
|
|
360 return 0;
|
|
361 }
|
|
362 }
|
|
363 else
|
|
364 {
|
|
365 warn ("Attempt to play with no audio init\n");
|
|
366 #ifdef ROBUST_PLAY
|
|
367 signal (SIGPIPE, old_sigpipe);
|
|
368 #endif
|
|
369 return 0;
|
|
370 }
|
|
371
|
|
372 #ifndef CACHE_SOUNDS
|
|
373 sounds_in_play++;
|
|
374 AuSoundPlayFromFile (aud,
|
|
375 sound_file,
|
|
376 AuNone,
|
|
377 AuFixedPointFromFraction (volume,100),
|
|
378 doneCB, (AuPointer) &sounds_in_play,
|
|
379 NULL,
|
|
380 NULL,
|
|
381 NULL,
|
|
382 NULL);
|
|
383 #else
|
|
384 /* Cache the sounds in buckets on the server */
|
|
385
|
|
386 {
|
|
387 Sound s;
|
|
388
|
|
389 if ((s = SoundOpenFileForReading (sound_file))==NULL)
|
|
390 {
|
|
391 #ifdef ROBUST_PLAY
|
|
392 signal (SIGPIPE, old_sigpipe);
|
|
393 #endif
|
|
394 return 0;
|
|
395 }
|
|
396
|
|
397 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
|
|
398 {
|
|
399 SoundComment (s) = FileCommentFromFilename (sound_file);
|
|
400 }
|
|
401
|
|
402 do_caching_play (s, volume, NULL);
|
|
403
|
|
404 SoundCloseFile (s);
|
|
405
|
|
406 }
|
|
407 #endif /* CACHE_SOUNDS */
|
|
408
|
|
409 #ifndef XTEVENTS
|
|
410 wait_for_sounds ();
|
|
411 #else
|
|
412 if (!NILP (Vsynchronous_sounds))
|
|
413 {
|
|
414 wait_for_sounds ();
|
|
415 }
|
|
416 #endif
|
|
417
|
|
418 #ifdef ROBUST_PLAY
|
|
419 signal (SIGPIPE, old_sigpipe);
|
|
420 #endif
|
|
421
|
|
422 return 1;
|
|
423 }
|
|
424
|
|
425 int
|
|
426 play_sound_data (unsigned char *data,
|
|
427 int length,
|
|
428 int volume)
|
|
429 {
|
|
430 Sound s;
|
|
431 int offset;
|
|
432 SIGTYPE (*old_sigpipe) ();
|
|
433
|
|
434 #if !defined (XTEVENTS)
|
|
435 AuEvent ev;
|
|
436 #endif
|
|
437
|
|
438 #ifdef ROBUST_PLAY
|
|
439 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
|
|
440 if (setjmp (AuXtErrorJump) !=0)
|
|
441 {
|
|
442 signal (SIGPIPE, old_sigpipe);
|
|
443 return 0;
|
|
444 }
|
|
445 #endif
|
|
446
|
|
447
|
|
448 if (aud == NULL)
|
|
449 if (aud_server != NULL)
|
|
450 {
|
|
451 char *m;
|
|
452 /* attempt to reconect */
|
|
453 if ((m = init_play (aud_server)) != NULL)
|
|
454 {
|
|
455 #ifdef ROBUST_PLAY
|
|
456 signal (SIGPIPE, old_sigpipe);
|
|
457 #endif
|
|
458 return 0;
|
|
459 }
|
|
460 }
|
|
461 else
|
|
462 {
|
|
463 warn ("Attempt to play with no audio init\n");
|
|
464 #ifdef ROBUST_PLAY
|
|
465 signal (SIGPIPE, old_sigpipe);
|
|
466 #endif
|
|
467 return 0;
|
|
468 }
|
|
469
|
|
470 if ((s=SoundOpenDataForReading (data, length))==NULL)
|
|
471 {
|
|
472 warn ("unknown sound type");
|
|
473 #ifdef ROBUST_PLAY
|
|
474 signal (SIGPIPE, old_sigpipe);
|
|
475 #endif
|
|
476 return 0;
|
|
477 }
|
|
478
|
|
479 if (SoundFileFormat (s) == SoundFileFormatSnd)
|
|
480 {
|
|
481 /* hack, hack */
|
|
482 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
|
|
483 }
|
|
484 else
|
|
485 {
|
|
486 warn ("only understand snd files at the moment");
|
|
487 SoundCloseFile (s);
|
|
488 #ifdef ROBUST_PLAY
|
|
489 signal (SIGPIPE, old_sigpipe);
|
|
490 #endif
|
|
491 return 0;
|
|
492 }
|
|
493
|
|
494 #ifndef CACHE_SOUNDS
|
|
495 sounds_in_play++;
|
|
496 AuSoundPlayFromData (aud,
|
|
497 s,
|
|
498 data+offset,
|
|
499 AuNone,
|
|
500 AuFixedPointFromFraction (volume,100),
|
|
501 doneCB, (AuPointer) &sounds_in_play,
|
|
502 NULL,
|
|
503 NULL,
|
|
504 NULL,
|
|
505 NULL);
|
|
506 #else
|
|
507 /* Cache the sounds in buckets on the server */
|
|
508
|
|
509 {
|
|
510 do_caching_play (s, volume, data+offset);
|
|
511 }
|
|
512 #endif /* CACHE_SOUNDS */
|
|
513
|
|
514
|
|
515 #ifndef XTEVENTS
|
|
516 wait_for_sounds ();
|
|
517 #else
|
|
518 if (!NILP (Vsynchronous_sounds))
|
|
519 {
|
|
520 wait_for_sounds ();
|
|
521 }
|
|
522 #endif
|
|
523
|
|
524 SoundCloseFile (s);
|
|
525
|
|
526 #ifdef ROBUST_PLAY
|
|
527 signal (SIGPIPE, old_sigpipe);
|
|
528 #endif
|
|
529
|
|
530 return 1;
|
|
531 }
|
|
532
|
|
533 #if defined (ROBUST_PLAY)
|
|
534
|
|
535 /********************************************************************\
|
|
536 * *
|
|
537 * Code to protect the client from server shutdowns. *
|
|
538 * *
|
|
539 * This is unbelievably horrible. *
|
|
540 * *
|
|
541 \********************************************************************/
|
|
542
|
|
543 static AuBool
|
|
544 CatchIoErrorAndJump (AuServer *old_aud)
|
|
545 {
|
|
546 if (old_aud)
|
|
547 warn ("Audio Server connection broken");
|
|
548 else
|
|
549 warn ("Audio Server connection broken because of signal");
|
|
550
|
|
551 #ifdef XTEVENTS
|
|
552 #ifdef XTOOLKIT
|
|
553 {
|
|
554 AuXtAppRemoveAudioHandler (aud, input_id);
|
|
555 }
|
|
556 #endif
|
|
557
|
|
558 if (aud)
|
|
559 AuCloseServer (aud);
|
|
560 aud = NULL;
|
|
561 sounds_in_play = 0;
|
|
562
|
|
563 longjmp (AuXtErrorJump, 1);
|
|
564
|
|
565 #else /* not XTEVENTS */
|
|
566
|
|
567 if (aud)
|
|
568 AuCloseServer (aud);
|
|
569 aud = NULL;
|
|
570 sounds_in_play = 0;
|
|
571 longjmp (AuXtErrorJump, 1);
|
|
572
|
|
573 #endif /* XTEVENTS */
|
|
574 }
|
|
575
|
|
576 SIGTYPE
|
|
577 sigpipe_handle (int signo)
|
|
578 {
|
|
579 CatchIoErrorAndJump (NULL);
|
|
580 }
|
|
581
|
|
582 static AuBool
|
|
583 CatchErrorAndJump (AuServer *old_aud,
|
|
584 AuErrorEvent *event)
|
|
585 {
|
|
586 return CatchIoErrorAndJump (old_aud);
|
|
587 }
|
|
588
|
|
589 #endif /* ROBUST_PLAY */
|
|
590
|
|
591 /********************************************************************\
|
|
592 * *
|
|
593 * This code is here because the nas Sound library doesn't *
|
|
594 * support playing from a file buffered in memory. It's a fairly *
|
|
595 * direct translation of the file-based equivalent. *
|
|
596 * *
|
|
597 * Since we don't have a filename, samples with no comment field *
|
|
598 * are named by a section of their content. *
|
|
599 * *
|
|
600 \********************************************************************/
|
|
601
|
|
602 /* Create a name from the sound. */
|
|
603
|
|
604 static char *
|
|
605 NameFromData (CONST unsigned char *buf,
|
|
606 int len)
|
|
607
|
|
608 {
|
|
609 unsigned char name[9];
|
|
610 int i;
|
|
611 char *s;
|
|
612
|
|
613 buf+=len/2;
|
|
614 len -= len/2;
|
|
615
|
|
616 i=0;
|
|
617 while (i<8 && len >0)
|
|
618 {
|
|
619 while (*buf < 32 && len>0)
|
|
620 {
|
|
621 buf++;
|
|
622 len--;
|
|
623 }
|
|
624 name[i]= *buf;
|
|
625 i++;
|
|
626 buf++;
|
|
627 len--;
|
|
628 }
|
|
629
|
|
630 name[i]='\0';
|
|
631
|
|
632 if (i==8)
|
|
633 {
|
|
634 strcpy (s=malloc (10), name);
|
|
635 }
|
|
636 else
|
|
637 {
|
|
638 strcpy (s=malloc (15), "short sound");
|
|
639 }
|
|
640
|
|
641 return s;
|
|
642 }
|
|
643
|
|
644 /* Code to do a pseudo-open on a data buffer. Only for snd files at the
|
|
645 moment.
|
|
646 */
|
|
647
|
|
648 static SndInfo *
|
|
649 SndOpenDataForReading (CONST char *data,
|
|
650 int length)
|
|
651
|
|
652 {
|
|
653 SndInfo *si;
|
|
654 int size;
|
|
655
|
|
656 if (!(si = (SndInfo *) malloc (sizeof (SndInfo))))
|
|
657 return NULL;
|
|
658
|
|
659 si->comment = NULL;
|
|
660 si->writing = 0;
|
|
661
|
|
662 memcpy (&si->h, data, sizeof (SndHeader));
|
|
663
|
|
664 if (LITTLE_ENDIAN)
|
|
665 {
|
|
666 char n;
|
|
667
|
|
668 swapl (&si->h.magic, n);
|
|
669 swapl (&si->h.dataOffset, n);
|
|
670 swapl (&si->h.dataSize, n);
|
|
671 swapl (&si->h.format, n);
|
|
672 swapl (&si->h.sampleRate, n);
|
|
673 swapl (&si->h.tracks, n);
|
|
674 }
|
|
675
|
|
676 if (si->h.magic != SND_MAGIC_NUM)
|
|
677 {
|
|
678 free (si);
|
|
679 return NULL;
|
|
680 }
|
|
681
|
|
682 size = si->h.dataOffset - sizeof (SndHeader);
|
|
683
|
|
684 if (size)
|
|
685 {
|
|
686 if (!(si->comment = (char *) malloc (size + 1)))
|
|
687 {
|
|
688 free (si);
|
|
689 return NULL;
|
|
690 }
|
|
691
|
|
692 memcpy (si->comment, data+sizeof (SndHeader), size);
|
|
693
|
|
694 *(si->comment + size) = 0;
|
|
695 if (*si->comment == '\0')
|
|
696 si->comment =
|
|
697 NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
|
|
698 }
|
|
699 else
|
|
700 si->comment = NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
|
|
701
|
|
702 si->h.dataSize = length-si->h.dataOffset;
|
|
703
|
|
704 si->fp=NULL;
|
|
705
|
|
706 return si;
|
|
707 }
|
|
708
|
|
709 static Sound
|
|
710 SoundOpenDataForReading (unsigned char *data,
|
|
711 int length)
|
|
712
|
|
713 {
|
|
714 Sound s;
|
|
715
|
|
716 if (!(s = (Sound) malloc (sizeof (SoundRec))))
|
|
717 return NULL;
|
|
718
|
|
719 if ((s->formatInfo = SndOpenDataForReading (data, length))==NULL)
|
|
720 {
|
|
721 free (s);
|
|
722 return NULL;
|
|
723 }
|
|
724
|
|
725
|
|
726 if (!(SoundFileInfo[SoundFileFormatSnd].toSound) (s))
|
|
727 {
|
|
728 SndCloseFile (s->formatInfo);
|
|
729 free (s);
|
|
730 return NULL;
|
|
731 }
|
|
732
|
|
733 return s;
|
|
734 }
|
|
735
|