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