/*
 * $Id: audiosrv.c,v 1.2 1999/04/26 16:54:56 bernie Exp $
 *
 * inetd audio server for BNAS (Linux specific)
 * Home Page: http://www.codewiz.org/projects/unix/bnas.html
 *
 * Copyright (C) 1999  Bernardo Innocenti
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/soundcard.h>
#include <fcntl.h>

#define DEF_BUFSIZE	16768
#define DEF_FILE	"/dev/dsp"
#define DEF_RATE	44100
#define DEF_STEREO	1
#define DEF_FMT		AFMT_S16_NE

int main (int argc, char *argv[])
{
	unsigned short int buf[DEF_BUFSIZE/2];
	int audiofd;
	int len, i;
	int rate	= DEF_RATE;
	int stereo	= DEF_STEREO;
	int fmt		= DEF_FMT;

	if ((audiofd = open (DEF_FILE, O_WRONLY)) > 0)
	{
		ioctl (audiofd, SNDCTL_DSP_SPEED, &rate);
		ioctl (audiofd, SNDCTL_DSP_STEREO, &stereo);
		ioctl (audiofd, SNDCTL_DSP_SETFMT, &fmt);

		while ((len = read (0, buf, DEF_BUFSIZE)) > 0)
		{
			i = len / 2;
			while (--i >= 0)
			{
				buf[i] = (buf[i] >> 8) + (buf[i] << 8);
			}
			if (write (audiofd, buf, len) == -1)
			{
				perror (argv[0]);
				break;
			}
		}

		close (audiofd);
	}
	else
		perror(argv[0]);

	return 0;
}

