2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
19 /*************************************************************************/
21 /* Centre for Speech Technology Research */
22 /* University of Edinburgh, UK */
23 /* Copyright (c) 1999 */
24 /* All Rights Reserved. */
26 /* Permission is hereby granted, free of charge, to use and distribute */
27 /* this software and its documentation without restriction, including */
28 /* without limitation the rights to use, copy, modify, merge, publish, */
29 /* distribute, sublicense, and/or sell copies of this work, and to */
30 /* permit persons to whom this work is furnished to do so, subject to */
31 /* the following conditions: */
32 /* 1. The code must retain the above copyright notice, this list of */
33 /* conditions and the following disclaimer. */
34 /* 2. Any modifications must be clearly marked as such. */
35 /* 3. Original authors' names are not deleted. */
36 /* 4. The authors' names are not used to endorse or promote products */
37 /* derived from this software without specific prior written */
40 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
41 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
42 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
43 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
44 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
45 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
46 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
47 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
50 /*************************************************************************/
51 /* Author : Alan W Black (awb@cstr.ed.ac.uk) */
52 /* Date : March 1999 */
53 /*-----------------------------------------------------------------------*/
55 /* Client end of Festival server API in C designed specifically for */
56 /* Galaxy Communicator use though might be of use for other things */
58 /* This is a modified version of the standalone client as provided in */
59 /* festival example code: festival_client.c */
61 /*=======================================================================*/
64 * SECTION:element-festival
67 * This element connects to a
68 * [festival](http://www.festvox.org/festival/index.html) server process and
69 * uses it to synthesize speech. Festival need to run already in server mode,
70 * started as `festival --server`
74 * echo 'Hello G-Streamer!' | gst-launch-1.0 fdsrc fd=0 ! festival ! wavparse ! audioconvert ! alsasink
83 #include <glib.h> /* Needed for G_OS_XXXX macros */
94 #include <sys/types.h>
99 #include <sys/socket.h>
101 #include <netinet/in.h>
102 #include <arpa/inet.h>
105 #include "gstfestival.h"
106 #include <gst/audio/audio.h>
108 GST_DEBUG_CATEGORY_STATIC (festival_debug);
109 #define GST_CAT_DEFAULT festival_debug
111 static void gst_festival_finalize (GObject * object);
113 static GstFlowReturn gst_festival_chain (GstPad * pad, GstObject * parent,
115 static gboolean gst_festival_src_query (GstPad * pad, GstObject * parent,
117 static GstStateChangeReturn gst_festival_change_state (GstElement * element,
118 GstStateChange transition);
120 static FT_Info *festival_default_info (void);
121 static char *socket_receive_file_to_buff (int fd, int *size);
122 static char *client_accept_s_expr (int fd);
124 static GstStaticPadTemplate sink_template_factory =
125 GST_STATIC_PAD_TEMPLATE ("sink",
128 GST_STATIC_CAPS ("text/x-raw, format=(string)utf8")
131 static GstStaticPadTemplate src_template_factory =
132 GST_STATIC_PAD_TEMPLATE ("src",
135 GST_STATIC_CAPS ("audio/x-wav")
138 /* Festival signals and args */
151 /*static guint gst_festival_signals[LAST_SIGNAL] = { 0 }; */
153 G_DEFINE_TYPE (GstFestival, gst_festival, GST_TYPE_ELEMENT)
155 static void gst_festival_class_init (GstFestivalClass * klass)
157 GObjectClass *gobject_class;
158 GstElementClass *gstelement_class;
160 gobject_class = G_OBJECT_CLASS (klass);
161 gstelement_class = GST_ELEMENT_CLASS (klass);
163 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_festival_finalize);
164 gstelement_class->change_state =
165 GST_DEBUG_FUNCPTR (gst_festival_change_state);
168 gst_element_class_add_static_pad_template (gstelement_class,
169 &sink_template_factory);
170 gst_element_class_add_static_pad_template (gstelement_class,
171 &src_template_factory);
173 gst_element_class_set_static_metadata (gstelement_class,
174 "Festival Text-to-Speech synthesizer", "Filter/Effect/Audio",
175 "Synthesizes plain text into audio",
176 "Wim Taymans <wim.taymans@gmail.com>");
180 gst_festival_init (GstFestival * festival)
183 gst_pad_new_from_static_template (&sink_template_factory, "sink");
184 gst_pad_set_chain_function (festival->sinkpad, gst_festival_chain);
185 gst_element_add_pad (GST_ELEMENT (festival), festival->sinkpad);
188 gst_pad_new_from_static_template (&src_template_factory, "src");
189 gst_pad_set_query_function (festival->srcpad, gst_festival_src_query);
190 gst_element_add_pad (GST_ELEMENT (festival), festival->srcpad);
192 festival->info = festival_default_info ();
196 gst_festival_finalize (GObject * object)
198 GstFestival *festival = GST_FESTIVAL (object);
200 g_free (festival->info);
202 G_OBJECT_CLASS (gst_festival_parent_class)->finalize (object);
206 read_response (GstFestival * festival)
215 fd = festival->info->server_fd;
218 n += read (fd, ack + n, 3 - n);
220 GST_DEBUG_OBJECT (festival, "got response %s", ack);
221 if (strcmp (ack, "WV\n") == 0) {
224 /* receive a waveform */
225 data = socket_receive_file_to_buff (fd, &filesize);
226 GST_DEBUG_OBJECT (festival, "received %d bytes of waveform data",
229 /* push contents as a buffer */
230 buffer = gst_buffer_new_wrapped (data, filesize);
231 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
232 gst_pad_push (festival->srcpad, buffer);
234 } else if (strcmp (ack, "LP\n") == 0) {
235 /* receive an s-expr */
236 data = client_accept_s_expr (fd);
237 GST_DEBUG_OBJECT (festival, "received s-expression: %s", data);
239 } else if (strcmp (ack, "ER\n") == 0) {
240 /* server got an error */
241 GST_ELEMENT_ERROR (festival,
244 ("Festival speech server returned an error"),
245 ("Make sure you have voices/languages installed"));
250 } while (strcmp (ack, "OK\n") != 0);
256 gst_festival_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
258 GstFlowReturn ret = GST_FLOW_OK;
259 GstFestival *festival;
265 festival = GST_FESTIVAL (parent);
267 GST_LOG_OBJECT (festival, "Got text buffer, %" G_GSIZE_FORMAT " bytes",
268 gst_buffer_get_size (buf));
270 f = dup (festival->info->server_fd);
273 fd = fdopen (f, "wb");
279 /* Copy text over to server, escaping any quotes */
280 fprintf (fd, "(Parameter.set 'Audio_Required_Rate 16000)\n");
282 GST_DEBUG_OBJECT (festival, "issued Parameter.set command");
283 if (read_response (festival) == FALSE) {
288 fprintf (fd, "(tts_textall \"");
289 gst_buffer_map (buf, &info, GST_MAP_READ);
292 for (; p < ep && (*p != '\0'); p++) {
293 if ((*p == '"') || (*p == '\\')) {
299 fprintf (fd, "\" \"%s\")\n", festival->info->text_mode);
301 gst_buffer_unmap (buf, &info);
303 GST_DEBUG_OBJECT (festival, "issued tts_textall command");
305 /* Read back info from server */
306 if (read_response (festival) == FALSE)
310 gst_buffer_unref (buf);
316 GST_ELEMENT_ERROR (festival, RESOURCE, OPEN_WRITE, (NULL), (NULL));
317 ret = GST_FLOW_ERROR;
322 GST_ELEMENT_ERROR (festival, RESOURCE, READ, (NULL), (NULL));
323 ret = GST_FLOW_ERROR;
329 festival_default_info (void)
333 info = (FT_Info *) malloc (1 * sizeof (FT_Info));
335 info->server_host = FESTIVAL_DEFAULT_SERVER_HOST;
336 info->server_port = FESTIVAL_DEFAULT_SERVER_PORT;
337 info->text_mode = FESTIVAL_DEFAULT_TEXT_MODE;
339 info->server_fd = -1;
345 festival_socket_open (const char *host, int port)
347 /* Return an FD to a remote server */
348 struct sockaddr_in serv_addr;
349 struct hostent *serverhost;
352 fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
355 fprintf (stderr, "festival_client: can't get socket\n");
358 memset (&serv_addr, 0, sizeof (serv_addr));
359 if ((serv_addr.sin_addr.s_addr = inet_addr (host)) == -1) {
360 /* its a name rather than an ipnum */
361 serverhost = gethostbyname (host);
362 if (serverhost == (struct hostent *) 0) {
363 fprintf (stderr, "festival_client: gethostbyname failed\n");
367 memmove (&serv_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
369 serv_addr.sin_family = AF_INET;
370 serv_addr.sin_port = htons (port);
372 if (connect (fd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) != 0) {
373 fprintf (stderr, "festival_client: connect to server failed\n");
382 client_accept_s_expr (int fd)
384 /* Read s-expression from server, as a char * */
388 expr = socket_receive_file_to_buff (fd, &filesize);
389 expr[filesize] = '\0';
395 socket_receive_file_to_buff (int fd, int *size)
397 /* Receive file (probably a waveform file) from socket using */
398 /* Festival key stuff technique, but long winded I know, sorry */
399 /* but will receive any file without closeing the stream or */
401 static const char file_stuff_key[] = "ft_StUfF_key"; /* must == Festival's key */
408 buff = (char *) g_malloc (bufflen);
411 for (k = 0; file_stuff_key[k] != '\0';) {
412 n = read (fd, &c, 1);
414 break; /* hit stream eof before end of file */
416 if ((*size) + k + 1 >= bufflen) {
417 /* +1 so you can add a NULL if you want */
418 bufflen += bufflen / 4;
419 buff = (char *) g_realloc (buff, bufflen);
421 if (file_stuff_key[k] == c)
423 else if ((c == 'X') && (file_stuff_key[k + 1] == '\0')) {
424 /* It looked like the key but wasn't */
425 for (i = 0; i < k; i++, (*size)++)
426 buff[*size] = file_stuff_key[i];
428 /* omit the stuffed 'X' */
430 for (i = 0; i < k; i++, (*size)++)
431 buff[*size] = file_stuff_key[i];
441 /***********************************************************************/
442 /* Public Functions to this API */
443 /***********************************************************************/
446 gst_festival_open (GstFestival * festival)
448 /* Open socket to server */
449 if (festival->info == NULL)
450 festival->info = festival_default_info ();
452 festival->info->server_fd =
453 festival_socket_open (festival->info->server_host,
454 festival->info->server_port);
455 if (festival->info->server_fd == -1) {
457 ("Could not talk to festival server (no server running or wrong host/port?)");
460 GST_OBJECT_FLAG_SET (festival, GST_FESTIVAL_OPEN);
465 gst_festival_close (GstFestival * festival)
467 if (festival->info == NULL)
470 if (festival->info->server_fd != -1)
471 close (festival->info->server_fd);
472 GST_OBJECT_FLAG_UNSET (festival, GST_FESTIVAL_OPEN);
476 static GstStateChangeReturn
477 gst_festival_change_state (GstElement * element, GstStateChange transition)
479 g_return_val_if_fail (GST_IS_FESTIVAL (element), GST_STATE_CHANGE_FAILURE);
481 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
482 if (GST_OBJECT_FLAG_IS_SET (element, GST_FESTIVAL_OPEN)) {
483 GST_DEBUG ("Closing connection ");
484 gst_festival_close (GST_FESTIVAL (element));
487 if (!GST_OBJECT_FLAG_IS_SET (element, GST_FESTIVAL_OPEN)) {
488 GST_DEBUG ("Opening connection ");
489 if (!gst_festival_open (GST_FESTIVAL (element)))
490 return GST_STATE_CHANGE_FAILURE;
494 if (GST_ELEMENT_CLASS (gst_festival_parent_class)->change_state)
495 return GST_ELEMENT_CLASS (gst_festival_parent_class)->change_state (element,
498 return GST_STATE_CHANGE_SUCCESS;
502 gst_festival_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
504 switch (GST_QUERY_TYPE (query)) {
505 case GST_QUERY_POSITION:
508 case GST_QUERY_DURATION:
509 gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
511 case GST_QUERY_SEEKING:
512 gst_query_set_seeking (query, GST_FORMAT_BYTES, FALSE, 0, -1);
514 case GST_QUERY_FORMATS:
515 gst_query_set_formats (query, 1, GST_FORMAT_BYTES);
521 return gst_pad_query_default (pad, parent, query);
525 plugin_init (GstPlugin * plugin)
527 GST_DEBUG_CATEGORY_INIT (festival_debug, "festival",
528 0, "Festival text-to-speech synthesizer");
530 if (!gst_element_register (plugin, "festival", GST_RANK_NONE,
537 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
540 "Synthesizes plain text into audio",
541 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);