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