/* mm1/audio.c
	vi:ts=3 sw=3:
 */

/* $Id: audio.c,v 1.4 1995/09/17 23:27:02 espie Exp espie $
 * $Log: audio.c,v $
 * Revision 1.4  1995/09/17 23:27:02  espie
 * *** empty log message ***
 *
 * Revision 1.3  1995/09/03 14:20:49  espie
 * *** empty log message ***
 *
 * Revision 1.2  1995/09/03 13:38:58  espie
 * Corrected data sizes.
 *
 * Revision 1.1  1995/04/22  17:49:58  espie
 * Initial revision
 *
 */

#include "defs.h"
#include "extern.h"
#include <sound.h>
#include <errno.h>

struct options_set *port_options=0;

#define BUFSIZE 100000
#define SIG_CODE 234


#define DEFAULT_SET_MIX
#define DEFAULT_BUFFERS
#define NEW_OUTPUT_SAMPLES_AWARE
#define UNSIGNED8

#include "Arch/common.c"

ID("$Id: audio.c,v 1.4 1995/09/17 23:27:02 espie Exp espie $)

LOCAL unsigned char *buffer1, *buffer2, *ptr, *curbuf;
LOCAL unsigned long sample_rate, samples_max;
LOCAL int sigarrived;

gotsignl()
{
   sigarrived = SIG_CODE;
}

unsigned long open_audio(frequency, stereo)
unsigned long frequency;
int stereo;
	{
   sample_rate = frequency ? frequency : 8000;
   buffer1 = malloc(BUFSIZE);
   buffer2 = malloc(BUFSIZE);
   if ( buffer1 == NULL || buffer2 == NULL )
      exit(_errmsg(errno, "cannot allocate buffers\n"));
	buffer = buffer1;
	idx = 0;
	samples_max = BUFSIZE
	/*
   ptr = curbuf = buffer1;
	*/
   sigarrived = 1;

   signal(SIG_CODE, gotsignl);

   return sample_rate;
	}

void output_samples(left, right, n)
long left, right;
int n;
	{
	if (idx >= samples_max - 1)
		actually_flush_buffer();
	
	add_samples8(left, right, n);
	}


void actually_flush_buffers()
	{
	if (sigarrived == 0)
		sleep(0);
	sigarrived = 0;
	_ss_play(1, buffer, idx, sample_rate, SND_SENDSIG, SIG_CODE);
	if (buffer == buffer1)
		buffer = buffer2;
	else
		buffer = buffer1;
	idx = 0;
   }

void flush_buffer()
	{
	}

void close_audio()
{
   _ss_play(1, curbuf, ptr - curbuf, sample_rate, SND_SENDSIG, SIG_CODE);
}

unsigned long update_frequency()
	{
   return 0;
	}

void discard_buffer()
	{
	}

int output_resolution()
	{
	return 16;
	}
