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  strncpy(data->text, new_text, sizeof(data->text));
101  data_changed = true;
102 }
103 
104 /** Get counter value.
105  *
106  Counter for messages. Increased after each new recognized string.
107 
108  * @return counter value
109  */
110 uint32_t
112 {
113  return data->counter;
114 }
115 
116 /** Get maximum length of counter value.
117  * @return length of counter value, can be length of the array or number of
118  * maximum number of characters for a string
119  */
120 size_t
122 {
123  return 1;
124 }
125 
126 /** Set counter value.
127  *
128  Counter for messages. Increased after each new recognized string.
129 
130  * @param new_counter new counter value
131  */
132 void
133 SpeechRecognitionInterface::set_counter(const uint32_t new_counter)
134 {
135  data->counter = new_counter;
136  data_changed = true;
137 }
138 
139 /** Get processing value.
140  *
141  True, if the the speech recognition is currently processing.
142 
143  * @return processing value
144  */
145 bool
147 {
148  return data->processing;
149 }
150 
151 /** Get maximum length of processing value.
152  * @return length of processing value, can be length of the array or number of
153  * maximum number of characters for a string
154  */
155 size_t
157 {
158  return 1;
159 }
160 
161 /** Set processing value.
162  *
163  True, if the the speech recognition is currently processing.
164 
165  * @param new_processing new processing value
166  */
167 void
169 {
170  data->processing = new_processing;
171  data_changed = true;
172 }
173 
174 /** Get enabled value.
175  *
176  True, if speech processing is currently enabled, false otherwise.
177 
178  * @return enabled value
179  */
180 bool
182 {
183  return data->enabled;
184 }
185 
186 /** Get maximum length of enabled value.
187  * @return length of enabled value, can be length of the array or number of
188  * maximum number of characters for a string
189  */
190 size_t
192 {
193  return 1;
194 }
195 
196 /** Set enabled value.
197  *
198  True, if speech processing is currently enabled, false otherwise.
199 
200  * @param new_enabled new enabled value
201  */
202 void
204 {
205  data->enabled = new_enabled;
206  data_changed = true;
207 }
208 
209 /* =========== message create =========== */
210 Message *
212 {
213  if ( strncmp("ResetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
214  return new ResetMessage();
215  } else if ( strncmp("SetEnabledMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
216  return new SetEnabledMessage();
217  } else {
218  throw UnknownTypeException("The given type '%s' does not match any known "
219  "message type for this interface type.", type);
220  }
221 }
222 
223 
224 /** Copy values from other interface.
225  * @param other other interface to copy values from
226  */
227 void
229 {
230  const SpeechRecognitionInterface *oi = dynamic_cast<const SpeechRecognitionInterface *>(other);
231  if (oi == NULL) {
232  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
233  type(), other->type());
234  }
235  memcpy(data, oi->data, sizeof(SpeechRecognitionInterface_data_t));
236 }
237 
238 const char *
239 SpeechRecognitionInterface::enum_tostring(const char *enumtype, int val) const
240 {
241  throw UnknownTypeException("Unknown enum type %s", enumtype);
242 }
243 
244 /* =========== messages =========== */
245 /** @class SpeechRecognitionInterface::ResetMessage <interfaces/SpeechRecognitionInterface.h>
246  * ResetMessage Fawkes BlackBoard Interface Message.
247  *
248 
249  */
250 
251 
252 /** Constructor */
254 {
255  data_size = sizeof(ResetMessage_data_t);
256  data_ptr = malloc(data_size);
257  memset(data_ptr, 0, data_size);
258  data = (ResetMessage_data_t *)data_ptr;
260 }
261 
262 /** Destructor */
264 {
265  free(data_ptr);
266 }
267 
268 /** Copy constructor.
269  * @param m message to copy from
270  */
272 {
273  data_size = m->data_size;
274  data_ptr = malloc(data_size);
275  memcpy(data_ptr, m->data_ptr, data_size);
276  data = (ResetMessage_data_t *)data_ptr;
278 }
279 
280 /* Methods */
281 /** Clone this message.
282  * Produces a message of the same type as this message and copies the
283  * data to the new message.
284  * @return clone of this message
285  */
286 Message *
288 {
290 }
291 /** @class SpeechRecognitionInterface::SetEnabledMessage <interfaces/SpeechRecognitionInterface.h>
292  * SetEnabledMessage Fawkes BlackBoard Interface Message.
293  *
294 
295  */
296 
297 
298 /** Constructor with initial values.
299  * @param ini_enabled initial value for enabled
300  */
301 SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage(const bool ini_enabled) : Message("SetEnabledMessage")
302 {
303  data_size = sizeof(SetEnabledMessage_data_t);
304  data_ptr = malloc(data_size);
305  memset(data_ptr, 0, data_size);
306  data = (SetEnabledMessage_data_t *)data_ptr;
308  data->enabled = ini_enabled;
309  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
310 }
311 /** Constructor */
313 {
314  data_size = sizeof(SetEnabledMessage_data_t);
315  data_ptr = malloc(data_size);
316  memset(data_ptr, 0, data_size);
317  data = (SetEnabledMessage_data_t *)data_ptr;
319  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
320 }
321 
322 /** Destructor */
324 {
325  free(data_ptr);
326 }
327 
328 /** Copy constructor.
329  * @param m message to copy from
330  */
332 {
333  data_size = m->data_size;
334  data_ptr = malloc(data_size);
335  memcpy(data_ptr, m->data_ptr, data_size);
336  data = (SetEnabledMessage_data_t *)data_ptr;
338 }
339 
340 /* Methods */
341 /** Get enabled value.
342  *
343  True, if speech processing is currently enabled, false otherwise.
344 
345  * @return enabled value
346  */
347 bool
349 {
350  return data->enabled;
351 }
352 
353 /** Get maximum length of enabled value.
354  * @return length of enabled value, can be length of the array or number of
355  * maximum number of characters for a string
356  */
357 size_t
359 {
360  return 1;
361 }
362 
363 /** Set enabled value.
364  *
365  True, if speech processing is currently enabled, false otherwise.
366 
367  * @param new_enabled new enabled value
368  */
369 void
371 {
372  data->enabled = new_enabled;
373 }
374 
375 /** Clone this message.
376  * Produces a message of the same type as this message and copies the
377  * data to the new message.
378  * @return clone of this message
379  */
380 Message *
382 {
384 }
385 /** Check if message is valid and can be enqueued.
386  * @param message Message to check
387  * @return true if the message is valid, false otherwise.
388  */
389 bool
391 {
392  const ResetMessage *m0 = dynamic_cast<const ResetMessage *>(message);
393  if ( m0 != NULL ) {
394  return true;
395  }
396  const SetEnabledMessage *m1 = dynamic_cast<const SetEnabledMessage *>(message);
397  if ( m1 != NULL ) {
398  return true;
399  }
400  return false;
401 }
402 
403 /// @cond INTERNALS
404 EXPORT_INTERFACE(SpeechRecognitionInterface)
405 /// @endcond
406 
407 
408 } // end namespace fawkes
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
bool is_processing() const
Get processing value.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
Fawkes library namespace.
SetEnabledMessage Fawkes BlackBoard Interface Message.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
bool is_enabled() const
Get enabled value.
string field
Definition: types.h:47
void set_processing(const bool new_processing)
Set processing value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
void set_counter(const uint32_t new_counter)
Set counter value.
uint32_t counter() const
Get counter value.
void set_text(const char *new_text)
Set text value.
void set_enabled(const bool new_enabled)
Set enabled value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
void set_enabled(const bool new_enabled)
Set enabled value.
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:373
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
const char * type() const
Get type of interface.
Definition: interface.cpp:651
SpeechRecognitionInterface Fawkes BlackBoard Interface.
size_t maxlenof_enabled() const
Get maximum length of enabled value.
virtual Message * clone() const
Clone this message.
ResetMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_text() const
Get maximum length of text value.
virtual Message * create_message(const char *type) const
Create message based on type name.
virtual Message * clone() const
Clone this message.
virtual void copy_values(const Interface *other)
Copy values from other interface.
size_t maxlenof_enabled() const
Get maximum length of enabled value.
size_t maxlenof_processing() const
Get maximum length of processing value.
size_t maxlenof_counter() const
Get maximum length of counter value.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
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:436
boolean field
Definition: types.h:36
const char * type() const
Get message type.
Definition: message.cpp:378
32 bit unsigned integer field
Definition: types.h:42
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.