/********************************************************
*
* sound.c	Allow R4 to make sound
*
* Author:	Gary Hoggard	1 Jan 1999
*
* Change History:
********************************************************/

#include "r4.h"

main()
	{
	unsigned char r4;	/* byte to write to I/O card */
	char pipe[128];		/* pathname of named pipe */
	FILE *PIPE;		/* file pointer for named pipe */
	char soundcmd[128];	/* commadn to play sound file */
	char buffer[BUFSIZ];
	char *ptr;

	if (getenv("DEBUG"))
		{
		debug=YES;
		dlog = fopen(DEBUGFILE, "a");
		}

	/* open the named pipe for reading */
	if (getenv("R4_LIB"))
		strcpy(pipe, getenv("R4_LIB"));
	else
		strcpy(pipe, LIB);
	strcat(pipe, "/sound_pipe");
	if ((PIPE = fopen(pipe, "r")) == NULL)
		{
		perror(pipe);
		exit(1);
		}

	while (YES)
		{
		if ((fgets(buffer, BUFSIZ, PIPE)) == 0)
			{
			fclose(PIPE);
			exit(0);
			}
		if (debug)
			{
			fprintf(dlog, "sound: read '%s' from sound_pipe\n", buffer);
			fflush(dlog);
			}
		if (*buffer != 'p')	/* only 'p'lay commmands are allowed */
			continue;
		for (ptr = buffer; *ptr; ++ptr)	/* clean up and EOS crud */
			if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
				{
				*ptr = '\0';
				break;
				}
		if (debug)
			{
			fprintf(dlog, "sound file: '%s'\n", &buffer[2]);
			fflush(dlog);
			}
		switch (buffer[1])
			{
			case 'a':
				sprintf(soundcmd,
				"cat /usr/local/sound/audio/%s.au >/dev/audio",
					&buffer[2]);
				system(soundcmd);
				break;
			case 'w':
				sprintf(soundcmd,
				"wavplay /usr/local/sound/wave/%s.wav >/dev/null 2>&1",
					&buffer[2]);
				system(soundcmd);
				break;
			}
		}
	}

/*_______________________end_of_sound.c________________________*/

