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   data_ts   = (interface_data_ts_t *)data_ptr;
00053   memset(data_ptr, 0, data_size);
00054   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00055   add_fieldinfo(IFT_UINT32, "msgid", 1, &data->msgid);
00056   add_fieldinfo(IFT_BOOL, "final", 1, &data->final);
00057   add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
00058   add_messageinfo("SayMessage");
00059   unsigned char tmp_hash[] = {0x28, 0x11, 0x46, 0x87, 0xb1, 0x65, 0x92, 0x96, 0xe6, 0x6e, 0x18, 0x8a, 0xdc, 0x8, 0xb0, 0x69};
00060   set_hash(tmp_hash);
00061 }
00062 
00063 /** Destructor */
00064 SpeechSynthInterface::~SpeechSynthInterface()
00065 {
00066   free(data_ptr);
00067 }
00068 /* Methods */
00069 /** Get text value.
00070  * 
00071       Last spoken string. Must be properly null-terminated.
00072     
00073  * @return text value
00074  */
00075 char *
00076 SpeechSynthInterface::text() const
00077 {
00078   return data->text;
00079 }
00080 
00081 /** Get maximum length of text value.
00082  * @return length of text value, can be length of the array or number of 
00083  * maximum number of characters for a string
00084  */
00085 size_t
00086 SpeechSynthInterface::maxlenof_text() const
00087 {
00088   return 1024;
00089 }
00090 
00091 /** Set text value.
00092  * 
00093       Last spoken string. Must be properly null-terminated.
00094     
00095  * @param new_text new text value
00096  */
00097 void
00098 SpeechSynthInterface::set_text(const char * new_text)
00099 {
00100   strncpy(data->text, new_text, sizeof(data->text));
00101   data_changed = true;
00102 }
00103 
00104 /** Get msgid value.
00105  * 
00106       The ID of the message that is currently being processed,
00107       or 0 if no message is being processed.
00108     
00109  * @return msgid value
00110  */
00111 uint32_t
00112 SpeechSynthInterface::msgid() const
00113 {
00114   return data->msgid;
00115 }
00116 
00117 /** Get maximum length of msgid value.
00118  * @return length of msgid value, can be length of the array or number of 
00119  * maximum number of characters for a string
00120  */
00121 size_t
00122 SpeechSynthInterface::maxlenof_msgid() const
00123 {
00124   return 1;
00125 }
00126 
00127 /** Set msgid value.
00128  * 
00129       The ID of the message that is currently being processed,
00130       or 0 if no message is being processed.
00131     
00132  * @param new_msgid new msgid value
00133  */
00134 void
00135 SpeechSynthInterface::set_msgid(const uint32_t new_msgid)
00136 {
00137   data->msgid = new_msgid;
00138   data_changed = true;
00139 }
00140 
00141 /** Get final value.
00142  * 
00143       True, if the last text has been spoken, false if it is still running.
00144     
00145  * @return final value
00146  */
00147 bool
00148 SpeechSynthInterface::is_final() const
00149 {
00150   return data->final;
00151 }
00152 
00153 /** Get maximum length of final value.
00154  * @return length of final value, can be length of the array or number of 
00155  * maximum number of characters for a string
00156  */
00157 size_t
00158 SpeechSynthInterface::maxlenof_final() const
00159 {
00160   return 1;
00161 }
00162 
00163 /** Set final value.
00164  * 
00165       True, if the last text has been spoken, false if it is still running.
00166     
00167  * @param new_final new final value
00168  */
00169 void
00170 SpeechSynthInterface::set_final(const bool new_final)
00171 {
00172   data->final = new_final;
00173   data_changed = true;
00174 }
00175 
00176 /** Get duration value.
00177  * 
00178       Length in seconds that it takes to speek the current text, -1 if
00179       unknown. This is the total duration of the current string, *not* the
00180       duration of already spoken or yet to speak text!
00181     
00182  * @return duration value
00183  */
00184 float
00185 SpeechSynthInterface::duration() const
00186 {
00187   return data->duration;
00188 }
00189 
00190 /** Get maximum length of duration value.
00191  * @return length of duration value, can be length of the array or number of 
00192  * maximum number of characters for a string
00193  */
00194 size_t
00195 SpeechSynthInterface::maxlenof_duration() const
00196 {
00197   return 1;
00198 }
00199 
00200 /** Set duration value.
00201  * 
00202       Length in seconds that it takes to speek the current text, -1 if
00203       unknown. This is the total duration of the current string, *not* the
00204       duration of already spoken or yet to speak text!
00205     
00206  * @param new_duration new duration value
00207  */
00208 void
00209 SpeechSynthInterface::set_duration(const float new_duration)
00210 {
00211   data->duration = new_duration;
00212   data_changed = true;
00213 }
00214 
00215 /* =========== message create =========== */
00216 Message *
00217 SpeechSynthInterface::create_message(const char *type) const
00218 {
00219   if ( strncmp("SayMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00220     return new SayMessage();
00221   } else {
00222     throw UnknownTypeException("The given type '%s' does not match any known "
00223                                "message type for this interface type.", type);
00224   }
00225 }
00226 
00227 
00228 /** Copy values from other interface.
00229  * @param other other interface to copy values from
00230  */
00231 void
00232 SpeechSynthInterface::copy_values(const Interface *other)
00233 {
00234   const SpeechSynthInterface *oi = dynamic_cast<const SpeechSynthInterface *>(other);
00235   if (oi == NULL) {
00236     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00237                                 type(), other->type());
00238   }
00239   memcpy(data, oi->data, sizeof(SpeechSynthInterface_data_t));
00240 }
00241 
00242 const char *
00243 SpeechSynthInterface::enum_tostring(const char *enumtype, int val) const
00244 {
00245   throw UnknownTypeException("Unknown enum type %s", enumtype);
00246 }
00247 
00248 /* =========== messages =========== */
00249 /** @class SpeechSynthInterface::SayMessage <interfaces/SpeechSynthInterface.h>
00250  * SayMessage Fawkes BlackBoard Interface Message.
00251  * 
00252     
00253  */
00254 
00255 
00256 /** Constructor with initial values.
00257  * @param ini_text initial value for text
00258  */
00259 SpeechSynthInterface::SayMessage::SayMessage(const char * ini_text) : Message("SayMessage")
00260 {
00261   data_size = sizeof(SayMessage_data_t);
00262   data_ptr  = malloc(data_size);
00263   memset(data_ptr, 0, data_size);
00264   data      = (SayMessage_data_t *)data_ptr;
00265   data_ts   = (message_data_ts_t *)data_ptr;
00266   strncpy(data->text, ini_text, 1024);
00267   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00268 }
00269 /** Constructor */
00270 SpeechSynthInterface::SayMessage::SayMessage() : Message("SayMessage")
00271 {
00272   data_size = sizeof(SayMessage_data_t);
00273   data_ptr  = malloc(data_size);
00274   memset(data_ptr, 0, data_size);
00275   data      = (SayMessage_data_t *)data_ptr;
00276   data_ts   = (message_data_ts_t *)data_ptr;
00277   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00278 }
00279 
00280 /** Destructor */
00281 SpeechSynthInterface::SayMessage::~SayMessage()
00282 {
00283   free(data_ptr);
00284 }
00285 
00286 /** Copy constructor.
00287  * @param m message to copy from
00288  */
00289 SpeechSynthInterface::SayMessage::SayMessage(const SayMessage *m) : Message("SayMessage")
00290 {
00291   data_size = m->data_size;
00292   data_ptr  = malloc(data_size);
00293   memcpy(data_ptr, m->data_ptr, data_size);
00294   data      = (SayMessage_data_t *)data_ptr;
00295   data_ts   = (message_data_ts_t *)data_ptr;
00296 }
00297 
00298 /* Methods */
00299 /** Get text value.
00300  * 
00301       Last spoken string. Must be properly null-terminated.
00302     
00303  * @return text value
00304  */
00305 char *
00306 SpeechSynthInterface::SayMessage::text() const
00307 {
00308   return data->text;
00309 }
00310 
00311 /** Get maximum length of text value.
00312  * @return length of text value, can be length of the array or number of 
00313  * maximum number of characters for a string
00314  */
00315 size_t
00316 SpeechSynthInterface::SayMessage::maxlenof_text() const
00317 {
00318   return 1024;
00319 }
00320 
00321 /** Set text value.
00322  * 
00323       Last spoken string. Must be properly null-terminated.
00324     
00325  * @param new_text new text value
00326  */
00327 void
00328 SpeechSynthInterface::SayMessage::set_text(const char * new_text)
00329 {
00330   strncpy(data->text, new_text, sizeof(data->text));
00331 }
00332 
00333 /** Clone this message.
00334  * Produces a message of the same type as this message and copies the
00335  * data to the new message.
00336  * @return clone of this message
00337  */
00338 Message *
00339 SpeechSynthInterface::SayMessage::clone() const
00340 {
00341   return new SpeechSynthInterface::SayMessage(this);
00342 }
00343 /** Check if message is valid and can be enqueued.
00344  * @param message Message to check
00345  * @return true if the message is valid, false otherwise.
00346  */
00347 bool
00348 SpeechSynthInterface::message_valid(const Message *message) const
00349 {
00350   const SayMessage *m0 = dynamic_cast<const SayMessage *>(message);
00351   if ( m0 != NULL ) {
00352     return true;
00353   }
00354   return false;
00355 }
00356 
00357 /// @cond INTERNALS
00358 EXPORT_INTERFACE(SpeechSynthInterface)
00359 /// @endcond
00360 
00361 
00362 } // end namespace fawkes