flite_time_main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 2001 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: June 2001 */
  35. /*************************************************************************/
  36. /* */
  37. /* Example of clunits voice (ldom time) */
  38. /* */
  39. /*************************************************************************/
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <sys/time.h>
  44. #include <unistd.h>
  45. #include "flite.h"
  46. #include "cst_clunits.h"
  47. #include "cst_regex.h"
  48. #include "cst_units.h"
  49. #include "cst_wave.h"
  50. #include "cst_track.h"
  51. #include "cst_sigpr.h"
  52. cst_voice *register_cmu_time_awb(const char *voxdir);
  53. static const char *time_approx(int hour, int minute);
  54. static const char *time_min(int hour, int minute);
  55. static const char *time_hour(int hour, int minute);
  56. static const char *time_tod(int hour, int minute);
  57. int main(int argc, char **argv)
  58. {
  59. cst_voice *v;
  60. char thetime[1024];
  61. char b[3];
  62. int hour, min;
  63. cst_regex *timex;
  64. char *output = "play";
  65. if (argc != 2)
  66. {
  67. fprintf(stderr,"usage: flite_time HH:MM\n");
  68. exit(-1);
  69. }
  70. timex = new_cst_regex("[012][0-9]:[0-5][0-9]");
  71. if (!cst_regex_match(timex,argv[1]))
  72. {
  73. fprintf(stderr,"not a valid time\n");
  74. fprintf(stderr,"usage: flite_time HH:MM\n");
  75. exit(-1);
  76. }
  77. delete_cst_regex(timex);
  78. b[2] = '\0';
  79. b[0] = argv[1][0];
  80. b[1] = argv[1][1];
  81. hour = atoi(b);
  82. b[0] = argv[1][3];
  83. b[1] = argv[1][4];
  84. min = atoi(b);
  85. flite_init();
  86. v = register_cmu_time_awb(NULL);
  87. sprintf(thetime,
  88. "The time is now, %s %s %s, %s.",
  89. time_approx(hour,min),
  90. time_min(hour,min),
  91. time_hour(hour,min),
  92. time_tod(hour,min));
  93. printf("%s\n",thetime);
  94. flite_text_to_speech(thetime,v,output);
  95. return 0;
  96. }
  97. static const char *time_approx(int hour, int minute)
  98. {
  99. int mm;
  100. mm = minute % 5;
  101. if ((mm == 0) || (mm == 4))
  102. return "exactly";
  103. else if (mm == 1)
  104. return "just after";
  105. else if (mm == 2)
  106. return "a little after";
  107. else
  108. return "almost";
  109. }
  110. static const char *time_min(int hour, int minute)
  111. {
  112. int mm;
  113. mm = minute / 5;
  114. if ((minute % 5) > 2)
  115. mm += 1;
  116. mm = mm * 5;
  117. if (mm > 55)
  118. mm = 0;
  119. if (mm == 0)
  120. return "";
  121. else if (mm == 5)
  122. return "five past";
  123. else if (mm == 10)
  124. return "ten past";
  125. else if (mm == 15)
  126. return "quarter past";
  127. else if (mm == 20)
  128. return "twenty past";
  129. else if (mm == 25)
  130. return "twenty-five past";
  131. else if (mm == 30)
  132. return "half past";
  133. else if (mm == 35)
  134. return "twenty-five to";
  135. else if (mm == 40)
  136. return "twenty to";
  137. else if (mm == 45)
  138. return "quarter to";
  139. else if (mm == 50)
  140. return "ten to";
  141. else if (mm == 55)
  142. return "five to";
  143. else
  144. return "five to";
  145. }
  146. static const char *time_hour(int hour, int minute)
  147. {
  148. int hh;
  149. hh = hour;
  150. if (minute > 32)
  151. hh += 1;
  152. if (hh == 24)
  153. hh = 0;
  154. if (hh > 12)
  155. hh -= 12;
  156. if (hh == 0)
  157. return "midnight";
  158. else if (hh == 1)
  159. return "one";
  160. else if (hh == 2)
  161. return "two";
  162. else if (hh == 3)
  163. return "three";
  164. else if (hh == 4)
  165. return "four";
  166. else if (hh == 5)
  167. return "five";
  168. else if (hh == 6)
  169. return "six";
  170. else if (hh == 7)
  171. return "seven";
  172. else if (hh == 8)
  173. return "eight";
  174. else if (hh == 9)
  175. return "nine";
  176. else if (hh == 10)
  177. return "ten";
  178. else if (hh == 11)
  179. return "eleven";
  180. else if (hh == 12)
  181. return "twelve";
  182. else
  183. return "twelve";
  184. }
  185. static const char *time_tod(int hour, int minute)
  186. {
  187. int hh = hour;
  188. if (minute > 58)
  189. hh++;
  190. if (hh == 24)
  191. return "";
  192. else if (hh > 17)
  193. return "in the evening";
  194. else if (hh > 11)
  195. return "in the afternoon";
  196. else if ((hh == 0) && (minute < 33))
  197. return "";
  198. else
  199. return "in the morning";
  200. }