/*
 * This file is a product of Sun Microsystems, Inc. and is provided for
 * unrestricted use provided that this legend is included on all tape
 * media and as a part of the software program in whole or part.  Users
 * may copy or modify this file without charge, but are not authorized to
 * license or distribute it to anyone else except as part of a product
 * or program developed by the user.
 * 
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 * 
 * This file is provided with no support and without any obligation on the
 * part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 * 
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
 * OR ANY PART THEREOF.
 * 
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even
 * if Sun has been advised of the possibility of such damages.
 * 
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */
#include<stdio.h>
#include<sys/file.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include <signal.h>
#include "audio.h"

/*
 Speak program -
	if argv[1] = display name of another server, eg mac:0,
	and mac is running the server with audio extension
	then this program will record from default server,say
	bozo, and play on mac.
	If mac runs the same program and set argv[1] to bozo:0,
	then mac and bozo can have conversations on the workstations.

	typing q'<return> will get you out of the program.
*/

int	ok = 1;
int
done()
{
	ok = 0;
}

main(argc,argv)
char	**argv;
{
Display	*dpy, *dpy2;
int	nextensions, i;
int	mj,error,event;
int	f, sizeofbuffer;
char	buffer[10000];
int	pid;

	if((dpy=XOpenDisplay(NULL))== NULL)
	{
		fprintf(stderr,"can't open display\n");
		exit(0);
	}
	if ( argc > 1)
	{
		if((dpy2=XOpenDisplay(argv[1]))== NULL)
		{
			fprintf(stderr,"can't open display\n");
			exit(0);
		}
	}
	else
		dpy2= dpy;

	if (XAudioOpen(dpy) < 1)
		fprintf(stderr,"cannot open audio\n");
	if (XAudioOpen(dpy2) < 1)
		fprintf(stderr,"cannot open audio2\n");
	fprintf(stderr,"To quit - type q' <return>.\n");
	pid = fork();
	if (pid == 0)
	{
	int lastpass = 1;
		signal(SIGUSR2, done);
		XAudioSetVolume(dpy,12,12);
		while (ok)
		{
			sizeofbuffer = XAudioRecord(dpy,buffer);
#ifdef DEBUG
			fprintf(stderr,"recorded %d bytes\n",sizeofbuffer);
#endif DEBUG
			if ((lastpass | sizeofbuffer) <= 0)
				break;
			XAudioPlay(dpy2,buffer,sizeofbuffer);
			sleep(1);
			lastpass = sizeofbuffer;
		}
		XAudioRecordOff(dpy);
		XAudioPlayOff(dpy2,0);
		if (dpy != dpy2)
			XCloseDisplay(dpy);
		while( XAudioClose(dpy2) == 0)
		{
			fprintf(stderr,"waiting\n");
			sleep(1);
		}
		XCloseDisplay(dpy2);
	}
	else
	{
		if (getchar() == q')
			kill(pid, SIGUSR2);
	}
}
