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 
42 #include <stdio.h>
43 #include <string.h>
44 #include "flite.h"
45 
46 cst_voice *register_cmu_us_kal();
47 
audio_stream_chunk_by_word(const cst_wave * w,int start,int size,int last,cst_audio_streaming_info * asi)48 int audio_stream_chunk_by_word(const cst_wave *w, int start, int size,
49                                int last, cst_audio_streaming_info *asi)
50 {
51     /* Called with new samples from start for size samples */
52     /* last is true if this is the last segment. */
53     /* This is really just an example that you can copy for your streaming */
54     /* function */
55     /* This particular example is *not* thread safe */
56     int n;
57     static cst_audiodev *ad = 0;
58     float start_time;
59     int start_sample;
60     const char *ws, *prepunc, *token, *postpunc;
61 
62     /*    printf("in by word streaming\n"); */
63 
64     if (start == 0)
65         ad = audio_open(w->sample_rate,w->num_channels,CST_AUDIO_LINEAR16);
66 
67     if (asi->item == NULL)
68         asi->item = relation_head(utt_relation(asi->utt,"Token"));
69     if (asi->item)
70     {
71         start_time = flite_ffeature_float(asi->item,"R:Token.daughter1.R:SylStructure.daughter1.daughter1.R:Segment.p.end");
72         start_sample = (int)(start_time * (float)w->sample_rate);
73         /*        printf("start_time %f start_sample %d start %d\n",
74                   start_time,start_sample,start); */
75         if ((start_sample >= start) &&
76             (start_sample < start+size))
77         {
78             ws = flite_ffeature_string(asi->item,"whitespace");
79             prepunc = flite_ffeature_string(asi->item,"prepunctuation");
80             if (cst_streq("0",prepunc))
81                 prepunc = "";
82             token = flite_ffeature_string(asi->item,"name");
83             postpunc = flite_ffeature_string(asi->item,"punc");
84             if (cst_streq("0",postpunc))
85                 postpunc = "";
86             printf("%s%s%s%s",ws,prepunc,token,postpunc);
87             fflush(stdout);
88             asi->item = item_next(asi->item);
89         }
90 
91     }
92     n = audio_write(ad,&w->samples[start],size*sizeof(short));
93 
94     if (last == 1)
95     {
96         audio_close(ad);
97         asi->item = NULL;
98         ad = NULL;
99     }
100 
101     /* if you want to stop return CST_AUDIO_STREAM_STOP */
102     return CST_AUDIO_STREAM_CONT;
103 }
104 
105 
106 
main(int argc,char ** argv)107 int main(int argc, char **argv)
108 {
109     cst_voice *v;
110     cst_audio_streaming_info *asi;
111 
112     if (argc != 2)
113     {
114 	fprintf(stderr,"usage: TEXTFILE\n");
115 	return 1;
116     }
117 
118     flite_init();
119 
120     v = register_cmu_us_kal();
121 
122     asi = new_audio_streaming_info();
123     asi->asc = audio_stream_chunk_by_word;
124     feat_set(v->features,"streaming_info",audio_streaming_info_val(asi));
125 
126     flite_file_to_speech(argv[1],v,"none"); /* streaming will play */
127 
128     return 0;
129 }
130