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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <flite/flite.h>
22 #include <alsa/asoundlib.h>
24 cst_voice *register_cmu_us_kal16();
25 cst_voice *register_cmu_us_kal();
27 static cst_voice *voice;
29 static FILE *pipe_write;
30 static GThread *aoview_flite_thread;
32 static snd_pcm_t *alsa_handle;
35 aoview_flite_task(gpointer data)
46 err = snd_pcm_open(&alsa_handle, "default",
47 SND_PCM_STREAM_PLAYBACK, 0);
49 fprintf(stderr, "alsa open failed %s\n",
55 while (fgets(line, sizeof (line) - 1, input) != NULL) {
58 wave = flite_text_to_wave(line, voice);
59 if (wave->sample_rate != rate ||
60 wave->num_channels != channels)
62 rate = wave->sample_rate;
63 channels = wave->num_channels;
64 err = snd_pcm_set_params(alsa_handle,
66 SND_PCM_ACCESS_RW_INTERLEAVED,
72 fprintf(stderr, "alsa set_params error %s\n",
75 err = snd_pcm_prepare(alsa_handle);
77 fprintf(stderr, "alsa pcm_prepare error %s\n",
79 samples = (char *) wave->samples;
80 num_samples = wave->num_samples;
81 while (num_samples > 0) {
82 err = snd_pcm_writei(alsa_handle,
83 samples, num_samples);
85 fprintf(stderr, "alsa write error %s\n",
90 samples += err * 2 * channels;
92 snd_pcm_drain(alsa_handle);
95 snd_pcm_close(alsa_handle);
101 aoview_flite_stop(void)
108 if (aoview_flite_thread) {
109 g_thread_join(aoview_flite_thread);
110 aoview_flite_thread = NULL;
115 aoview_flite_start(void)
124 #if HAVE_REGISTER_CMU_US_KAL16
125 voice = register_cmu_us_kal16();
127 #if HAVE_REGISTER_CMU_US_KAL
128 voice = register_cmu_us_kal();
132 perror("register voice");
138 pipe_read = fdopen(p[0], "r");
139 pipe_write = fdopen(p[1], "w");
140 g_thread_create(aoview_flite_task, pipe_read, TRUE, &error);