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))
115 # include <X11/Intrinsic.h>
116 # include <audio/Xtutil.h>
119 #if defined (ROBUST_PLAY)
120 static AuBool CatchIoErrorAndJump (AuServer *aud);
121 static AuBool CatchErrorAndJump (AuServer *aud, AuErrorEvent *event);
122 SIGTYPE sigpipe_handle (int signo);
125 extern Lisp_Object Vsynchronous_sounds;
127 static Sound SoundOpenDataForReading (unsigned char *data, int length);
129 static AuServer *aud;
131 /* count of sounds currently being played. */
132 static int sounds_in_play;
136 static Display *aud_server;
137 static XtInputId input_id;
139 static char *aud_server;
140 #endif /* XTOOLKIT */
152 SIGTYPE (*old_sigpipe) ();
155 char * server = DisplayString (display);
156 XtAppContext app_context = XtDisplayToApplicationContext (display);
158 aud_server = display;
165 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
166 if (setjmp (AuXtErrorJump))
168 signal (SIGPIPE, old_sigpipe);
172 return "error in NAS";
176 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
177 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
178 AuDefaultErrorHandler = CatchErrorAndJump;
184 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
191 signal (SIGPIPE, old_sigpipe);
193 if (err_message == NULL)
194 return "Can't connect to audio server";
199 #if defined (ROBUST_PLAY)
200 # if defined (NAS_NO_ERROR_JUMP)
201 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
202 aud->funcs.error_handler = CatchErrorAndJump;
203 # else /* !NAS_NO_ERROR_JUMP */
204 AuDefaultIOErrorHandler = NULL;
205 AuDefaultErrorHandler = NULL;
210 input_id = AuXtAppAddAudioHandler (app_context, aud);
214 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
218 signal (SIGPIPE, old_sigpipe);
227 close_down_play (void)
231 warn ("disconnected from audio server");
234 /********************************************************************\
236 * Callback which is run when the sound finishes playing. *
238 \********************************************************************/
241 doneCB (AuServer *aud,
242 AuEventHandlerRec *handler,
246 int *in_play_p = (int *) data;
253 /********************************************************************\
255 * Play a sound by playing the relevant bucket, if any or *
256 * downloading it if not. *
258 \********************************************************************/
261 do_caching_play (Sound s,
266 AuBucketAttributes *list, b;
270 AuSetString (AuBucketDescription (&b),
271 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
273 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
277 unsigned char *my_buf;
281 if ((my_buf=malloc (SoundNumBytes (s)))==NULL)
286 if (SoundReadFile (my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
295 id = AuSoundCreateBucketFromData (aud,
304 else /* found cached sound */
306 id = AuBucketIdentifier (list);
307 AuFreeBucketAttributes (aud, n, list);
312 AuSoundPlayFromBucket (aud,
315 AuFixedPointFromFraction (volume, 100),
316 doneCB, (AuPointer) &sounds_in_play,
322 #endif /* CACHE_SOUNDS */
326 wait_for_sounds (void)
331 while (sounds_in_play>0)
333 AuNextEvent (aud, AuTrue, &ev);
334 AuDispatchEvent (aud, &ev);
339 play_sound_file (char *sound_file,
342 SIGTYPE (*old_sigpipe) ();
345 old_sigpipe=signal (SIGPIPE, sigpipe_handle);
346 if (setjmp (AuXtErrorJump))
348 signal (SIGPIPE, old_sigpipe);
354 if (aud_server != NULL)
357 /* attempt to reconect */
358 if ((m=init_play (aud_server))!= NULL)
362 signal (SIGPIPE, old_sigpipe);
369 warn ("Attempt to play with no audio init\n");
371 signal (SIGPIPE, old_sigpipe);
379 AuSoundPlayFromFile (aud,
382 AuFixedPointFromFraction (volume,100),
383 doneCB, (AuPointer) &sounds_in_play,
389 /* Cache the sounds in buckets on the server */
394 if ((s = SoundOpenFileForReading (sound_file))==NULL)
397 signal (SIGPIPE, old_sigpipe);
402 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
404 SoundComment (s) = FileCommentFromFilename (sound_file);
407 do_caching_play (s, volume, NULL);
412 #endif /* CACHE_SOUNDS */
417 if (!NILP (Vsynchronous_sounds))
424 signal (SIGPIPE, old_sigpipe);
431 play_sound_data (unsigned char *data,
437 SIGTYPE (*old_sigpipe) ();
439 #if !defined (XTEVENTS)
444 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
445 if (setjmp (AuXtErrorJump) !=0)
447 signal (SIGPIPE, old_sigpipe);
454 if (aud_server != NULL)
457 /* attempt to reconect */
458 if ((m = init_play (aud_server)) != NULL)
461 signal (SIGPIPE, old_sigpipe);
468 warn ("Attempt to play with no audio init\n");
470 signal (SIGPIPE, old_sigpipe);
476 if ((s=SoundOpenDataForReading (data, length))==NULL)
478 warn ("unknown sound type");
480 signal (SIGPIPE, old_sigpipe);
485 if (SoundFileFormat (s) == SoundFileFormatSnd)
488 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
490 else if (SoundFileFormat (s) == SoundFileFormatWave)
492 offset = ((WaveInfo *) (s->formatInfo))->dataOffset;
496 warn ("only understand snd and wave files at the moment");
499 signal (SIGPIPE, old_sigpipe);
506 AuSoundPlayFromData (aud,
510 AuFixedPointFromFraction (volume,100),
511 doneCB, (AuPointer) &sounds_in_play,
517 /* Cache the sounds in buckets on the server */
520 do_caching_play (s, volume, data+offset);
522 #endif /* CACHE_SOUNDS */
528 if (!NILP (Vsynchronous_sounds))
537 signal (SIGPIPE, old_sigpipe);
543 #if defined (ROBUST_PLAY)
545 /********************************************************************\
547 * Code to protect the client from server shutdowns. *
549 * This is unbelievably horrible. *
551 \********************************************************************/
554 CatchIoErrorAndJump (AuServer *old_aud)
557 warn ("Audio Server connection broken");
559 warn ("Audio Server connection broken because of signal");
564 AuXtAppRemoveAudioHandler (aud, input_id);
573 longjmp (AuXtErrorJump, 1);
575 #else /* not XTEVENTS */
581 longjmp (AuXtErrorJump, 1);
583 #endif /* XTEVENTS */
588 sigpipe_handle (int signo)
590 CatchIoErrorAndJump (NULL);
594 CatchErrorAndJump (AuServer *old_aud,
597 return CatchIoErrorAndJump (old_aud);
600 #endif /* ROBUST_PLAY */
602 /********************************************************************\
604 * This code is here because the nas Sound library doesn't *
605 * support playing from a file buffered in memory. It's a fairly *
606 * direct translation of the file-based equivalent. *
608 * Since we don't have a filename, samples with no comment field *
609 * are named by a section of their content. *
611 \********************************************************************/
613 /* Create a name from the sound. */
616 NameFromData (CONST unsigned char *buf,
620 unsigned char name[9];
628 while (i<8 && len >0)
630 while (*buf < 32 && len>0)
645 strcpy (s=malloc (10), name);
649 strcpy (s=malloc (15), "short sound");
655 /* Code to do a pseudo-open on a data buffer. Only for snd files at the
660 SndOpenDataForReading (CONST char *data,
667 if (!(si = (SndInfo *) malloc (sizeof (SndInfo))))
673 memcpy (&si->h, data, sizeof (SndHeader));
679 swapl (&si->h.magic, n);
680 swapl (&si->h.dataOffset, n);
681 swapl (&si->h.dataSize, n);
682 swapl (&si->h.format, n);
683 swapl (&si->h.sampleRate, n);
684 swapl (&si->h.tracks, n);
687 if (si->h.magic != SND_MAGIC_NUM)
693 size = si->h.dataOffset - sizeof (SndHeader);
697 if (!(si->comment = (char *) malloc (size + 1)))
703 memcpy (si->comment, data+sizeof (SndHeader), size);
705 *(si->comment + size) = 0;
706 if (*si->comment == '\0')
708 NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
711 si->comment = NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
713 si->h.dataSize = length-si->h.dataOffset;
720 /* Stuff taken from wave.c from NAS. Just like snd files, NAS can't
721 read wave data from memory, so these functions do that for us. */
723 #define Err() { return NULL; }
724 #define readFourcc(_f) dread(_f, sizeof(RIFF_FOURCC), 1)
725 #define cmpID(_x, _y) \
726 strncmp((char *) (_x), (char *) (_y), sizeof(RIFF_FOURCC))
727 #define PAD2(_x) (((_x) + 1) & ~1)
729 /* These functions here are for faking file I/O from buffer. */
731 /* The "file" position */
732 static int file_posn;
733 /* The length of the "file" */
735 /* The actual "file" data. */
736 CONST static char* file_data;
738 /* Like fopen, but for a buffer in memory */
740 dopen(CONST char* data, int length)
747 /* Like fread, but for a buffer in memory */
749 dread(char* buf, int size, int nitems)
753 nread = size * nitems;
755 if (file_posn + nread <= file_len)
757 memcpy(buf, file_data + file_posn, size * nitems);
767 /* Like fgetc, but for a buffer in memory */
773 if (file_posn < file_len)
774 return file_data[file_posn++];
779 /* Like fseek, but for a buffer in memory */
781 dseek(long offset, int from)
788 file_posn = file_len + offset;
793 /* Like ftell, but for a buffer in memory */
800 /* Data buffer analogs for FileReadS and FileReadL in NAS. */
802 static unsigned short
803 DataReadS(int swapit)
814 DataReadL(int swapit)
825 readChunk(RiffChunk *c)
830 if ((status = dread(c, sizeof(RiffChunk), 1)))
832 swapl(&c->ckSize, n);
837 /* A very straight-forward translation of WaveOpenFileForReading to
838 read the wave data from a buffer in memory. */
841 WaveOpenDataForReading(CONST char *data,
850 if (!(wi = (WaveInfo *) malloc(sizeof(WaveInfo))))
854 wi->dataOffset = wi->format = wi->writing = 0;
858 if (!readChunk(&ck) ||
859 cmpID(&ck.ckID, RIFF_RiffID) ||
860 !readFourcc(&fourcc) ||
861 cmpID(&fourcc, RIFF_WaveID))
864 fileSize = PAD2(ck.ckSize) - sizeof(RIFF_FOURCC);
866 while (fileSize >= sizeof(RiffChunk))
871 fileSize -= sizeof(RiffChunk) + PAD2(ck.ckSize);
874 if (!cmpID(&ck.ckID, RIFF_ListID))
876 if (!readFourcc(&fourcc))
880 if (!cmpID(&fourcc, RIFF_ListInfoID))
882 ck.ckSize -= sizeof(RIFF_FOURCC);
892 if (!cmpID(&c.ckID, RIFF_InfoIcmtID))
894 if (!(wi->comment = (char *) malloc(c.ckSize)) ||
895 !dread(wi->comment, c.ckSize, 1))
899 dgetc(); /* eat the pad byte */
902 /* skip unknown chunk */
903 dseek(PAD2(c.ckSize), 1);
905 ck.ckSize -= sizeof(RiffChunk) + PAD2(c.ckSize);
909 /* skip unknown chunk */
910 dseek(PAD2(ck.ckSize) - sizeof(RIFF_FOURCC), 1);
912 /* wave format chunk */
913 else if (!cmpID(&ck.ckID, RIFF_WaveFmtID) && !wi->format)
917 wi->format = DataReadS(BIG_ENDIAN);
918 wi->channels = DataReadS(BIG_ENDIAN);
919 wi->sampleRate = DataReadL(BIG_ENDIAN);
921 /* we don't care about the next two fields */
922 dummy = DataReadL(BIG_ENDIAN);
923 dummy = DataReadS(BIG_ENDIAN);
925 if (wi->format != RIFF_WAVE_FORMAT_PCM)
928 wi->bitsPerSample = DataReadS(BIG_ENDIAN);
930 /* skip any other format specific fields */
931 dseek(PAD2(ck.ckSize - 16), 1);
933 /* wave data chunk */
934 else if (!cmpID(&ck.ckID, RIFF_WaveDataID) && !wi->dataOffset)
938 wi->dataOffset = dtell();
939 wi->dataSize = ck.ckSize;
943 /* seek past the data */
944 if (dseek(wi->dataOffset + PAD2(ck.ckSize), 0) ||
947 /* the seek failed, assume the size is bogus */
949 wi->dataSize = dtell() - wi->dataOffset;
952 wi->dataOffset -= sizeof(long);
955 /* skip unknown chunk */
956 dseek(PAD2(ck.ckSize), 1);
962 wi->numSamples = wi->dataSize / wi->channels / (wi->bitsPerSample >> 3);
965 wi->comment = NameFromData (data + wi->dataOffset,
966 length - wi->dataOffset);
975 SoundOpenDataForReading (unsigned char *data,
981 if (!(s = (Sound) malloc (sizeof (SoundRec))))
984 if ((s->formatInfo = SndOpenDataForReading (data, length)) != NULL)
986 if (!(SoundFileInfo[SoundFileFormatSnd].toSound) (s))
988 SndCloseFile (s->formatInfo);
993 else if ((s->formatInfo = WaveOpenDataForReading (data, length)) != NULL)
995 if (!(SoundFileInfo[SoundFileFormatWave].toSound) (s))
997 WaveCloseFile (s->formatInfo);