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