/* randword.c

   Generate random "words" which will last a specified time if sent
   in morse code at the specified speed

   Author: Tom Russo
           russo@swcp.com

   Copyright 1999, Thomas V. Russo, KM5VY, All rights reserved.

  You may freely distribute this software so long as you retain this notice 
  and provide a detailed description of any changes you might have made.

 This software provided with absolutely no warranty whatsoever.  Use it if it
  is useful, file it in your circular hacks file if not.

Usage: randword fwpm wpm mins alphabet

fwpm = character speed in WPM
wpm = word speed in WPM
mins = total duration  of text to generate in minutes
alphabet = letters, numbers, characters and prosigns to use in sample text

Prosigns: BT is represented by =, AR by +, KN by # and SK by $

Example:
   randword 20 20 5 kmrs

will produce random "words" out of the letters k,m,r and s that would take
5 minutes to send if sent in morse code at 20WPM with farnsworth speed of 
20WPM.

 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>

#ifdef __sun__
extern double drand48();
#endif

#define SRATE 11000.0
#define RISE_FRACT .1
#define PI 3.14159265358979
#define  PARIS 50.0
#define  SECPM 60.0
#define hdr 01
#define MAXALPH 80

double ticks_per_minute,secs_per_tick,intrachar_space,interchar_space,interword_space;
char  *code[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
	       "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
	       "..-","...-",".--","-..-","-.--","--.."};
char *num[]={"-----",".----","..---","...--","....-",".....","-....","--...",
	       "---..","----."};
char *punct[]={"-..-.", /* slash */ 
	       "--..--", /* comma */
	       ".-.-.-", /* period */
	       "..--..", /* question */
               "-...-",/*BT  = */
	       ".-.-.",/*AR  +*/
	       "-.--.",/* KN #*/ 
	     "...-.-"};/*  SK $*/
double timecode(char c)
{

  int off;
  char *dida;
  double retval;
  dida = "";

  if (islower(c))
    {
      off = c-'a';
      dida = code[off];
    }
  if (isupper(c))
    {
      off = c-'A';
      dida=code[off];
    }
  if (isdigit(c))
    {
      off = c-'0';
      dida = num[off];
    }
  if (ispunct(c))
    {
      off=3; /* unknown stuff defaults to ? */
      if (c == '/') off=0;
      if (c == ',') off = 1;
      if (c == '.') off = 2;
      if (c == '?') off = 3;
      if (c == '=') off = 4;
      if (c == '+') off = 5;
      if (c == '#') off = 6;
      if (c == '$') off = 7;
      dida = punct[off];
    }

  if (dida[0]=='\0')
    dida=punct[3];

  retval=0;
  while (*dida != '\0')
    {
      if (*dida == '.')
	retval+=secs_per_tick;
      else
	retval+=3*secs_per_tick;
      retval+=intrachar_space;
      dida++;
    }
  retval+=interchar_space-intrachar_space;
  return(retval);
}

main(int argc, char **argv)
{
  int i;
  unsigned char foo;
     
  FILE *junk;
  double length_of_silence,length_of_paris;
  char line[128];
  char alphabet[MAXALPH];
  int duration;
  float sofar;
  double FWPM,WPM,TONE;
  int nalph,nline;
  double spacechance;
  int selchar;

  FWPM=18;
  WPM=13;

  if (argc < 5)
    {
      fprintf(stderr,"Usage: %s fwpm wpm mins alphabet [outfil]\n",argv[0]);
      exit(1);
    }
  if (argc >= 5)
    {
      FWPM = atof(argv[1]);
      WPM = atof(argv[2]);
      duration=atoi(argv[3]);
      strncpy(alphabet,argv[4],MAXALPH);
      if (argc==6)
	junk=fopen(argv[5],"w");
      else 
	junk=stdout;
      if (junk == NULL)
	{
	  fprintf(stderr,"Cannot open output file\n");
	  exit(1);
	}
      nalph=strlen(alphabet);
      /*      fprintf(stderr,"Farnsworth: %lf, WPM %lf\n",FWPM,WPM); */
    }

  ticks_per_minute = PARIS*FWPM;
  secs_per_tick = SECPM/ticks_per_minute;
  intrachar_space = secs_per_tick;
  length_of_paris=SECPM/WPM;
  length_of_silence=length_of_paris-31*secs_per_tick; 
  interchar_space = length_of_silence/(6.+1./3.);
  interword_space = (7.*interchar_space)/3.0;

  sofar=0;
  nline=0;
  srand48(time(NULL));
  while (sofar < 60*duration)
    {
      /* pick a letter or a space  --- odds of a space are 20%, all letters
         equal probability, but never start a line with a space */
      /* check if space */
      if (nline == 79)   /* flush the line */
	{
	  line[nline]='\0';
	  fprintf(junk,"%s\n",line);
	  nline=0;
	}
      if (nline != 0 && line[nline-1] != ' ')
	{
	  spacechance=drand48()*100.0;
	  if (spacechance <= 20)
	    {
	      sofar+=interword_space-interchar_space+intrachar_space;
	      line[nline++]=' ';
	      continue;
	    }
	}
      /* it's not a space, so pick a random character from the alphabet */
      selchar=(int)(drand48()*nalph);
      sofar+= timecode(alphabet[selchar]);
      line[nline++]=alphabet[selchar];
    }
  if (nline != 0)
    {
      line[nline]='\0';
      fprintf(junk,"%s\n",line);
    }

  /*  fprintf(stderr,"That should be %f seconds \n",sofar); */
}

