Advertisement
Guest User

test_festival

a guest
Sep 21st, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. // preprocessor directives
  2. #include <festival.h>
  3. #include <sys/time.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. //
  9. EST_Val default_val_float(0.0f);
  10.  
  11.  
  12.  
  13. // functions for accessing word start & end times in an utterance tree
  14.  
  15. // returns segment start time      
  16. EST_Val ff_seg_start(EST_Item* s)
  17. {
  18.   EST_Item* n = as(s, "Segment");
  19.   if (prev(n) == 0) return default_val_float;
  20.   return prev(n)->F("end", 0);
  21. } // ff_seg_start(EST_Item*)
  22.  
  23.  
  24.  
  25. // returns segment end time
  26. EST_Val ff_seg_end(EST_Item* s)
  27. {
  28.   return s->F("end", 0);
  29. } // ff_seg_end(EST_Item*)
  30.  
  31.  
  32.  
  33. // returns syllable end time
  34. EST_Val ff_syl_end(EST_Item* s)
  35. {
  36.   EST_Item* n = as(s, "SylStructure");
  37.   if (daughtern(n) == 0) return default_val_float;
  38.   return ff_seg_end(daughtern(n));
  39. } // ff_syl_end(EST_Item*)
  40.  
  41.  
  42.  
  43. // returns word start time
  44. EST_Val ff_word_start(EST_Item* s)
  45. {
  46.   EST_Item* n = as(s, "SylStructure");
  47.   if (daughter1(daughter1(n)) == 0) return default_val_float;
  48.   return ff_seg_start(daughter1(daughter1(n)));
  49. } // ff_word_start(EST_Item*)
  50.  
  51.  
  52.  
  53. // returns word end time
  54. EST_Val ff_word_end(EST_Item* s)
  55. {
  56.   EST_Item* n = as(s, "SylStructure");
  57.   if (daughtern(n) == 0) return default_val_float;
  58.   return ff_syl_end(daughtern(n));
  59. } // ff_word_end(EST_Item*)
  60.  
  61.  
  62.  
  63. // executes main program code
  64. int main(int argc, char** argv)
  65. {
  66.   if (argc < 2)
  67.   {
  68.     printf(">> ERROR: Invalid arguments...\n\n");
  69.     return 1;
  70.   }
  71.  
  72.   // Festival local variables
  73.   int heap_size       = 210000;  // default scheme heap size
  74.   int load_init_files = 1;       // we want the festival init files loaded
  75.  
  76.   // initialize the Festival text-to-speech (TTS) engine
  77.   festival_initialize(load_init_files, heap_size);
  78.  
  79.  
  80.   // iterate through text input; append words to string str
  81.   EST_String str;
  82.   for(int i = 1; i < argc; ++i)
  83.   {
  84.     //printf("arg %d: %s\n", i, argv[i]);
  85.     str += argv[i];
  86.     str += " ";
  87.   }
  88.  
  89.   // Lisp command to create and synthesize a text utterance with the user input
  90.   EST_String command1 = "(set! utt1 (utt.synth(Utterance Text \"" +
  91.                         str + "\")))";
  92.  
  93.   // Lisp command to save the utterance to a file
  94.   EST_String command2 = "(utt.save utt1 \"inputText.utt\")";
  95.  
  96.   // evaluate the Lisp command
  97.   bool success1 = festival_eval_command(command1);
  98.   bool success2 = festival_eval_command(command2);
  99.   //cout << "Command[1] success: " << success1 << endl;
  100.   //cout << "Command[2] success: " << success2 << endl;
  101.  
  102.   // load the utterance previously made
  103.   EST_Utterance myUtt;
  104.   myUtt.load("inputText.utt");
  105.  
  106.   // print out how many words are in this utterance
  107.   EST_Relation myRel = *myUtt.relation("Word");
  108.   cout << "# words: " << myRel.length() << endl;
  109.  
  110.   // iterate through the utterance tree to get each word
  111.   EST_Item* s = NULL;
  112.   for (s = myUtt.relation("Word")->head(); s != 0; s = next(s))
  113.   {
  114.     //print out features of the word: name, start time, end time
  115.     cout << "Word: "   << s->S("name");
  116.     cout << "\tstart: "<< ff_word_start(s);
  117.     cout << "\tend: "  << ff_word_end(s) << endl;  
  118.  
  119.     // print out the "word_end" feature of the word  <-- does not work
  120.     //cout << s->A("word_end") << endl;
  121.   }
  122.  
  123.   // synthesize the parameterized text and time its duration
  124.   timeval t_start, t_stop;
  125.   gettimeofday(&t_start, NULL);
  126.   festival_say_text(str);
  127.   gettimeofday(&t_stop, NULL);
  128.   double t_elapsed = (t_stop.tv_sec  + (double)t_stop.tv_usec  * 1e-6) -
  129.                      (t_start.tv_sec + (double)t_start.tv_usec * 1e-6);
  130.   printf("Time elapsed: %.6f\n", t_elapsed);
  131.  
  132.   return 0;
  133. } // main(int, char**)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement