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 <cstring>
29 #include <cstdlib>
30 
31 namespace fawkes {
32 
33 /** @class SpeechRecognitionInterface <interfaces/SpeechRecognitionInterface.h>
34  * SpeechRecognitionInterface Fawkes BlackBoard Interface.
35  *
36  The interface provides access to a spech recognition facility.
37 
38  * @ingroup FawkesInterfaces
39  */
40 
41 
42 
43 /** Constructor */
44 SpeechRecognitionInterface::SpeechRecognitionInterface() : Interface()
45 {
46  data_size = sizeof(SpeechRecognitionInterface_data_t);
47  data_ptr = malloc(data_size);
48  data = (SpeechRecognitionInterface_data_t *)data_ptr;
49  data_ts = (interface_data_ts_t *)data_ptr;
50  memset(data_ptr, 0, data_size);
51  add_fieldinfo(IFT_STRING, "text", 1024, data->text, "");
52  add_fieldinfo(IFT_UINT32, "counter", 1, &data->counter, "");
53  add_fieldinfo(IFT_BOOL, "processing", 1, &data->processing, "");
54  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled, "");
55  add_messageinfo("ResetMessage");
56  add_messageinfo("SetEnabledMessage");
57  unsigned char tmp_hash[] = {0x8f, 0x5c, 0xd, 0x42, 0x1b, 0x22, 0x75, 0x3d, 0x50, 0x66, 0x70, 0x8, 0x1f, 0x47, 0xa7, 0xfd};
58  set_hash(tmp_hash);
59 }
60 
61 /** Destructor */
62 SpeechRecognitionInterface::~SpeechRecognitionInterface()
63 {
64  free(data_ptr);
65 }
66 /* Methods */
67 /** Get text value.
68  *
69  Last spoken string. Must be properly null-terminated.
70 
71  * @return text value
72  */
73 char *
75 {
76  return data->text;
77 }
78 
79 /** Get maximum length of text value.
80  * @return length of text value, can be length of the array or number of
81  * maximum number of characters for a string
82  */
83 size_t
85 {
86  return 1024;
87 }
88 
89 /** Set text value.
90  *
91  Last spoken string. Must be properly null-terminated.
92 
93  * @param new_text new text value
94  */
95 void
97 {
98  strncpy(data->text, new_text, sizeof(data->text));
99  data_changed = true;
100 }
101 
102 /** Get counter value.
103  *
104  Counter for messages. Increased after each new recognized string.
105 
106  * @return counter value
107  */
108 uint32_t
110 {
111  return data->counter;
112 }
113 
114 /** Get maximum length of counter value.
115  * @return length of counter value, can be length of the array or number of
116  * maximum number of characters for a string
117  */
118 size_t
120 {
121  return 1;
122 }
123 
124 /** Set counter value.
125  *
126  Counter for messages. Increased after each new recognized string.
127 
128  * @param new_counter new counter value
129  */
130 void
131 SpeechRecognitionInterface::set_counter(const uint32_t new_counter)
132 {
133  data->counter = new_counter;
134  data_changed = true;
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->processing = new_processing;
169  data_changed = true;
170 }
171 
172 /** Get enabled value.
173  *
174  True, if speech processing is currently enabled, false otherwise.
175 
176  * @return enabled value
177  */
178 bool
180 {
181  return data->enabled;
182 }
183 
184 /** Get maximum length of enabled value.
185  * @return length of enabled value, can be length of the array or number of
186  * maximum number of characters for a string
187  */
188 size_t
190 {
191  return 1;
192 }
193 
194 /** Set enabled value.
195  *
196  True, if speech processing is currently enabled, false otherwise.
197 
198  * @param new_enabled new enabled value
199  */
200 void
202 {
203  data->enabled = new_enabled;
204  data_changed = true;
205 }
206 
207 /* =========== message create =========== */
208 Message *
210 {
211  if ( strncmp("ResetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
212  return new ResetMessage();
213  } else if ( strncmp("SetEnabledMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
214  return new SetEnabledMessage();
215  } else {
216  throw UnknownTypeException("The given type '%s' does not match any known "
217  "message type for this interface type.", type);
218  }
219 }
220 
221 
222 /** Copy values from other interface.
223  * @param other other interface to copy values from
224  */
225 void
227 {
228  const SpeechRecognitionInterface *oi = dynamic_cast<const SpeechRecognitionInterface *>(other);
229  if (oi == NULL) {
230  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
231  type(), other->type());
232  }
233  memcpy(data, oi->data, sizeof(SpeechRecognitionInterface_data_t));
234 }
235 
236 const char *
237 SpeechRecognitionInterface::enum_tostring(const char *enumtype, int val) const
238 {
239  throw UnknownTypeException("Unknown enum type %s", enumtype);
240 }
241 
242 /* =========== messages =========== */
243 /** @class SpeechRecognitionInterface::ResetMessage <interfaces/SpeechRecognitionInterface.h>
244  * ResetMessage Fawkes BlackBoard Interface Message.
245  *
246 
247  */
248 
249 
250 /** Constructor */
252 {
253  data_size = sizeof(ResetMessage_data_t);
254  data_ptr = malloc(data_size);
255  memset(data_ptr, 0, data_size);
256  data = (ResetMessage_data_t *)data_ptr;
258 }
259 
260 /** Destructor */
262 {
263  free(data_ptr);
264 }
265 
266 /** Copy constructor.
267  * @param m message to copy from
268  */
270 {
271  data_size = m->data_size;
272  data_ptr = malloc(data_size);
273  memcpy(data_ptr, m->data_ptr, data_size);
274  data = (ResetMessage_data_t *)data_ptr;
276 }
277 
278 /* Methods */
279 /** Clone this message.
280  * Produces a message of the same type as this message and copies the
281  * data to the new message.
282  * @return clone of this message
283  */
284 Message *
286 {
288 }
289 /** @class SpeechRecognitionInterface::SetEnabledMessage <interfaces/SpeechRecognitionInterface.h>
290  * SetEnabledMessage Fawkes BlackBoard Interface Message.
291  *
292 
293  */
294 
295 
296 /** Constructor with initial values.
297  * @param ini_enabled initial value for enabled
298  */
299 SpeechRecognitionInterface::SetEnabledMessage::SetEnabledMessage(const bool ini_enabled) : Message("SetEnabledMessage")
300 {
301  data_size = sizeof(SetEnabledMessage_data_t);
302  data_ptr = malloc(data_size);
303  memset(data_ptr, 0, data_size);
304  data = (SetEnabledMessage_data_t *)data_ptr;
306  data->enabled = ini_enabled;
307  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled, "");
308 }
309 /** Constructor */
311 {
312  data_size = sizeof(SetEnabledMessage_data_t);
313  data_ptr = malloc(data_size);
314  memset(data_ptr, 0, data_size);
315  data = (SetEnabledMessage_data_t *)data_ptr;
317  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled, "");
318 }
319 
320 /** Destructor */
322 {
323  free(data_ptr);
324 }
325 
326 /** Copy constructor.
327  * @param m message to copy from
328  */
330 {
331  data_size = m->data_size;
332  data_ptr = malloc(data_size);
333  memcpy(data_ptr, m->data_ptr, data_size);
334  data = (SetEnabledMessage_data_t *)data_ptr;
336 }
337 
338 /* Methods */
339 /** Get enabled value.
340  *
341  True, if speech processing is currently enabled, false otherwise.
342 
343  * @return enabled value
344  */
345 bool
347 {
348  return data->enabled;
349 }
350 
351 /** Get maximum length of enabled value.
352  * @return length of enabled value, can be length of the array or number of
353  * maximum number of characters for a string
354  */
355 size_t
357 {
358  return 1;
359 }
360 
361 /** Set enabled value.
362  *
363  True, if speech processing is currently enabled, false otherwise.
364 
365  * @param new_enabled new enabled value
366  */
367 void
369 {
370  data->enabled = new_enabled;
371 }
372 
373 /** Clone this message.
374  * Produces a message of the same type as this message and copies the
375  * data to the new message.
376  * @return clone of this message
377  */
378 Message *
380 {
382 }
383 /** Check if message is valid and can be enqueued.
384  * @param message Message to check
385  * @return true if the message is valid, false otherwise.
386  */
387 bool
389 {
390  const ResetMessage *m0 = dynamic_cast<const ResetMessage *>(message);
391  if ( m0 != NULL ) {
392  return true;
393  }
394  const SetEnabledMessage *m1 = dynamic_cast<const SetEnabledMessage *>(message);
395  if ( m1 != NULL ) {
396  return true;
397  }
398  return false;
399 }
400 
401 /// @cond INTERNALS
402 EXPORT_INTERFACE(SpeechRecognitionInterface)
403 /// @endcond
404 
405 
406 } // end namespace fawkes
size_t maxlenof_text() const
Get maximum length of text value.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:114
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:43
virtual Message * clone() const
Clone this message.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:312
const char * type() const
Get message type.
Definition: message.cpp:378
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:119
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
string field
Definition: types.h:45
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
void set_processing(const bool new_processing)
Set processing value.
bool is_processing() const
Get processing value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
void set_counter(const uint32_t new_counter)
Set counter value.
void set_text(const char *new_text)
Set text value.
size_t maxlenof_processing() const
Get maximum length of processing value.
bool is_enabled() const
Get enabled value.
void set_enabled(const bool new_enabled)
Set enabled value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:123
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:115
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:368
size_t maxlenof_counter() const
Get maximum length of counter value.
bool data_changed
Indicator if data has changed.
Definition: interface.h:208
SpeechRecognitionInterface Fawkes BlackBoard Interface.
virtual Message * clone() const
Clone this message.
size_t maxlenof_enabled() const
Get maximum length of enabled value.
virtual Message * create_message(const char *type) const
Create message based on type name.
ResetMessage Fawkes BlackBoard Interface Message.
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0)
Add an entry to the info list.
Definition: message.cpp:435
virtual void copy_values(const Interface *other)
Copy values from other interface.
boolean field
Definition: types.h:34
const char * type() const
Get type of interface.
Definition: interface.cpp:635
32 bit unsigned integer field
Definition: types.h:40
uint32_t counter() const
Get counter value.
size_t maxlenof_enabled() const
Get maximum length of enabled value.