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 */
160 SIGTYPE (*old_sigpipe) (int);
163 char * server = DisplayString (display);
164 XtAppContext app_context = XtDisplayToApplicationContext (display);
166 aud_server = display;
173 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
174 if (setjmp (AuXtErrorJump))
176 signal (SIGPIPE, old_sigpipe);
180 return "error in NAS";
184 #if defined (ROBUST_PLAY) && !defined (NAS_NO_ERROR_JUMP)
185 AuDefaultIOErrorHandler = CatchIoErrorAndJump;
186 AuDefaultErrorHandler = CatchErrorAndJump;
192 aud = AuOpenServer (server, 0, NULL, 0, NULL, &err_message);
199 signal (SIGPIPE, old_sigpipe);
201 if (err_message == NULL)
202 return "Can't connect to audio server";
207 #if defined (ROBUST_PLAY)
208 # if defined (NAS_NO_ERROR_JUMP)
209 aud->funcs.ioerror_handler = CatchIoErrorAndJump;
210 aud->funcs.error_handler = CatchErrorAndJump;
211 # else /* !NAS_NO_ERROR_JUMP */
212 AuDefaultIOErrorHandler = NULL;
213 AuDefaultErrorHandler = NULL;
218 input_id = AuXtAppAddAudioHandler (app_context, aud);
222 AuSetCloseDownMode (aud, AuCloseDownRetainPermanent, NULL);
226 signal (SIGPIPE, old_sigpipe);
235 close_down_play (void)
239 warn ("disconnected from audio server");
242 /********************************************************************\
244 * Callback which is run when the sound finishes playing. *
246 \********************************************************************/
249 doneCB (AuServer *auserver,
250 AuEventHandlerRec *handler,
254 int *in_play_p = (int *) data;
261 /********************************************************************\
263 * Play a sound by playing the relevant bucket, if any or *
264 * downloading it if not. *
266 \********************************************************************/
269 do_caching_play (Sound s,
274 AuBucketAttributes *list, b;
278 AuSetString (AuBucketDescription (&b),
279 AuStringLatin1, strlen (SoundComment (s)), SoundComment (s));
281 list = AuListBuckets (aud, AuCompCommonDescriptionMask, &b, &n, NULL);
289 if ((my_buf= (AuPointer) malloc (SoundNumBytes (s)))==NULL)
294 if (SoundReadFile ((char *) my_buf, SoundNumBytes (s), s) != SoundNumBytes (s))
301 my_buf = (AuPointer) buf;
303 id = AuSoundCreateBucketFromData (aud,
312 else /* found cached sound */
314 id = AuBucketIdentifier (list);
315 AuFreeBucketAttributes (aud, n, list);
320 AuSoundPlayFromBucket (aud,
323 AuFixedPointFromFraction (volume, 100),
324 doneCB, (AuPointer) &sounds_in_play,
330 #endif /* CACHE_SOUNDS */
333 void wait_for_sounds (void);
335 wait_for_sounds (void)
340 while (sounds_in_play>0)
342 AuNextEvent (aud, AuTrue, &ev);
343 AuDispatchEvent (aud, &ev);
347 int play_sound_file (char *sound_file, int volume);
349 play_sound_file (char *sound_file,
352 SIGTYPE (*old_sigpipe) (int);
355 old_sigpipe=signal (SIGPIPE, sigpipe_handle);
356 if (setjmp (AuXtErrorJump))
358 signal (SIGPIPE, old_sigpipe);
364 if (aud_server != NULL)
367 /* attempt to reconect */
368 if ((m=init_play (aud_server))!= NULL)
372 signal (SIGPIPE, old_sigpipe);
379 warn ("Attempt to play with no audio init\n");
381 signal (SIGPIPE, old_sigpipe);
389 AuSoundPlayFromFile (aud,
392 AuFixedPointFromFraction (volume,100),
393 doneCB, (AuPointer) &sounds_in_play,
399 /* Cache the sounds in buckets on the server */
404 if ((s = SoundOpenFileForReading (sound_file))==NULL)
407 signal (SIGPIPE, old_sigpipe);
412 if (SoundComment (s) == NULL || SoundComment (s)[0] == '\0')
414 SoundComment (s) = FileCommentFromFilename (sound_file);
417 do_caching_play (s, volume, NULL);
422 #endif /* CACHE_SOUNDS */
427 if (!NILP (Vsynchronous_sounds))
434 signal (SIGPIPE, old_sigpipe);
440 int play_sound_data (unsigned char *data, int length, int volume);
442 play_sound_data (unsigned char *data,
448 SIGTYPE (*old_sigpipe) (int);
450 #if !defined (XTEVENTS)
455 old_sigpipe = signal (SIGPIPE, sigpipe_handle);
456 if (setjmp (AuXtErrorJump) !=0)
458 signal (SIGPIPE, old_sigpipe);
465 if (aud_server != NULL)
468 /* attempt to reconect */
469 if ((m = init_play (aud_server)) != NULL)
472 signal (SIGPIPE, old_sigpipe);
479 warn ("Attempt to play with no audio init\n");
481 signal (SIGPIPE, old_sigpipe);
487 if ((s=SoundOpenDataForReading (data, length))==NULL)
489 warn ("unknown sound type");
491 signal (SIGPIPE, old_sigpipe);
496 if (SoundFileFormat (s) == SoundFileFormatSnd)
499 offset = ((SndInfo *) (s->formatInfo))->h.dataOffset;
501 else if (SoundFileFormat (s) == SoundFileFormatWave)
503 offset = ((WaveInfo *) (s->formatInfo))->dataOffset;
507 warn ("only understand snd and wave files at the moment");
510 signal (SIGPIPE, old_sigpipe);
517 AuSoundPlayFromData (aud,
521 AuFixedPointFromFraction (volume,100),
522 doneCB, (AuPointer) &sounds_in_play,
528 /* Cache the sounds in buckets on the server */
531 do_caching_play (s, volume, data+offset);
533 #endif /* CACHE_SOUNDS */
539 if (!NILP (Vsynchronous_sounds))
548 signal (SIGPIPE, old_sigpipe);
554 #if defined (ROBUST_PLAY)
556 /********************************************************************\
558 * Code to protect the client from server shutdowns. *
560 * This is unbelievably horrible. *
562 \********************************************************************/
565 CatchIoErrorAndJump (AuServer *old_aud)
568 warn ("Audio Server connection broken");
570 warn ("Audio Server connection broken because of signal");
575 AuXtAppRemoveAudioHandler (aud, input_id);
584 longjmp (AuXtErrorJump, 1);
586 #else /* not XTEVENTS */
592 longjmp (AuXtErrorJump, 1);
594 #endif /* XTEVENTS */
599 sigpipe_handle (int signo)
601 CatchIoErrorAndJump (NULL);
605 CatchErrorAndJump (AuServer *old_aud,
608 return CatchIoErrorAndJump (old_aud);
611 #endif /* ROBUST_PLAY */
613 /********************************************************************\
615 * This code is here because the nas Sound library doesn't *
616 * support playing from a file buffered in memory. It's a fairly *
617 * direct translation of the file-based equivalent. *
619 * Since we don't have a filename, samples with no comment field *
620 * are named by a section of their content. *
622 \********************************************************************/
624 /* Create a name from the sound. */
627 NameFromData (CONST char *buf,
639 while (i<8 && len >0)
641 while (*buf < 32 && len>0)
656 strcpy (s = (char *) malloc (10), name);
660 strcpy (s = (char *) malloc (15), "short sound");
666 /* Code to do a pseudo-open on a data buffer. Only for snd files at the
671 SndOpenDataForReading (CONST char *data,
678 if (!(si = (SndInfo *) malloc (sizeof (SndInfo))))
684 memcpy (&si->h, data, sizeof (SndHeader));
690 swapl (&si->h.magic, n);
691 swapl (&si->h.dataOffset, n);
692 swapl (&si->h.dataSize, n);
693 swapl (&si->h.format, n);
694 swapl (&si->h.sampleRate, n);
695 swapl (&si->h.tracks, n);
698 if (si->h.magic != SND_MAGIC_NUM)
704 size = si->h.dataOffset - sizeof (SndHeader);
708 if (!(si->comment = (char *) malloc (size + 1)))
714 memcpy (si->comment, data+sizeof (SndHeader), size);
716 *(si->comment + size) = 0;
717 if (*si->comment == '\0')
719 NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
722 si->comment = NameFromData (data+si->h.dataOffset, length-si->h.dataOffset);
724 si->h.dataSize = length-si->h.dataOffset;
731 /* Stuff taken from wave.c from NAS. Just like snd files, NAS can't
732 read wave data from memory, so these functions do that for us. */
734 #define Err() { return NULL; }
735 #define readFourcc(_f) dread(_f, sizeof(RIFF_FOURCC), 1)
736 #define cmpID(_x, _y) \
737 strncmp((char *) (_x), (char *) (_y), sizeof(RIFF_FOURCC))
738 #define PAD2(_x) (((_x) + 1) & ~1)
740 /* These functions here are for faking file I/O from buffer. */
742 /* The "file" position */
743 static size_t file_posn;
744 /* The length of the "file" */
745 static size_t file_len;
746 /* The actual "file" data. */
747 static CONST void* file_data;
749 /* Like fopen, but for a buffer in memory */
751 dopen (CONST void* data, size_t length)
758 /* Like fread, but for a buffer in memory */
760 dread (void* buf, size_t size, size_t nitems)
762 size_t nread = size * nitems;
764 if (file_posn + nread <= file_len)
766 memcpy(buf, (char *) file_data + file_posn, size * nitems);
776 /* Like fgetc, but for a buffer in memory */
780 if (file_posn < file_len)
781 return ((char *)file_data)[file_posn++];
786 /* Like fseek, but for a buffer in memory */
788 dseek (long offset, int from)
795 file_posn = file_len + offset;
800 /* Like ftell, but for a buffer in memory */
807 /* Data buffer analogs for FileReadS and FileReadL in NAS. */
809 static unsigned short
810 DataReadS(int swapit)
821 DataReadL(int swapit)
832 readChunk(RiffChunk *c)
837 if ((status = dread(c, sizeof(RiffChunk), 1)))
839 swapl(&c->ckSize, n);
844 /* A very straight-forward translation of WaveOpenFileForReading to
845 read the wave data from a buffer in memory. */
848 WaveOpenDataForReading(CONST char *data,
857 if (!(wi = (WaveInfo *) malloc(sizeof(WaveInfo))))
861 wi->dataOffset = wi->format = wi->writing = 0;
865 if (!readChunk(&ck) ||
866 cmpID(&ck.ckID, RIFF_RiffID) ||
867 !readFourcc(&fourcc) ||
868 cmpID(&fourcc, RIFF_WaveID))
871 fileSize = PAD2(ck.ckSize) - sizeof(RIFF_FOURCC);
873 while (fileSize >= sizeof(RiffChunk))
878 fileSize -= sizeof(RiffChunk) + PAD2(ck.ckSize);
881 if (!cmpID(&ck.ckID, RIFF_ListID))
883 if (!readFourcc(&fourcc))
887 if (!cmpID(&fourcc, RIFF_ListInfoID))
889 ck.ckSize -= sizeof(RIFF_FOURCC);
899 if (!cmpID(&c.ckID, RIFF_InfoIcmtID))
901 if (!(wi->comment = (char *) malloc(c.ckSize)) ||
902 !dread(wi->comment, c.ckSize, 1))
906 dgetc(); /* eat the pad byte */
909 /* skip unknown chunk */
910 dseek(PAD2(c.ckSize), 1);
912 ck.ckSize -= sizeof(RiffChunk) + PAD2(c.ckSize);
916 /* skip unknown chunk */
917 dseek(PAD2(ck.ckSize) - sizeof(RIFF_FOURCC), 1);
919 /* wave format chunk */
920 else if (!cmpID(&ck.ckID, RIFF_WaveFmtID) && !wi->format)
924 wi->format = DataReadS(BIG_ENDIAN);
925 wi->channels = DataReadS(BIG_ENDIAN);
926 wi->sampleRate = DataReadL(BIG_ENDIAN);
928 /* we don't care about the next two fields */
929 dummy = DataReadL(BIG_ENDIAN);
930 dummy = DataReadS(BIG_ENDIAN);
932 if (wi->format != RIFF_WAVE_FORMAT_PCM)
935 wi->bitsPerSample = DataReadS(BIG_ENDIAN);
937 /* skip any other format specific fields */
938 dseek(PAD2(ck.ckSize - 16), 1);
940 /* wave data chunk */
941 else if (!cmpID(&ck.ckID, RIFF_WaveDataID) && !wi->dataOffset)
945 wi->dataOffset = dtell();
946 wi->dataSize = ck.ckSize;
950 /* seek past the data */
951 if (dseek(wi->dataOffset + PAD2(ck.ckSize), 0) ||
954 /* the seek failed, assume the size is bogus */
956 wi->dataSize = dtell() - wi->dataOffset;
959 wi->dataOffset -= sizeof(long);
962 /* skip unknown chunk */
963 dseek(PAD2(ck.ckSize), 1);
969 wi->numSamples = wi->dataSize / wi->channels / (wi->bitsPerSample >> 3);
972 wi->comment = NameFromData (data + wi->dataOffset,
973 length - wi->dataOffset);
982 SoundOpenDataForReading (unsigned char *data,
988 if (!(s = (Sound) malloc (sizeof (SoundRec))))
991 if ((s->formatInfo = SndOpenDataForReading ((char *) data, length)) != NULL)
993 if (!((int(*)(Sound))(SoundFileInfo[SoundFileFormatSnd].toSound)) (s))
995 SndCloseFile ((SndInfo *) (s->formatInfo));
1000 else if ((s->formatInfo = WaveOpenDataForReading ((char *) data, length)) != NULL)
1002 if (!((int(*)(Sound))(SoundFileInfo[SoundFileFormatWave].toSound)) (s))
1004 WaveCloseFile ((WaveInfo *) (s->formatInfo));