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.
60 #if __STDC__ || defined (STDC_HEADERS)
71 #include "syssignal.h"
75 #include <audio/audiolib.h>
76 #include <audio/soundlib.h>
77 #include <audio/snd.h>
78 #include <audio/wave.h>
79 #include <audio/fileutil.h>
89 * For old NAS libraries, force playing to be synchronous
90 * and declare the long jump point locally.
93 # if defined (NAS_NO_ERROR_JUMP)
98 jmp_buf AuXtErrorJump;
101 /* The GETTEXT is correct. --ben */
102 # define warn(str) warn_when_safe (Qnas, Qwarning, "nas: %s ", GETTEXT (str))
104 # define play_sound_file nas_play_sound_file
105 # define play_sound_data nas_play_sound_data
106 # define wait_for_sounds nas_wait_for_sounds
107 # define init_play nas_init_play
108 # define close_down_play nas_close_down_play
111 # define warn(str) fprintf (stderr, "%s\n", (str))
116 # include <X11/Intrinsic.h>
117 # include <audio/Xtutil.h>
120 #if defined (ROBUST_PLAY)
121 static AuBool CatchIoErrorAndJump (AuServer *aud);
122 static AuBool CatchErrorAndJump (AuServer *aud, AuErrorEvent *event);
123 SIGTYPE sigpipe_handle (int signo);
126 extern Lisp_Object Vsynchronous_sounds;
128 static Sound SoundOpenDataForReading (unsigned char *data, int length);
130 static AuServer *aud;
132 /* count of sounds currently being played. */
133 static int sounds_in_play;
137 static Display *aud_server;
138 static XtInputId input_id;
140 static char *aud_server;
141 #endif /* XTOOLKIT */
153 SIGTYPE (*old_sigpipe) ();
156 char * server = DisplayString (display);
157 XtAppContext app_context = XtDisplayToApplicationContext (display);
159 aud_server = display;
166 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
167 if (setjmp (AuXtErrorJump))
169 signal (SIGPIPE, old_sigpipe);
173 return "error in NAS";
177 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
178 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
179 AuDefaultErrorHandler = CatchErrorAndJump;
185 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
192 signal (SIGPIPE, old_sigpipe);
194 if (err_message == NULL)
195 return "Can't connect to audio server";
200 #if defined (ROBUST_PLAY)
201 # if defined (NAS_NO_ERROR_JUMP)
202 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
203 aud->funcs.error_handler = CatchErrorAndJump;
204 # else /* !NAS_NO_ERROR_JUMP */
205 AuDefaultIOErrorHandler = NULL;
206 AuDefaultErrorHandler = NULL;
211 input_id = AuXtAppAddAudioHandler (app_context, aud);
215 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
219 signal (SIGPIPE, old_sigpipe);
228 close_down_play (void)
232 warn ("disconnected from audio server");
235 /********************************************************************\
237 * Callback which is run when the sound finishes playing. *
239 \********************************************************************/
242 doneCB (AuServer *aud,
243 AuEventHandlerRec *handler,
247 int *in_play_p = (int *) data;
254 /********************************************************************\
256 * Play a sound by playing the relevant bucket, if any or *
257 * downloading it if not. *
259 \********************************************************************/
262 do_caching_play (Sound s,
267 AuBucketAttributes *list, b;
271 AuSetString (AuBucketDescription (&b),
272 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
274 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
278 unsigned char *my_buf;
282 if ((my_buf=malloc (SoundNumBytes (s)))==NULL)
287 if (SoundReadFile (my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
296 id = AuSoundCreateBucketFromData (aud,
305 else /* found cached sound */
307 id = AuBucketIdentifier (list);
308 AuFreeBucketAttributes (aud, n, list);
313 AuSoundPlayFromBucket (aud,
316 AuFixedPointFromFraction (volume, 100),
317 doneCB, (AuPointer) &sounds_in_play,
323 #endif /* CACHE_SOUNDS */
327 wait_for_sounds (void)
332 while (sounds_in_play>0)
334 AuNextEvent (aud, AuTrue, &ev);
335 AuDispatchEvent (aud, &ev);
340 play_sound_file (char *sound_file,
343 SIGTYPE (*old_sigpipe) ();
346 old_sigpipe=signal (SIGPIPE, sigpipe_handle);
347 if (setjmp (AuXtErrorJump))
349 signal (SIGPIPE, old_sigpipe);
355 if (aud_server != NULL)
358 /* attempt to reconect */
359 if ((m=init_play (aud_server))!= NULL)
363 signal (SIGPIPE, old_sigpipe);
370 warn ("Attempt to play with no audio init\n");
372 signal (SIGPIPE, old_sigpipe);
380 AuSoundPlayFromFile (aud,
383 AuFixedPointFromFraction (volume,100),
384 doneCB, (AuPointer) &sounds_in_play,
390 /* Cache the sounds in buckets on the server */
395 if ((s = SoundOpenFileForReading (sound_file))==NULL)
398 signal (SIGPIPE, old_sigpipe);
403 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
405 SoundComment (s) = FileCommentFromFilename (sound_file);
408 do_caching_play (s, volume, NULL);
413 #endif /* CACHE_SOUNDS */
418 if (!NILP (Vsynchronous_sounds))
425 signal (SIGPIPE, old_sigpipe);
432 play_sound_data (unsigned char *data,
438 SIGTYPE (*old_sigpipe) ();
440 #if !defined (XTEVENTS)
445 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
446 if (setjmp (AuXtErrorJump) !=0)
448 signal (SIGPIPE, old_sigpipe);
455 if (aud_server != NULL)
458 /* attempt to reconect */
459 if ((m = init_play (aud_server)) != NULL)
462 signal (SIGPIPE, old_sigpipe);
469 warn ("Attempt to play with no audio init\n");
471 signal (SIGPIPE, old_sigpipe);
477 if ((s=SoundOpenDataForReading (data, length))==NULL)
479 warn ("unknown sound type");
481 signal (SIGPIPE, old_sigpipe);
486 if (SoundFileFormat (s) == SoundFileFormatSnd)
489 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
491 else if (SoundFileFormat (s) == SoundFileFormatWave)
493 offset = ((WaveInfo *) (s->formatInfo))->dataOffset;
497 warn ("only understand snd and wave files at the moment");
500 signal (SIGPIPE, old_sigpipe);
507 AuSoundPlayFromData (aud,
511 AuFixedPointFromFraction (volume,100),
512 doneCB, (AuPointer) &sounds_in_play,
518 /* Cache the sounds in buckets on the server */
521 do_caching_play (s, volume, data+offset);
523 #endif /* CACHE_SOUNDS */
529 if (!NILP (Vsynchronous_sounds))
538 signal (SIGPIPE, old_sigpipe);
544 #if defined (ROBUST_PLAY)
546 /********************************************************************\
548 * Code to protect the client from server shutdowns. *
550 * This is unbelievably horrible. *
552 \********************************************************************/
555 CatchIoErrorAndJump (AuServer *old_aud)
558 warn ("Audio Server connection broken");
560 warn ("Audio Server connection broken because of signal");
565 AuXtAppRemoveAudioHandler (aud, input_id);
574 longjmp (AuXtErrorJump, 1);
576 #else /* not XTEVENTS */
582 longjmp (AuXtErrorJump, 1);
584 #endif /* XTEVENTS */
589 sigpipe_handle (int signo)
591 CatchIoErrorAndJump (NULL);
595 CatchErrorAndJump (AuServer *old_aud,
598 return CatchIoErrorAndJump (old_aud);
601 #endif /* ROBUST_PLAY */
603 /********************************************************************\
605 * This code is here because the nas Sound library doesn't *
606 * support playing from a file buffered in memory. It's a fairly *
607 * direct translation of the file-based equivalent. *
609 * Since we don't have a filename, samples with no comment field *
610 * are named by a section of their content. *
612 \********************************************************************/
614 /* Create a name from the sound. */
617 NameFromData (CONST unsigned char *buf,
621 unsigned char name[9];
629 while (i<8 && len >0)
631 while (*buf < 32 && len>0)
646 strcpy (s=malloc (10), name);
650 strcpy (s=malloc (15), "short sound");
656 /* Code to do a pseudo-open on a data buffer. Only for snd files at the
661 SndOpenDataForReading (CONST char *data,
668 if (!(si = (SndInfo *) malloc (sizeof (SndInfo))))
674 memcpy (&si->h, data, sizeof (SndHeader));
680 swapl (&si->h.magic, n);
681 swapl (&si->h.dataOffset, n);
682 swapl (&si->h.dataSize, n);
683 swapl (&si->h.format, n);
684 swapl (&si->h.sampleRate, n);
685 swapl (&si->h.tracks, n);
688 if (si->h.magic != SND_MAGIC_NUM)
694 size = si->h.dataOffset - sizeof (SndHeader);
698 if (!(si->comment = (char *) malloc (size + 1)))
704 memcpy (si->comment, data+sizeof (SndHeader), size);
706 *(si->comment + size) = 0;
707 if (*si->comment == '\0')
709 NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
712 si->comment = NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
714 si->h.dataSize = length-si->h.dataOffset;
721 /* Stuff taken from wave.c from NAS. Just like snd files, NAS can't
722 read wave data from memory, so these functions do that for us. */
724 #define Err() { return NULL; }
725 #define readFourcc(_f) dread(_f, sizeof(RIFF_FOURCC), 1)
726 #define cmpID(_x, _y) \
727 strncmp((char *) (_x), (char *) (_y), sizeof(RIFF_FOURCC))
728 #define PAD2(_x) (((_x) + 1) & ~1)
730 /* These functions here are for faking file I/O from buffer. */
732 /* The "file" position */
733 static int file_posn;
734 /* The length of the "file" */
736 /* The actual "file" data. */
737 CONST static char* file_data;
739 /* Like fopen, but for a buffer in memory */
741 dopen(CONST char* data, int length)
748 /* Like fread, but for a buffer in memory */
750 dread(char* buf, int size, int nitems)
754 nread = size * nitems;
756 if (file_posn + nread <= file_len)
758 memcpy(buf, file_data + file_posn, size * nitems);
768 /* Like fgetc, but for a buffer in memory */
774 if (file_posn < file_len)
775 return file_data[file_posn++];
780 /* Like fseek, but for a buffer in memory */
782 dseek(long offset, int from)
789 file_posn = file_len + offset;
794 /* Like ftell, but for a buffer in memory */
801 /* Data buffer analogs for FileReadS and FileReadL in NAS. */
803 static unsigned short
804 DataReadS(int swapit)
815 DataReadL(int swapit)
826 readChunk(RiffChunk *c)
831 if ((status = dread(c, sizeof(RiffChunk), 1)))
833 swapl(&c->ckSize, n);
838 /* A very straight-forward translation of WaveOpenFileForReading to
839 read the wave data from a buffer in memory. */
842 WaveOpenDataForReading(CONST char *data,
851 if (!(wi = (WaveInfo *) malloc(sizeof(WaveInfo))))
855 wi->dataOffset = wi->format = wi->writing = 0;
859 if (!readChunk(&ck) ||
860 cmpID(&ck.ckID, RIFF_RiffID) ||
861 !readFourcc(&fourcc) ||
862 cmpID(&fourcc, RIFF_WaveID))
865 fileSize = PAD2(ck.ckSize) - sizeof(RIFF_FOURCC);
867 while (fileSize >= sizeof(RiffChunk))
872 fileSize -= sizeof(RiffChunk) + PAD2(ck.ckSize);
875 if (!cmpID(&ck.ckID, RIFF_ListID))
877 if (!readFourcc(&fourcc))
881 if (!cmpID(&fourcc, RIFF_ListInfoID))
883 ck.ckSize -= sizeof(RIFF_FOURCC);
893 if (!cmpID(&c.ckID, RIFF_InfoIcmtID))
895 if (!(wi->comment = (char *) malloc(c.ckSize)) ||
896 !dread(wi->comment, c.ckSize, 1))
900 dgetc(); /* eat the pad byte */
903 /* skip unknown chunk */
904 dseek(PAD2(c.ckSize), 1);
906 ck.ckSize -= sizeof(RiffChunk) + PAD2(c.ckSize);
910 /* skip unknown chunk */
911 dseek(PAD2(ck.ckSize) - sizeof(RIFF_FOURCC), 1);
913 /* wave format chunk */
914 else if (!cmpID(&ck.ckID, RIFF_WaveFmtID) && !wi->format)
918 wi->format = DataReadS(BIG_ENDIAN);
919 wi->channels = DataReadS(BIG_ENDIAN);
920 wi->sampleRate = DataReadL(BIG_ENDIAN);
922 /* we don't care about the next two fields */
923 dummy = DataReadL(BIG_ENDIAN);
924 dummy = DataReadS(BIG_ENDIAN);
926 if (wi->format != RIFF_WAVE_FORMAT_PCM)
929 wi->bitsPerSample = DataReadS(BIG_ENDIAN);
931 /* skip any other format specific fields */
932 dseek(PAD2(ck.ckSize - 16), 1);
934 /* wave data chunk */
935 else if (!cmpID(&ck.ckID, RIFF_WaveDataID) && !wi->dataOffset)
939 wi->dataOffset = dtell();
940 wi->dataSize = ck.ckSize;
944 /* seek past the data */
945 if (dseek(wi->dataOffset + PAD2(ck.ckSize), 0) ||
948 /* the seek failed, assume the size is bogus */
950 wi->dataSize = dtell() - wi->dataOffset;
953 wi->dataOffset -= sizeof(long);
956 /* skip unknown chunk */
957 dseek(PAD2(ck.ckSize), 1);
963 wi->numSamples = wi->dataSize / wi->channels / (wi->bitsPerSample >> 3);
966 wi->comment = NameFromData (data + wi->dataOffset,
967 length - wi->dataOffset);
976 SoundOpenDataForReading (unsigned char *data,
982 if (!(s = (Sound) malloc (sizeof (SoundRec))))
985 if ((s->formatInfo = SndOpenDataForReading (data, length)) != NULL)
987 if (!(SoundFileInfo[SoundFileFormatSnd].toSound) (s))
989 SndCloseFile (s->formatInfo);
994 else if ((s->formatInfo = WaveOpenDataForReading (data, length)) != NULL)
996 if (!(SoundFileInfo[SoundFileFormatWave].toSound) (s))
998 WaveCloseFile (s->formatInfo);