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 *
78 SpeechSynthInterface::text() const
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
88 SpeechSynthInterface::maxlenof_text() const
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  set_field(data->text, new_text);
103 }
104 
105 /** Get msgid value.
106  *
107  The ID of the message that is currently being processed,
108  or 0 if no message is being processed.
109 
110  * @return msgid value
111  */
112 uint32_t
113 SpeechSynthInterface::msgid() const
114 {
115  return data->msgid;
116 }
117 
118 /** Get maximum length of msgid value.
119  * @return length of msgid value, can be length of the array or number of
120  * maximum number of characters for a string
121  */
122 size_t
123 SpeechSynthInterface::maxlenof_msgid() const
124 {
125  return 1;
126 }
127 
128 /** Set msgid value.
129  *
130  The ID of the message that is currently being processed,
131  or 0 if no message is being processed.
132 
133  * @param new_msgid new msgid value
134  */
135 void
136 SpeechSynthInterface::set_msgid(const uint32_t new_msgid)
137 {
138  set_field(data->msgid, new_msgid);
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
148 SpeechSynthInterface::is_final() const
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
158 SpeechSynthInterface::maxlenof_final() const
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  set_field(data->final, new_final);
173 }
174 
175 /** Get duration value.
176  *
177  Length in seconds that it takes to speek the current text, -1 if
178  unknown. This is the total duration of the current string, *not* the
179  duration of already spoken or yet to speak text!
180 
181  * @return duration value
182  */
183 float
184 SpeechSynthInterface::duration() const
185 {
186  return data->duration;
187 }
188 
189 /** Get maximum length of duration value.
190  * @return length of duration value, can be length of the array or number of
191  * maximum number of characters for a string
192  */
193 size_t
194 SpeechSynthInterface::maxlenof_duration() const
195 {
196  return 1;
197 }
198 
199 /** Set duration value.
200  *
201  Length in seconds that it takes to speek the current text, -1 if
202  unknown. This is the total duration of the current string, *not* the
203  duration of already spoken or yet to speak text!
204 
205  * @param new_duration new duration value
206  */
207 void
208 SpeechSynthInterface::set_duration(const float new_duration)
209 {
210  set_field(data->duration, new_duration);
211 }
212 
213 /* =========== message create =========== */
214 Message *
215 SpeechSynthInterface::create_message(const char *type) const
216 {
217  if ( strncmp("SayMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
218  return new SayMessage();
219  } else {
220  throw UnknownTypeException("The given type '%s' does not match any known "
221  "message type for this interface type.", type);
222  }
223 }
224 
225 
226 /** Copy values from other interface.
227  * @param other other interface to copy values from
228  */
229 void
230 SpeechSynthInterface::copy_values(const Interface *other)
231 {
232  const SpeechSynthInterface *oi = dynamic_cast<const SpeechSynthInterface *>(other);
233  if (oi == NULL) {
234  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
235  type(), other->type());
236  }
237  memcpy(data, oi->data, sizeof(SpeechSynthInterface_data_t));
238 }
239 
240 const char *
241 SpeechSynthInterface::enum_tostring(const char *enumtype, int val) const
242 {
243  throw UnknownTypeException("Unknown enum type %s", enumtype);
244 }
245 
246 /* =========== messages =========== */
247 /** @class SpeechSynthInterface::SayMessage <interfaces/SpeechSynthInterface.h>
248  * SayMessage Fawkes BlackBoard Interface Message.
249  *
250 
251  */
252 
253 
254 /** Constructor with initial values.
255  * @param ini_text initial value for text
256  */
257 SpeechSynthInterface::SayMessage::SayMessage(const char * ini_text) : Message("SayMessage")
258 {
259  data_size = sizeof(SayMessage_data_t);
260  data_ptr = malloc(data_size);
261  memset(data_ptr, 0, data_size);
262  data = (SayMessage_data_t *)data_ptr;
264  strncpy(data->text, ini_text, 1024-1);
265  data->text[1024-1] = 0;
266  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
267 }
268 /** Constructor */
270 {
271  data_size = sizeof(SayMessage_data_t);
272  data_ptr = malloc(data_size);
273  memset(data_ptr, 0, data_size);
274  data = (SayMessage_data_t *)data_ptr;
276  add_fieldinfo(IFT_STRING, "text", 1024, data->text);
277 }
278 
279 /** Destructor */
281 {
282  free(data_ptr);
283 }
284 
285 /** Copy constructor.
286  * @param m message to copy from
287  */
289 {
290  data_size = m->data_size;
291  data_ptr = malloc(data_size);
292  memcpy(data_ptr, m->data_ptr, data_size);
293  data = (SayMessage_data_t *)data_ptr;
295 }
296 
297 /* Methods */
298 /** Get text value.
299  *
300  Last spoken string. Must be properly null-terminated.
301 
302  * @return text value
303  */
304 char *
306 {
307  return data->text;
308 }
309 
310 /** Get maximum length of text value.
311  * @return length of text value, can be length of the array or number of
312  * maximum number of characters for a string
313  */
314 size_t
316 {
317  return 1024;
318 }
319 
320 /** Set text value.
321  *
322  Last spoken string. Must be properly null-terminated.
323 
324  * @param new_text new text value
325  */
326 void
328 {
329  set_field(data->text, new_text);
330 }
331 
332 /** Clone this message.
333  * Produces a message of the same type as this message and copies the
334  * data to the new message.
335  * @return clone of this message
336  */
337 Message *
339 {
340  return new SpeechSynthInterface::SayMessage(this);
341 }
342 /** Check if message is valid and can be enqueued.
343  * @param message Message to check
344  * @return true if the message is valid, false otherwise.
345  */
346 bool
348 {
349  const SayMessage *m0 = dynamic_cast<const SayMessage *>(message);
350  if ( m0 != NULL ) {
351  return true;
352  }
353  return false;
354 }
355 
356 /// @cond INTERNALS
357 EXPORT_INTERFACE(SpeechSynthInterface)
358 /// @endcond
359 
360 
361 } // 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
SayMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_text() const
Get maximum length of text value.
void set_text(const char *new_text)
Set text value.
virtual Message * clone() const
Clone this message.
SpeechSynthInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Fawkes library namespace.
@ IFT_STRING
string field
Definition: types.h:48
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152