altos/test: Adjust CRC error rate after FEC fix
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #include <stdio.h>
20 #include <flite/flite.h>
21 #include "aoview.h"
22 #include <alsa/asoundlib.h>
23
24 cst_voice *register_cmu_us_kal16();
25 cst_voice *register_cmu_us_kal();
26
27 static cst_voice *voice;
28
29 static FILE *pipe_write;
30 static GThread *aoview_flite_thread;
31
32 static snd_pcm_t        *alsa_handle;
33
34 gpointer
35 aoview_flite_task(gpointer data)
36 {
37         FILE            *input = data;
38         char            line[1024];
39         cst_wave        *wave;
40         int             rate;
41         int             channels;
42         int             err;
43         char            *samples;
44         int             num_samples;
45
46         err = snd_pcm_open(&alsa_handle, "default",
47                            SND_PCM_STREAM_PLAYBACK, 0);
48         if (err < 0) {
49                 fprintf(stderr, "alsa open failed %s\n",
50                         strerror(-err));
51                 alsa_handle = NULL;
52         }
53         rate = 0;
54         channels = 0;
55         while (fgets(line, sizeof (line) - 1, input) != NULL) {
56                 if (!alsa_handle)
57                         continue;
58                 wave = flite_text_to_wave(line, voice);
59                 if (wave->sample_rate != rate ||
60                     wave->num_channels != channels)
61                 {
62                         rate = wave->sample_rate;
63                         channels = wave->num_channels;
64                         err = snd_pcm_set_params(alsa_handle,
65                                                  SND_PCM_FORMAT_S16,
66                                                  SND_PCM_ACCESS_RW_INTERLEAVED,
67                                                  channels,
68                                                  rate,
69                                                  1,
70                                                  100000);
71                         if (err < 0)
72                                 fprintf(stderr, "alsa set_params error %s\n",
73                                         strerror(-err));
74                 }
75                 err = snd_pcm_prepare(alsa_handle);
76                 if (err < 0)
77                         fprintf(stderr, "alsa pcm_prepare error %s\n",
78                                 strerror(-err));
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);
84                         if (err <= 0) {
85                                 fprintf(stderr, "alsa write error %s\n",
86                                         strerror(-err));
87                                 break;
88                         }
89                         num_samples -= err;
90                         samples += err * 2 * channels;
91                 }
92                 snd_pcm_drain(alsa_handle);
93                 delete_wave(wave);
94         }
95         snd_pcm_close(alsa_handle);
96         alsa_handle = 0;
97         return NULL;
98 }
99
100 void
101 aoview_flite_stop(void)
102 {
103         int status;
104         if (pipe_write) {
105                 fclose(pipe_write);
106                 pipe_write = NULL;
107         }
108         if (aoview_flite_thread) {
109                 g_thread_join(aoview_flite_thread);
110                 aoview_flite_thread = NULL;
111         }
112 }
113
114 FILE *
115 aoview_flite_start(void)
116 {
117         static once;
118         int     p[2];
119         GError  *error;
120         FILE    *pipe_read;
121
122         if (!once) {
123                 flite_init();
124 #if HAVE_REGISTER_CMU_US_KAL16
125                 voice = register_cmu_us_kal16();
126 #else
127 #if HAVE_REGISTER_CMU_US_KAL
128                 voice = register_cmu_us_kal();
129 #endif
130 #endif
131                 if (!voice) {
132                         perror("register voice");
133                         exit(1);
134                 }
135         }
136         aoview_flite_stop();
137         pipe(p);
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);
141         return pipe_write;
142 }