hlrc / server / src / Arbiter.cpp @ 90fa4b4e
History | View | Annotate | Download (11.359 KB)
1 | 0c286af0 | Simon Schulz | /*
|
---|---|---|---|
2 | f150aab5 | Robert Haschke | * 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 | 0c286af0 | Simon Schulz | |
29 | #include "Arbiter.h" |
||
30 | #include "AudioPlayerLibAO.h" |
||
31 | #include "AudioPlayerRSB.h" |
||
32 | #include <boost/algorithm/string/predicate.hpp> |
||
33 | 90fa4b4e | Robert Haschke | #include <boost/thread/mutex.hpp> |
34 | #include <iostream> |
||
35 | 0c286af0 | Simon Schulz | |
36 | using namespace boost; |
||
37 | using namespace std; |
||
38 | |||
39 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | } |
68 | |||
69 | f150aab5 | Robert Haschke | Arbiter::~Arbiter() { |
70 | 0c286af0 | Simon Schulz | } |
71 | |||
72 | f150aab5 | Robert Haschke | void Arbiter::set_default_emotion(EmotionState e) {
|
73 | // lock access
|
||
74 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock(emotion_config_default_mutex); |
75 | 0c286af0 | Simon Schulz | |
76 | f150aab5 | Robert Haschke | // 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 | efbcb710 | sschulz | |
80 | f150aab5 | Robert Haschke | gaze_state_animation_restart = true;
|
81 | 0c286af0 | Simon Schulz | |
82 | f150aab5 | Robert Haschke | printf("> stored default emotion\n");
|
83 | 0c286af0 | Simon Schulz | } |
84 | |||
85 | f150aab5 | Robert Haschke | void Arbiter::set_current_emotion(EmotionState e) {
|
86 | // lock access
|
||
87 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock(emotion_config_current_mutex); |
88 | 0c286af0 | Simon Schulz | |
89 | f150aab5 | Robert Haschke | // 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 | 0c286af0 | Simon Schulz | |
94 | f150aab5 | Robert Haschke | emotion_target = &emotion_config_current; |
95 | gaze_state_animation_restart = true;
|
||
96 | efbcb710 | sschulz | |
97 | f150aab5 | Robert Haschke | printf("> stored current emotion (val=%d, duration = %dms)\n", e.value, e.duration);
|
98 | 0c286af0 | Simon Schulz | } |
99 | |||
100 | f150aab5 | Robert Haschke | void Arbiter::set_mouth_config(MouthConfig m) {
|
101 | // lock access
|
||
102 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock(mouth_config_override_mutex); |
103 | 0c286af0 | Simon Schulz | |
104 | f150aab5 | Robert Haschke | mouth_config = m; |
105 | 0c286af0 | Simon Schulz | } |
106 | |||
107 | f150aab5 | Robert Haschke | void Arbiter::set_gaze_target(humotion::GazeState target) {
|
108 | // lock access
|
||
109 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock(requested_gaze_state_mutex); |
110 | 0c286af0 | Simon Schulz | |
111 | f150aab5 | Robert Haschke | requested_gaze_state = target; |
112 | 0c286af0 | Simon Schulz | } |
113 | |||
114 | f150aab5 | Robert Haschke | void Arbiter::set_mouth_target(humotion::MouthState target) {
|
115 | // lock access
|
||
116 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock(requested_mouth_state_mutex); |
117 | 0c286af0 | Simon Schulz | |
118 | f150aab5 | Robert Haschke | requested_mouth_state = target; |
119 | 0c286af0 | Simon Schulz | } |
120 | |||
121 | //! note: this is blocking!
|
||
122 | f150aab5 | Robert Haschke | 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 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock lock_av(active_animation_vector_mutex); |
126 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | } |
166 | |||
167 | f150aab5 | Robert Haschke | 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 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock_audio(audio_player_mutex); |
171 | f150aab5 | Robert Haschke | |
172 | // lock utterance & store data:
|
||
173 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock(utterance_mutex); |
174 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | } |
201 | |||
202 | f150aab5 | Robert Haschke | bool Arbiter::speak_active() {
|
203 | if (audio_player == NULL) { |
||
204 | return false; |
||
205 | } |
||
206 | else {
|
||
207 | return audio_player->is_playing();
|
||
208 | } |
||
209 | 0c286af0 | Simon Schulz | } |
210 | |||
211 | f150aab5 | Robert Haschke | void Arbiter::arbitrate() {
|
212 | // handle arbitration, DO NOT RE-ORDER these calls!
|
||
213 | 0c286af0 | Simon Schulz | |
214 | f150aab5 | Robert Haschke | // try to process all incoming requests
|
215 | override_by_emotion(); |
||
216 | 0c286af0 | Simon Schulz | |
217 | f150aab5 | Robert Haschke | // overwrite with mouth targets from outside:
|
218 | override_by_mouth(); |
||
219 | 0c286af0 | Simon Schulz | |
220 | f150aab5 | Robert Haschke | // lip animation
|
221 | override_by_utterance(); |
||
222 | 0c286af0 | Simon Schulz | |
223 | f150aab5 | Robert Haschke | // animations
|
224 | override_by_animation(); |
||
225 | 0c286af0 | Simon Schulz | |
226 | f150aab5 | Robert Haschke | // gaze target is set by requested_gaze_target
|
227 | override_by_gaze(); |
||
228 | 0c286af0 | Simon Schulz | } |
229 | |||
230 | f150aab5 | Robert Haschke | void Arbiter::override_by_utterance() {
|
231 | // fetch MouthState by utterance:
|
||
232 | if (!utterance->is_playing()) {
|
||
233 | // not playing -> return
|
||
234 | return;
|
||
235 | } |
||
236 | 0c286af0 | Simon Schulz | |
237 | f150aab5 | Robert Haschke | // fetch symbol
|
238 | string symbol = utterance->currently_active_phoneme();
|
||
239 | if ((symbol.empty()) || (symbol == "")) { |
||
240 | return;
|
||
241 | } |
||
242 | 0c286af0 | Simon Schulz | |
243 | f150aab5 | Robert Haschke | // fetch config
|
244 | mouth_config.init_by_symbol(symbol); |
||
245 | 0c286af0 | Simon Schulz | |
246 | f150aab5 | Robert Haschke | // overwrite mouth with offsets from utterance player
|
247 | mouth_config.apply_on_mouth_state(&mouth_state); |
||
248 | 0c286af0 | Simon Schulz | } |
249 | |||
250 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | } |
260 | |||
261 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | |
267 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | |
271 | f150aab5 | Robert Haschke | // 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 | 0c286af0 | Simon Schulz | } |
275 | |||
276 | f150aab5 | Robert Haschke | void Arbiter::override_by_emotion() {
|
277 | if (emotion_target == NULL) { |
||
278 | return;
|
||
279 | } |
||
280 | |||
281 | // lock access
|
||
282 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock scoped_lock1(emotion_config_default_mutex); |
283 | f150aab5 | Robert Haschke | |
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 | efbcb710 | sschulz | } |
310 | |||
311 | f150aab5 | Robert Haschke | humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, humotion::GazeState gaze_target, |
312 | efbcb710 | sschulz | unsigned int overblend_time) { |
313 | f150aab5 | Robert Haschke | 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 | 90fa4b4e | Robert Haschke | gaze_state_end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(overblend_time); |
323 | f150aab5 | Robert Haschke | gaze_state_animation_restart = false;
|
324 | } |
||
325 | // do smooth overblend, all targets should be reached at gaze_state_end_time
|
||
326 | 90fa4b4e | Robert Haschke | 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 | f150aab5 | Robert Haschke | // 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 | 0c286af0 | Simon Schulz | } |
351 | |||
352 | f150aab5 | Robert Haschke | void Arbiter::override_by_animation() {
|
353 | // lock access & iterate over vector:
|
||
354 | 90fa4b4e | Robert Haschke | boost::mutex::scoped_lock lock_av(active_animation_vector_mutex); |
355 | f150aab5 | Robert Haschke | 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 | 0c286af0 | Simon Schulz | } |
364 | |||
365 | f150aab5 | Robert Haschke | humotion::GazeState Arbiter::get_gaze_state() { |
366 | // gaze_state.dump();
|
||
367 | return gaze_state;
|
||
368 | 0c286af0 | Simon Schulz | } |
369 | |||
370 | f150aab5 | Robert Haschke | humotion::MouthState Arbiter::get_mouth_state() { |
371 | return mouth_state;
|
||
372 | 0c286af0 | Simon Schulz | } |