2 * This file is part of mapper
4 * Copyright (C) 2007 Kaj-Michael Lang
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <glib/gstdio.h>
27 #if defined (WITH_GST) && defined (WITH_ESPEAK)
30 #include <gst/app/gstappsrc.h>
31 #include <gst/app/gstappbuffer.h>
32 #include <gst/app/gstappsink.h>
33 #include <espeak/speak_lib.h>
38 typedef struct _gst_espeak gst_espeak;
53 static gboolean speaking=FALSE;
54 static gboolean speak_ok;
55 static gboolean espeak_ok;
58 bus_call (GstBus *bus, GstMessage *msg, gpointer data)
63 switch (GST_MESSAGE_TYPE (msg)) {
70 case GST_MESSAGE_ERROR:
71 gst_message_parse_error (msg, &err, &debug);
74 g_debug("Error: %s", err->message);
81 case GST_MESSAGE_STATE_CHANGED:
82 g_debug("GST: state changed");
85 g_debug("GST: %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
92 speak_create_pipeline(void)
94 ge.pipeline=gst_pipeline_new("pipeline");
95 g_assert(ge.pipeline);
97 ge.src=gst_element_factory_make("appsrc", "source");
100 ge.srccaps=gst_caps_new_simple ("audio/x-raw-int",
101 "depth", G_TYPE_INT, 16,
102 "signed", G_TYPE_BOOLEAN, TRUE,
103 "width", G_TYPE_INT, 16,
104 "rate", G_TYPE_INT, erate,
105 "channels", G_TYPE_INT, 1,
107 g_assert(ge.srccaps);
109 ge.queue=gst_element_factory_make("queue", "queue");
112 ge.sink=gst_element_factory_make(AUDIO_SINK, "sink");
115 gst_bin_add_many (GST_BIN(ge.pipeline), ge.src, ge.queue, ge.sink, NULL);
117 if (!gst_element_link_filtered(ge.src, ge.queue, ge.srccaps)) {
118 g_warning ("Failed to link source to queue with caps.");
121 gst_caps_unref(ge.srccaps);
123 if (!gst_element_link(ge.queue, ge.sink)) {
124 g_warning ("Failed to link queue to sink.");
132 espeak_buffer_free(void *p)
138 espeak_cb(short *wav, int numsamples, espeak_EVENT *events)
143 g_print("Adding buffer %d\n", numsamples);
146 gst_app_src_end_of_stream(GST_APP_SRC (ge.src));
148 } else if (numsamples>0) {
149 numsamples=numsamples*2;
150 data=g_memdup(wav, numsamples);
151 buf=gst_app_buffer_new(data, numsamples, espeak_buffer_free, data);
152 gst_buffer_set_caps(buf, ge.srccaps);
153 gst_app_src_push_buffer(GST_APP_SRC (ge.src), buf);
160 speak_set_parameters(guint speed, guint pitch)
162 espeak_SetParameter(espeakRATE, speed, 0);
163 espeak_SetParameter(espeakPITCH, pitch, 0);
167 speak_init(gchar *voice, guint speed, guint pitch)
170 erate=espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 100, NULL, 0);
174 espeak_SetSynthCallback(espeak_cb);
175 espeak_SetVoiceByName(voice);
176 speak_set_parameters(speed, pitch);
177 espeak_SetParameter(espeakVOLUME, 100, 0);
178 if (speak_create_pipeline()==FALSE)
181 bus=gst_pipeline_get_bus(GST_PIPELINE (ge.pipeline));
183 gst_bus_add_watch(bus, bus_call, NULL);
192 if (espeak_ok==FALSE)
195 gst_element_set_state(ge.pipeline, GST_STATE_NULL);
196 gst_object_unref(ge.pipeline);
203 speak_text(gchar *text)
210 g_debug("Speaking: %s (%u)\n", text, (guint)strlen(text));
211 espeak_Synth(text, strlen(text)+1, 0, POS_CHARACTER, 0, espeakCHARS_UTF8, NULL, NULL);
213 if (speaking!=TRUE) {
214 if (gst_element_set_state (ge.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
215 g_print("Failed to play\n");
221 g_print("Playing...\n");
234 if (gst_element_set_state(ge.pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE)
241 gboolean speak_init(gchar voice, guint speed, guint pitch) {return TRUE;}
242 void speak_deinit(void) {}
243 gboolean speak_stop(void) {return TRUE;}
244 gboolean speak_speaking(void) {return FALSE;}
245 void speak_set_parameters(guint speed, guint pitch) {}
248 speak_text(gchar *text)
251 #define _voice_synth_path "/usr/bin/espeak"
253 #define _voice_synth_path "/usr/bin/flite"
257 /* We are the fork child. Synthesize the voice. */
260 g_debug("%s %s\n", _voice_synth_path, text);
262 execl(_voice_synth_path, _voice_synth_path, "-t", text, (char *)NULL);
264 execl(_voice_synth_path, _voice_synth_path, "-s", "150", text, (char *)NULL);