hlrc / server / src / Arbiter.cpp @ 90fa4b4e
History | View | Annotate | Download (11.359 KB)
1 |
/*
|
---|---|
2 |
* This file is part of hlrc_server
|
3 |
*
|
4 |
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
5 |
* http://opensource.cit-ec.de/projects/hlrc_server
|
6 |
*
|
7 |
* This file may be licensed under the terms of the
|
8 |
* GNU General Public License Version 3 (the ``GPL''),
|
9 |
* or (at your option) any later version.
|
10 |
*
|
11 |
* Software distributed under the License is distributed
|
12 |
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
13 |
* express or implied. See the GPL for the specific language
|
14 |
* governing rights and limitations.
|
15 |
*
|
16 |
* You should have received a copy of the GPL along with this
|
17 |
* program. If not, go to http://www.gnu.org/licenses/gpl.html
|
18 |
* or write to the Free Software Foundation, Inc.,
|
19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
*
|
21 |
* The development of this software was supported by the
|
22 |
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
23 |
* The Excellence Cluster EXC 277 is a grant of the Deutsche
|
24 |
* Forschungsgemeinschaft (DFG) in the context of the German
|
25 |
* Excellence Initiative.
|
26 |
*
|
27 |
*/
|
28 |
|
29 |
#include "Arbiter.h" |
30 |
#include "AudioPlayerLibAO.h" |
31 |
#include "AudioPlayerRSB.h" |
32 |
#include <boost/algorithm/string/predicate.hpp> |
33 |
#include <boost/thread/mutex.hpp> |
34 |
#include <iostream> |
35 |
|
36 |
using namespace boost; |
37 |
using namespace std; |
38 |
|
39 |
Arbiter::Arbiter(std::string audio_output) {
|
40 |
// initialize audio player:
|
41 |
if (iequals(audio_output, "none")) { |
42 |
// allow none for no sound output
|
43 |
audio_player = NULL;
|
44 |
} |
45 |
else if (iequals(audio_output.substr(0, 3), "rsb")) { |
46 |
#ifdef RSB_SUPPORT
|
47 |
audio_player = new AudioPlayerRSB(audio_output);
|
48 |
#else
|
49 |
printf("> ERROR: hlc is compiled without RSB support, RSB audio transport not available, defaulting to libao (default "
|
50 |
"output!)\n");
|
51 |
audio_player = new AudioPlayerLibAO(""); |
52 |
#endif
|
53 |
} |
54 |
else {
|
55 |
audio_player = new AudioPlayerLibAO(audio_output);
|
56 |
} |
57 |
|
58 |
// set default emotion:
|
59 |
emotion_config_current.init_sad(); |
60 |
emotion_config_current.set_duration(2000);
|
61 |
emotion_config_default.init_neutral(); |
62 |
|
63 |
gaze_state_animation_restart = true;
|
64 |
emotion_target = &emotion_config_default; |
65 |
|
66 |
utterance = boost::shared_ptr<Utterance>(new Utterance());
|
67 |
} |
68 |
|
69 |
Arbiter::~Arbiter() { |
70 |
} |
71 |
|
72 |
void Arbiter::set_default_emotion(EmotionState e) {
|
73 |
// lock access
|
74 |
boost::mutex::scoped_lock scoped_lock(emotion_config_default_mutex); |
75 |
|
76 |
// update our config, ignore duration as this is the default emotion
|
77 |
emotion_config_default.select_config(e.value); |
78 |
emotion_target = &emotion_config_default; |
79 |
|
80 |
gaze_state_animation_restart = true;
|
81 |
|
82 |
printf("> stored default emotion\n");
|
83 |
} |
84 |
|
85 |
void Arbiter::set_current_emotion(EmotionState e) {
|
86 |
// lock access
|
87 |
boost::mutex::scoped_lock scoped_lock(emotion_config_current_mutex); |
88 |
|
89 |
// this will set a emotion override for a given time, after the timeout
|
90 |
// we will return to the default emotion state:
|
91 |
emotion_config_current.select_config(e.value); |
92 |
emotion_config_current.set_duration(e.duration); |
93 |
|
94 |
emotion_target = &emotion_config_current; |
95 |
gaze_state_animation_restart = true;
|
96 |
|
97 |
printf("> stored current emotion (val=%d, duration = %dms)\n", e.value, e.duration);
|
98 |
} |
99 |
|
100 |
void Arbiter::set_mouth_config(MouthConfig m) {
|
101 |
// lock access
|
102 |
boost::mutex::scoped_lock scoped_lock(mouth_config_override_mutex); |
103 |
|
104 |
mouth_config = m; |
105 |
} |
106 |
|
107 |
void Arbiter::set_gaze_target(humotion::GazeState target) {
|
108 |
// lock access
|
109 |
boost::mutex::scoped_lock scoped_lock(requested_gaze_state_mutex); |
110 |
|
111 |
requested_gaze_state = target; |
112 |
} |
113 |
|
114 |
void Arbiter::set_mouth_target(humotion::MouthState target) {
|
115 |
// lock access
|
116 |
boost::mutex::scoped_lock scoped_lock(requested_mouth_state_mutex); |
117 |
|
118 |
requested_mouth_state = target; |
119 |
} |
120 |
|
121 |
//! note: this is blocking!
|
122 |
void Arbiter::play_animation(boost::shared_ptr<Animation> incoming_animation) {
|
123 |
// incoming animation, check if this would conflict with any pending animations:
|
124 |
// lock access & iterate over vector:
|
125 |
boost::mutex::scoped_lock lock_av(active_animation_vector_mutex); |
126 |
active_animation_vector_t::iterator it; |
127 |
for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
|
128 |
boost::shared_ptr<Animation> current_ani = *it; |
129 |
// check if the running animation collides with the incoming animation:
|
130 |
if (current_ani->collides_with(incoming_animation.get())) {
|
131 |
// this would fail, we can not play this animation right now!
|
132 |
throw runtime_error("incoming animation collides with unfinished active animation"); |
133 |
return;
|
134 |
} |
135 |
} |
136 |
|
137 |
// ok, fine. no other animations are active that would collide with the new request
|
138 |
// thus it is safe to enqueue this item:
|
139 |
active_animation_vector.push_back(incoming_animation); |
140 |
|
141 |
// unlock mutex
|
142 |
lock_av.unlock(); |
143 |
|
144 |
// start playback!
|
145 |
incoming_animation->start(); |
146 |
|
147 |
// block until ani was played back
|
148 |
while (incoming_animation->is_active()) {
|
149 |
// printf("A"); fflush(stdout);
|
150 |
usleep(1 * 1000); // 1ms, save cpu cycles |
151 |
} |
152 |
|
153 |
// ok, it finished. we can safely remove it now:
|
154 |
lock_av.lock(); |
155 |
for (it = active_animation_vector.begin(); it < active_animation_vector.end();) {
|
156 |
boost::shared_ptr<Animation> current_ani = *it; |
157 |
if (*it == incoming_animation) {
|
158 |
// printf(">match -> remove incoming ani again\n");
|
159 |
it = active_animation_vector.erase(it); |
160 |
} |
161 |
else {
|
162 |
++it; |
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
void Arbiter::speak(
|
168 |
boost::shared_ptr<Utterance> u) { //, ao_sample_format audio_format, char *audio_data, unsigned int audio_len){
|
169 |
// lock audio playback as such:
|
170 |
boost::mutex::scoped_lock scoped_lock_audio(audio_player_mutex); |
171 |
|
172 |
// lock utterance & store data:
|
173 |
boost::mutex::scoped_lock scoped_lock(utterance_mutex); |
174 |
utterance = u; |
175 |
scoped_lock.unlock(); |
176 |
|
177 |
// start audio playback, this function returns once we started playback
|
178 |
if (audio_player == NULL) { |
179 |
printf("> audio_player disabled, not speaking '%s'\n", u.get()->get_text().c_str());
|
180 |
} |
181 |
else {
|
182 |
audio_player->play(utterance->get_audio_data()); |
183 |
} |
184 |
utterance->start_playback(); |
185 |
|
186 |
// wait until audio playback was finished:
|
187 |
if (audio_player != NULL) { |
188 |
while (audio_player->is_playing()) {
|
189 |
// save some cpu cycles:
|
190 |
usleep(1 * 1000); // 1ms |
191 |
} |
192 |
} |
193 |
|
194 |
// in case the audio output fails, we end up here before the utterance is finished.
|
195 |
// so check now if the utterance finished as well:
|
196 |
while (utterance->is_playing()) {
|
197 |
// save some cpu cycles:
|
198 |
usleep(1 * 1000); // 1ms |
199 |
} |
200 |
} |
201 |
|
202 |
bool Arbiter::speak_active() {
|
203 |
if (audio_player == NULL) { |
204 |
return false; |
205 |
} |
206 |
else {
|
207 |
return audio_player->is_playing();
|
208 |
} |
209 |
} |
210 |
|
211 |
void Arbiter::arbitrate() {
|
212 |
// handle arbitration, DO NOT RE-ORDER these calls!
|
213 |
|
214 |
// try to process all incoming requests
|
215 |
override_by_emotion(); |
216 |
|
217 |
// overwrite with mouth targets from outside:
|
218 |
override_by_mouth(); |
219 |
|
220 |
// lip animation
|
221 |
override_by_utterance(); |
222 |
|
223 |
// animations
|
224 |
override_by_animation(); |
225 |
|
226 |
// gaze target is set by requested_gaze_target
|
227 |
override_by_gaze(); |
228 |
} |
229 |
|
230 |
void Arbiter::override_by_utterance() {
|
231 |
// fetch MouthState by utterance:
|
232 |
if (!utterance->is_playing()) {
|
233 |
// not playing -> return
|
234 |
return;
|
235 |
} |
236 |
|
237 |
// fetch symbol
|
238 |
string symbol = utterance->currently_active_phoneme();
|
239 |
if ((symbol.empty()) || (symbol == "")) { |
240 |
return;
|
241 |
} |
242 |
|
243 |
// fetch config
|
244 |
mouth_config.init_by_symbol(symbol); |
245 |
|
246 |
// overwrite mouth with offsets from utterance player
|
247 |
mouth_config.apply_on_mouth_state(&mouth_state); |
248 |
} |
249 |
|
250 |
void Arbiter::override_by_mouth() {
|
251 |
// external mouth requests can overwrite the mouth targets!
|
252 |
if ((requested_mouth_state.position_center + requested_mouth_state.position_left + requested_mouth_state.position_right) >
|
253 |
0.0) { |
254 |
// values given -> override emotion data
|
255 |
mouth_state = requested_mouth_state; |
256 |
} |
257 |
// requested_mouth_state.dump();
|
258 |
// mouth_state.dump();
|
259 |
} |
260 |
|
261 |
void Arbiter::override_by_gaze() {
|
262 |
// copy from requested target:
|
263 |
gaze_state.pan = requested_gaze_state.pan; |
264 |
gaze_state.tilt = requested_gaze_state.tilt; |
265 |
gaze_state.roll = requested_gaze_state.roll; |
266 |
|
267 |
gaze_state.gaze_type = requested_gaze_state.gaze_type; |
268 |
gaze_state.timestamp = requested_gaze_state.timestamp; |
269 |
gaze_state.vergence = requested_gaze_state.vergence; |
270 |
|
271 |
// add offset values from user:
|
272 |
gaze_state.pan_offset += requested_gaze_state.pan_offset; |
273 |
gaze_state.tilt_offset += requested_gaze_state.tilt_offset; |
274 |
} |
275 |
|
276 |
void Arbiter::override_by_emotion() {
|
277 |
if (emotion_target == NULL) { |
278 |
return;
|
279 |
} |
280 |
|
281 |
// lock access
|
282 |
boost::mutex::scoped_lock scoped_lock1(emotion_config_default_mutex); |
283 |
|
284 |
// did the current emotion time out?
|
285 |
if (!emotion_target->is_active()) {
|
286 |
// revert to default:
|
287 |
emotion_target = &emotion_config_default; |
288 |
// trigger soft fade
|
289 |
gaze_state_animation_restart = true;
|
290 |
} |
291 |
|
292 |
// emotions will set the mouth state here (will be overwritten by speak)
|
293 |
mouth_state = emotion_target->mouth_override; |
294 |
// mouth_state = ...
|
295 |
|
296 |
// emotions will add an offset to the gaze state:
|
297 |
// pan,tilt,roll will be overwritten by override_by_gaze() we only keep the offset values from this!
|
298 |
// FIXME: change to eyelid opening upper/lower once humotion supports it
|
299 |
// eyebrow angles come from this
|
300 |
humotion::GazeState gaze_state_target = emotion_target->gaze_override; |
301 |
// apply a soft overblending to the new gaze target
|
302 |
gaze_state = soft_overblend_gaze(gaze_state, gaze_state_target, emotion_target->overblend_time_ms); |
303 |
|
304 |
// gaze_state.dump();
|
305 |
|
306 |
// IMPORTANT: clear request from emotion target:
|
307 |
emotion_target->gaze_override.eyeblink_request_left = 0;
|
308 |
emotion_target->gaze_override.eyeblink_request_right = 0;
|
309 |
} |
310 |
|
311 |
humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, humotion::GazeState gaze_target, |
312 |
unsigned int overblend_time) { |
313 |
humotion::GazeState result = gaze_now; |
314 |
|
315 |
result.eyeblink_request_left = 0;
|
316 |
result.eyeblink_request_right = 0;
|
317 |
|
318 |
if (gaze_state_animation_restart) {
|
319 |
// new incoming target, set up soft fade:
|
320 |
cout << "NEW EMOTION\n";
|
321 |
gaze_state_old = gaze_state; |
322 |
gaze_state_end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(overblend_time); |
323 |
gaze_state_animation_restart = false;
|
324 |
} |
325 |
// do smooth overblend, all targets should be reached at gaze_state_end_time
|
326 |
double diff_ms =
|
327 |
std::chrono::duration<double, std::milli>(gaze_state_end_time - std::chrono::steady_clock::now()).count();
|
328 |
if (diff_ms < 0) { |
329 |
// animation is done, exit now
|
330 |
return gaze_target;
|
331 |
} |
332 |
else {
|
333 |
// do smooth animation
|
334 |
double tp = 1.0 - diff_ms / static_cast<double>(overblend_time); |
335 |
|
336 |
result.pan_offset = gaze_state_old.pan_offset + tp * (gaze_target.pan_offset - gaze_state_old.pan_offset); |
337 |
result.tilt_offset = gaze_state_old.tilt_offset + tp * (gaze_target.tilt_offset - gaze_state_old.tilt_offset); |
338 |
result.roll_offset = gaze_state_old.roll_offset + tp * (gaze_target.roll_offset - gaze_state_old.roll_offset); |
339 |
|
340 |
result.eyelid_opening_lower = |
341 |
gaze_state_old.eyelid_opening_lower + tp * (gaze_target.eyelid_opening_lower - gaze_state_old.eyelid_opening_lower); |
342 |
result.eyelid_opening_upper = |
343 |
gaze_state_old.eyelid_opening_upper + tp * (gaze_target.eyelid_opening_upper - gaze_state_old.eyelid_opening_upper); |
344 |
|
345 |
result.eyebrow_left = gaze_target.eyebrow_left; |
346 |
result.eyebrow_right = gaze_target.eyebrow_right; |
347 |
} |
348 |
|
349 |
return result;
|
350 |
} |
351 |
|
352 |
void Arbiter::override_by_animation() {
|
353 |
// lock access & iterate over vector:
|
354 |
boost::mutex::scoped_lock lock_av(active_animation_vector_mutex); |
355 |
active_animation_vector_t::iterator it; |
356 |
for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
|
357 |
boost::shared_ptr<Animation> current_ani = *it; |
358 |
|
359 |
// gaze_state.dump();
|
360 |
current_ani->apply_on_gazestate(&gaze_state); |
361 |
// gaze_state.dump();
|
362 |
} |
363 |
} |
364 |
|
365 |
humotion::GazeState Arbiter::get_gaze_state() { |
366 |
// gaze_state.dump();
|
367 |
return gaze_state;
|
368 |
} |
369 |
|
370 |
humotion::MouthState Arbiter::get_mouth_state() { |
371 |
return mouth_state;
|
372 |
} |