/*
	GIER oss sound interface

	(C) Copyright 2001 by Mogens Kjaer


   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
*/

extern int sound_update_interval;

#ifdef HAS_OSS_SOUND

#include <stdio.h>
#include "GIER.h"
#include "common.h"
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>


#include <sys/soundcard.h>
#include <sys/ioctl.h>

#ifndef False
#define False 0
#endif

#ifndef True
#define True 1
#endif

/* global variables */
extern int debug;

/* local variables */

#ifdef NOTNOT
#define DUMPSOUND
#endif

/* default is 45400 samples per second, 8 bits unsigned, mono */

static int sound_rate=gier_clock; /* samples per second */
static int sound_size=8; /* 8 or 16 bits per sample */
static int sound_sign=False; /* true if signed */
static int sound_stereo=False; /* true if stereo */
static int sound_buffersize;
static char *sound_buffer=NULL;
static int sound_buffer_pnt;
static int sound_sample_size;
static int value_0, value_1;
static int sound_fd=-1;
static int sound_debug_fd= -1;
static int last_value=0;

int oss_sound_init()
{
  int st;
  int tmp;

  /*
  	Return true iff oss sound can be used.
  */

  if(sound_fd != -1)
  {
    close(sound_fd);
    sound_fd = -1;
  }

  sound_fd = open("/dev/dsp", O_WRONLY);
  if(sound_fd == -1)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Can't open /dev/dsp for writing.\n");
    return False;
  }

#ifdef DUMPSOUND
  sound_debug_fd = open("sound.debug", O_WRONLY|O_CREAT|O_TRUNC, 0666);
#endif

  if(ioctl(sound_fd, SNDCTL_DSP_RESET, 0)<0)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Can't reset OSS driver.\n");
    goto error_exit;
  }

  sound_buffersize=0;
  ioctl(sound_fd, SNDCTL_DSP_GETBLKSIZE, &sound_buffersize);
  if(debug&DEBUGsound) fprintf(debug_fh, "buffersize: %d\n", sound_buffersize);

  sound_buffer = malloc(sound_buffersize);
  if(sound_buffer == NULL)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Can't allocate buffer.\n");
    goto error_exit;
  }

  if(ioctl(sound_fd, SNDCTL_DSP_SYNC, NULL)<0)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Can't set sync.\n");
    goto error_exit;
  }

  tmp = sound_size;
  if(ioctl(sound_fd, SNDCTL_DSP_SAMPLESIZE, &tmp) < 0)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Can't set samplesize to %d.\n", sound_size);
    goto error_exit;
  }

  if(tmp != sound_size)
  {
    if(tmp == 16)
    {
      sound_size = 16;
      sound_sign = 1;
      if(debug&DEBUGsound) fprintf(debug_fh, "Setting 16 bit signed.\n");
    }
    else
    {
      sound_size = 8;
      sound_sign = 0;
      if(debug&DEBUGsound) fprintf(debug_fh, "Setting 8 bit unsigned.\n");
    }
  }
  
  tmp = sound_stereo;
  if(ioctl(sound_fd, SNDCTL_DSP_STEREO, &tmp) < 0)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Couldn't set to %s\n", sound_stereo?"stereo":"mono");
    sound_stereo=0;
  }

  if(tmp != sound_stereo)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Couldn't set to %s. Changed.\n", sound_stereo?"stereo":"mono");
    sound_stereo = tmp;
  }

  sound_sample_size = sound_size/8*(sound_stereo+1);
  if(debug&DEBUGsound) fprintf(debug_fh, "sound_sample_size: %d\n", sound_sample_size);

  if(sound_sign)
  {
    value_0 = -128;
    value_1 = 127;
  }
  else
  {
    value_0 = 0;
    value_1 = 255;
  }

  tmp = sound_rate;
  if(ioctl(sound_fd, SNDCTL_DSP_SPEED, &tmp) < 0 || tmp != sound_rate)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "Couldn't set rate to %d, got %d.\n", sound_rate, tmp);
    sound_rate = tmp;
  }

  sound_update_interval = gier_clock/sound_rate;
  sound_buffer_pnt = 0;

  return True;
error_exit:
  close(sound_fd);
  sound_fd = -1;
  return False;
}

void oss_sound_close()
{
  if(sound_fd != -1)
  {
    close(sound_fd);
    sound_fd = -1;
  }
}

void sound_update(int value)
{
  int i,st;
  for(i=0; i<sound_sample_size; i++) sound_buffer[sound_buffer_pnt++] = value?value_1:value_0;
  if(sound_buffer_pnt>=sound_buffersize)
  {
    st=write(sound_fd, sound_buffer, sound_buffer_pnt);
    if(st != sound_buffer_pnt)
    {
      if(debug&DEBUGsound) fprintf(debug_fh, "sound_buffer_pnt: %d, st: %d\n", sound_buffer_pnt, st);
    }
#ifdef DUMPSOUND
    write(sound_debug_fd, sound_buffer, sound_buffer_pnt);
#endif
    sound_buffer_pnt=0;
  }
}

void sound_sync()
{
  int st;
  if(sound_buffer_pnt==0) return;
  st=write(sound_fd, sound_buffer, sound_buffer_pnt);
  if(st != sound_buffer_pnt)
  {
    if(debug&DEBUGsound) fprintf(debug_fh, "sound_buffer_pnt: %d, st: %d\n", sound_buffer_pnt, st);
  }
#ifdef DUMPSOUND
  write(sound_debug_fd, sound_buffer, sound_buffer_pnt);
#endif
  sound_buffer_pnt=0;
  ioctl(sound_fd, SNDCTL_DSP_SYNC, NULL);
}

#else
int oss_sound_init()
{
  return 0;
}
#endif
