by_word_main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 2010 */
  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: May 2010 */
  35. /*************************************************************************/
  36. /* */
  37. /* Streaming and events */
  38. /* Outputs the token as it is spoken */
  39. /* If the token doesn't give rise to speech -- it wont be outputed */
  40. /*************************************************************************/
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "flite.h"
  44. cst_voice *register_cmu_us_kal();
  45. int audio_stream_chunk_by_word(const cst_wave *w, int start, int size,
  46. int last, cst_audio_streaming_info *asi)
  47. {
  48. /* Called with new samples from start for size samples */
  49. /* last is true if this is the last segment. */
  50. /* This is really just an example that you can copy for your streaming */
  51. /* function */
  52. /* This particular example is *not* thread safe */
  53. int n;
  54. static cst_audiodev *ad = 0;
  55. float start_time;
  56. int start_sample;
  57. const char *ws, *prepunc, *token, *postpunc;
  58. /* printf("in by word streaming\n"); */
  59. if (start == 0)
  60. ad = audio_open(w->sample_rate,w->num_channels,CST_AUDIO_LINEAR16);
  61. if (asi->item == NULL)
  62. asi->item = relation_head(utt_relation(asi->utt,"Token"));
  63. if (asi->item)
  64. {
  65. start_time = flite_ffeature_float(asi->item,"R:Token.daughter1.R:SylStructure.daughter1.daughter1.R:Segment.p.end");
  66. start_sample = (int)(start_time * (float)w->sample_rate);
  67. /* printf("start_time %f start_sample %d start %d\n",
  68. start_time,start_sample,start); */
  69. if ((start_sample >= start) &&
  70. (start_sample < start+size))
  71. {
  72. ws = flite_ffeature_string(asi->item,"whitespace");
  73. prepunc = flite_ffeature_string(asi->item,"prepunctuation");
  74. if (cst_streq("0",prepunc))
  75. prepunc = "";
  76. token = flite_ffeature_string(asi->item,"name");
  77. postpunc = flite_ffeature_string(asi->item,"punc");
  78. if (cst_streq("0",postpunc))
  79. postpunc = "";
  80. printf("%s%s%s%s",ws,prepunc,token,postpunc);
  81. fflush(stdout);
  82. asi->item = item_next(asi->item);
  83. }
  84. }
  85. n = audio_write(ad,&w->samples[start],size*sizeof(short));
  86. if (last == 1)
  87. {
  88. audio_close(ad);
  89. asi->item = NULL;
  90. ad = NULL;
  91. }
  92. /* if you want to stop return CST_AUDIO_STREAM_STOP */
  93. return CST_AUDIO_STREAM_CONT;
  94. }
  95. int main(int argc, char **argv)
  96. {
  97. cst_voice *v;
  98. cst_audio_streaming_info *asi;
  99. if (argc != 2)
  100. {
  101. fprintf(stderr,"usage: TEXTFILE\n");
  102. return 1;
  103. }
  104. flite_init();
  105. v = register_cmu_us_kal();
  106. asi = new_audio_streaming_info();
  107. asi->asc = audio_stream_chunk_by_word;
  108. feat_set(v->features,"streaming_info",audio_streaming_info_val(asi));
  109. flite_file_to_speech(argv[1],v,"none"); /* streaming will play */
  110. return 0;
  111. }