From fef7334bddb9fccfbd6deab7d5d466ab3e76323a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 11 Jul 2009 00:56:13 -0700 Subject: [PATCH] Hook aoview directly to alsa This skips the flite internal audio stuff which opened and closed the audio device for each phrase. This caused the first part of some phrases to be missed when using an external audio device. Signed-off-by: Keith Packard --- aoview/Makefile.am | 4 ++-- aoview/aoview_flite.c | 49 +++++++++++++++++++++++++++++++++++++++++-- aoview/aoview_voice.c | 2 ++ configure.ac | 2 ++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/aoview/Makefile.am b/aoview/Makefile.am index c354aa0f..00851b67 100644 --- a/aoview/Makefile.am +++ b/aoview/Makefile.am @@ -1,9 +1,9 @@ VERSION=$(shell git describe) -AM_CFLAGS=$(GNOME_CFLAGS) -I$(top_srcdir)/src -DAOVIEW_VERSION=\"$(VERSION)\" @FLITE_INCS@ +AM_CFLAGS=$(GNOME_CFLAGS) $(ALSA_CFLAGS) -I$(top_srcdir)/src -DAOVIEW_VERSION=\"$(VERSION)\" @FLITE_INCS@ bin_PROGRAMS=aoview -aoview_LDADD=$(GNOME_LIBS) $(FLITE_LIBS) +aoview_LDADD=$(GNOME_LIBS) $(FLITE_LIBS) $(ALSA_LIBS) aoview_SOURCES = \ aoview_main.c \ diff --git a/aoview/aoview_flite.c b/aoview/aoview_flite.c index bca19043..2673824d 100644 --- a/aoview/aoview_flite.c +++ b/aoview/aoview_flite.c @@ -18,6 +18,7 @@ #include #include #include "aoview.h" +#include 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; } diff --git a/aoview/aoview_voice.c b/aoview/aoview_voice.c index f7c099b1..24422df6 100644 --- a/aoview/aoview_voice.c +++ b/aoview/aoview_voice.c @@ -24,6 +24,8 @@ FILE *aoview_flite; void aoview_voice_open(void) { + int err; + if (!aoview_flite) aoview_flite = aoview_flite_start(); } diff --git a/configure.ac b/configure.ac index f55c7ec8..495e1185 100644 --- a/configure.ac +++ b/configure.ac @@ -57,6 +57,8 @@ PKG_CHECK_MODULES([GNOME], [gtk+-2.0 libglade-2.0 gconf-2.0]) PKG_CHECK_MODULES([LIBUSB], [libusb-1.0]) +PKG_CHECK_MODULES([ALSA], [alsa]) + AC_OUTPUT([ Makefile aoview/Makefile -- 2.30.2