bca19043083f0cc6078d1699313623617d851c7e
[fw/altos] / aoview / aoview_flite.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <stdio.h>
19 #include <flite/flite.h>
20 #include "aoview.h"
21
22 cst_voice *register_cmu_us_kal();
23 static cst_voice *voice;
24
25 static FILE *pipe_write;
26 static GThread *aoview_flite_thread;
27
28 gpointer
29 aoview_flite_task(gpointer data)
30 {
31         FILE            *input = data;
32         char            line[1024];
33
34         while (fgets(line, sizeof (line) - 1, input) != NULL)
35                 flite_text_to_speech(line, voice, "play");
36         return NULL;
37 }
38
39 void
40 aoview_flite_stop(void)
41 {
42         int status;
43         if (pipe_write) {
44                 fclose(pipe_write);
45                 pipe_write = NULL;
46         }
47         if (aoview_flite_thread) {
48                 g_thread_join(aoview_flite_thread);
49                 aoview_flite_thread = NULL;
50         }
51 }
52
53 FILE *
54 aoview_flite_start(void)
55 {
56         static once;
57         int     p[2];
58         GError  *error;
59         FILE    *pipe_read;
60
61         if (!once) {
62                 flite_init();
63                 voice = register_cmu_us_kal();
64                 if (!voice) {
65                         perror("register voice");
66                         exit(1);
67                 }
68         }
69         aoview_flite_stop();
70         pipe(p);
71         pipe_read = fdopen(p[0], "r");
72         pipe_write = fdopen(p[1], "w");
73         g_thread_create(aoview_flite_task, pipe_read, TRUE, &error);
74         return pipe_write;
75 }