File: flowm_flite.c

package info (click to toggle)
flite 2.1-release-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 83,704 kB
  • sloc: ansic: 693,308; sh: 4,998; lisp: 3,013; cpp: 994; makefile: 905; xml: 52; perl: 33
file content (418 lines) | stat: -rw-r--r-- 13,261 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*************************************************************************/
/*                                                                       */
/*                  Language Technologies Institute                      */
/*                     Carnegie Mellon University                        */
/*                         Copyright (c) 2009                            */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission is hereby granted, free of charge, to use and distribute  */
/*  this software and its documentation without restriction, including   */
/*  without limitation the rights to use, copy, modify, merge, publish,  */
/*  distribute, sublicense, and/or sell copies of this work, and to      */
/*  permit persons to whom this work is furnished to do so, subject to   */
/*  the following conditions:                                            */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*   4. The authors' names are not used to endorse or promote products   */
/*      derived from this software without specific prior written        */
/*      permission.                                                      */
/*                                                                       */
/*  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*             Author:  Alan W Black (awb@cs.cmu.edu)                    */
/*               Date:  January 2009                                     */
/*************************************************************************/
/*                                                                       */
/*  flowm functions for flite access                                     */
/*                                                                       */
/*************************************************************************/

#include <windows.h>
#include <commctrl.h>                
#include <aygshell.h>

#include "cst_wchar.h"
#include "flite.h"
#include "flowm.h"

/* For debugging its sometimes good to switch off the actual synthesis */
#define DOTTS 1 

static cst_audiodev *fl_ad = 0;

#ifdef DOTTS
cst_voice *register_cmu_us_kal(const char *voxdir);
void unregister_cmu_us_kal(cst_voice *v);
cst_voice *register_cmu_us_awb(const char *voxdir);
void unregister_cmu_us_awb(cst_voice *v);
cst_voice *register_cmu_us_rms(const char *voxdir);
void unregister_cmu_us_rms(cst_voice *v);
cst_voice *register_cmu_us_slt(const char *voxdir);
void unregister_cmu_us_slt(cst_voice *v);
#endif

cst_wave *previous_wave = NULL;

typedef struct VoxDef_struct
{
    TCHAR *name;
    cst_voice *(*rv)(const char *voxdir);  /* register_voice */
    void (*urv)(cst_voice *v);             /* unregister_voice */
    int min_buffsize;                      /* for audio streaming */
    cst_voice *v;
} VoxDef;

VoxDef VoxDefs[] = {
#ifdef cmu_us_kal
    { L"kal", register_cmu_us_kal, unregister_cmu_us_kal, 256, NULL },
#endif
#ifdef cmu_us_awb
    { L"awb", register_cmu_us_awb, unregister_cmu_us_awb, 2000, NULL },
#endif
#ifdef cmu_us_rms
    { L"rms", register_cmu_us_rms, unregister_cmu_us_rms, 2000, NULL },
#endif
#ifdef cmu_us_slt
    { L"slt", register_cmu_us_slt, unregister_cmu_us_slt, 2000, NULL },
#endif
    { NULL, NULL }
};

cst_utterance *flowm_print_relation_callback(cst_utterance *u);
cst_utterance *flowm_utt_callback(cst_utterance *u);
int flowm_audio_stream_chunk(const cst_wave *w, int start, int size, 
                             int last, cst_audio_streaming_info *asi);

float flowm_find_file_percentage()
{
    if (flowm_file_size <= 0)
        return 0.0;
    else
        return (flowm_file_pos*100.0)/flowm_file_size;
}

TCHAR *flowm_voice_name(int i)
{
    /* In order not to have flite things in flowm_main, we provide an */
    /* interface to the voice list */
    return VoxDefs[i].name;
}

void flowm_init()
{
#ifdef DOTTS
    int i;
    cst_audio_streaming_info *asi;

    flite_init();        /* Initialize flite interface */

    for (i=0; VoxDefs[i].name; i++)
    {
        VoxDefs[i].v = (VoxDefs[i].rv)(NULL); /* register voice */

        /* Set up call back function for low level audio streaming */
        /* This way it plays the waveform as it synthesizes it */
        /* This is necessary for the slower (CG) voices */
        asi = new_audio_streaming_info();
        asi->asc = flowm_audio_stream_chunk;
        asi->min_buffsize = VoxDefs[i].min_buffsize;
        feat_set(VoxDefs[i].v->features,
                 "streaming_info",
                 audio_streaming_info_val(asi));

        /* Set up call back function for sending what tokens are being */
        /* synthesized and for keeping track of the current position in */
        /* the file */
        feat_set(VoxDefs[i].v->features,
                 "utt_user_callback",
                 uttfunc_val(flowm_utt_callback));

        /* For outputing results of a relation (only used in play) */
        feat_set(VoxDefs[i].v->features,
                 "post_synth_hook_func",
                 uttfunc_val(flowm_print_relation_callback));
    }

#endif
    return;
}

void flowm_terminate()
{
#ifdef DOTTS
    int i;

    for (i=0; VoxDefs[i].name; i++)
    {
        (VoxDefs[i].urv)(VoxDefs[i].v); /* unregister voice */
    }
#endif
    if (previous_wave)
    {
        delete_wave(previous_wave);
        previous_wave = NULL;
    }
        
    return;
}

int flowm_save_wave(TCHAR *filename)
{
    /* Save the Last synthesized waveform file to filename */
    char *sfilename;
    int rc;

    if (!previous_wave)
        return -1;

    sfilename = cst_wstr2cstr(filename);
    rc = cst_wave_save_riff(previous_wave,sfilename);
    cst_free(sfilename);

    return rc;
}

#ifdef DOTTS
int flowm_say_text(TCHAR *text)
{
    char *s;
    int ns;
    cst_voice *v;

    if (previous_wave)
    {
        delete_wave(previous_wave);
        previous_wave = NULL;
    }

    s = cst_wstr2cstr(text);               /* text to synthesize */
    v = VoxDefs[flowm_selected_voice].v;   /* voice to synthesize with */

    feat_remove(v->features,"print_info_relation");
    if (flowm_selected_relation == 1)
        feat_set_string(v->features, "print_info_relation", "Word");
    if (flowm_selected_relation == 2)
        feat_set_string(v->features, "print_info_relation", "Segment");

    /* Do the synthesis */
    previous_wave = flite_text_to_wave(s,v);

    ns = cst_wave_num_samples(previous_wave);

    cst_free(s);
    audio_flush(fl_ad);
    audio_close(fl_ad); 
    fl_ad = NULL;

    return ns;
}
#else
int flowm_say_text(TCHAR *text)
{
    MessageBoxW(0,text,L"SayText",0);
    return 0;
}
#endif

cst_utterance *flowm_print_relation_callback(cst_utterance *u)
{
    /* Say the details of a named relation for display */
    char rst[FL_MAX_MSG_CHARS];
    const char *name;
    const char *relname;
    cst_item *item;
    char *space;

    space = "";
    relname = get_param_string(u->features,"print_info_relation", NULL);
    cst_sprintf(rst,"%s: ",relname);

    if (!relname)
    {
        mbstowcs(fl_tts_msg,"",FL_MAX_MSG_CHARS);
        return u;
    }

    for (item=relation_head(utt_relation(u,relname)); 
         item; item=item_next(item))
    {
        name = item_feat_string(item,"name");
        
        if (cst_strlen(name)+1+4 < FL_MAX_MSG_CHARS)
            cst_sprintf(rst,"%s%s%s",rst,space,name);
        else if (cst_strlen(rst)+4 < FL_MAX_MSG_CHARS)
            cst_sprintf(rst,"%s ...",rst);
        else
            break;
        space = " ";
    }
    mbstowcs(fl_tts_msg,rst,FL_MAX_MSG_CHARS);

    return u;
}

cst_utterance *flowm_utt_callback(cst_utterance *u)
{
    char rst[FL_MAX_MSG_CHARS];
    const char *tok;
    cst_item *item;
    char *space;
    int extend_length;
    
    /* In order to stop the synthesizer if the STOP button is pressed */
    /* This stops the synthesis of the next utterance */

    if ((flowm_play_status == FLOWM_PLAY) ||
        (flowm_play_status == FLOWM_SKIP))
    {
        if (TTSWindow)
        {
            rst[0] = '\0';
            space = "";
            for (item=relation_head(utt_relation(u,"Token")); 
                 item; item=item_next(item))
            {
                tok = item_feat_string(item,"name");
                if (cst_streq("",space))
                    /* Only do this on the first token/word */
                    flowm_file_pos = item_feat_int(item,"file_pos");
                extend_length = cst_strlen(rst) + 1 +
                    cst_strlen(item_feat_string(item,"prepunctuation"))+
                    cst_strlen(item_feat_string(item,"punc"));
                if (cst_strlen(tok)+extend_length+4 < FL_MAX_MSG_CHARS)
                    cst_sprintf(rst,"%s%s%s%s%s",rst,space,
                                item_feat_string(item,"prepunctuation"),
                                tok,
                                item_feat_string(item,"punc"));
                else 
                {
                    if (cst_strlen(rst)+4 < FL_MAX_MSG_CHARS)
                        cst_sprintf(rst,"%s ...",rst);
                    break;
                }
                space = " ";
            }

            if (flowm_file_pos > flowm_prev_utt_pos[flowm_utt_pos_pos])
            {
                if ((flowm_utt_pos_pos+1) >= FLOWM_NUM_UTT_POS)
                {
                    /* Filled it up, so move it down */
                    memmove(flowm_prev_utt_pos,&flowm_prev_utt_pos[1],
                            sizeof(int)*(FLOWM_NUM_UTT_POS-10));
                    flowm_utt_pos_pos = (FLOWM_NUM_UTT_POS-10);
                }
                flowm_utt_pos_pos++;
                flowm_prev_utt_pos[flowm_utt_pos_pos] = flowm_file_pos;
            }

            /* Send text to TTSWindow */
            mbstowcs(fl_tts_msg,rst,FL_MAX_MSG_CHARS);
            SetDlgItemText(TTSWindow, FL_SYNTHTEXT, fl_tts_msg);

            /* Update file pos percentage in FilePos window */
            cst_sprintf(rst,"%2.3f",flowm_find_file_percentage());
            mbstowcs(fl_fp_msg,rst,FL_MAX_MSG_CHARS);
            SetDlgItemText(TTSWindow, FL_FILEPOS, fl_fp_msg);

            SystemIdleTimerReset();  /* keep alive while synthesizing */
            if (flowm_play_status == FLOWM_SKIP)
                flowm_play_status = FLOWM_PLAY;
        }
        return u;
    }
    else
    {
        delete_utterance(u);
        return 0;
    }
}

int flowm_audio_stream_chunk(const cst_wave *w, int start, int size, 
                             int last, cst_audio_streaming_info *asi)
{

    if (fl_ad == NULL)
    {
        fl_ad = audio_open(w->sample_rate,w->num_channels,CST_AUDIO_LINEAR16);
    }

    if (flowm_play_status == FLOWM_PLAY)
    {
        audio_write(fl_ad,&w->samples[start],size*sizeof(short));
        return CST_AUDIO_STREAM_CONT;
    }
    else if (flowm_play_status == FLOWM_BENCH)
    {   /* Do TTS but don't actually play it */
        /* How much have we played */
        flowm_duration += (size*1.0)/w->sample_rate;
        return CST_AUDIO_STREAM_CONT;
    }
    else
    {   /* for STOP, and the SKIPS (if they get here) */
        return CST_AUDIO_STREAM_STOP;
    }
}

#ifdef DOTTS
int flowm_say_file(TCHAR *tfilename)
{
    int rc = 0;
    char *filename;
    cst_voice *v;
    
    if (previous_wave)
    {   /* This is really tidy up from Play -- but might say space */
        delete_wave(previous_wave);
        previous_wave = NULL;
    }

    if (fl_ad)
    {
        MessageBoxW(0,L"audio fd still open",L"SayFile",0);
        audio_close(fl_ad); 
        fl_ad = NULL;
    }

    v = VoxDefs[flowm_selected_voice].v;

    /* Where we want to start from */
    feat_set_int(v->features, "file_start_position", flowm_file_pos);

    /* Only do print_info in play mode */
    feat_remove(v->features,"print_info_relation");

    filename = cst_wstr2cstr(tfilename);
    rc = flite_file_to_speech(filename, v, "stream");
    cst_free(filename);

    audio_flush(fl_ad);
    audio_close(fl_ad); 
    fl_ad = NULL;

    return rc;

}
#else
int flowm_say_file(TCHAR *text)
{
    MessageBoxW(0,text,L"SayFile",0);
    return 0;
}
#endif