/*
 *  CWStation
 *  Copyright (c) 2004 by Thierry Leconte (F4DWV)
 *
 *      $Id: image.c,v 1.10 2004/04/24 07:19:48 f4dwv Exp $
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library 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 Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
                                                                                                                              
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

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

#include <math.h>

#include "cwtab.h"

static int sndfd = 0;
static int srate;

int
open_snd (char *dev, uint rate)
{
  uint param;

  sndfd = open (dev, O_RDWR);

  param = AFMT_S16_NE;
  ioctl (sndfd, SNDCTL_DSP_SETFMT, &param);
  param = 0;
  ioctl (sndfd, SNDCTL_DSP_STEREO, &param);
  srate = rate;
  ioctl (sndfd, SNDCTL_DSP_SPEED, &srate);
  param = 0x0004000A;
  ioctl (sndfd, SNDCTL_DSP_SETFRAGMENT, &param);

  return 0;
}

void
close_snd (void)
{
  close (sndfd);
  sndfd = 0;
}

static uint ditlen, spacelen;
static short dit_sample[24000];
static short dah_sample[36000];
static short space_sample[48000];

void
setcwdit (uint speed, uint space, uint pitch, uint shape)
{
  int i, l;
  double v;

  ditlen = (int) (60.0 * srate / (50.0 * speed));
  spacelen = ditlen * space / 100;

  for (i = 0; i < ditlen; i++)
    {
      v = 32000 * sin (2 * M_PI * pitch * i / srate);
      dit_sample[i] = dah_sample[i] = (short) v;
    }
  for (; i < 3 * ditlen; i++)
    {
      v = 32000 * sin (2 * M_PI * pitch * i / srate);
      dah_sample[i] = (short) v;
    }
  for (i = ditlen; i < 2 * ditlen; i++)
    dit_sample[i] = 0;
  for (i = 3 * ditlen; i < 4 * ditlen; i++)
    dah_sample[i] = 0;

  l = ditlen * shape / 200;
  for (i = 0; i < l; i++)
    {
      v = sin (i * M_PI / 2 / l);
      dit_sample[i] *= v;
      dit_sample[ditlen - i] *= v;
      dah_sample[i] *= v;
      dah_sample[3 * ditlen - i] *= v;
    }

  for (i = 0; i < 4 * spacelen; i++)
    space_sample[i] = 0;

}

void
writecwchar (uint c)
{
  int i, len;
  char *code;

  code = cwtab[c];
  if (code == NULL)
    {
      write (sndfd, space_sample, 4 * spacelen * sizeof (short));
      return;
    }

  len = strlen (code);

  for (i = 0; i < len; i++, code++)
    {
      if (*code == '-')
	{
	  write (sndfd, dah_sample, 4 * ditlen * sizeof (short));
	}
      else
	{
	  write (sndfd, dit_sample, 2 * ditlen * sizeof (short));
	}
    }
  write (sndfd, space_sample, (3 * spacelen - ditlen) * sizeof (short));
}
