/*
	GIER Pulse Audio sound interface

	(C) Copyright 2010 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_PA_SOUND

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

#include <pulse/simple.h>
#include <pulse/error.h>

#ifndef False
#define False 0
#endif

#ifndef True
#define True 1
#endif

/* global variables */
extern int debug;

/* local variables */

#ifdef UAKK
#define DUMPSOUND
#endif

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

static int sound_rate=45400; /* 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=1;
static int value_0, value_1;
static int sound_debug_fd= -1;
static int last_value=0;

static pa_simple *pa_s=NULL;
static pa_sample_spec pa_ss;
static int pa_error;

int pa_sound_init()
{
  int st;
  int tmp;

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

  sound_rate=gier_clock/10;
  if(pa_s != NULL)
  {
    pa_simple_free(pa_s);
    pa_s = NULL;
  }

  pa_ss.format = PA_SAMPLE_U8;
  pa_ss.channels = 1;
  pa_ss.rate = sound_rate;

  pa_s = pa_simple_new(NULL, "GIER Sound", PA_STREAM_PLAYBACK, NULL,
                       "GIER Loudspeaker", &pa_ss, NULL, NULL, &pa_error);

  if(!pa_s)
  {
    fprintf(stderr, "pa_simple_new failed: %s\n", pa_strerror(pa_error));
    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

  sound_buffersize = gier_clock/100;

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

  if(sound_sign)
  {
    value_0 = -16;
    value_1 = 15;
  }
  else
  {
    value_0 = 0;
    value_1 = 255;
  }

  sound_update_interval = gier_clock/sound_rate;
  sound_buffer_pnt = 0;

  return True;
error_exit:
  pa_simple_free(pa_s);
  pa_s = NULL;
  return False;
}

void pa_sound_close()
{
  if(pa_s != NULL)
  {
    pa_simple_free(pa_s);
    pa_s = NULL;
#ifdef DUMPSOUND
    close(sound_debug_fd);
#endif
  }
}

void sound_sync()
{
  if(sound_buffer_pnt==0) return;
  if(pa_simple_write(pa_s, sound_buffer, sound_buffer_pnt, &pa_error) < 0)
  {
    fprintf(stderr, "pa_simple_write failed: %s\n", pa_strerror(pa_error));
  }
#ifdef DUMPSOUND
  write(sound_debug_fd, sound_buffer, sound_buffer_pnt);
#endif
  sound_buffer_pnt=0;
}

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) sound_sync();
}

#else
int pa_sound_init()
{
  return 0;
}

void sound_sync()
{
}

void sound_update(int value)
{
}
#endif
