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 *
76 SpeechRecognitionInterface::text() const
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
86 SpeechRecognitionInterface::maxlenof_text() const
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
98 SpeechRecognitionInterface::set_text(const char * new_text)
99 {
100  set_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
110 SpeechRecognitionInterface::counter() const
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
120 SpeechRecognitionInterface::maxlenof_counter() const
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  set_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
144 SpeechRecognitionInterface::is_processing() const
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
154 SpeechRecognitionInterface::maxlenof_processing() const
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
166 SpeechRecognitionInterface::set_processing(const bool new_processing)
167 {
168  set_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
178 SpeechRecognitionInterface::is_enabled() const
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
188 SpeechRecognitionInterface::maxlenof_enabled() const
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
200 SpeechRecognitionInterface::set_enabled(const bool new_enabled)
201 {
202  set_field(data->enabled, new_enabled);
203 }
204 
205 /* =========== message create =========== */
206 Message *
207 SpeechRecognitionInterface::create_message(const char *type) const
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
224 SpeechRecognitionInterface::copy_values(const Interface *other)
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 */
249 SpeechRecognitionInterface::ResetMessage::ResetMessage() : Message("ResetMessage")
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  set_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
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:244
void set_field(FieldT &field, DataT &data)
Set a field, set data_changed to true and update data_changed accordingly.
Definition: interface.h:304
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
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:435
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
ResetMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
SetEnabledMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_enabled() const
Get maximum length of enabled value.
void set_enabled(const bool new_enabled)
Set enabled value.
SpeechRecognitionInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Fawkes library namespace.
@ IFT_BOOL
boolean field
Definition: types.h:37
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152