0ada4e141bd5fd37cc3989b1e4357da80538714d
[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 #include <alsa/asoundlib.h>
22
23 cst_voice *register_cmu_us_kal();
24 static cst_voice *voice;
25
26 static FILE *pipe_write;
27 static GThread *aoview_flite_thread;
28
29 static snd_pcm_t        *alsa_handle;
30
31 gpointer
32 aoview_flite_task(gpointer data)
33 {
34         FILE            *input = data;
35         char            line[1024];
36         cst_wave        *wave;
37         int             rate;
38         int             channels;
39         int             err;
40
41         err = snd_pcm_open(&alsa_handle, "default",
42                            SND_PCM_STREAM_PLAYBACK, 0);
43         if (err >= 0)
44         {
45                 if (err < 0) {
46                         snd_pcm_close(alsa_handle);
47                         alsa_handle = 0;
48                 }
49         }
50         rate = 0;
51         channels = 0;
52         while (fgets(line, sizeof (line) - 1, input) != NULL) {
53                 if (!alsa_handle)
54                         continue;
55                 wave = flite_text_to_wave(line, voice);
56                 if (wave->sample_rate != rate ||
57                     wave->num_channels != channels)
58                 {
59                         rate = wave->sample_rate;
60                         channels = wave->num_channels;
61                         err = snd_pcm_set_params(alsa_handle,
62                                                  SND_PCM_FORMAT_S16,
63                                                  SND_PCM_ACCESS_RW_INTERLEAVED,
64                                                  channels,
65                                                  rate,
66                                                  1,
67                                                  100000);
68                         if (err < 0)
69                                 fprintf(stderr, "alsa set_params error %s\n",
70                                         strerror(-err));
71                 }
72                 err = snd_pcm_prepare(alsa_handle);
73                 if (err < 0)
74                         fprintf(stderr, "alsa pcm_prepare error %s\n",
75                                 strerror(-err));
76                 err = snd_pcm_writei(alsa_handle,
77                                      wave->samples,
78                                      wave->num_samples);
79                 if (err < 0)
80                         fprintf(stderr, "alsa write error %s\n",
81                                 strerror(-err));
82                 snd_pcm_drain(alsa_handle);
83                 delete_wave(wave);
84         }
85         snd_pcm_close(alsa_handle);
86         alsa_handle = 0;
87         return NULL;
88 }
89
90 void
91 aoview_flite_stop(void)
92 {
93         int status;
94         if (pipe_write) {
95                 fclose(pipe_write);
96                 pipe_write = NULL;
97         }
98         if (aoview_flite_thread) {
99                 g_thread_join(aoview_flite_thread);
100                 aoview_flite_thread = NULL;
101         }
102 }
103
104 FILE *
105 aoview_flite_start(void)
106 {
107         static once;
108         int     p[2];
109         GError  *error;
110         FILE    *pipe_read;
111
112         if (!once) {
113                 flite_init();
114                 voice = register_cmu_us_kal();
115                 if (!voice) {
116                         perror("register voice");
117                         exit(1);
118                 }
119         }
120         aoview_flite_stop();
121         pipe(p);
122         pipe_read = fdopen(p[0], "r");
123         pipe_write = fdopen(p[1], "w");
124         g_thread_create(aoview_flite_task, pipe_read, TRUE, &error);
125         return pipe_write;
126 }