Hook aoview directly to alsa
[fw/altos] / aoview / aoview_flite.c
index bca19043083f0cc6078d1699313623617d851c7e..2673824dbdcfaf0780babfd20093fa4a0ca775a6 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <flite/flite.h>
 #include "aoview.h"
+#include <alsa/asoundlib.h>
 
 cst_voice *register_cmu_us_kal();
 static cst_voice *voice;
@@ -25,14 +26,58 @@ static cst_voice *voice;
 static FILE *pipe_write;
 static GThread *aoview_flite_thread;
 
+static snd_pcm_t       *alsa_handle;
+
 gpointer
 aoview_flite_task(gpointer data)
 {
        FILE            *input = data;
        char            line[1024];
+       cst_wave        *wave;
+       int             rate;
+       int             channels;
+       int             err;
 
-       while (fgets(line, sizeof (line) - 1, input) != NULL)
-               flite_text_to_speech(line, voice, "play");
+       err = snd_pcm_open(&alsa_handle, "default",
+                          SND_PCM_STREAM_PLAYBACK, 0);
+       if (err >= 0)
+       {
+               if (err < 0) {
+                       snd_pcm_close(alsa_handle);
+                       alsa_handle = 0;
+               }
+       }
+       rate = 0;
+       channels = 0;
+       while (fgets(line, sizeof (line) - 1, input) != NULL) {
+               if (!alsa_handle)
+                       continue;
+               wave = flite_text_to_wave(line, voice);
+               if (wave->sample_rate != rate ||
+                   wave->num_channels != channels)
+               {
+                       rate = wave->sample_rate;
+                       channels = wave->num_channels;
+                       snd_pcm_set_params(alsa_handle,
+                                          SND_PCM_FORMAT_S16,
+                                          SND_PCM_ACCESS_RW_INTERLEAVED,
+                                          channels,
+                                          rate,
+                                          1,
+                                          100000);
+               }
+               snd_pcm_prepare(alsa_handle);
+               err = snd_pcm_writei(alsa_handle,
+                                    wave->samples,
+                                    wave->num_samples);
+               if (err < 0)
+                       fprintf(stderr, "alsa write error %s\n",
+                               strerror(-err));
+               snd_pcm_drain(alsa_handle);
+               delete_wave(wave);
+       }
+       snd_pcm_close(alsa_handle);
+       alsa_handle = 0;
        return NULL;
 }