Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / Arbiter.cpp @ 995b7f4d

History | View | Annotate | Download (12.124 KB)

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