1 /* nas.c --- XEmacs support for the Network Audio System server.
3 * Author: Richard Caley <R.Caley@ed.ac.uk>
5 * Copyright 1994 Free Software Foundation, Inc.
6 * Copyright 1993 Network Computing Devices, Inc.
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.
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.
27 /* Synched up with: Not in FSF. */
29 /* There are four compile-time options.
31 * XTOOLKIT This will be part of an Xt program.
33 * XTEVENTS The playing will be supervised asynchronously by the Xt event
34 * loop. If not set, playing will be completed within the call
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.
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.
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 * 1998-10-01 rlt Added support for WAVE files.
59 #include "syssignal.h"
74 #include <audio/audiolib.h>
75 #include <audio/soundlib.h>
76 #include <audio/snd.h>
77 #include <audio/wave.h>
78 #include <audio/fileutil.h>
88 * For old NAS libraries, force playing to be synchronous
89 * and declare the long jump point locally.
92 # if defined (NAS_NO_ERROR_JUMP)
97 jmp_buf AuXtErrorJump;
100 /* The GETTEXT is correct. --ben */
101 # define warn(str) warn_when_safe (Qnas, Qwarning, "nas: %s ", GETTEXT (str))
103 # define play_sound_file nas_play_sound_file
104 # define play_sound_data nas_play_sound_data
105 # define wait_for_sounds nas_wait_for_sounds
106 # define init_play nas_init_play
107 # define close_down_play nas_close_down_play
110 # define warn(str) fprintf (stderr, "%s\n", (str))
114 # include <X11/Intrinsic.h>
115 # include <audio/Xtutil.h>
118 #if defined (ROBUST_PLAY)
119 static AuBool CatchIoErrorAndJump (AuServer *aud);
120 static AuBool CatchErrorAndJump (AuServer *aud, AuErrorEvent *event);
121 SIGTYPE sigpipe_handle (int signo);
124 extern Lisp_Object Vsynchronous_sounds;
126 static Sound SoundOpenDataForReading (unsigned char *data, int length);
128 static AuServer *aud;
130 /* count of sounds currently being played. */
131 static int sounds_in_play;
135 static Display *aud_server;
136 static XtInputId input_id;
138 static char *aud_server;
139 #endif /* XTOOLKIT */
159 SIGTYPE (*old_sigpipe) (int);
162 char * server = DisplayString (display);
163 XtAppContext app_context = XtDisplayToApplicationContext (display);
165 aud_server = display;
172 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
173 if (setjmp (AuXtErrorJump))
175 signal (SIGPIPE, old_sigpipe);
179 return "error in NAS";
183 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
184 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
185 AuDefaultErrorHandler = CatchErrorAndJump;
191 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
198 signal (SIGPIPE, old_sigpipe);
200 if (err_message == NULL)
201 return "Can't connect to audio server";
206 #if defined (ROBUST_PLAY)
207 # if defined (NAS_NO_ERROR_JUMP)
208 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
209 aud->funcs.error_handler = CatchErrorAndJump;
210 # else /* !NAS_NO_ERROR_JUMP */
211 AuDefaultIOErrorHandler = NULL;
212 AuDefaultErrorHandler = NULL;
217 input_id = AuXtAppAddAudioHandler (app_context, aud);
221 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
225 signal (SIGPIPE, old_sigpipe);
234 close_down_play (void)
238 warn ("disconnected from audio server");
241 /********************************************************************\
243 * Callback which is run when the sound finishes playing. *
245 \********************************************************************/
248 doneCB (AuServer *auserver,
249 AuEventHandlerRec *handler,
253 int *in_play_p = (int *) data;
260 /********************************************************************\
262 * Play a sound by playing the relevant bucket, if any or *
263 * downloading it if not. *
265 \********************************************************************/
268 do_caching_play (Sound s,
273 AuBucketAttributes *list, b;
277 AuSetString (AuBucketDescription (&b),
278 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
280 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
288 if ((my_buf= (AuPointer) malloc (SoundNumBytes (s)))==NULL)
293 if (SoundReadFile ((char *) my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
300 my_buf = (AuPointer) buf;
302 id = AuSoundCreateBucketFromData (aud,
311 else /* found cached sound */
313 id = AuBucketIdentifier (list);
314 AuFreeBucketAttributes (aud, n, list);
319 AuSoundPlayFromBucket (aud,
322 AuFixedPointFromFraction (volume, 100),
323 doneCB, (AuPointer) &sounds_in_play,
329 #endif /* CACHE_SOUNDS */
332 void wait_for_sounds (void);
334 wait_for_sounds (void)
339 while (sounds_in_play>0)
341 AuNextEvent (aud, AuTrue, &ev);
342 AuDispatchEvent (aud, &ev);
346 int play_sound_file (char *sound_file, int volume);
348 play_sound_file (char *sound_file,
351 SIGTYPE (*old_sigpipe) (int);
354 old_sigpipe=signal (SIGPIPE, sigpipe_handle);
355 if (setjmp (AuXtErrorJump))
357 signal (SIGPIPE, old_sigpipe);
363 if (aud_server != NULL)
366 /* attempt to reconect */
367 if ((m=init_play (aud_server))!= NULL)
371 signal (SIGPIPE, old_sigpipe);
378 warn ("Attempt to play with no audio init\n");
380 signal (SIGPIPE, old_sigpipe);
388 AuSoundPlayFromFile (aud,
391 AuFixedPointFromFraction (volume,100),
392 doneCB, (AuPointer) &sounds_in_play,
398 /* Cache the sounds in buckets on the server */
403 if ((s = SoundOpenFileForReading (sound_file))==NULL)
406 signal (SIGPIPE, old_sigpipe);
411 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
413 SoundComment (s) = FileCommentFromFilename (sound_file);
416 do_caching_play (s, volume, NULL);
421 #endif /* CACHE_SOUNDS */
426 if (!NILP (Vsynchronous_sounds))
433 signal (SIGPIPE, old_sigpipe);
439 int play_sound_data (unsigned char *data, int length, int volume);
441 play_sound_data (unsigned char *data,
447 SIGTYPE (*old_sigpipe) (int);
449 #if !defined (XTEVENTS)
454 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
455 if (setjmp (AuXtErrorJump) !=0)
457 signal (SIGPIPE, old_sigpipe);
464 if (aud_server != NULL)
467 /* attempt to reconect */
468 if ((m = init_play (aud_server)) != NULL)
471 signal (SIGPIPE, old_sigpipe);
478 warn ("Attempt to play with no audio init\n");
480 signal (SIGPIPE, old_sigpipe);
486 if ((s=SoundOpenDataForReading (data, length))==NULL)
488 warn ("unknown sound type");
490 signal (SIGPIPE, old_sigpipe);
495 if (SoundFileFormat (s) == SoundFileFormatSnd)
498 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
500 else if (SoundFileFormat (s) == SoundFileFormatWave)
502 offset = ((WaveInfo *) (s->formatInfo))->dataOffset;
506 warn ("only understand snd and wave files at the moment");
509 signal (SIGPIPE, old_sigpipe);
516 AuSoundPlayFromData (aud,
520 AuFixedPointFromFraction (volume,100),
521 doneCB, (AuPointer) &sounds_in_play,
527 /* Cache the sounds in buckets on the server */
530 do_caching_play (s, volume, data+offset);
532 #endif /* CACHE_SOUNDS */
538 if (!NILP (Vsynchronous_sounds))
547 signal (SIGPIPE, old_sigpipe);
553 #if defined (ROBUST_PLAY)
555 /********************************************************************\
557 * Code to protect the client from server shutdowns. *
559 * This is unbelievably horrible. *
561 \********************************************************************/
564 CatchIoErrorAndJump (AuServer *old_aud)
567 warn ("Audio Server connection broken");
569 warn ("Audio Server connection broken because of signal");
574 AuXtAppRemoveAudioHandler (aud, input_id);
583 longjmp (AuXtErrorJump, 1);
585 #else /* not XTEVENTS */
591 longjmp (AuXtErrorJump, 1);
593 #endif /* XTEVENTS */
598 sigpipe_handle (int signo)
600 CatchIoErrorAndJump (NULL);
604 CatchErrorAndJump (AuServer *old_aud,
607 return CatchIoErrorAndJump (old_aud);
610 #endif /* ROBUST_PLAY */
612 /********************************************************************\
614 * This code is here because the nas Sound library doesn't *
615 * support playing from a file buffered in memory. It's a fairly *
616 * direct translation of the file-based equivalent. *
618 * Since we don't have a filename, samples with no comment field *
619 * are named by a section of their content. *
621 \********************************************************************/
623 /* Create a name from the sound. */
626 NameFromData (const char *buf,
638 while (i<8 && len >0)
640 while (*buf < 32 && len>0)
655 strcpy (s = (char *) malloc (10), name);
659 strcpy (s = (char *) malloc (15), "short sound");
665 /* Code to do a pseudo-open on a data buffer. Only for snd files at the
670 SndOpenDataForReading (const char *data,
677 if (!(si = (SndInfo *) malloc (sizeof (SndInfo))))
683 memcpy (&si->h, data, sizeof (SndHeader));
689 swapl (&si->h.magic, n);
690 swapl (&si->h.dataOffset, n);
691 swapl (&si->h.dataSize, n);
692 swapl (&si->h.format, n);
693 swapl (&si->h.sampleRate, n);
694 swapl (&si->h.tracks, n);
697 if (si->h.magic != SND_MAGIC_NUM)
703 size = si->h.dataOffset - sizeof (SndHeader);
707 if (!(si->comment = (char *) malloc (size + 1)))
713 memcpy (si->comment, data+sizeof (SndHeader), size);
715 *(si->comment + size) = 0;
716 if (*si->comment == '\0')
718 NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
721 si->comment = NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
723 si->h.dataSize = length-si->h.dataOffset;
730 /* Stuff taken from wave.c from NAS. Just like snd files, NAS can't
731 read wave data from memory, so these functions do that for us. */
733 #define Err() { return NULL; }
734 #define readFourcc(_f) dread(_f, sizeof(RIFF_FOURCC), 1)
735 #define cmpID(_x, _y) \
736 strncmp((char *) (_x), (char *) (_y), sizeof(RIFF_FOURCC))
737 #define PAD2(_x) (((_x) + 1) & ~1)
739 /* These functions here are for faking file I/O from buffer. */
741 /* The "file" position */
742 static size_t file_posn;
743 /* The length of the "file" */
744 static size_t file_len;
745 /* The actual "file" data. */
746 static const void* file_data;
748 /* Like fopen, but for a buffer in memory */
750 dopen (const void* data, size_t length)
757 /* Like fread, but for a buffer in memory */
759 dread (void* buf, size_t size, size_t nitems)
761 size_t nread = size * nitems;
763 if (file_posn + nread <= file_len)
765 memcpy(buf, (char *) file_data + file_posn, size * nitems);
775 /* Like fgetc, but for a buffer in memory */
779 if (file_posn < file_len)
780 return ((char *)file_data)[file_posn++];
785 /* Like fseek, but for a buffer in memory */
787 dseek (long offset, int from)
794 file_posn = file_len + offset;
799 /* Like ftell, but for a buffer in memory */
806 /* Data buffer analogs for FileReadS and FileReadL in NAS. */
808 static unsigned short
809 DataReadS (int swapit)
820 DataReadL (int swapit)
831 readChunk (RiffChunk *c)
836 if ((status = dread(c, sizeof(RiffChunk), 1)))
838 swapl(&c->ckSize, n);
843 /* A very straight-forward translation of WaveOpenFileForReading to
844 read the wave data from a buffer in memory. */
847 WaveOpenDataForReading (const char *data,
856 if (!(wi = (WaveInfo *) malloc(sizeof(WaveInfo))))
860 wi->dataOffset = wi->format = wi->writing = 0;
864 if (!readChunk(&ck) ||
865 cmpID(&ck.ckID, RIFF_RiffID) ||
866 !readFourcc(&fourcc) ||
867 cmpID(&fourcc, RIFF_WaveID))
870 fileSize = PAD2(ck.ckSize) - sizeof(RIFF_FOURCC);
872 while (fileSize >= sizeof(RiffChunk))
877 fileSize -= sizeof(RiffChunk) + PAD2(ck.ckSize);
880 if (!cmpID(&ck.ckID, RIFF_ListID))
882 if (!readFourcc(&fourcc))
886 if (!cmpID(&fourcc, RIFF_ListInfoID))
888 ck.ckSize -= sizeof(RIFF_FOURCC);
898 if (!cmpID(&c.ckID, RIFF_InfoIcmtID))
900 if (!(wi->comment = (char *) malloc(c.ckSize)) ||
901 !dread(wi->comment, c.ckSize, 1))
905 dgetc(); /* eat the pad byte */
908 /* skip unknown chunk */
909 dseek(PAD2(c.ckSize), 1);
911 ck.ckSize -= sizeof(RiffChunk) + PAD2(c.ckSize);
915 /* skip unknown chunk */
916 dseek(PAD2(ck.ckSize) - sizeof(RIFF_FOURCC), 1);
918 /* wave format chunk */
919 else if (!cmpID(&ck.ckID, RIFF_WaveFmtID) && !wi->format)
923 wi->format = DataReadS(BIG_ENDIAN);
924 wi->channels = DataReadS(BIG_ENDIAN);
925 wi->sampleRate = DataReadL(BIG_ENDIAN);
927 /* we don't care about the next two fields */
928 dummy = DataReadL(BIG_ENDIAN);
929 dummy = DataReadS(BIG_ENDIAN);
931 if (wi->format != RIFF_WAVE_FORMAT_PCM)
934 wi->bitsPerSample = DataReadS(BIG_ENDIAN);
936 /* skip any other format specific fields */
937 dseek(PAD2(ck.ckSize - 16), 1);
939 /* wave data chunk */
940 else if (!cmpID(&ck.ckID, RIFF_WaveDataID) && !wi->dataOffset)
944 wi->dataOffset = dtell();
945 wi->dataSize = ck.ckSize;
949 /* seek past the data */
950 if (dseek(wi->dataOffset + PAD2(ck.ckSize), 0) ||
953 /* the seek failed, assume the size is bogus */
955 wi->dataSize = dtell() - wi->dataOffset;
958 wi->dataOffset -= sizeof(long);
961 /* skip unknown chunk */
962 dseek(PAD2(ck.ckSize), 1);
968 wi->numSamples = wi->dataSize / wi->channels / (wi->bitsPerSample >> 3);
971 wi->comment = NameFromData (data + wi->dataOffset,
972 length - wi->dataOffset);
981 SoundOpenDataForReading (unsigned char *data,
987 if (!(s = (Sound) malloc (sizeof (SoundRec))))
990 if ((s->formatInfo = SndOpenDataForReading ((char *) data, length)) != NULL)
992 if (!((int(*)(Sound))(SoundFileInfo[SoundFileFormatSnd].toSound)) (s))
994 SndCloseFile ((SndInfo *) (s->formatInfo));
999 else if ((s->formatInfo = WaveOpenDataForReading ((char *) data, length)) != NULL)
1001 if (!((int(*)(Sound))(SoundFileInfo[SoundFileFormatWave].toSound)) (s))
1003 WaveCloseFile ((WaveInfo *) (s->formatInfo));