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;
54 static gboolean speaking=FALSE;
55 static gboolean speak_ok;
56 static gboolean espeak_ok;
59 bus_call (GstBus *bus, GstMessage *msg, gpointer data)
64 switch (GST_MESSAGE_TYPE (msg)) {
71 case GST_MESSAGE_ERROR:
72 gst_message_parse_error (msg, &err, &debug);
75 g_printf ("Error: %s\n", err->message);
82 case GST_MESSAGE_STATE_CHANGED:
83 g_print ("GST: state changed\n");
86 g_printf("GST: %s\n", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
93 speak_create_pipeline(void)
95 ge.pipeline=gst_pipeline_new("pipeline");
96 ge.src=gst_element_factory_make("appsrc", "source");
97 ge.caps=gst_element_factory_make("capsfilter", "caps");
98 ge.srccaps=gst_caps_new_simple ("audio/x-raw-int",
99 "depth", G_TYPE_INT, 16,
100 "signed", G_TYPE_BOOLEAN, TRUE,
101 "width", G_TYPE_INT, 16,
102 "rate", G_TYPE_INT, erate,
103 "channels", G_TYPE_INT, 1,
106 ge.queue=gst_element_factory_make("queue", "queue");
107 ge.sink=gst_element_factory_make(AUDIO_SINK, "sink");
109 g_assert(ge.pipeline);
112 g_assert(ge.srccaps);
116 g_object_set(ge.caps, "caps", ge.srccaps, NULL);
118 gst_bin_add_many (GST_BIN(ge.pipeline), ge.src, ge.queue, ge.sink, NULL);
120 if (!gst_element_link_filtered(ge.src, ge.queue, ge.srccaps)) {
121 g_warning ("Failed to link elements 1!");
125 if (!gst_element_link_filtered(ge.queue, ge.sink, ge.srccaps)) {
126 g_warning ("Failed to link elements 2!");
134 espeak_buffer_free(void *p)
140 espeak_cb(short *wav, int numsamples, espeak_EVENT *events)
145 g_print("Adding buffer %d\n", numsamples);
148 gst_app_src_end_of_stream (GST_APP_SRC (ge.src));
150 } else if (numsamples>0) {
151 numsamples=numsamples*2;
152 data=g_memdup(wav, numsamples);
153 buf=gst_app_buffer_new (data, numsamples, espeak_buffer_free, data);
154 gst_buffer_set_caps(buf, ge.srccaps);
155 gst_app_src_push_buffer(GST_APP_SRC (ge.src), buf);
162 speak_set_parameters(guint speed, guint pitch)
164 espeak_SetParameter(espeakRATE, speed, 0);
165 espeak_SetParameter(espeakPITCH, pitch, 0);
169 speak_init(gchar *voice, guint speed, guint pitch)
172 erate=espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 100, NULL, 0);
176 espeak_SetSynthCallback(espeak_cb);
177 espeak_SetVoiceByName(voice);
178 speak_set_parameters(speed, pitch);
179 espeak_SetParameter(espeakVOLUME, 100, 0);
180 if (speak_create_pipeline()==FALSE)
183 bus=gst_pipeline_get_bus(GST_PIPELINE (ge.pipeline));
185 gst_bus_add_watch(bus, bus_call, NULL);
193 if (espeak_ok==FALSE)
196 gst_element_set_state(ge.pipeline, GST_STATE_NULL);
197 gst_object_unref(ge.pipeline);
204 speak_text(gchar *text)
211 g_printf("Speaking: %s (%d)\n", text, strlen(text));
212 espeak_Synth(text, strlen(text)+1, 0, POS_CHARACTER, 0, espeakCHARS_UTF8, NULL, NULL);
214 if (speaking!=TRUE) {
215 if (gst_element_set_state (ge.pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
216 g_print("Failed to play\n");
222 g_print("Playing...\n");
235 if (gst_element_set_state(ge.pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE)
242 gboolean speak_init(gchar voice, guint speed, guint pitch) {return TRUE;}
243 void speak_deinit(void) {}
244 gboolean speak_stop(void) {return TRUE;}
245 gboolean speak_speaking(void) {return FALSE;}
246 void speak_set_parameters(guint speed, guint pitch) {}
249 speak_text(gchar *text)
252 #define _voice_synth_path "/usr/bin/espeak"
254 #define _voice_synth_path "/usr/bin/flite"
258 /* We are the fork child. Synthesize the voice. */
261 printf("%s %s\n", _voice_synth_path, text);
263 execl(_voice_synth_path, _voice_synth_path, "-t", text, (char *)NULL);
265 execl(_voice_synth_path, _voice_synth_path, "-s", "150", text, (char *)NULL);