Fawkes API  Fawkes Development Version
SpeechRecognitionInterface.cpp
1 
2 /***************************************************************************
3  * SpeechRecognitionInterface.cpp - Fawkes BlackBoard Interface - SpeechRecognitionInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2009 Tim Niemueller and Masrur Doostdar
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/SpeechRecognitionInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class SpeechRecognitionInterface <interfaces/SpeechRecognitionInterface.h>
36  * SpeechRecognitionInterface Fawkes BlackBoard Interface.
37  *
38  The interface provides access to a spech recognition facility.
39 
40  * @ingroup FawkesInterfaces
41  */
42 
43 
44 
45 /** Constructor */
46 SpeechRecognitionInterface::SpeechRecognitionInterface() : Interface()
47 {
48  data_size = sizeof(SpeechRecognitionInterface_data_t);
49  data_ptr = malloc(data_size);
50  data = (SpeechRecognitionInterface_data_t *)data_ptr;
51  data_ts = (interface_data_ts_t *)data_ptr;
52  memset(data_ptr, 0, data_size);
53  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
54  add_fieldinfo(IFT_UINT32, "counter", 1, &data->counter);
55  add_fieldinfo(IFT_BOOL, "processing", 1, &data->processing);
56  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
57  add_messageinfo("ResetMessage");
58  add_messageinfo("SetEnabledMessage");
59  unsigned char tmp_hash[] = {0x8f, 0x5c, 0xd, 0x42, 0x1b, 0x22, 0x75, 0x3d, 0x50, 0x66, 0x70, 0x8, 0x1f, 0x47, 0xa7, 0xfd};
60  set_hash(tmp_hash);
61 }
62 
63 /** Destructor */
64 SpeechRecognitionInterface::~SpeechRecognitionInterface()
65 {
66  free(data_ptr);
67 }
68 /* Methods */
69 /** Get text value.
70  *
71  Last spoken string. Must be properly null-terminated.
72 
73  * @return text value
74  */
75 char *
77 {
78  return data->text;
79 }
80 
81 /** Get maximum length of text value.
82  * @return length of text value, can be length of the array or number of
83  * maximum number of characters for a string
84  */
85 size_t
87 {
88  return 1024;
89 }
90 
91 /** Set text value.
92  *
93  Last spoken string. Must be properly null-terminated.
94 
95  * @param new_text new text value
96  */
97 void
99 {
100  data_changed |= change_field(data->text, new_text);
101 }
102 
103 /** Get counter value.
104  *
105  Counter for messages. Increased after each new recognized string.
106 
107  * @return counter value
108  */
109 uint32_t
111 {
112  return data->counter;
113 }
114 
115 /** Get maximum length of counter value.
116  * @return length of counter value, can be length of the array or number of
117  * maximum number of characters for a string
118  */
119 size_t
121 {
122  return 1;
123 }
124 
125 /** Set counter value.
126  *
127  Counter for messages. Increased after each new recognized string.
128 
129  * @param new_counter new counter value
130  */
131 void
132 SpeechRecognitionInterface::set_counter(const uint32_t new_counter)
133 {
134  data_changed |= change_field(data->counter, new_counter);
135 }
136 
137 /** Get processing value.
138  *
139  True, if the the speech recognition is currently processing.
140 
141  * @return processing value
142  */
143 bool
145 {
146  return data->processing;
147 }
148 
149 /** Get maximum length of processing value.
150  * @return length of processing value, can be length of the array or number of
151  * maximum number of characters for a string
152  */
153 size_t
155 {
156  return 1;
157 }
158 
159 /** Set processing value.
160  *
161  True, if the the speech recognition is currently processing.
162 
163  * @param new_processing new processing value
164  */
165 void
167 {
168  data_changed |= change_field(data->processing, new_processing);
169 }
170 
171 /** Get enabled value.
172  *
173  True, if speech processing is currently enabled, false otherwise.
174 
175  * @return enabled value
176  */
177 bool
179 {
180  return data->enabled;
181 }
182 
183 /** Get maximum length of enabled value.
184  * @return length of enabled value, can be length of the array or number of
185  * maximum number of characters for a string
186  */
187 size_t
189 {
190  return 1;
191 }
192 
193 /** Set enabled value.
194  *
195  True, if speech processing is currently enabled, false otherwise.
196 
197  * @param new_enabled new enabled value
198  */
199 void
201 {
202  data_changed |= change_field(data->enabled, new_enabled);
203 }
204 
205 /* =========== message create =========== */
206 Message *
208 {
209  if ( strncmp("ResetMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
210  return new ResetMessage();
211  } else if ( strncmp("SetEnabledMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
212  return new SetEnabledMessage();
213  } else {
214  throw UnknownTypeException("The given type '%s' does not match any known "
215  "message type for this interface type.", type);
216  }
217 }
218 
219 
220 /** Copy values from other interface.
221  * @param other other interface to copy values from
222  */
223 void
225 {
226  const SpeechRecognitionInterface *oi = dynamic_cast<const SpeechRecognitionInterface *>(other);
227  if (oi == NULL) {
228  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
229  type(), other->type());
230  }
231  memcpy(data, oi->data, sizeof(SpeechRecognitionInterface_data_t));
232 }
233 
234 const char *
235 SpeechRecognitionInterface::enum_tostring(const char *enumtype, int val) const
236 {
237  throw UnknownTypeException("Unknown enum type %s", enumtype);
238 }
239 
240 /* =========== messages =========== */
241 /** @class SpeechRecognitionInterface::ResetMessage <interfaces/SpeechRecognitionInterface.h>
242  * ResetMessage Fawkes BlackBoard Interface Message.
243  *
244 
245  */
246 
247 
248 /** Constructor */
250 {
251  data_size = sizeof(ResetMessage_data_t);
252  data_ptr = malloc(data_size);
253  memset(data_ptr, 0, data_size);
254  data = (ResetMessage_data_t *)data_ptr;
256 }
257 
258 /** Destructor */
260 {
261  free(data_ptr);
262 }
263 
264 /** Copy constructor.
265  * @param m message to copy from
266  */
268 {
269  data_size = m->data_size;
270  data_ptr = malloc(data_size);
271  memcpy(data_ptr, m->data_ptr, data_size);
272  data = (ResetMessage_data_t *)data_ptr;
274 }
275 
276 /* Methods */
277 /** Clone this message.
278  * Produces a message of the same type as this message and copies the
279  * data to the new message.
280  * @return clone of this message
281  */
282 Message *
284 {
286 }
287 /** @class SpeechRecognitionInterface::SetEnabledMessage <interfaces/SpeechRecognitionInterface.h>
288  * SetEnabledMessage Fawkes BlackBoard Interface Message.
289  *
290 
291  */
292 
293 
294 /** Constructor with initial values.
295  * @param ini_enabled initial value for enabled
296  */
297 SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage(const bool ini_enabled) : Message("SetEnabledMessage")
298 {
299  data_size = sizeof(SetEnabledMessage_data_t);
300  data_ptr = malloc(data_size);
301  memset(data_ptr, 0, data_size);
302  data = (SetEnabledMessage_data_t *)data_ptr;
304  data->enabled = ini_enabled;
305  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
306 }
307 /** Constructor */
309 {
310  data_size = sizeof(SetEnabledMessage_data_t);
311  data_ptr = malloc(data_size);
312  memset(data_ptr, 0, data_size);
313  data = (SetEnabledMessage_data_t *)data_ptr;
315  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
316 }
317 
318 /** Destructor */
320 {
321  free(data_ptr);
322 }
323 
324 /** Copy constructor.
325  * @param m message to copy from
326  */
328 {
329  data_size = m->data_size;
330  data_ptr = malloc(data_size);
331  memcpy(data_ptr, m->data_ptr, data_size);
332  data = (SetEnabledMessage_data_t *)data_ptr;
334 }
335 
336 /* Methods */
337 /** Get enabled value.
338  *
339  True, if speech processing is currently enabled, false otherwise.
340 
341  * @return enabled value
342  */
343 bool
345 {
346  return data->enabled;
347 }
348 
349 /** Get maximum length of enabled value.
350  * @return length of enabled value, can be length of the array or number of
351  * maximum number of characters for a string
352  */
353 size_t
355 {
356  return 1;
357 }
358 
359 /** Set enabled value.
360  *
361  True, if speech processing is currently enabled, false otherwise.
362 
363  * @param new_enabled new enabled value
364  */
365 void
367 {
368  change_field(data->enabled, new_enabled);
369 }
370 
371 /** Clone this message.
372  * Produces a message of the same type as this message and copies the
373  * data to the new message.
374  * @return clone of this message
375  */
376 Message *
378 {
380 }
381 /** Check if message is valid and can be enqueued.
382  * @param message Message to check
383  * @return true if the message is valid, false otherwise.
384  */
385 bool
387 {
388  const ResetMessage *m0 = dynamic_cast<const ResetMessage *>(message);
389  if ( m0 != NULL ) {
390  return true;
391  }
392  const SetEnabledMessage *m1 = dynamic_cast<const SetEnabledMessage *>(message);
393  if ( m1 != NULL ) {
394  return true;
395  }
396  return false;
397 }
398 
399 /// @cond INTERNALS
400 EXPORT_INTERFACE(SpeechRecognitionInterface)
401 /// @endcond
402 
403 
404 } // end namespace fawkes
fawkes::SpeechRecognitionInterface::set_processing
void set_processing(const bool new_processing)
Set processing value.
Definition: SpeechRecognitionInterface.cpp:166
fawkes::SpeechRecognitionInterface::SetEnabledMessage
SetEnabledMessage Fawkes BlackBoard Interface Message.
Definition: SpeechRecognitionInterface.h:85
fawkes::Interface::data_ptr
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
fawkes::SpeechRecognitionInterface::counter
uint32_t counter() const
Get counter value.
Definition: SpeechRecognitionInterface.cpp:110
fawkes::SpeechRecognitionInterface::SetEnabledMessage::is_enabled
bool is_enabled() const
Get enabled value.
Definition: SpeechRecognitionInterface.cpp:344
fawkes::SpeechRecognitionInterface::copy_values
virtual void copy_values(const Interface *other)
Copy values from other interface.
Definition: SpeechRecognitionInterface.cpp:224
fawkes::SpeechRecognitionInterface::maxlenof_text
size_t maxlenof_text() const
Get maximum length of text value.
Definition: SpeechRecognitionInterface.cpp:86
fawkes::SpeechRecognitionInterface::ResetMessage::~ResetMessage
~ResetMessage()
Destructor.
Definition: SpeechRecognitionInterface.cpp:259
fawkes::SpeechRecognitionInterface::message_valid
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Definition: SpeechRecognitionInterface.cpp:386
fawkes::Message
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
fawkes::SpeechRecognitionInterface::ResetMessage::clone
virtual Message * clone() const
Clone this message.
Definition: SpeechRecognitionInterface.cpp:283
fawkes::SpeechRecognitionInterface::set_enabled
void set_enabled(const bool new_enabled)
Set enabled value.
Definition: SpeechRecognitionInterface.cpp:200
fawkes::Message::data_ptr
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:128
fawkes::IFT_BOOL
@ IFT_BOOL
boolean field
Definition: types.h:37
fawkes::Message::data_ts
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:138
fawkes::IFT_UINT32
@ IFT_UINT32
32 bit unsigned integer field
Definition: types.h:43
fawkes::SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage
SetEnabledMessage()
Constructor.
Definition: SpeechRecognitionInterface.cpp:308
fawkes::SpeechRecognitionInterface::is_enabled
bool is_enabled() const
Get enabled value.
Definition: SpeechRecognitionInterface.cpp:178
fawkes::Interface::type
const char * type() const
Get type of interface.
Definition: interface.cpp:643
fawkes::SpeechRecognitionInterface::maxlenof_counter
size_t maxlenof_counter() const
Get maximum length of counter value.
Definition: SpeechRecognitionInterface.cpp:120
fawkes::Message::message_data_ts_t
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:134
fawkes::SpeechRecognitionInterface::set_counter
void set_counter(const uint32_t new_counter)
Set counter value.
Definition: SpeechRecognitionInterface.cpp:132
fawkes::TypeMismatchException
Type mismatch.
Definition: software.h:44
fawkes::Interface::data_changed
bool data_changed
Indicator if data has changed.
Definition: interface.h:226
fawkes::UnknownTypeException
Unknown type.
Definition: software.h:50
fawkes::SpeechRecognitionInterface::create_message
virtual Message * create_message(const char *type) const
Create message based on type name.
Definition: SpeechRecognitionInterface.cpp:207
fawkes::SpeechRecognitionInterface::SetEnabledMessage::maxlenof_enabled
size_t maxlenof_enabled() const
Get maximum length of enabled value.
Definition: SpeechRecognitionInterface.cpp:354
fawkes
Fawkes library namespace.
fawkes::SpeechRecognitionInterface::enum_tostring
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
Definition: SpeechRecognitionInterface.cpp:235
fawkes::Interface::set_hash
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:316
fawkes::SpeechRecognitionInterface::SetEnabledMessage::clone
virtual Message * clone() const
Clone this message.
Definition: SpeechRecognitionInterface.cpp:377
fawkes::Message::data_size
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:129
fawkes::SpeechRecognitionInterface::ResetMessage
ResetMessage Fawkes BlackBoard Interface Message.
Definition: SpeechRecognitionInterface.h:65
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
fawkes::SpeechRecognitionInterface::maxlenof_processing
size_t maxlenof_processing() const
Get maximum length of processing value.
Definition: SpeechRecognitionInterface.cpp:154
fawkes::Message::add_fieldinfo
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:400
fawkes::SpeechRecognitionInterface::text
char * text() const
Get text value.
Definition: SpeechRecognitionInterface.cpp:76
fawkes::SpeechRecognitionInterface::ResetMessage::ResetMessage
ResetMessage()
Constructor.
Definition: SpeechRecognitionInterface.cpp:249
fawkes::IFT_STRING
@ IFT_STRING
string field
Definition: types.h:48
fawkes::SpeechRecognitionInterface::SetEnabledMessage::~SetEnabledMessage
~SetEnabledMessage()
Destructor.
Definition: SpeechRecognitionInterface.cpp:319
fawkes::SpeechRecognitionInterface::SetEnabledMessage::set_enabled
void set_enabled(const bool new_enabled)
Set enabled value.
Definition: SpeechRecognitionInterface.cpp:366
fawkes::SpeechRecognitionInterface
SpeechRecognitionInterface Fawkes BlackBoard Interface.
Definition: SpeechRecognitionInterface.h:34
fawkes::SpeechRecognitionInterface::maxlenof_enabled
size_t maxlenof_enabled() const
Get maximum length of enabled value.
Definition: SpeechRecognitionInterface.cpp:188
fawkes::Interface::add_messageinfo
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:375
fawkes::change_field
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: message.h:167
fawkes::SpeechRecognitionInterface::set_text
void set_text(const char *new_text)
Set text value.
Definition: SpeechRecognitionInterface.cpp:98
fawkes::SpeechRecognitionInterface::is_processing
bool is_processing() const
Get processing value.
Definition: SpeechRecognitionInterface.cpp:144