/* program to speak multiple lines of text
        lines read from standard in
           each line may be up to 1000 characters in length
           including terminating null
        speech comes from espeak program
        program halts when line "eSpeakABBCCCDDDEEEEFFFFdone" read
*/

/* compile with the command:
   gcc -lespeak -o textlines-to-speech textlines-to-speech.c

 */

#include <stdio.h>
#include <string.h>
#include <espeak/speak_lib.h>

/* libraries for the random number generator */
#include <stdlib.h>
#include <time.h>

int main ()
{
  int MAX = 1000;   // maximum characters in a line
  char line[MAX];
  char * terminating_string = "eSpeakABBCCCDDDEEEEFFFFdone";
  
  /* initalize eSpeak package */
  espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, NULL, 0);
  espeak_SetVoiceByName("en-us");
  // espeak_SetVoiceByName("en-uk");
  espeak_SetParameter(espeakRATE, 140, 0);

  /* set voice randomly to female or male */
  srand (time ((time_t *) 0) ); // initialize random generator

  int i, rand_num;
  for (i = 0; i < 10; i++)
    rand_num = rand() % 10;
  printf ("rand_num = %d\n", rand_num);
  if (rand_num < 5)
    {
      espeak_VOICE his_voice = {NULL, NULL, NULL, 1, 55, 0, 0, 0};
      espeak_SetVoiceByProperties(&his_voice);
          // speak line
      espeak_Synth("initialization male", strlen("initialization male"), 0, POS_CHARACTER, 0, 0,
                       NULL, NULL);
          // wait for speaking to finish
          espeak_Synchronize();  
     printf ("initialization male\n");
    }   
  else
    {
      espeak_VOICE her_voice = {NULL, NULL, NULL, 2, 32, 0, 0, 0};
       espeak_Synth("initialization female", strlen("initialization female"), 0, POS_CHARACTER, 0, 0,
                       NULL, NULL);
          // wait for speaking to finish
          espeak_Synchronize();  
     espeak_SetVoiceByProperties(&her_voice);
      printf ("initialization female\n");
    }

  // retrieve first line
  for (i = 0; i < MAX; i++)  // first, null entire line array
    line[i] = 0;
  fgets (line, MAX, stdin);
  // remove newline character, if present
  if (line[strlen(line)-1] == '\n')
    line[strlen(line)-1] = 0;

  while (strcmp (line, terminating_string) != 0)
    { 
      printf ("Line:  '%s'\n", line);
      // check voice parameter commands 
      if (strcmp(line, "eSpeakABBCCCDDDEEEEFFFFmale") == 0)
        {
          espeak_VOICE his_voice = {NULL, NULL, NULL, 1, 0, 0, 0, 0};
          espeak_SetVoiceByProperties(&his_voice);
      printf ("change male\n");
        }
      else if (strcmp(line, "eSpeakABBCCCDDDEEEEFFFFfemale") == 0)
        {
          espeak_VOICE her_voice = {NULL, NULL, NULL, 2, 0, 0, 0, 0};
          espeak_SetVoiceByProperties(&her_voice);
      printf ("change female\n");
        }
      else
        {
   printf ("here5\n");
         // speak line
          espeak_Synth(line, strlen(line), 0, POS_CHARACTER, 0, 0,
                       NULL, NULL);
          // wait for speaking to finish
          espeak_Synchronize();  
        }

      // get next line
   for (i = 0; i < MAX; i++)  // first, null entire line array
    line[i] = 0;
    fgets (line, MAX, stdin);
      // remove newline character, if present
      if (line[strlen(line)-1] == '\n')
        line[strlen(line)-1] = 0;      
    }

  return 0;
}
