Revision efbcb710 server/src/Arbiter.cpp

View differences:

server/src/Arbiter.cpp
34 34
using namespace boost;
35 35
using namespace std;
36 36

  
37

  
38 37
Arbiter::Arbiter(std::string audio_output){
39 38
    //initialize audio player:
40 39
    if (iequals(audio_output, "none")){
......
56 55
    emotion_config_current.set_duration(2000);
57 56
    emotion_config_default.init_neutral();
58 57

  
58
    gaze_state_animation_restart = true;
59
    emotion_target = &emotion_config_default;
60

  
59 61
    utterance = boost::shared_ptr<Utterance>(new Utterance());
60 62
}
61 63

  
......
68 70

  
69 71
    //update our config, ignore duration as this is the default emotion
70 72
    emotion_config_default.select_config(e.value);
73
    emotion_target = &emotion_config_default;
74

  
75
    gaze_state_animation_restart = true;
71 76

  
72 77
    printf("> stored default emotion\n");
73 78
}
......
81 86
    emotion_config_current.select_config(e.value);
82 87
    emotion_config_current.set_duration(e.duration);
83 88

  
89
    emotion_target = &emotion_config_current;
90
    gaze_state_animation_restart = true;
91

  
84 92
    printf("> stored current emotion (val=%d, duration = %dms)\n",e.value, e.duration);
85 93
}
86 94

  
......
257 265
}
258 266

  
259 267
void Arbiter::override_by_emotion(){
268
    if (emotion_target == NULL) {
269
        return;
270
    }
271

  
260 272
    //lock access
261 273
    mutex::scoped_lock scoped_lock1(emotion_config_default_mutex);
262
    mutex::scoped_lock scoped_lock2(emotion_config_current_mutex);
263 274

  
264
    EmotionConfig *emotion_target = &emotion_config_default;
265 275

  
266
    //which emotion is active?
267
    if (emotion_config_current.is_active()){
268
        //current emotion is not timed out, override default
269
        emotion_target = &emotion_config_current;
276
    // did the current emotion time out?
277
    if (!emotion_target->is_active()){
278
        // revert to default:
279
        emotion_target = &emotion_config_default;
280
        // trigger soft fade
281
        gaze_state_animation_restart = true;
270 282
    }
271 283

  
272 284
    //emotions will set the mouth state here (will be overwritten by speak)
273 285
    mouth_state = emotion_target->mouth_override;
286
    //mouth_state = ...
274 287

  
275 288
    //emotions will add an offset to the gaze state:
276 289
    //pan,tilt,roll will be overwritten by override_by_gaze() we only keep the offset values from this!
277 290
    //FIXME: change to eyelid opening  upper/lower once humotion supports it
278 291
    //eyebrow angles come from this
279
    gaze_state = emotion_target->gaze_override;
292
    humotion::GazeState gaze_state_target = emotion_target->gaze_override;
293
    // apply a soft overblending to the new gaze target
294
    gaze_state = soft_overblend_gaze(gaze_state, gaze_state_target, emotion_target->overblend_time_ms);
295

  
280 296
    //gaze_state.dump();
281 297

  
282 298
    //IMPORTANT: clear request from emotion target:
283 299
    emotion_target->gaze_override.eyeblink_request_left = 0;
284 300
    emotion_target->gaze_override.eyeblink_request_right = 0;
301
}
302

  
303
humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now,
304
                                                 humotion::GazeState gaze_target,
305
                                                 unsigned int overblend_time) {
306
    humotion::GazeState result = gaze_now;
307

  
308
    result.eyeblink_request_left = 0;
309
    result.eyeblink_request_right = 0;
310

  
311
    if (gaze_state_animation_restart) {
312
        // new incoming target, set up soft fade:
313
        gaze_state_old = gaze_state;
314
        gaze_state_end_time = get_system_time() + boost::posix_time::milliseconds(overblend_time);
315
        gaze_state_animation_restart = false;
316
    }
317
    // do smooth overblend, all targets should be reached at gaze_state_end_time
318
    boost::posix_time::time_duration tdiff = gaze_state_end_time - get_system_time();
319
    if (tdiff.is_negative()) {
320
        // animation is done, exit now
321
        return result;
322
    } else {
323
        // do smooth animation
324
        double diff_ms = tdiff.total_milliseconds();
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 = gaze_state_old.eyelid_opening_lower + tp * (gaze_target.eyelid_opening_lower - gaze_state_old.eyelid_opening_lower);
332
        result.eyelid_opening_upper = gaze_state_old.eyelid_opening_upper + tp * (gaze_target.eyelid_opening_upper - gaze_state_old.eyelid_opening_upper);
333

  
334
        result.eyebrow_left = gaze_state_old.eyebrow_left + tp * (gaze_target.eyebrow_left - gaze_state_old.eyebrow_left);
335
        result.eyebrow_right = gaze_state_old.eyebrow_right + tp * (gaze_target.eyebrow_right - gaze_state_old.eyebrow_right);
336

  
337
    }
285 338

  
339
    return result;
286 340
}
287 341

  
288 342

  

Also available in: Unified diff