• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19 /*************************************************************************/
20 /*                                                                       */
21 /*                Centre for Speech Technology Research                  */
22 /*                     University of Edinburgh, UK                       */
23 /*                        Copyright (c) 1999                             */
24 /*                        All Rights Reserved.                           */
25 /*                                                                       */
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        */
38 /*      permission.                                                      */
39 /*                                                                       */
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       */
48 /*  THIS SOFTWARE.                                                       */
49 /*                                                                       */
50 /*************************************************************************/
51 /*             Author :  Alan W Black (awb@cstr.ed.ac.uk)                */
52 /*             Date   :  March 1999                                      */
53 /*-----------------------------------------------------------------------*/
54 /*                                                                       */
55 /* Client end of Festival server API in C designed specifically for      */
56 /* Galaxy Communicator use though might be of use for other things       */
57 /*                                                                       */
58 /* This is a modified version of the standalone client as provided in    */
59 /* festival example code: festival_client.c                              */
60 /*                                                                       */
61 /*=======================================================================*/
62 
63 /**
64  * SECTION:element-festival
65  * @title: festival
66  *
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`
71  *
72  * ## Example pipeline
73  * |[
74  * echo 'Hello G-Streamer!' | gst-launch-1.0 fdsrc fd=0 ! festival ! wavparse ! audioconvert ! alsasink
75  * ]|
76  *
77  */
78 
79 #ifdef HAVE_CONFIG_H
80 #include "config.h"
81 #endif
82 
83 #include <glib.h>               /* Needed for G_OS_XXXX macros */
84 
85 #include <stdio.h>
86 #include <stdlib.h>
87 
88 #ifdef HAVE_UNISTD_H
89 #include <unistd.h>
90 #endif
91 
92 #include <ctype.h>
93 #include <string.h>
94 #include <sys/types.h>
95 #ifdef G_OS_WIN32
96 #include <winsock2.h>
97 #include <ws2tcpip.h>
98 #else
99 #include <sys/socket.h>
100 #include <netdb.h>
101 #include <netinet/in.h>
102 #include <arpa/inet.h>
103 #endif
104 
105 #include "gstfestival.h"
106 #include <gst/audio/audio.h>
107 
108 GST_DEBUG_CATEGORY_STATIC (festival_debug);
109 #define GST_CAT_DEFAULT festival_debug
110 
111 static void gst_festival_finalize (GObject * object);
112 
113 static GstFlowReturn gst_festival_chain (GstPad * pad, GstObject * parent,
114     GstBuffer * buf);
115 static gboolean gst_festival_src_query (GstPad * pad, GstObject * parent,
116     GstQuery * query);
117 static GstStateChangeReturn gst_festival_change_state (GstElement * element,
118     GstStateChange transition);
119 
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);
123 
124 static GstStaticPadTemplate sink_template_factory =
125 GST_STATIC_PAD_TEMPLATE ("sink",
126     GST_PAD_SINK,
127     GST_PAD_ALWAYS,
128     GST_STATIC_CAPS ("text/x-raw, format=(string)utf8")
129     );
130 
131 static GstStaticPadTemplate src_template_factory =
132 GST_STATIC_PAD_TEMPLATE ("src",
133     GST_PAD_SRC,
134     GST_PAD_ALWAYS,
135     GST_STATIC_CAPS ("audio/x-wav")
136     );
137 
138 /* Festival signals and args */
139 enum
140 {
141   /* FILL ME */
142   LAST_SIGNAL
143 };
144 
145 enum
146 {
147   PROP_0
148       /* FILL ME */
149 };
150 
151 /*static guint gst_festival_signals[LAST_SIGNAL] = { 0 }; */
152 
153 G_DEFINE_TYPE (GstFestival, gst_festival, GST_TYPE_ELEMENT);
154 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (festival, "festival", GST_RANK_NONE,
155     GST_TYPE_FESTIVAL, GST_DEBUG_CATEGORY_INIT (festival_debug, "festival",
156         0, "Festival text-to-speech synthesizer");
157     );;
158 
159 static void
gst_festival_class_init(GstFestivalClass * klass)160 gst_festival_class_init (GstFestivalClass * klass)
161 {
162   GObjectClass *gobject_class;
163   GstElementClass *gstelement_class;
164 
165   gobject_class = G_OBJECT_CLASS (klass);
166   gstelement_class = GST_ELEMENT_CLASS (klass);
167 
168   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_festival_finalize);
169   gstelement_class->change_state =
170       GST_DEBUG_FUNCPTR (gst_festival_change_state);
171 
172   /* register pads */
173   gst_element_class_add_static_pad_template (gstelement_class,
174       &sink_template_factory);
175   gst_element_class_add_static_pad_template (gstelement_class,
176       &src_template_factory);
177 
178   gst_element_class_set_static_metadata (gstelement_class,
179       "Festival Text-to-Speech synthesizer", "Filter/Effect/Audio",
180       "Synthesizes plain text into audio",
181       "Wim Taymans <wim.taymans@gmail.com>");
182 }
183 
184 static void
gst_festival_init(GstFestival * festival)185 gst_festival_init (GstFestival * festival)
186 {
187   festival->sinkpad =
188       gst_pad_new_from_static_template (&sink_template_factory, "sink");
189   gst_pad_set_chain_function (festival->sinkpad, gst_festival_chain);
190   gst_element_add_pad (GST_ELEMENT (festival), festival->sinkpad);
191 
192   festival->srcpad =
193       gst_pad_new_from_static_template (&src_template_factory, "src");
194   gst_pad_set_query_function (festival->srcpad, gst_festival_src_query);
195   gst_element_add_pad (GST_ELEMENT (festival), festival->srcpad);
196 
197   festival->info = festival_default_info ();
198 }
199 
200 static void
gst_festival_finalize(GObject * object)201 gst_festival_finalize (GObject * object)
202 {
203   GstFestival *festival = GST_FESTIVAL (object);
204 
205   g_free (festival->info);
206 
207   G_OBJECT_CLASS (gst_festival_parent_class)->finalize (object);
208 }
209 
210 static gboolean
read_response(GstFestival * festival)211 read_response (GstFestival * festival)
212 {
213   char ack[4];
214   char *data;
215   int filesize;
216   int fd;
217   int n;
218   gboolean ret = TRUE;
219 
220   fd = festival->info->server_fd;
221   do {
222     for (n = 0; n < 3;)
223       n += read (fd, ack + n, 3 - n);
224     ack[3] = '\0';
225     GST_DEBUG_OBJECT (festival, "got response %s", ack);
226     if (strcmp (ack, "WV\n") == 0) {
227       GstBuffer *buffer;
228 
229       /* receive a waveform */
230       data = socket_receive_file_to_buff (fd, &filesize);
231       GST_DEBUG_OBJECT (festival, "received %d bytes of waveform data",
232           filesize);
233 
234       /* push contents as a buffer */
235       buffer = gst_buffer_new_wrapped (data, filesize);
236       GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
237       gst_pad_push (festival->srcpad, buffer);
238 
239     } else if (strcmp (ack, "LP\n") == 0) {
240       /* receive an s-expr */
241       data = client_accept_s_expr (fd);
242       GST_DEBUG_OBJECT (festival, "received s-expression: %s", data);
243       g_free (data);
244     } else if (strcmp (ack, "ER\n") == 0) {
245       /* server got an error */
246       GST_ELEMENT_ERROR (festival,
247           LIBRARY,
248           FAILED,
249           ("Festival speech server returned an error"),
250           ("Make sure you have voices/languages installed"));
251       ret = FALSE;
252       break;
253     }
254 
255   } while (strcmp (ack, "OK\n") != 0);
256 
257   return ret;
258 }
259 
260 static GstFlowReturn
gst_festival_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)261 gst_festival_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
262 {
263   GstFlowReturn ret = GST_FLOW_OK;
264   GstFestival *festival;
265   GstMapInfo info;
266   guint8 *p, *ep;
267   gint f;
268   FILE *fd;
269 
270   festival = GST_FESTIVAL (parent);
271 
272   GST_LOG_OBJECT (festival, "Got text buffer, %" G_GSIZE_FORMAT " bytes",
273       gst_buffer_get_size (buf));
274 
275   f = dup (festival->info->server_fd);
276   if (f < 0)
277     goto fail_open;
278   fd = fdopen (f, "wb");
279   if (fd == NULL) {
280     close (f);
281     goto fail_open;
282   }
283 
284   /* Copy text over to server, escaping any quotes */
285   fprintf (fd, "(Parameter.set 'Audio_Required_Rate 16000)\n");
286   fflush (fd);
287   GST_DEBUG_OBJECT (festival, "issued Parameter.set command");
288   if (read_response (festival) == FALSE) {
289     fclose (fd);
290     goto fail_read;
291   }
292 
293   fprintf (fd, "(tts_textall \"");
294   gst_buffer_map (buf, &info, GST_MAP_READ);
295   p = info.data;
296   ep = p + info.size;
297   for (; p < ep && (*p != '\0'); p++) {
298     if ((*p == '"') || (*p == '\\')) {
299       putc ('\\', fd);
300     }
301 
302     putc (*p, fd);
303   }
304   fprintf (fd, "\" \"%s\")\n", festival->info->text_mode);
305   fclose (fd);
306   gst_buffer_unmap (buf, &info);
307 
308   GST_DEBUG_OBJECT (festival, "issued tts_textall command");
309 
310   /* Read back info from server */
311   if (read_response (festival) == FALSE)
312     goto fail_read;
313 
314 out:
315   gst_buffer_unref (buf);
316   return ret;
317 
318   /* ERRORS */
319 fail_open:
320   {
321     GST_ELEMENT_ERROR (festival, RESOURCE, OPEN_WRITE, (NULL), (NULL));
322     ret = GST_FLOW_ERROR;
323     goto out;
324   }
325 fail_read:
326   {
327     GST_ELEMENT_ERROR (festival, RESOURCE, READ, (NULL), (NULL));
328     ret = GST_FLOW_ERROR;
329     goto out;
330   }
331 }
332 
333 static FT_Info *
festival_default_info(void)334 festival_default_info (void)
335 {
336   FT_Info *info;
337 
338   info = (FT_Info *) malloc (1 * sizeof (FT_Info));
339 
340   info->server_host = FESTIVAL_DEFAULT_SERVER_HOST;
341   info->server_port = FESTIVAL_DEFAULT_SERVER_PORT;
342   info->text_mode = FESTIVAL_DEFAULT_TEXT_MODE;
343 
344   info->server_fd = -1;
345 
346   return info;
347 }
348 
349 static int
festival_socket_open(const char * host,int port)350 festival_socket_open (const char *host, int port)
351 {
352   /* Return an FD to a remote server */
353   struct sockaddr_in serv_addr;
354   struct hostent *serverhost;
355   int fd;
356 
357   fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
358 
359   if (fd < 0) {
360     fprintf (stderr, "festival_client: can't get socket\n");
361     return -1;
362   }
363   memset (&serv_addr, 0, sizeof (serv_addr));
364   if ((serv_addr.sin_addr.s_addr = inet_addr (host)) == -1) {
365     /* its a name rather than an ipnum */
366     serverhost = gethostbyname (host);
367     if (serverhost == (struct hostent *) 0) {
368       fprintf (stderr, "festival_client: gethostbyname failed\n");
369       close (fd);
370       return -1;
371     }
372     memmove (&serv_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
373   }
374   serv_addr.sin_family = AF_INET;
375   serv_addr.sin_port = htons (port);
376 
377   if (connect (fd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) != 0) {
378     fprintf (stderr, "festival_client: connect to server failed\n");
379     close (fd);
380     return -1;
381   }
382 
383   return fd;
384 }
385 
386 static char *
client_accept_s_expr(int fd)387 client_accept_s_expr (int fd)
388 {
389   /* Read s-expression from server, as a char * */
390   char *expr;
391   int filesize;
392 
393   expr = socket_receive_file_to_buff (fd, &filesize);
394   expr[filesize] = '\0';
395 
396   return expr;
397 }
398 
399 static char *
socket_receive_file_to_buff(int fd,int * size)400 socket_receive_file_to_buff (int fd, int *size)
401 {
402   /* Receive file (probably a waveform file) from socket using   */
403   /* Festival key stuff technique, but long winded I know, sorry */
404   /* but will receive any file without closing the stream or    */
405   /* using OOB data                                              */
406   static const char file_stuff_key[] = "ft_StUfF_key";  /* must == Festival's key */
407   char *buff;
408   int bufflen;
409   int n, k, i;
410   char c;
411 
412   bufflen = 1024;
413   buff = (char *) g_malloc (bufflen);
414   *size = 0;
415 
416   for (k = 0; file_stuff_key[k] != '\0';) {
417     n = read (fd, &c, 1);
418     if (n == 0)
419       break;                    /* hit stream eof before end of file */
420 
421     if ((*size) + k + 1 >= bufflen) {
422       /* +1 so you can add a NULL if you want */
423       bufflen += bufflen / 4;
424       buff = (char *) g_realloc (buff, bufflen);
425     }
426     if (file_stuff_key[k] == c)
427       k++;
428     else if ((c == 'X') && (file_stuff_key[k + 1] == '\0')) {
429       /* It looked like the key but wasn't */
430       for (i = 0; i < k; i++, (*size)++)
431         buff[*size] = file_stuff_key[i];
432       k = 0;
433       /* omit the stuffed 'X' */
434     } else {
435       for (i = 0; i < k; i++, (*size)++)
436         buff[*size] = file_stuff_key[i];
437       k = 0;
438       buff[*size] = c;
439       (*size)++;
440     }
441   }
442 
443   return buff;
444 }
445 
446 /***********************************************************************/
447 /* Public Functions to this API                                        */
448 /***********************************************************************/
449 
450 static gboolean
gst_festival_open(GstFestival * festival)451 gst_festival_open (GstFestival * festival)
452 {
453   /* Open socket to server */
454   if (festival->info == NULL)
455     festival->info = festival_default_info ();
456 
457   festival->info->server_fd =
458       festival_socket_open (festival->info->server_host,
459       festival->info->server_port);
460   if (festival->info->server_fd == -1) {
461     GST_ERROR
462         ("Could not talk to festival server (no server running or wrong host/port?)");
463     return FALSE;
464   }
465   GST_OBJECT_FLAG_SET (festival, GST_FESTIVAL_OPEN);
466   return TRUE;
467 }
468 
469 static void
gst_festival_close(GstFestival * festival)470 gst_festival_close (GstFestival * festival)
471 {
472   if (festival->info == NULL)
473     return;
474 
475   if (festival->info->server_fd != -1)
476     close (festival->info->server_fd);
477   GST_OBJECT_FLAG_UNSET (festival, GST_FESTIVAL_OPEN);
478   return;
479 }
480 
481 static GstStateChangeReturn
gst_festival_change_state(GstElement * element,GstStateChange transition)482 gst_festival_change_state (GstElement * element, GstStateChange transition)
483 {
484   g_return_val_if_fail (GST_IS_FESTIVAL (element), GST_STATE_CHANGE_FAILURE);
485 
486   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
487     if (GST_OBJECT_FLAG_IS_SET (element, GST_FESTIVAL_OPEN)) {
488       GST_DEBUG ("Closing connection ");
489       gst_festival_close (GST_FESTIVAL (element));
490     }
491   } else {
492     if (!GST_OBJECT_FLAG_IS_SET (element, GST_FESTIVAL_OPEN)) {
493       GST_DEBUG ("Opening connection ");
494       if (!gst_festival_open (GST_FESTIVAL (element)))
495         return GST_STATE_CHANGE_FAILURE;
496     }
497   }
498 
499   if (GST_ELEMENT_CLASS (gst_festival_parent_class)->change_state)
500     return GST_ELEMENT_CLASS (gst_festival_parent_class)->change_state (element,
501         transition);
502 
503   return GST_STATE_CHANGE_SUCCESS;
504 }
505 
506 static gboolean
gst_festival_src_query(GstPad * pad,GstObject * parent,GstQuery * query)507 gst_festival_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
508 {
509   switch (GST_QUERY_TYPE (query)) {
510     case GST_QUERY_POSITION:
511       /* Not supported */
512       return FALSE;
513     case GST_QUERY_DURATION:
514       gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
515       return TRUE;
516     case GST_QUERY_SEEKING:
517       gst_query_set_seeking (query, GST_FORMAT_BYTES, FALSE, 0, -1);
518       return TRUE;
519     case GST_QUERY_FORMATS:
520       gst_query_set_formats (query, 1, GST_FORMAT_BYTES);
521       return TRUE;
522     default:
523       break;
524   }
525 
526   return gst_pad_query_default (pad, parent, query);
527 }
528 
529 static gboolean
plugin_init(GstPlugin * plugin)530 plugin_init (GstPlugin * plugin)
531 {
532   return GST_ELEMENT_REGISTER (festival, plugin);
533 }
534 
535 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
536     GST_VERSION_MINOR,
537     festival,
538     "Synthesizes plain text into audio",
539     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
540