#include "config.h"

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef VOXWARE
#include <sys/soundcard.h>
#endif
#include "pokey.h"
#include "pokey11.h"

static char *rcsid = "$Id: sound.c,v 1.2 1997/04/18 22:23:38 david Exp $";

/* 0002 = 2 Fragments */
/* 0007 = means each fragment is 2^2 or 128 bytes */
/* See voxware docs in /usr/src/linux/drivers/sound for more info */

#define FRAG_SPEC 0x0002000a

#define FALSE 0
#define TRUE 1

#ifdef VOXWARE
static char dsp_buffer[44100];
static int sndbufsize;
static int dsp_fd;
#endif

static sound_enabled = TRUE;

static int dsp_sample_rate;
static int dsp_sample_rate_divisor = 35;

void Voxware_Initialise (int *argc, char *argv[])
{
  int i, j;

  for (i=j=1;i<*argc;i++)
    {
      if (strcmp(argv[i],"-sound") == 0)
        sound_enabled = TRUE;
      else if (strcmp(argv[i],"-nosound") == 0)
        sound_enabled = FALSE;
      else if (strcmp(argv[i],"-dsp_divisor") == 0)
        sscanf (argv[++i],"%d",&dsp_sample_rate_divisor);
      else
        argv[j++] = argv[i];
    }

  *argc = j;
 
#ifdef VOXWARE
  if (sound_enabled)
    {
      int channel;
      int dspbits;
      unsigned int formats;
      int tmp;

      dsp_fd = open ("/dev/dsp", O_WRONLY, 0777);
      if (dsp_fd == -1)
        {
          perror ("/dev/dsp");
          exit (1);
        }

      /*
       * Get sound formats
       */

      ioctl (dsp_fd, SNDCTL_DSP_GETFMTS, &formats);

      /*
       * Set sound of sound fragment to special?
       */

      tmp = FRAG_SPEC;
      ioctl (dsp_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);

      /*
       * Get preferred buffer size
       */

      ioctl (dsp_fd, SNDCTL_DSP_GETBLKSIZE, &sndbufsize);

      /*
       * Set to 8bit sound
       */

      dspbits = 8;
      ioctl (dsp_fd, SNDCTL_DSP_SAMPLESIZE, &dspbits);
      ioctl (dsp_fd, SOUND_PCM_READ_BITS, &dspbits);

      /*
       * Set sample rate
       */

      ioctl (dsp_fd, SNDCTL_DSP_SPEED, &dsp_sample_rate);
      ioctl (dsp_fd, SOUND_PCM_READ_RATE, &dsp_sample_rate);

      Pokey_sound_init (FREQ_17_EXACT, dsp_sample_rate);
    }
#else
  dsp_sample_rate=1;
  Pokey_sound_init(FREQ_17_EXACT,dsp_sample_rate);
#endif
}

void Voxware_Exit (void)
{
#ifdef VOXWARE
  if (sound_enabled)
    close (dsp_fd);
#endif
}

void Voxware_UpdateSound (void)
{
#ifdef VOXWARE
  if (sound_enabled)
    {
      sndbufsize = dsp_sample_rate / dsp_sample_rate_divisor;

      Pokey_process (dsp_buffer, sndbufsize);
      /*
       * Send sound buffer to DSP device
       */

      write (dsp_fd, dsp_buffer, sndbufsize);
    }
#endif
}
