2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <flite/flite.h>
21 #include <alsa/asoundlib.h>
23 cst_voice *register_cmu_us_kal16();
24 static cst_voice *voice;
26 static FILE *pipe_write;
27 static GThread *aoview_flite_thread;
29 static snd_pcm_t *alsa_handle;
32 aoview_flite_task(gpointer data)
43 err = snd_pcm_open(&alsa_handle, "default",
44 SND_PCM_STREAM_PLAYBACK, 0);
46 fprintf(stderr, "alsa open failed %s\n",
52 while (fgets(line, sizeof (line) - 1, input) != NULL) {
55 wave = flite_text_to_wave(line, voice);
56 if (wave->sample_rate != rate ||
57 wave->num_channels != channels)
59 rate = wave->sample_rate;
60 channels = wave->num_channels;
61 err = snd_pcm_set_params(alsa_handle,
63 SND_PCM_ACCESS_RW_INTERLEAVED,
69 fprintf(stderr, "alsa set_params error %s\n",
72 err = snd_pcm_prepare(alsa_handle);
74 fprintf(stderr, "alsa pcm_prepare error %s\n",
76 samples = (char *) wave->samples;
77 num_samples = wave->num_samples;
78 while (num_samples > 0) {
79 err = snd_pcm_writei(alsa_handle,
80 samples, num_samples);
82 fprintf(stderr, "alsa write error %s\n",
87 samples += err * 2 * channels;
89 snd_pcm_drain(alsa_handle);
92 snd_pcm_close(alsa_handle);
98 aoview_flite_stop(void)
105 if (aoview_flite_thread) {
106 g_thread_join(aoview_flite_thread);
107 aoview_flite_thread = NULL;
112 aoview_flite_start(void)
121 voice = register_cmu_us_kal16();
123 perror("register voice");
129 pipe_read = fdopen(p[0], "r");
130 pipe_write = fdopen(p[1], "w");
131 g_thread_create(aoview_flite_task, pipe_read, TRUE, &error);