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