Fawkes API  Fawkes Development Version
SpeechSynthInterface.cpp
1 
2 /***************************************************************************
3  * SpeechSynthInterface.cpp - Fawkes BlackBoard Interface - SpeechSynthInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008 Tim Niemueller
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/SpeechSynthInterface.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 SpeechSynthInterface <interfaces/SpeechSynthInterface.h>
36  * SpeechSynthInterface Fawkes BlackBoard Interface.
37  *
38  The interface provides access to a spech synthesizer facility.
39  On systems that support this feature strings can be ordered for
40  synthesis and audio output. Multiple messages ordering speech
41  should be enqueued and processed one after another by providers.
42 
43  * @ingroup FawkesInterfaces
44  */
45 
46 
47 
48 /** Constructor */
49 SpeechSynthInterface::SpeechSynthInterface() : Interface()
50 {
51  data_size = sizeof(SpeechSynthInterface_data_t);
52  data_ptr = malloc(data_size);
53  data = (SpeechSynthInterface_data_t *)data_ptr;
54  data_ts = (interface_data_ts_t *)data_ptr;
55  memset(data_ptr, 0, data_size);
56  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
57  add_fieldinfo(IFT_UINT32, "msgid", 1, &data->msgid);
58  add_fieldinfo(IFT_BOOL, "final", 1, &data->final);
59  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
60  add_messageinfo("SayMessage");
61  unsigned char tmp_hash[] = {0x28, 0x11, 0x46, 0x87, 0xb1, 0x65, 0x92, 0x96, 0xe6, 0x6e, 0x18, 0x8a, 0xdc, 0x8, 0xb0, 0x69};
62  set_hash(tmp_hash);
63 }
64 
65 /** Destructor */
66 SpeechSynthInterface::~SpeechSynthInterface()
67 {
68  free(data_ptr);
69 }
70 /* Methods */
71 /** Get text value.
72  *
73  Last spoken string. Must be properly null-terminated.
74 
75  * @return text value
76  */
77 char *
79 {
80  return data->text;
81 }
82 
83 /** Get maximum length of text value.
84  * @return length of text value, can be length of the array or number of
85  * maximum number of characters for a string
86  */
87 size_t
89 {
90  return 1024;
91 }
92 
93 /** Set text value.
94  *
95  Last spoken string. Must be properly null-terminated.
96 
97  * @param new_text new text value
98  */
99 void
100 SpeechSynthInterface::set_text(const char * new_text)
101 {
102  strncpy(data->text, new_text, sizeof(data->text)-1);
103  data->text[sizeof(data->text)-1] = 0;
104  data_changed = true;
105 }
106 
107 /** Get msgid value.
108  *
109  The ID of the message that is currently being processed,
110  or 0 if no message is being processed.
111 
112  * @return msgid value
113  */
114 uint32_t
116 {
117  return data->msgid;
118 }
119 
120 /** Get maximum length of msgid value.
121  * @return length of msgid value, can be length of the array or number of
122  * maximum number of characters for a string
123  */
124 size_t
126 {
127  return 1;
128 }
129 
130 /** Set msgid value.
131  *
132  The ID of the message that is currently being processed,
133  or 0 if no message is being processed.
134 
135  * @param new_msgid new msgid value
136  */
137 void
138 SpeechSynthInterface::set_msgid(const uint32_t new_msgid)
139 {
140  data->msgid = new_msgid;
141  data_changed = true;
142 }
143 
144 /** Get final value.
145  *
146  True, if the last text has been spoken, false if it is still running.
147 
148  * @return final value
149  */
150 bool
152 {
153  return data->final;
154 }
155 
156 /** Get maximum length of final value.
157  * @return length of final value, can be length of the array or number of
158  * maximum number of characters for a string
159  */
160 size_t
162 {
163  return 1;
164 }
165 
166 /** Set final value.
167  *
168  True, if the last text has been spoken, false if it is still running.
169 
170  * @param new_final new final value
171  */
172 void
173 SpeechSynthInterface::set_final(const bool new_final)
174 {
175  data->final = new_final;
176  data_changed = true;
177 }
178 
179 /** Get duration value.
180  *
181  Length in seconds that it takes to speek the current text, -1 if
182  unknown. This is the total duration of the current string, *not* the
183  duration of already spoken or yet to speak text!
184 
185  * @return duration value
186  */
187 float
189 {
190  return data->duration;
191 }
192 
193 /** Get maximum length of duration value.
194  * @return length of duration value, can be length of the array or number of
195  * maximum number of characters for a string
196  */
197 size_t
199 {
200  return 1;
201 }
202 
203 /** Set duration value.
204  *
205  Length in seconds that it takes to speek the current text, -1 if
206  unknown. This is the total duration of the current string, *not* the
207  duration of already spoken or yet to speak text!
208 
209  * @param new_duration new duration value
210  */
211 void
212 SpeechSynthInterface::set_duration(const float new_duration)
213 {
214  data->duration = new_duration;
215  data_changed = true;
216 }
217 
218 /* =========== message create =========== */
219 Message *
221 {
222  if ( strncmp("SayMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_) == 0 ) {
223  return new SayMessage();
224  } else {
225  throw UnknownTypeException("The given type '%s' does not match any known "
226  "message type for this interface type.", type);
227  }
228 }
229 
230 
231 /** Copy values from other interface.
232  * @param other other interface to copy values from
233  */
234 void
236 {
237  const SpeechSynthInterface *oi = dynamic_cast<const SpeechSynthInterface *>(other);
238  if (oi == NULL) {
239  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
240  type(), other->type());
241  }
242  memcpy(data, oi->data, sizeof(SpeechSynthInterface_data_t));
243 }
244 
245 const char *
246 SpeechSynthInterface::enum_tostring(const char *enumtype, int val) const
247 {
248  throw UnknownTypeException("Unknown enum type %s", enumtype);
249 }
250 
251 /* =========== messages =========== */
252 /** @class SpeechSynthInterface::SayMessage <interfaces/SpeechSynthInterface.h>
253  * SayMessage Fawkes BlackBoard Interface Message.
254  *
255 
256  */
257 
258 
259 /** Constructor with initial values.
260  * @param ini_text initial value for text
261  */
262 SpeechSynthInterface::SayMessage::SayMessage(const char * ini_text) : Message("SayMessage")
263 {
264  data_size = sizeof(SayMessage_data_t);
265  data_ptr = malloc(data_size);
266  memset(data_ptr, 0, data_size);
267  data = (SayMessage_data_t *)data_ptr;
269  strncpy(data->text, ini_text, 1024-1);
270  data->text[1024-1] = 0;
271  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
272 }
273 /** Constructor */
275 {
276  data_size = sizeof(SayMessage_data_t);
277  data_ptr = malloc(data_size);
278  memset(data_ptr, 0, data_size);
279  data = (SayMessage_data_t *)data_ptr;
281  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
282 }
283 
284 /** Destructor */
286 {
287  free(data_ptr);
288 }
289 
290 /** Copy constructor.
291  * @param m message to copy from
292  */
294 {
295  data_size = m->data_size;
296  data_ptr = malloc(data_size);
297  memcpy(data_ptr, m->data_ptr, data_size);
298  data = (SayMessage_data_t *)data_ptr;
300 }
301 
302 /* Methods */
303 /** Get text value.
304  *
305  Last spoken string. Must be properly null-terminated.
306 
307  * @return text value
308  */
309 char *
311 {
312  return data->text;
313 }
314 
315 /** Get maximum length of text value.
316  * @return length of text value, can be length of the array or number of
317  * maximum number of characters for a string
318  */
319 size_t
321 {
322  return 1024;
323 }
324 
325 /** Set text value.
326  *
327  Last spoken string. Must be properly null-terminated.
328 
329  * @param new_text new text value
330  */
331 void
333 {
334  strncpy(data->text, new_text, sizeof(data->text)-1);
335  data->text[sizeof(data->text)-1] = 0;
336 }
337 
338 /** Clone this message.
339  * Produces a message of the same type as this message and copies the
340  * data to the new message.
341  * @return clone of this message
342  */
343 Message *
345 {
346  return new SpeechSynthInterface::SayMessage(this);
347 }
348 /** Check if message is valid and can be enqueued.
349  * @param message Message to check
350  * @return true if the message is valid, false otherwise.
351  */
352 bool
354 {
355  const SayMessage *m0 = dynamic_cast<const SayMessage *>(message);
356  if ( m0 != NULL ) {
357  return true;
358  }
359  return false;
360 }
361 
362 /// @cond INTERNALS
363 EXPORT_INTERFACE(SpeechSynthInterface)
364 /// @endcond
365 
366 
367 } // end namespace fawkes
size_t maxlenof_final() const
Get maximum length of final value.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:125
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
void set_duration(const float new_duration)
Set duration value.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:41
void set_final(const bool new_final)
Set final value.
uint32_t msgid() const
Get msgid value.
virtual void copy_values(const Interface *other)
Copy values from other interface.
virtual Message * clone() const
Clone this message.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:316
void set_text(const char *new_text)
Set text value.
virtual Message * create_message(const char *type) const
Create message based on type name.
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:130
char * text() const
Get text value.
string field
Definition: types.h:48
SayMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_msgid() const
Get maximum length of msgid value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:78
void set_msgid(const uint32_t new_msgid)
Set msgid 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 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
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
size_t maxlenof_text() const
Get maximum length of text value.
void set_text(const char *new_text)
Set text value.
bool is_final() const
Get final value.
size_t maxlenof_text() const
Get maximum length of text value.
float field
Definition: types.h:46
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
float duration() const
Get duration value.
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.
size_t maxlenof_duration() const
Get maximum length of duration value.
SpeechSynthInterface Fawkes BlackBoard Interface.