hlrc / server / src / Arbiter.cpp @ 84534fbf
History | View | Annotate | Download (11.079 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 <boost/algorithm/string/predicate.hpp> |
||
32 | 42084118 | Robert Haschke | #include <mutex> |
33 | #include <thread> |
||
34 | 90fa4b4e | Robert Haschke | #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 {
|
||
46 | audio_player = new AudioPlayerLibAO(audio_output);
|
||
47 | } |
||
48 | |||
49 | // set default emotion:
|
||
50 | emotion_config_current.init_sad(); |
||
51 | emotion_config_current.set_duration(2000);
|
||
52 | emotion_config_default.init_neutral(); |
||
53 | |||
54 | gaze_state_animation_restart = true;
|
||
55 | emotion_target = &emotion_config_default; |
||
56 | |||
57 | 482adb6d | Robert Haschke | utterance = std::shared_ptr<Utterance>(new Utterance());
|
58 | 0c286af0 | Simon Schulz | } |
59 | |||
60 | f150aab5 | Robert Haschke | Arbiter::~Arbiter() { |
61 | 0c286af0 | Simon Schulz | } |
62 | |||
63 | f150aab5 | Robert Haschke | void Arbiter::set_default_emotion(EmotionState e) {
|
64 | // lock access
|
||
65 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock(emotion_config_default_mutex);
|
66 | 0c286af0 | Simon Schulz | |
67 | f150aab5 | Robert Haschke | // update our config, ignore duration as this is the default emotion
|
68 | emotion_config_default.select_config(e.value); |
||
69 | emotion_target = &emotion_config_default; |
||
70 | efbcb710 | sschulz | |
71 | f150aab5 | Robert Haschke | gaze_state_animation_restart = true;
|
72 | 0c286af0 | Simon Schulz | |
73 | f150aab5 | Robert Haschke | printf("> stored default emotion\n");
|
74 | 0c286af0 | Simon Schulz | } |
75 | |||
76 | f150aab5 | Robert Haschke | void Arbiter::set_current_emotion(EmotionState e) {
|
77 | // lock access
|
||
78 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock(emotion_config_current_mutex);
|
79 | 0c286af0 | Simon Schulz | |
80 | f150aab5 | Robert Haschke | // this will set a emotion override for a given time, after the timeout
|
81 | // we will return to the default emotion state:
|
||
82 | emotion_config_current.select_config(e.value); |
||
83 | emotion_config_current.set_duration(e.duration); |
||
84 | 0c286af0 | Simon Schulz | |
85 | f150aab5 | Robert Haschke | emotion_target = &emotion_config_current; |
86 | gaze_state_animation_restart = true;
|
||
87 | efbcb710 | sschulz | |
88 | f150aab5 | Robert Haschke | printf("> stored current emotion (val=%d, duration = %dms)\n", e.value, e.duration);
|
89 | 0c286af0 | Simon Schulz | } |
90 | |||
91 | f150aab5 | Robert Haschke | void Arbiter::set_mouth_config(MouthConfig m) {
|
92 | // lock access
|
||
93 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock(mouth_config_override_mutex);
|
94 | 0c286af0 | Simon Schulz | |
95 | f150aab5 | Robert Haschke | mouth_config = m; |
96 | 0c286af0 | Simon Schulz | } |
97 | |||
98 | f150aab5 | Robert Haschke | void Arbiter::set_gaze_target(humotion::GazeState target) {
|
99 | // lock access
|
||
100 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock(requested_gaze_state_mutex);
|
101 | 0c286af0 | Simon Schulz | |
102 | f150aab5 | Robert Haschke | requested_gaze_state = target; |
103 | 0c286af0 | Simon Schulz | } |
104 | |||
105 | f150aab5 | Robert Haschke | void Arbiter::set_mouth_target(humotion::MouthState target) {
|
106 | // lock access
|
||
107 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock(requested_mouth_state_mutex);
|
108 | 0c286af0 | Simon Schulz | |
109 | f150aab5 | Robert Haschke | requested_mouth_state = target; |
110 | 0c286af0 | Simon Schulz | } |
111 | |||
112 | //! note: this is blocking!
|
||
113 | 482adb6d | Robert Haschke | void Arbiter::play_animation(std::shared_ptr<Animation> incoming_animation) {
|
114 | f150aab5 | Robert Haschke | // incoming animation, check if this would conflict with any pending animations:
|
115 | // lock access & iterate over vector:
|
||
116 | 42084118 | Robert Haschke | std::unique_lock<std::mutex> lock_av(active_animation_vector_mutex); |
117 | f150aab5 | Robert Haschke | active_animation_vector_t::iterator it; |
118 | for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
|
||
119 | 482adb6d | Robert Haschke | std::shared_ptr<Animation> current_ani = *it; |
120 | f150aab5 | Robert Haschke | // check if the running animation collides with the incoming animation:
|
121 | if (current_ani->collides_with(incoming_animation.get())) {
|
||
122 | // this would fail, we can not play this animation right now!
|
||
123 | throw runtime_error("incoming animation collides with unfinished active animation"); |
||
124 | return;
|
||
125 | } |
||
126 | } |
||
127 | |||
128 | // ok, fine. no other animations are active that would collide with the new request
|
||
129 | // thus it is safe to enqueue this item:
|
||
130 | active_animation_vector.push_back(incoming_animation); |
||
131 | |||
132 | // unlock mutex
|
||
133 | lock_av.unlock(); |
||
134 | |||
135 | // start playback!
|
||
136 | incoming_animation->start(); |
||
137 | |||
138 | // block until ani was played back
|
||
139 | while (incoming_animation->is_active()) {
|
||
140 | e8a50634 | Robert Haschke | std::this_thread::sleep_for(std::chrono::milliseconds(1)); // save cpu cycles |
141 | f150aab5 | Robert Haschke | } |
142 | |||
143 | // ok, it finished. we can safely remove it now:
|
||
144 | lock_av.lock(); |
||
145 | for (it = active_animation_vector.begin(); it < active_animation_vector.end();) {
|
||
146 | 482adb6d | Robert Haschke | std::shared_ptr<Animation> current_ani = *it; |
147 | f150aab5 | Robert Haschke | if (*it == incoming_animation) {
|
148 | // printf(">match -> remove incoming ani again\n");
|
||
149 | it = active_animation_vector.erase(it); |
||
150 | } |
||
151 | else {
|
||
152 | ++it; |
||
153 | } |
||
154 | } |
||
155 | 0c286af0 | Simon Schulz | } |
156 | |||
157 | f150aab5 | Robert Haschke | void Arbiter::speak(
|
158 | 482adb6d | Robert Haschke | std::shared_ptr<Utterance> u) { //, ao_sample_format audio_format, char *audio_data, unsigned int audio_len){
|
159 | f150aab5 | Robert Haschke | // lock audio playback as such:
|
160 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock_audio(audio_player_mutex);
|
161 | f150aab5 | Robert Haschke | |
162 | 42084118 | Robert Haschke | { |
163 | // lock utterance & store data:
|
||
164 | const std::lock_guard<std::mutex> lock(utterance_mutex);
|
||
165 | utterance = u; |
||
166 | } |
||
167 | f150aab5 | Robert Haschke | |
168 | // start audio playback, this function returns once we started playback
|
||
169 | if (audio_player == NULL) { |
||
170 | printf("> audio_player disabled, not speaking '%s'\n", u.get()->get_text().c_str());
|
||
171 | } |
||
172 | else {
|
||
173 | audio_player->play(utterance->get_audio_data()); |
||
174 | } |
||
175 | utterance->start_playback(); |
||
176 | |||
177 | // wait until audio playback was finished:
|
||
178 | if (audio_player != NULL) { |
||
179 | while (audio_player->is_playing()) {
|
||
180 | // save some cpu cycles:
|
||
181 | e8a50634 | Robert Haschke | std::this_thread::sleep_for(std::chrono::milliseconds(1)); // 1ms |
182 | f150aab5 | Robert Haschke | } |
183 | } |
||
184 | |||
185 | // in case the audio output fails, we end up here before the utterance is finished.
|
||
186 | // so check now if the utterance finished as well:
|
||
187 | while (utterance->is_playing()) {
|
||
188 | // save some cpu cycles:
|
||
189 | e8a50634 | Robert Haschke | std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
190 | f150aab5 | Robert Haschke | } |
191 | 0c286af0 | Simon Schulz | } |
192 | |||
193 | f150aab5 | Robert Haschke | bool Arbiter::speak_active() {
|
194 | if (audio_player == NULL) { |
||
195 | return false; |
||
196 | } |
||
197 | else {
|
||
198 | return audio_player->is_playing();
|
||
199 | } |
||
200 | 0c286af0 | Simon Schulz | } |
201 | |||
202 | f150aab5 | Robert Haschke | void Arbiter::arbitrate() {
|
203 | // handle arbitration, DO NOT RE-ORDER these calls!
|
||
204 | 0c286af0 | Simon Schulz | |
205 | f150aab5 | Robert Haschke | // try to process all incoming requests
|
206 | override_by_emotion(); |
||
207 | 0c286af0 | Simon Schulz | |
208 | f150aab5 | Robert Haschke | // overwrite with mouth targets from outside:
|
209 | override_by_mouth(); |
||
210 | 0c286af0 | Simon Schulz | |
211 | f150aab5 | Robert Haschke | // lip animation
|
212 | override_by_utterance(); |
||
213 | 0c286af0 | Simon Schulz | |
214 | f150aab5 | Robert Haschke | // animations
|
215 | override_by_animation(); |
||
216 | 0c286af0 | Simon Schulz | |
217 | f150aab5 | Robert Haschke | // gaze target is set by requested_gaze_target
|
218 | override_by_gaze(); |
||
219 | 0c286af0 | Simon Schulz | } |
220 | |||
221 | f150aab5 | Robert Haschke | void Arbiter::override_by_utterance() {
|
222 | // fetch MouthState by utterance:
|
||
223 | if (!utterance->is_playing()) {
|
||
224 | // not playing -> return
|
||
225 | return;
|
||
226 | } |
||
227 | 0c286af0 | Simon Schulz | |
228 | f150aab5 | Robert Haschke | // fetch symbol
|
229 | string symbol = utterance->currently_active_phoneme();
|
||
230 | if ((symbol.empty()) || (symbol == "")) { |
||
231 | return;
|
||
232 | } |
||
233 | 0c286af0 | Simon Schulz | |
234 | f150aab5 | Robert Haschke | // fetch config
|
235 | mouth_config.init_by_symbol(symbol); |
||
236 | 0c286af0 | Simon Schulz | |
237 | f150aab5 | Robert Haschke | // overwrite mouth with offsets from utterance player
|
238 | mouth_config.apply_on_mouth_state(&mouth_state); |
||
239 | 0c286af0 | Simon Schulz | } |
240 | |||
241 | f150aab5 | Robert Haschke | void Arbiter::override_by_mouth() {
|
242 | // external mouth requests can overwrite the mouth targets!
|
||
243 | if ((requested_mouth_state.position_center + requested_mouth_state.position_left + requested_mouth_state.position_right) >
|
||
244 | 0.0) { |
||
245 | // values given -> override emotion data
|
||
246 | mouth_state = requested_mouth_state; |
||
247 | } |
||
248 | // requested_mouth_state.dump();
|
||
249 | // mouth_state.dump();
|
||
250 | 0c286af0 | Simon Schulz | } |
251 | |||
252 | f150aab5 | Robert Haschke | void Arbiter::override_by_gaze() {
|
253 | // copy from requested target:
|
||
254 | gaze_state.pan = requested_gaze_state.pan; |
||
255 | gaze_state.tilt = requested_gaze_state.tilt; |
||
256 | gaze_state.roll = requested_gaze_state.roll; |
||
257 | 0c286af0 | Simon Schulz | |
258 | f150aab5 | Robert Haschke | gaze_state.gaze_type = requested_gaze_state.gaze_type; |
259 | gaze_state.timestamp = requested_gaze_state.timestamp; |
||
260 | gaze_state.vergence = requested_gaze_state.vergence; |
||
261 | 0c286af0 | Simon Schulz | |
262 | f150aab5 | Robert Haschke | // add offset values from user:
|
263 | gaze_state.pan_offset += requested_gaze_state.pan_offset; |
||
264 | gaze_state.tilt_offset += requested_gaze_state.tilt_offset; |
||
265 | 0c286af0 | Simon Schulz | } |
266 | |||
267 | f150aab5 | Robert Haschke | void Arbiter::override_by_emotion() {
|
268 | if (emotion_target == NULL) { |
||
269 | return;
|
||
270 | } |
||
271 | |||
272 | // lock access
|
||
273 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock1(emotion_config_default_mutex);
|
274 | f150aab5 | Robert Haschke | |
275 | // did the current emotion time out?
|
||
276 | if (!emotion_target->is_active()) {
|
||
277 | // revert to default:
|
||
278 | emotion_target = &emotion_config_default; |
||
279 | // trigger soft fade
|
||
280 | gaze_state_animation_restart = true;
|
||
281 | } |
||
282 | |||
283 | // emotions will set the mouth state here (will be overwritten by speak)
|
||
284 | mouth_state = emotion_target->mouth_override; |
||
285 | // mouth_state = ...
|
||
286 | |||
287 | // emotions will add an offset to the gaze state:
|
||
288 | // pan,tilt,roll will be overwritten by override_by_gaze() we only keep the offset values from this!
|
||
289 | // FIXME: change to eyelid opening upper/lower once humotion supports it
|
||
290 | // eyebrow angles come from this
|
||
291 | humotion::GazeState gaze_state_target = emotion_target->gaze_override; |
||
292 | // apply a soft overblending to the new gaze target
|
||
293 | gaze_state = soft_overblend_gaze(gaze_state, gaze_state_target, emotion_target->overblend_time_ms); |
||
294 | |||
295 | // gaze_state.dump();
|
||
296 | |||
297 | // IMPORTANT: clear request from emotion target:
|
||
298 | emotion_target->gaze_override.eyeblink_request_left = 0;
|
||
299 | emotion_target->gaze_override.eyeblink_request_right = 0;
|
||
300 | efbcb710 | sschulz | } |
301 | |||
302 | f150aab5 | Robert Haschke | humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, humotion::GazeState gaze_target, |
303 | efbcb710 | sschulz | unsigned int overblend_time) { |
304 | f150aab5 | Robert Haschke | humotion::GazeState result = gaze_now; |
305 | |||
306 | result.eyeblink_request_left = 0;
|
||
307 | result.eyeblink_request_right = 0;
|
||
308 | |||
309 | if (gaze_state_animation_restart) {
|
||
310 | // new incoming target, set up soft fade:
|
||
311 | cout << "NEW EMOTION\n";
|
||
312 | gaze_state_old = gaze_state; |
||
313 | 90fa4b4e | Robert Haschke | gaze_state_end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(overblend_time); |
314 | f150aab5 | Robert Haschke | gaze_state_animation_restart = false;
|
315 | } |
||
316 | // do smooth overblend, all targets should be reached at gaze_state_end_time
|
||
317 | 90fa4b4e | Robert Haschke | double diff_ms =
|
318 | std::chrono::duration<double, std::milli>(gaze_state_end_time - std::chrono::steady_clock::now()).count();
|
||
319 | if (diff_ms < 0) { |
||
320 | f150aab5 | Robert Haschke | // animation is done, exit now
|
321 | return gaze_target;
|
||
322 | } |
||
323 | else {
|
||
324 | // do smooth animation
|
||
325 | double tp = 1.0 - diff_ms / static_cast<double>(overblend_time); |
||
326 | |||
327 | result.pan_offset = gaze_state_old.pan_offset + tp * (gaze_target.pan_offset - gaze_state_old.pan_offset); |
||
328 | result.tilt_offset = gaze_state_old.tilt_offset + tp * (gaze_target.tilt_offset - gaze_state_old.tilt_offset); |
||
329 | result.roll_offset = gaze_state_old.roll_offset + tp * (gaze_target.roll_offset - gaze_state_old.roll_offset); |
||
330 | |||
331 | result.eyelid_opening_lower = |
||
332 | gaze_state_old.eyelid_opening_lower + tp * (gaze_target.eyelid_opening_lower - gaze_state_old.eyelid_opening_lower); |
||
333 | result.eyelid_opening_upper = |
||
334 | gaze_state_old.eyelid_opening_upper + tp * (gaze_target.eyelid_opening_upper - gaze_state_old.eyelid_opening_upper); |
||
335 | |||
336 | result.eyebrow_left = gaze_target.eyebrow_left; |
||
337 | result.eyebrow_right = gaze_target.eyebrow_right; |
||
338 | } |
||
339 | |||
340 | return result;
|
||
341 | 0c286af0 | Simon Schulz | } |
342 | |||
343 | f150aab5 | Robert Haschke | void Arbiter::override_by_animation() {
|
344 | // lock access & iterate over vector:
|
||
345 | 42084118 | Robert Haschke | const std::lock_guard<std::mutex> lock_av(active_animation_vector_mutex);
|
346 | f150aab5 | Robert Haschke | active_animation_vector_t::iterator it; |
347 | for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
|
||
348 | 482adb6d | Robert Haschke | std::shared_ptr<Animation> current_ani = *it; |
349 | f150aab5 | Robert Haschke | |
350 | // gaze_state.dump();
|
||
351 | current_ani->apply_on_gazestate(&gaze_state); |
||
352 | // gaze_state.dump();
|
||
353 | } |
||
354 | 0c286af0 | Simon Schulz | } |
355 | |||
356 | f150aab5 | Robert Haschke | humotion::GazeState Arbiter::get_gaze_state() { |
357 | // gaze_state.dump();
|
||
358 | return gaze_state;
|
||
359 | 0c286af0 | Simon Schulz | } |
360 | |||
361 | f150aab5 | Robert Haschke | humotion::MouthState Arbiter::get_mouth_state() { |
362 | return mouth_state;
|
||
363 | 0c286af0 | Simon Schulz | } |