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
|
272
|
54 #ifdef emacs
|
|
55 #include <config.h>
|
|
56 #include "lisp.h"
|
365
|
57 #include "sysdep.h" /* for (start|stop)_interrupts */
|
|
58 #include "syssignal.h"
|
272
|
59 #endif
|
|
60
|
82
|
61 #if __STDC__ || defined (STDC_HEADERS)
|
|
62 # include <stdlib.h>
|
|
63 # include <stdarg.h>
|
|
64 # include <string.h>
|
16
|
65 #endif
|
0
|
66
|
16
|
67 #ifdef HAVE_UNISTD_H
|
|
68 #include <unistd.h>
|
0
|
69 #endif
|
|
70
|
|
71 #include <stdio.h>
|
|
72
|
365
|
73 /* NAS 1.2p5 and previous define these in conflict with GNU libc (at least) */
|
|
74 #ifndef NAS_USES_OWN_NAMESPACE
|
272
|
75 #undef LITTLE_ENDIAN
|
|
76 #undef BIG_ENDIAN
|
365
|
77 #endif
|
|
78
|
0
|
79 #include <audio/audiolib.h>
|
|
80 #include <audio/soundlib.h>
|
|
81 #include <audio/snd.h>
|
|
82 #include <audio/fileutil.h>
|
|
83
|
365
|
84 /* If needed (NAS <= 1.2p5) define the NAS_ versions of *_ENDIAN */
|
|
85 #ifndef NAS_USES_OWN_NAMESPACE
|
|
86 #define NAS_LITTLE_ENDIAN LITTLE_ENDIAN
|
|
87 #define NAS_BIG_ENDIAN BIG_ENDIAN
|
|
88 #endif
|
|
89
|
0
|
90 #ifdef emacs
|
|
91
|
|
92 # define XTOOLKIT
|
|
93 # define XTEVENTS
|
|
94 # define ROBUST_PLAY
|
|
95 # define CACHE_SOUNDS
|
|
96
|
|
97 /*
|
|
98 * For old NAS libraries, force playing to be synchronous
|
|
99 * and declare the long jump point locally.
|
|
100 */
|
|
101
|
|
102 # if defined (NAS_NO_ERROR_JUMP)
|
|
103
|
|
104 # undef XTEVENTS
|
|
105
|
|
106 # include <setjmp.h>
|
|
107 jmp_buf AuXtErrorJump;
|
|
108 # endif
|
|
109
|
|
110 /* The GETTEXT is correct. --ben */
|
|
111 # define warn(str) warn_when_safe (Qnas, Qwarning, "nas: %s ", GETTEXT (str))
|
|
112
|
|
113 # define play_sound_file nas_play_sound_file
|
|
114 # define play_sound_data nas_play_sound_data
|
|
115 # define wait_for_sounds nas_wait_for_sounds
|
|
116 # define init_play nas_init_play
|
|
117 # define close_down_play nas_close_down_play
|
|
118
|
|
119 #else /* !emacs */
|
|
120 # define warn(str) fprintf (stderr, "%s\n", (str))
|
|
121 # define CONST const
|
|
122 #endif /* emacs */
|
|
123
|
|
124 #ifdef XTOOLKIT
|
|
125 # include <X11/Intrinsic.h>
|
|
126 # include <audio/Xtutil.h>
|
|
127 #endif
|
|
128
|
|
129 #if defined (ROBUST_PLAY)
|
|
130 static AuBool CatchIoErrorAndJump (AuServer *aud);
|
|
131 static AuBool CatchErrorAndJump (AuServer *aud, AuErrorEvent *event);
|
|
132 SIGTYPE sigpipe_handle (int signo);
|
|
133 #endif
|
|
134
|
|
135 extern Lisp_Object Vsynchronous_sounds;
|
|
136
|
|
137 static Sound SoundOpenDataForReading (unsigned char *data, int length);
|
|
138
|
|
139 static AuServer *aud;
|
|
140
|
|
141 /* count of sounds currently being played. */
|
|
142 static int sounds_in_play;
|
|
143
|
|
144
|
|
145 #ifdef XTOOLKIT
|
|
146 static Display *aud_server;
|
|
147 static XtInputId input_id;
|
|
148 #else
|
|
149 static char *aud_server;
|
|
150 #endif /* XTOOLKIT */
|
|
151
|
|
152 char *
|
|
153 init_play (
|
|
154 #ifdef XTOOLKIT
|
|
155 Display *display
|
|
156 #else
|
|
157 char *server
|
|
158 #endif
|
|
159 )
|
|
160 {
|
|
161 char *err_message;
|
|
162 SIGTYPE (*old_sigpipe) ();
|
|
163
|
|
164 #ifdef XTOOLKIT
|
|
165 char * server = DisplayString (display);
|
|
166 XtAppContext app_context = XtDisplayToApplicationContext (display);
|
|
167
|
|
168 aud_server = display;
|
|
169 #else
|
|
170
|
|
171 aud_server = server;
|
|
172 #endif
|
|
173
|
|
174 #ifdef ROBUST_PLAY
|
|
175 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
|
|
176 if (setjmp (AuXtErrorJump))
|
|
177 {
|
|
178 signal (SIGPIPE, old_sigpipe);
|
|
179 #ifdef emacs
|
|
180 start_interrupts ();
|
|
181 #endif
|
|
182 return "error in NAS";
|
|
183 }
|
|
184 #endif
|
|
185
|
|
186 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
|
|
187 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
|
|
188 AuDefaultErrorHandler = CatchErrorAndJump;
|
|
189 #endif
|
|
190
|
|
191 #ifdef emacs
|
|
192 stop_interrupts ();
|
|
193 #endif
|
|
194 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
|
|
195 #ifdef emacs
|
|
196 start_interrupts ();
|
|
197 #endif
|
|
198 if (!aud)
|
|
199 {
|
|
200 #ifdef ROBUST_PLAY
|
|
201 signal (SIGPIPE, old_sigpipe);
|
|
202 #endif
|
|
203 if (err_message == NULL)
|
|
204 return "Can't connect to audio server";
|
|
205 else
|
|
206 return err_message;
|
|
207 }
|
|
208
|
|
209 #if defined (ROBUST_PLAY)
|
|
210 # if defined (NAS_NO_ERROR_JUMP)
|
|
211 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
|
|
212 aud->funcs.error_handler = CatchErrorAndJump;
|
|
213 # else /* !NAS_NO_ERROR_JUMP */
|
|
214 AuDefaultIOErrorHandler = NULL;
|
|
215 AuDefaultErrorHandler = NULL;
|
|
216 # endif
|
|
217 #endif
|
|
218
|
|
219 #ifdef XTEVENTS
|
|
220 input_id = AuXtAppAddAudioHandler (app_context, aud);
|
|
221 #endif
|
|
222
|
|
223 #ifdef CACHE_SOUNDS
|
|
224 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
|
|
225 #endif
|
|
226
|
|
227 #ifdef ROBUST_PLAY
|
|
228 signal (SIGPIPE, old_sigpipe);
|
|
229 #endif
|
|
230
|
|
231 sounds_in_play = 0;
|
|
232
|
|
233 return NULL;
|
|
234 }
|
|
235
|
|
236 void
|
|
237 close_down_play (void)
|
|
238
|
|
239 {
|
|
240 AuCloseServer (aud);
|
|
241 warn ("disconnected from audio server");
|
|
242 }
|
|
243
|
|
244 /********************************************************************\
|
|
245 * *
|
|
246 * Callback which is run when the sound finishes playing. *
|
|
247 * *
|
|
248 \********************************************************************/
|
|
249
|
|
250 static void
|
|
251 doneCB (AuServer *aud,
|
|
252 AuEventHandlerRec *handler,
|
|
253 AuEvent *ev,
|
|
254 AuPointer data)
|
|
255 {
|
|
256 int *in_play_p = (int *) data;
|
|
257
|
|
258 (*in_play_p) --;
|
|
259 }
|
|
260
|
|
261 #ifdef CACHE_SOUNDS
|
|
262
|
|
263 /********************************************************************\
|
|
264 * *
|
|
265 * Play a sound by playing the relevant bucket, if any or *
|
|
266 * downloading it if not. *
|
|
267 * *
|
|
268 \********************************************************************/
|
|
269
|
|
270 static void
|
|
271 do_caching_play (Sound s,
|
|
272 int volume,
|
|
273 unsigned char *buf)
|
|
274
|
|
275 {
|
|
276 AuBucketAttributes *list, b;
|
|
277 AuBucketID id;
|
|
278 int n;
|
|
279
|
|
280 AuSetString (AuBucketDescription (&b),
|
|
281 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
|
|
282
|
|
283 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
|
|
284
|
|
285 if (list == NULL)
|
|
286 {
|
|
287 unsigned char *my_buf;
|
|
288
|
|
289 if (buf==NULL)
|
|
290 {
|
|
291 if ((my_buf=malloc (SoundNumBytes (s)))==NULL)
|
|
292 {
|
|
293 return;
|
|
294 }
|
|
295
|
|
296 if (SoundReadFile (my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
|
|
297 {
|
|
298 free (my_buf);
|
|
299 return;
|
|
300 }
|
|
301 }
|
|
302 else
|
|
303 my_buf=buf;
|
|
304
|
|
305 id = AuSoundCreateBucketFromData (aud,
|
|
306 s,
|
|
307 my_buf,
|
|
308 AuAccessAllMasks,
|
|
309 NULL,
|
|
310 NULL);
|
|
311 if (buf == NULL)
|
|
312 free (my_buf);
|
|
313 }
|
|
314 else /* found cached sound */
|
|
315 {
|
|
316 id = AuBucketIdentifier (list);
|
|
317 AuFreeBucketAttributes (aud, n, list);
|
|
318 }
|
|
319
|
|
320 sounds_in_play++;
|
|
321
|
|
322 AuSoundPlayFromBucket (aud,
|
|
323 id,
|
|
324 AuNone,
|
|
325 AuFixedPointFromFraction (volume, 100),
|
|
326 doneCB, (AuPointer) &sounds_in_play,
|
|
327 1,
|
|
328 NULL, NULL,
|
|
329 NULL, NULL);
|
|
330
|
|
331 }
|
|
332 #endif /* CACHE_SOUNDS */
|
|
333
|
|
334
|
|
335 void
|
|
336 wait_for_sounds (void)
|
|
337
|
|
338 {
|
|
339 AuEvent ev;
|
|
340
|
|
341 while (sounds_in_play>0)
|
|
342 {
|
|
343 AuNextEvent (aud, AuTrue, &ev);
|
|
344 AuDispatchEvent (aud, &ev);
|
|
345 }
|
|
346 }
|
|
347
|
|
348 int
|
|
349 play_sound_file (char *sound_file,
|
|
350 int volume)
|
|
351 {
|
|
352 SIGTYPE (*old_sigpipe) ();
|
|
353
|
|
354 #ifdef ROBUST_PLAY
|
|
355 old_sigpipe=signal (SIGPIPE, sigpipe_handle);
|
|
356 if (setjmp (AuXtErrorJump))
|
|
357 {
|
|
358 signal (SIGPIPE, old_sigpipe);
|
|
359 return 0;
|
|
360 }
|
|
361 #endif
|
|
362
|
263
|
363 if (aud==NULL) {
|
0
|
364 if (aud_server != NULL)
|
|
365 {
|
|
366 char *m;
|
|
367 /* attempt to reconect */
|
|
368 if ((m=init_play (aud_server))!= NULL)
|
|
369 {
|
|
370
|
|
371 #ifdef ROBUST_PLAY
|
|
372 signal (SIGPIPE, old_sigpipe);
|
|
373 #endif
|
|
374 return 0;
|
|
375 }
|
|
376 }
|
|
377 else
|
|
378 {
|
|
379 warn ("Attempt to play with no audio init\n");
|
|
380 #ifdef ROBUST_PLAY
|
|
381 signal (SIGPIPE, old_sigpipe);
|
|
382 #endif
|
|
383 return 0;
|
|
384 }
|
263
|
385 }
|
0
|
386
|
|
387 #ifndef CACHE_SOUNDS
|
|
388 sounds_in_play++;
|
|
389 AuSoundPlayFromFile (aud,
|
|
390 sound_file,
|
|
391 AuNone,
|
|
392 AuFixedPointFromFraction (volume,100),
|
|
393 doneCB, (AuPointer) &sounds_in_play,
|
|
394 NULL,
|
|
395 NULL,
|
|
396 NULL,
|
|
397 NULL);
|
|
398 #else
|
|
399 /* Cache the sounds in buckets on the server */
|
|
400
|
|
401 {
|
|
402 Sound s;
|
|
403
|
|
404 if ((s = SoundOpenFileForReading (sound_file))==NULL)
|
|
405 {
|
|
406 #ifdef ROBUST_PLAY
|
|
407 signal (SIGPIPE, old_sigpipe);
|
|
408 #endif
|
|
409 return 0;
|
|
410 }
|
|
411
|
|
412 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
|
|
413 {
|
|
414 SoundComment (s) = FileCommentFromFilename (sound_file);
|
|
415 }
|
|
416
|
|
417 do_caching_play (s, volume, NULL);
|
|
418
|
|
419 SoundCloseFile (s);
|
|
420
|
|
421 }
|
|
422 #endif /* CACHE_SOUNDS */
|
|
423
|
|
424 #ifndef XTEVENTS
|
|
425 wait_for_sounds ();
|
|
426 #else
|
|
427 if (!NILP (Vsynchronous_sounds))
|
|
428 {
|
|
429 wait_for_sounds ();
|
|
430 }
|
|
431 #endif
|
|
432
|
|
433 #ifdef ROBUST_PLAY
|
|
434 signal (SIGPIPE, old_sigpipe);
|
|
435 #endif
|
|
436
|
|
437 return 1;
|
|
438 }
|
|
439
|
|
440 int
|
|
441 play_sound_data (unsigned char *data,
|
|
442 int length,
|
|
443 int volume)
|
|
444 {
|
|
445 Sound s;
|
|
446 int offset;
|
|
447 SIGTYPE (*old_sigpipe) ();
|
|
448
|
|
449 #if !defined (XTEVENTS)
|
|
450 AuEvent ev;
|
|
451 #endif
|
|
452
|
|
453 #ifdef ROBUST_PLAY
|
|
454 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
|
|
455 if (setjmp (AuXtErrorJump) !=0)
|
|
456 {
|
|
457 signal (SIGPIPE, old_sigpipe);
|
|
458 return 0;
|
|
459 }
|
|
460 #endif
|
|
461
|
|
462
|
263
|
463 if (aud == NULL) {
|
0
|
464 if (aud_server != NULL)
|
|
465 {
|
|
466 char *m;
|
|
467 /* attempt to reconect */
|
|
468 if ((m = init_play (aud_server)) != NULL)
|
|
469 {
|
|
470 #ifdef ROBUST_PLAY
|
|
471 signal (SIGPIPE, old_sigpipe);
|
|
472 #endif
|
|
473 return 0;
|
|
474 }
|
|
475 }
|
|
476 else
|
|
477 {
|
|
478 warn ("Attempt to play with no audio init\n");
|
|
479 #ifdef ROBUST_PLAY
|
|
480 signal (SIGPIPE, old_sigpipe);
|
|
481 #endif
|
|
482 return 0;
|
|
483 }
|
263
|
484 }
|
0
|
485
|
|
486 if ((s=SoundOpenDataForReading (data, length))==NULL)
|
|
487 {
|
|
488 warn ("unknown sound type");
|
|
489 #ifdef ROBUST_PLAY
|
|
490 signal (SIGPIPE, old_sigpipe);
|
|
491 #endif
|
|
492 return 0;
|
|
493 }
|
|
494
|
|
495 if (SoundFileFormat (s) == SoundFileFormatSnd)
|
|
496 {
|
|
497 /* hack, hack */
|
|
498 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
|
|
499 }
|
|
500 else
|
|
501 {
|
|
502 warn ("only understand snd files at the moment");
|
|
503 SoundCloseFile (s);
|
|
504 #ifdef ROBUST_PLAY
|
|
505 signal (SIGPIPE, old_sigpipe);
|
|
506 #endif
|
|
507 return 0;
|
|
508 }
|
|
509
|
|
510 #ifndef CACHE_SOUNDS
|
|
511 sounds_in_play++;
|
|
512 AuSoundPlayFromData (aud,
|
|
513 s,
|
|
514 data+offset,
|
|
515 AuNone,
|
|
516 AuFixedPointFromFraction (volume,100),
|
|
517 doneCB, (AuPointer) &sounds_in_play,
|
|
518 NULL,
|
|
519 NULL,
|
|
520 NULL,
|
|
521 NULL);
|
|
522 #else
|
|
523 /* Cache the sounds in buckets on the server */
|
|
524
|
|
525 {
|
|
526 do_caching_play (s, volume, data+offset);
|
|
527 }
|
|
528 #endif /* CACHE_SOUNDS */
|
|
529
|
|
530
|
|
531 #ifndef XTEVENTS
|
|
532 wait_for_sounds ();
|
|
533 #else
|
|
534 if (!NILP (Vsynchronous_sounds))
|
|
535 {
|
|
536 wait_for_sounds ();
|
|
537 }
|
|
538 #endif
|
|
539
|
|
540 SoundCloseFile (s);
|
|
541
|
|
542 #ifdef ROBUST_PLAY
|
|
543 signal (SIGPIPE, old_sigpipe);
|
|
544 #endif
|
|
545
|
|
546 return 1;
|
|
547 }
|
|
548
|
|
549 #if defined (ROBUST_PLAY)
|
|
550
|
|
551 /********************************************************************\
|
|
552 * *
|
|
553 * Code to protect the client from server shutdowns. *
|
|
554 * *
|
|
555 * This is unbelievably horrible. *
|
|
556 * *
|
|
557 \********************************************************************/
|
|
558
|
|
559 static AuBool
|
|
560 CatchIoErrorAndJump (AuServer *old_aud)
|
|
561 {
|
|
562 if (old_aud)
|
|
563 warn ("Audio Server connection broken");
|
|
564 else
|
|
565 warn ("Audio Server connection broken because of signal");
|
|
566
|
|
567 #ifdef XTEVENTS
|
|
568 #ifdef XTOOLKIT
|
|
569 {
|
|
570 AuXtAppRemoveAudioHandler (aud, input_id);
|
|
571 }
|
|
572 #endif
|
|
573
|
|
574 if (aud)
|
|
575 AuCloseServer (aud);
|
|
576 aud = NULL;
|
|
577 sounds_in_play = 0;
|
|
578
|
|
579 longjmp (AuXtErrorJump, 1);
|
|
580
|
|
581 #else /* not XTEVENTS */
|
|
582
|
|
583 if (aud)
|
|
584 AuCloseServer (aud);
|
|
585 aud = NULL;
|
|
586 sounds_in_play = 0;
|
|
587 longjmp (AuXtErrorJump, 1);
|
|
588
|
|
589 #endif /* XTEVENTS */
|
|
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
|
|
620 static char *
|
|
621 NameFromData (CONST unsigned char *buf,
|
|
622 int len)
|
|
623
|
|
624 {
|
|
625 unsigned char name[9];
|
|
626 int i;
|
|
627 char *s;
|
|
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 {
|
|
650 strcpy (s=malloc (10), name);
|
|
651 }
|
|
652 else
|
|
653 {
|
|
654 strcpy (s=malloc (15), "short sound");
|
|
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 *
|
|
665 SndOpenDataForReading (CONST char *data,
|
|
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
|
365
|
680 if (NAS_LITTLE_ENDIAN)
|
0
|
681 {
|
|
682 char n;
|
|
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 {
|
|
702 if (!(si->comment = (char *) malloc (size + 1)))
|
|
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 static Sound
|
|
726 SoundOpenDataForReading (unsigned char *data,
|
|
727 int length)
|
|
728
|
|
729 {
|
|
730 Sound s;
|
|
731
|
|
732 if (!(s = (Sound) malloc (sizeof (SoundRec))))
|
|
733 return NULL;
|
|
734
|
|
735 if ((s->formatInfo = SndOpenDataForReading (data, length))==NULL)
|
|
736 {
|
|
737 free (s);
|
|
738 return NULL;
|
|
739 }
|
|
740
|
|
741
|
|
742 if (!(SoundFileInfo[SoundFileFormatSnd].toSound) (s))
|
|
743 {
|
|
744 SndCloseFile (s->formatInfo);
|
|
745 free (s);
|
|
746 return NULL;
|
|
747 }
|
|
748
|
|
749 return s;
|
|
750 }
|
|
751
|