/*******************************************/
/*  Raw Wave File Output Class,            */
/*  by Perry R. Cook, 1995-96              */ 
/*  This version opens a mono NeXT .snd    */
/*  file 16bit data at 22KHz, and          */
/*  pokes buffers of samples into it.      */
/*******************************************/

/*******************************************/
/*  SGI Real-Time Wave File Output Class,  */
/*  by Perry R. Cook, 1996                 */ 
/*  This Object can opens the SGI soundout */
/*  device, and pokes buffers of samples   */
/*  into it.  The real code that does the  */
/*  is from Doug Scott of SGI.             */
/*******************************************/

/*******************************************/
/*  USS Real-Time Wave File Output Class,  */
/*  by Tim Stilson, 1996                   */
/*  based on code by Perry R. Cook, 1996   */ 
/*  This Object opens the USS sound output */
/*  device, and pokes buffers of samples   */
/*  into it.                               */
/*******************************************/

#include "RTWvOut.h"

#if defined(__SGI_REALTIME_)
  #include "sgio.C"
#elif defined(__USS_REALTIME_)
  #include "usso.C"
#endif

RTWvOut :: RTWvOut()
{
  int fd;

  fd = init_sound(SRATE,1);
  if (fd<0)   {
	printf("Couldn't open SoundOut Device  !!!!!!!!\n");
	exit(0);
  }
  counter = 0;
}

RTWvOut :: ~RTWvOut()
{
    playbuf(data,counter);
    counter = 0;
    while (counter<RT_BUFFER_SIZE)	{
        data[counter++] = 0;
    }
    playbuf(data,counter);
    playbuf(data,counter);  // Are these extra writes necessary in USS?
    playbuf(data,counter);
    shuddup();
}

void RTWvOut :: tick(MY_FLOAT sample)
{

  data[counter++] = (short) (sample * 32000.0);
  if (counter >= RT_BUFFER_SIZE) {
	playbuf(data,counter);
	counter = 0;
  }
}
