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));
103  data_changed = true;
104 }
105 
106 /** Get msgid value.
107  *
108  The ID of the message that is currently being processed,
109  or 0 if no message is being processed.
110 
111  * @return msgid value
112  */
113 uint32_t
115 {
116  return data->msgid;
117 }
118 
119 /** Get maximum length of msgid value.
120  * @return length of msgid value, can be length of the array or number of
121  * maximum number of characters for a string
122  */
123 size_t
125 {
126  return 1;
127 }
128 
129 /** Set msgid value.
130  *
131  The ID of the message that is currently being processed,
132  or 0 if no message is being processed.
133 
134  * @param new_msgid new msgid value
135  */
136 void
137 SpeechSynthInterface::set_msgid(const uint32_t new_msgid)
138 {
139  data->msgid = new_msgid;
140  data_changed = true;
141 }
142 
143 /** Get final value.
144  *
145  True, if the last text has been spoken, false if it is still running.
146 
147  * @return final value
148  */
149 bool
151 {
152  return data->final;
153 }
154 
155 /** Get maximum length of final value.
156  * @return length of final value, can be length of the array or number of
157  * maximum number of characters for a string
158  */
159 size_t
161 {
162  return 1;
163 }
164 
165 /** Set final value.
166  *
167  True, if the last text has been spoken, false if it is still running.
168 
169  * @param new_final new final value
170  */
171 void
172 SpeechSynthInterface::set_final(const bool new_final)
173 {
174  data->final = new_final;
175  data_changed = true;
176 }
177 
178 /** Get duration value.
179  *
180  Length in seconds that it takes to speek the current text, -1 if
181  unknown. This is the total duration of the current string, *not* the
182  duration of already spoken or yet to speak text!
183 
184  * @return duration value
185  */
186 float
188 {
189  return data->duration;
190 }
191 
192 /** Get maximum length of duration value.
193  * @return length of duration value, can be length of the array or number of
194  * maximum number of characters for a string
195  */
196 size_t
198 {
199  return 1;
200 }
201 
202 /** Set duration value.
203  *
204  Length in seconds that it takes to speek the current text, -1 if
205  unknown. This is the total duration of the current string, *not* the
206  duration of already spoken or yet to speak text!
207 
208  * @param new_duration new duration value
209  */
210 void
211 SpeechSynthInterface::set_duration(const float new_duration)
212 {
213  data->duration = new_duration;
214  data_changed = true;
215 }
216 
217 /* =========== message create =========== */
218 Message *
220 {
221  if ( strncmp("SayMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
222  return new SayMessage();
223  } else {
224  throw UnknownTypeException("The given type '%s' does not match any known "
225  "message type for this interface type.", type);
226  }
227 }
228 
229 
230 /** Copy values from other interface.
231  * @param other other interface to copy values from
232  */
233 void
235 {
236  const SpeechSynthInterface *oi = dynamic_cast<const SpeechSynthInterface *>(other);
237  if (oi == NULL) {
238  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
239  type(), other->type());
240  }
241  memcpy(data, oi->data, sizeof(SpeechSynthInterface_data_t));
242 }
243 
244 const char *
245 SpeechSynthInterface::enum_tostring(const char *enumtype, int val) const
246 {
247  throw UnknownTypeException("Unknown enum type %s", enumtype);
248 }
249 
250 /* =========== messages =========== */
251 /** @class SpeechSynthInterface::SayMessage <interfaces/SpeechSynthInterface.h>
252  * SayMessage Fawkes BlackBoard Interface Message.
253  *
254 
255  */
256 
257 
258 /** Constructor with initial values.
259  * @param ini_text initial value for text
260  */
261 SpeechSynthInterface::SayMessage::SayMessage(const char * ini_text) : Message("SayMessage")
262 {
263  data_size = sizeof(SayMessage_data_t);
264  data_ptr = malloc(data_size);
265  memset(data_ptr, 0, data_size);
266  data = (SayMessage_data_t *)data_ptr;
268  strncpy(data->text, ini_text, 1024);
269  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
270 }
271 /** Constructor */
273 {
274  data_size = sizeof(SayMessage_data_t);
275  data_ptr = malloc(data_size);
276  memset(data_ptr, 0, data_size);
277  data = (SayMessage_data_t *)data_ptr;
279  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
280 }
281 
282 /** Destructor */
284 {
285  free(data_ptr);
286 }
287 
288 /** Copy constructor.
289  * @param m message to copy from
290  */
292 {
293  data_size = m->data_size;
294  data_ptr = malloc(data_size);
295  memcpy(data_ptr, m->data_ptr, data_size);
296  data = (SayMessage_data_t *)data_ptr;
298 }
299 
300 /* Methods */
301 /** Get text value.
302  *
303  Last spoken string. Must be properly null-terminated.
304 
305  * @return text value
306  */
307 char *
309 {
310  return data->text;
311 }
312 
313 /** Get maximum length of text value.
314  * @return length of text value, can be length of the array or number of
315  * maximum number of characters for a string
316  */
317 size_t
319 {
320  return 1024;
321 }
322 
323 /** Set text value.
324  *
325  Last spoken string. Must be properly null-terminated.
326 
327  * @param new_text new text value
328  */
329 void
331 {
332  strncpy(data->text, new_text, sizeof(data->text));
333 }
334 
335 /** Clone this message.
336  * Produces a message of the same type as this message and copies the
337  * data to the new message.
338  * @return clone of this message
339  */
340 Message *
342 {
343  return new SpeechSynthInterface::SayMessage(this);
344 }
345 /** Check if message is valid and can be enqueued.
346  * @param message Message to check
347  * @return true if the message is valid, false otherwise.
348  */
349 bool
351 {
352  const SayMessage *m0 = dynamic_cast<const SayMessage *>(message);
353  if ( m0 != NULL ) {
354  return true;
355  }
356  return false;
357 }
358 
359 /// @cond INTERNALS
360 EXPORT_INTERFACE(SpeechSynthInterface)
361 /// @endcond
362 
363 
364 } // 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:124
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:44
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:314
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! This leans on time...
Definition: message.h:129
char * text() const
Get text value.
string field
Definition: types.h:47
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:79
void set_msgid(const uint32_t new_msgid)
Set msgid value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:373
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
const char * type() const
Get type of interface.
Definition: interface.cpp:651
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:45
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:436
boolean field
Definition: types.h:36
float duration() const
Get duration value.
const char * type() const
Get message type.
Definition: message.cpp:378
32 bit unsigned integer field
Definition: types.h:42
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.