SpeechSynthInterface.cpp

00001
00002 /***************************************************************************
00003  *  SpeechSynthInterface.cpp - Fawkes BlackBoard Interface - SpeechSynthInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <interfaces/SpeechSynthInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class SpeechSynthInterface <interfaces/SpeechSynthInterface.h>
00034  * SpeechSynthInterface Fawkes BlackBoard Interface.
00035  * 
00036       The interface provides access to a spech synthesizer facility.
00037       On systems that support this feature strings can be ordered for
00038       synthesis and audio output. Multiple messages ordering speech
00039       should be enqueued and processed one after another by providers.
00040     
00041  * @ingroup FawkesInterfaces
00042  */
00043
00044
00045 
00046 /** Constructor */
00047 SpeechSynthInterface::SpeechSynthInterface() : Interface()
00048 {
00049   data_size = sizeof(SpeechSynthInterface_data_t);
00050   data_ptr  = malloc(data_size);
00051   data      = (SpeechSynthInterface_data_t *)data_ptr;
00052   memset(data_ptr, 0, data_size);
00053   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00054   add_fieldinfo(IFT_UINT, "msgid", 1, &data->msgid);
00055   add_fieldinfo(IFT_BOOL, "final", 1, &data->final);
00056   add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
00057   add_messageinfo("SayMessage");
00058   unsigned char tmp_hash[] = {0xd4, 0x89, 0x24, 0x17, 0x5a, 0xb8, 0xa9, 0x8e, 0x63, 0x80, 0xb3, 0xed, 0xb7, 0xc3, 0xb5, 0x90};
00059   set_hash(tmp_hash);
00060 }
00061 
00062 /** Destructor */
00063 SpeechSynthInterface::~SpeechSynthInterface()
00064 {
00065   free(data_ptr);
00066 }
00067 /* Methods */
00068 /** Get text value.
00069  * 
00070       Last spoken string. Must be properly null-terminated.
00071     
00072  * @return text value
00073  */
00074 char *
00075 SpeechSynthInterface::text() const
00076 {
00077   return data->text;
00078 }
00079 
00080 /** Get maximum length of text value.
00081  * @return length of text value, can be length of the array or number of 
00082  * maximum number of characters for a string
00083  */
00084 size_t
00085 SpeechSynthInterface::maxlenof_text() const
00086 {
00087   return 1024;
00088 }
00089 
00090 /** Set text value.
00091  * 
00092       Last spoken string. Must be properly null-terminated.
00093     
00094  * @param new_text new text value
00095  */
00096 void
00097 SpeechSynthInterface::set_text(const char * new_text)
00098 {
00099   strncpy(data->text, new_text, sizeof(data->text));
00100 }
00101 
00102 /** Get msgid value.
00103  * 
00104       The ID of the message that is currently being processed,
00105       or 0 if no message is being processed.
00106     
00107  * @return msgid value
00108  */
00109 unsigned int
00110 SpeechSynthInterface::msgid() const
00111 {
00112   return data->msgid;
00113 }
00114 
00115 /** Get maximum length of msgid value.
00116  * @return length of msgid value, can be length of the array or number of 
00117  * maximum number of characters for a string
00118  */
00119 size_t
00120 SpeechSynthInterface::maxlenof_msgid() const
00121 {
00122   return 1;
00123 }
00124 
00125 /** Set msgid value.
00126  * 
00127       The ID of the message that is currently being processed,
00128       or 0 if no message is being processed.
00129     
00130  * @param new_msgid new msgid value
00131  */
00132 void
00133 SpeechSynthInterface::set_msgid(const unsigned int new_msgid)
00134 {
00135   data->msgid = new_msgid;
00136 }
00137 
00138 /** Get final value.
00139  * 
00140       True, if the last text has been spoken, false if it is still running.
00141     
00142  * @return final value
00143  */
00144 bool
00145 SpeechSynthInterface::is_final() const
00146 {
00147   return data->final;
00148 }
00149 
00150 /** Get maximum length of final value.
00151  * @return length of final value, can be length of the array or number of 
00152  * maximum number of characters for a string
00153  */
00154 size_t
00155 SpeechSynthInterface::maxlenof_final() const
00156 {
00157   return 1;
00158 }
00159 
00160 /** Set final value.
00161  * 
00162       True, if the last text has been spoken, false if it is still running.
00163     
00164  * @param new_final new final value
00165  */
00166 void
00167 SpeechSynthInterface::set_final(const bool new_final)
00168 {
00169   data->final = new_final;
00170 }
00171 
00172 /** Get duration value.
00173  * 
00174       Length in seconds that it takes to speek the current text, -1 if
00175       unknown. This is the total duration of the current string, *not* the
00176       duration of already spoken or yet to speak text!
00177     
00178  * @return duration value
00179  */
00180 float
00181 SpeechSynthInterface::duration() const
00182 {
00183   return data->duration;
00184 }
00185 
00186 /** Get maximum length of duration value.
00187  * @return length of duration value, can be length of the array or number of 
00188  * maximum number of characters for a string
00189  */
00190 size_t
00191 SpeechSynthInterface::maxlenof_duration() const
00192 {
00193   return 1;
00194 }
00195 
00196 /** Set duration value.
00197  * 
00198       Length in seconds that it takes to speek the current text, -1 if
00199       unknown. This is the total duration of the current string, *not* the
00200       duration of already spoken or yet to speak text!
00201     
00202  * @param new_duration new duration value
00203  */
00204 void
00205 SpeechSynthInterface::set_duration(const float new_duration)
00206 {
00207   data->duration = new_duration;
00208 }
00209
00210 /* =========== message create =========== */
00211 Message *
00212 SpeechSynthInterface::create_message(const char *type) const
00213 {
00214   if ( strncmp("SayMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00215     return new SayMessage();
00216   } else {
00217     throw UnknownTypeException("The given type '%s' does not match any known "
00218                                "message type for this interface type.", type);
00219   }
00220 }
00221
00222 
00223 /** Copy values from other interface.
00224  * @param other other interface to copy values from
00225  */
00226 void
00227 SpeechSynthInterface::copy_values(const Interface *other)
00228 {
00229   const SpeechSynthInterface *oi = dynamic_cast<const SpeechSynthInterface *>(other);
00230   if (oi == NULL) {
00231     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00232                                 type(), other->type());
00233   }
00234   memcpy(data, oi->data, sizeof(SpeechSynthInterface_data_t));
00235 }
00236
00237 /* =========== messages =========== */
00238 /** @class SpeechSynthInterface::SayMessage <interfaces/SpeechSynthInterface.h>
00239  * SayMessage Fawkes BlackBoard Interface Message.
00240  * 
00241     
00242  */
00243
00244 
00245 /** Constructor with initial values.
00246  * @param ini_text initial value for text
00247  */
00248 SpeechSynthInterface::SayMessage::SayMessage(const char * ini_text) : Message("SayMessage")
00249 {
00250   data_size = sizeof(SayMessage_data_t);
00251   data_ptr  = malloc(data_size);
00252   memset(data_ptr, 0, data_size);
00253   data      = (SayMessage_data_t *)data_ptr;
00254   strncpy(data->text, ini_text, 1024);
00255   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00256 }
00257 /** Constructor */
00258 SpeechSynthInterface::SayMessage::SayMessage() : Message("SayMessage")
00259 {
00260   data_size = sizeof(SayMessage_data_t);
00261   data_ptr  = malloc(data_size);
00262   memset(data_ptr, 0, data_size);
00263   data      = (SayMessage_data_t *)data_ptr;
00264   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00265 }
00266 
00267 /** Destructor */
00268 SpeechSynthInterface::SayMessage::~SayMessage()
00269 {
00270   free(data_ptr);
00271 }
00272 
00273 /** Copy constructor.
00274  * @param m message to copy from
00275  */
00276 SpeechSynthInterface::SayMessage::SayMessage(const SayMessage *m) : Message("SayMessage")
00277 {
00278   data_size = m->data_size;
00279   data_ptr  = malloc(data_size);
00280   memcpy(data_ptr, m->data_ptr, data_size);
00281   data      = (SayMessage_data_t *)data_ptr;
00282 }
00283
00284 /* Methods */
00285 /** Get text value.
00286  * 
00287       Last spoken string. Must be properly null-terminated.
00288     
00289  * @return text value
00290  */
00291 char *
00292 SpeechSynthInterface::SayMessage::text() const
00293 {
00294   return data->text;
00295 }
00296 
00297 /** Get maximum length of text value.
00298  * @return length of text value, can be length of the array or number of 
00299  * maximum number of characters for a string
00300  */
00301 size_t
00302 SpeechSynthInterface::SayMessage::maxlenof_text() const
00303 {
00304   return 1024;
00305 }
00306 
00307 /** Set text value.
00308  * 
00309       Last spoken string. Must be properly null-terminated.
00310     
00311  * @param new_text new text value
00312  */
00313 void
00314 SpeechSynthInterface::SayMessage::set_text(const char * new_text)
00315 {
00316   strncpy(data->text, new_text, sizeof(data->text));
00317 }
00318 
00319 /** Clone this message.
00320  * Produces a message of the same type as this message and copies the
00321  * data to the new message.
00322  * @return clone of this message
00323  */
00324 Message *
00325 SpeechSynthInterface::SayMessage::clone() const
00326 {
00327   return new SpeechSynthInterface::SayMessage(this);
00328 }
00329 /** Check if message is valid and can be enqueued.
00330  * @param message Message to check
00331  */
00332 bool
00333 SpeechSynthInterface::message_valid(const Message *message) const
00334 {
00335   const SayMessage *m0 = dynamic_cast<const SayMessage *>(message);
00336   if ( m0 != NULL ) {
00337     return true;
00338   }
00339   return false;
00340 }
00341 
00342 /// @cond INTERNALS
00343 EXPORT_INTERFACE(SpeechSynthInterface)
00344 /// @endcond
00345 
00346
00347 } // end namespace fawkes