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  data_changed |= change_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
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
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  data_changed |= change_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
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_changed |= change_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
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
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  data_changed |= change_field(data->duration, new_duration);
211 }
212 
213 /* =========== message create =========== */
214 Message *
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
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  change_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
fawkes::SpeechSynthInterface::SayMessage::maxlenof_text
size_t maxlenof_text() const
Get maximum length of text value.
Definition: SpeechSynthInterface.cpp:315
fawkes::Interface::data_ptr
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
fawkes::SpeechSynthInterface::text
char * text() const
Get text value.
Definition: SpeechSynthInterface.cpp:78
fawkes::SpeechSynthInterface::SayMessage::SayMessage
SayMessage()
Constructor.
Definition: SpeechSynthInterface.cpp:269
fawkes::SpeechSynthInterface::set_final
void set_final(const bool new_final)
Set final value.
Definition: SpeechSynthInterface.cpp:170
fawkes::SpeechSynthInterface::copy_values
virtual void copy_values(const Interface *other)
Copy values from other interface.
Definition: SpeechSynthInterface.cpp:230
fawkes::Message
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
fawkes::SpeechSynthInterface::enum_tostring
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
Definition: SpeechSynthInterface.cpp:241
fawkes::SpeechSynthInterface::message_valid
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Definition: SpeechSynthInterface.cpp:347
fawkes::Message::data_ptr
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:128
fawkes::IFT_BOOL
@ IFT_BOOL
boolean field
Definition: types.h:37
fawkes::IFT_FLOAT
@ IFT_FLOAT
float field
Definition: types.h:46
fawkes::SpeechSynthInterface::maxlenof_duration
size_t maxlenof_duration() const
Get maximum length of duration value.
Definition: SpeechSynthInterface.cpp:194
fawkes::Message::data_ts
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:138
fawkes::SpeechSynthInterface::SayMessage::~SayMessage
~SayMessage()
Destructor.
Definition: SpeechSynthInterface.cpp:280
fawkes::IFT_UINT32
@ IFT_UINT32
32 bit unsigned integer field
Definition: types.h:43
fawkes::SpeechSynthInterface::SayMessage::text
char * text() const
Get text value.
Definition: SpeechSynthInterface.cpp:305
fawkes::Interface::type
const char * type() const
Get type of interface.
Definition: interface.cpp:643
fawkes::Message::message_data_ts_t
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:134
fawkes::SpeechSynthInterface::set_msgid
void set_msgid(const uint32_t new_msgid)
Set msgid value.
Definition: SpeechSynthInterface.cpp:136
fawkes::TypeMismatchException
Type mismatch.
Definition: software.h:44
fawkes::Interface::data_changed
bool data_changed
Indicator if data has changed.
Definition: interface.h:226
fawkes::UnknownTypeException
Unknown type.
Definition: software.h:50
fawkes::SpeechSynthInterface::is_final
bool is_final() const
Get final value.
Definition: SpeechSynthInterface.cpp:148
fawkes
Fawkes library namespace.
fawkes::Interface::set_hash
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:316
fawkes::Message::data_size
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:129
fawkes::SpeechSynthInterface
SpeechSynthInterface Fawkes BlackBoard Interface.
Definition: SpeechSynthInterface.h:34
fawkes::SpeechSynthInterface::maxlenof_msgid
size_t maxlenof_msgid() const
Get maximum length of msgid value.
Definition: SpeechSynthInterface.cpp:123
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
fawkes::SpeechSynthInterface::duration
float duration() const
Get duration value.
Definition: SpeechSynthInterface.cpp:184
fawkes::SpeechSynthInterface::maxlenof_text
size_t maxlenof_text() const
Get maximum length of text value.
Definition: SpeechSynthInterface.cpp:88
fawkes::SpeechSynthInterface::SayMessage
SayMessage Fawkes BlackBoard Interface Message.
Definition: SpeechSynthInterface.h:68
fawkes::SpeechSynthInterface::set_text
void set_text(const char *new_text)
Set text value.
Definition: SpeechSynthInterface.cpp:100
fawkes::Message::add_fieldinfo
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:400
fawkes::SpeechSynthInterface::SayMessage::set_text
void set_text(const char *new_text)
Set text value.
Definition: SpeechSynthInterface.cpp:327
fawkes::SpeechSynthInterface::create_message
virtual Message * create_message(const char *type) const
Create message based on type name.
Definition: SpeechSynthInterface.cpp:215
fawkes::SpeechSynthInterface::maxlenof_final
size_t maxlenof_final() const
Get maximum length of final value.
Definition: SpeechSynthInterface.cpp:158
fawkes::SpeechSynthInterface::SayMessage::clone
virtual Message * clone() const
Clone this message.
Definition: SpeechSynthInterface.cpp:338
fawkes::SpeechSynthInterface::msgid
uint32_t msgid() const
Get msgid value.
Definition: SpeechSynthInterface.cpp:113
fawkes::IFT_STRING
@ IFT_STRING
string field
Definition: types.h:48
fawkes::SpeechSynthInterface::set_duration
void set_duration(const float new_duration)
Set duration value.
Definition: SpeechSynthInterface.cpp:208
fawkes::Interface::add_messageinfo
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:375
fawkes::change_field
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: message.h:167