Fawkes API  Fawkes Development Version
SpeechRecognitionInterface.cpp
00001 
00002 /***************************************************************************
00003  *  SpeechRecognitionInterface.cpp - Fawkes BlackBoard Interface - SpeechRecognitionInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2009  Tim Niemueller and Masrur Doostdar
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/SpeechRecognitionInterface.h>
00025 
00026 #include <core/exceptions/software.h>
00027 
00028 #include <cstring>
00029 #include <cstdlib>
00030 
00031 namespace fawkes {
00032 
00033 /** @class SpeechRecognitionInterface <interfaces/SpeechRecognitionInterface.h>
00034  * SpeechRecognitionInterface Fawkes BlackBoard Interface.
00035  * 
00036       The interface provides access to a spech recognition facility.
00037     
00038  * @ingroup FawkesInterfaces
00039  */
00040 
00041 
00042 
00043 /** Constructor */
00044 SpeechRecognitionInterface::SpeechRecognitionInterface() : Interface()
00045 {
00046   data_size = sizeof(SpeechRecognitionInterface_data_t);
00047   data_ptr  = malloc(data_size);
00048   data      = (SpeechRecognitionInterface_data_t *)data_ptr;
00049   data_ts   = (interface_data_ts_t *)data_ptr;
00050   memset(data_ptr, 0, data_size);
00051   add_fieldinfo(IFT_STRING, "text", 1024, data->text);
00052   add_fieldinfo(IFT_UINT32, "counter", 1, &data->counter);
00053   add_fieldinfo(IFT_BOOL, "processing", 1, &data->processing);
00054   add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00055   add_messageinfo("ResetMessage");
00056   add_messageinfo("SetEnabledMessage");
00057   unsigned char tmp_hash[] = {0x8f, 0x5c, 0xd, 0x42, 0x1b, 0x22, 0x75, 0x3d, 0x50, 0x66, 0x70, 0x8, 0x1f, 0x47, 0xa7, 0xfd};
00058   set_hash(tmp_hash);
00059 }
00060 
00061 /** Destructor */
00062 SpeechRecognitionInterface::~SpeechRecognitionInterface()
00063 {
00064   free(data_ptr);
00065 }
00066 /* Methods */
00067 /** Get text value.
00068  * 
00069       Last spoken string. Must be properly null-terminated.
00070     
00071  * @return text value
00072  */
00073 char *
00074 SpeechRecognitionInterface::text() const
00075 {
00076   return data->text;
00077 }
00078 
00079 /** Get maximum length of text value.
00080  * @return length of text value, can be length of the array or number of 
00081  * maximum number of characters for a string
00082  */
00083 size_t
00084 SpeechRecognitionInterface::maxlenof_text() const
00085 {
00086   return 1024;
00087 }
00088 
00089 /** Set text value.
00090  * 
00091       Last spoken string. Must be properly null-terminated.
00092     
00093  * @param new_text new text value
00094  */
00095 void
00096 SpeechRecognitionInterface::set_text(const char * new_text)
00097 {
00098   strncpy(data->text, new_text, sizeof(data->text));
00099   data_changed = true;
00100 }
00101 
00102 /** Get counter value.
00103  * 
00104       Counter for messages. Increased after each new recognized string.
00105     
00106  * @return counter value
00107  */
00108 uint32_t
00109 SpeechRecognitionInterface::counter() const
00110 {
00111   return data->counter;
00112 }
00113 
00114 /** Get maximum length of counter value.
00115  * @return length of counter value, can be length of the array or number of 
00116  * maximum number of characters for a string
00117  */
00118 size_t
00119 SpeechRecognitionInterface::maxlenof_counter() const
00120 {
00121   return 1;
00122 }
00123 
00124 /** Set counter value.
00125  * 
00126       Counter for messages. Increased after each new recognized string.
00127     
00128  * @param new_counter new counter value
00129  */
00130 void
00131 SpeechRecognitionInterface::set_counter(const uint32_t new_counter)
00132 {
00133   data->counter = new_counter;
00134   data_changed = true;
00135 }
00136 
00137 /** Get processing value.
00138  * 
00139       True, if the the speech recognition is currently processing.
00140     
00141  * @return processing value
00142  */
00143 bool
00144 SpeechRecognitionInterface::is_processing() const
00145 {
00146   return data->processing;
00147 }
00148 
00149 /** Get maximum length of processing value.
00150  * @return length of processing value, can be length of the array or number of 
00151  * maximum number of characters for a string
00152  */
00153 size_t
00154 SpeechRecognitionInterface::maxlenof_processing() const
00155 {
00156   return 1;
00157 }
00158 
00159 /** Set processing value.
00160  * 
00161       True, if the the speech recognition is currently processing.
00162     
00163  * @param new_processing new processing value
00164  */
00165 void
00166 SpeechRecognitionInterface::set_processing(const bool new_processing)
00167 {
00168   data->processing = new_processing;
00169   data_changed = true;
00170 }
00171 
00172 /** Get enabled value.
00173  * 
00174       True, if speech processing is currently enabled, false otherwise.
00175     
00176  * @return enabled value
00177  */
00178 bool
00179 SpeechRecognitionInterface::is_enabled() const
00180 {
00181   return data->enabled;
00182 }
00183 
00184 /** Get maximum length of enabled value.
00185  * @return length of enabled value, can be length of the array or number of 
00186  * maximum number of characters for a string
00187  */
00188 size_t
00189 SpeechRecognitionInterface::maxlenof_enabled() const
00190 {
00191   return 1;
00192 }
00193 
00194 /** Set enabled value.
00195  * 
00196       True, if speech processing is currently enabled, false otherwise.
00197     
00198  * @param new_enabled new enabled value
00199  */
00200 void
00201 SpeechRecognitionInterface::set_enabled(const bool new_enabled)
00202 {
00203   data->enabled = new_enabled;
00204   data_changed = true;
00205 }
00206 
00207 /* =========== message create =========== */
00208 Message *
00209 SpeechRecognitionInterface::create_message(const char *type) const
00210 {
00211   if ( strncmp("ResetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00212     return new ResetMessage();
00213   } else if ( strncmp("SetEnabledMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00214     return new SetEnabledMessage();
00215   } else {
00216     throw UnknownTypeException("The given type '%s' does not match any known "
00217                                "message type for this interface type.", type);
00218   }
00219 }
00220 
00221 
00222 /** Copy values from other interface.
00223  * @param other other interface to copy values from
00224  */
00225 void
00226 SpeechRecognitionInterface::copy_values(const Interface *other)
00227 {
00228   const SpeechRecognitionInterface *oi = dynamic_cast<const SpeechRecognitionInterface *>(other);
00229   if (oi == NULL) {
00230     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00231                                 type(), other->type());
00232   }
00233   memcpy(data, oi->data, sizeof(SpeechRecognitionInterface_data_t));
00234 }
00235 
00236 const char *
00237 SpeechRecognitionInterface::enum_tostring(const char *enumtype, int val) const
00238 {
00239   throw UnknownTypeException("Unknown enum type %s", enumtype);
00240 }
00241 
00242 /* =========== messages =========== */
00243 /** @class SpeechRecognitionInterface::ResetMessage <interfaces/SpeechRecognitionInterface.h>
00244  * ResetMessage Fawkes BlackBoard Interface Message.
00245  * 
00246     
00247  */
00248 
00249 
00250 /** Constructor */
00251 SpeechRecognitionInterface::ResetMessage::ResetMessage() : Message("ResetMessage")
00252 {
00253   data_size = sizeof(ResetMessage_data_t);
00254   data_ptr  = malloc(data_size);
00255   memset(data_ptr, 0, data_size);
00256   data      = (ResetMessage_data_t *)data_ptr;
00257   data_ts   = (message_data_ts_t *)data_ptr;
00258 }
00259 
00260 /** Destructor */
00261 SpeechRecognitionInterface::ResetMessage::~ResetMessage()
00262 {
00263   free(data_ptr);
00264 }
00265 
00266 /** Copy constructor.
00267  * @param m message to copy from
00268  */
00269 SpeechRecognitionInterface::ResetMessage::ResetMessage(const ResetMessage *m) : Message("ResetMessage")
00270 {
00271   data_size = m->data_size;
00272   data_ptr  = malloc(data_size);
00273   memcpy(data_ptr, m->data_ptr, data_size);
00274   data      = (ResetMessage_data_t *)data_ptr;
00275   data_ts   = (message_data_ts_t *)data_ptr;
00276 }
00277 
00278 /* Methods */
00279 /** Clone this message.
00280  * Produces a message of the same type as this message and copies the
00281  * data to the new message.
00282  * @return clone of this message
00283  */
00284 Message *
00285 SpeechRecognitionInterface::ResetMessage::clone() const
00286 {
00287   return new SpeechRecognitionInterface::ResetMessage(this);
00288 }
00289 /** @class SpeechRecognitionInterface::SetEnabledMessage <interfaces/SpeechRecognitionInterface.h>
00290  * SetEnabledMessage Fawkes BlackBoard Interface Message.
00291  * 
00292     
00293  */
00294 
00295 
00296 /** Constructor with initial values.
00297  * @param ini_enabled initial value for enabled
00298  */
00299 SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage(const bool ini_enabled) : Message("SetEnabledMessage")
00300 {
00301   data_size = sizeof(SetEnabledMessage_data_t);
00302   data_ptr  = malloc(data_size);
00303   memset(data_ptr, 0, data_size);
00304   data      = (SetEnabledMessage_data_t *)data_ptr;
00305   data_ts   = (message_data_ts_t *)data_ptr;
00306   data->enabled = ini_enabled;
00307   add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00308 }
00309 /** Constructor */
00310 SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage() : Message("SetEnabledMessage")
00311 {
00312   data_size = sizeof(SetEnabledMessage_data_t);
00313   data_ptr  = malloc(data_size);
00314   memset(data_ptr, 0, data_size);
00315   data      = (SetEnabledMessage_data_t *)data_ptr;
00316   data_ts   = (message_data_ts_t *)data_ptr;
00317   add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00318 }
00319 
00320 /** Destructor */
00321 SpeechRecognitionInterface::SetEnabledMessage::~SetEnabledMessage()
00322 {
00323   free(data_ptr);
00324 }
00325 
00326 /** Copy constructor.
00327  * @param m message to copy from
00328  */
00329 SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage(const SetEnabledMessage *m) : Message("SetEnabledMessage")
00330 {
00331   data_size = m->data_size;
00332   data_ptr  = malloc(data_size);
00333   memcpy(data_ptr, m->data_ptr, data_size);
00334   data      = (SetEnabledMessage_data_t *)data_ptr;
00335   data_ts   = (message_data_ts_t *)data_ptr;
00336 }
00337 
00338 /* Methods */
00339 /** Get enabled value.
00340  * 
00341       True, if speech processing is currently enabled, false otherwise.
00342     
00343  * @return enabled value
00344  */
00345 bool
00346 SpeechRecognitionInterface::SetEnabledMessage::is_enabled() const
00347 {
00348   return data->enabled;
00349 }
00350 
00351 /** Get maximum length of enabled value.
00352  * @return length of enabled value, can be length of the array or number of 
00353  * maximum number of characters for a string
00354  */
00355 size_t
00356 SpeechRecognitionInterface::SetEnabledMessage::maxlenof_enabled() const
00357 {
00358   return 1;
00359 }
00360 
00361 /** Set enabled value.
00362  * 
00363       True, if speech processing is currently enabled, false otherwise.
00364     
00365  * @param new_enabled new enabled value
00366  */
00367 void
00368 SpeechRecognitionInterface::SetEnabledMessage::set_enabled(const bool new_enabled)
00369 {
00370   data->enabled = new_enabled;
00371 }
00372 
00373 /** Clone this message.
00374  * Produces a message of the same type as this message and copies the
00375  * data to the new message.
00376  * @return clone of this message
00377  */
00378 Message *
00379 SpeechRecognitionInterface::SetEnabledMessage::clone() const
00380 {
00381   return new SpeechRecognitionInterface::SetEnabledMessage(this);
00382 }
00383 /** Check if message is valid and can be enqueued.
00384  * @param message Message to check
00385  * @return true if the message is valid, false otherwise.
00386  */
00387 bool
00388 SpeechRecognitionInterface::message_valid(const Message *message) const
00389 {
00390   const ResetMessage *m0 = dynamic_cast<const ResetMessage *>(message);
00391   if ( m0 != NULL ) {
00392     return true;
00393   }
00394   const SetEnabledMessage *m1 = dynamic_cast<const SetEnabledMessage *>(message);
00395   if ( m1 != NULL ) {
00396     return true;
00397   }
00398   return false;
00399 }
00400 
00401 /// @cond INTERNALS
00402 EXPORT_INTERFACE(SpeechRecognitionInterface)
00403 /// @endcond
00404 
00405 
00406 } // end namespace fawkes