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