Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / Arbiter.cpp @ f150aab5

History | View | Annotate | Download (11.238 KB)

1
/*
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

    
34
using namespace boost;
35
using namespace std;
36

    
37
Arbiter::Arbiter(std::string audio_output) {
38
        // initialize audio player:
39
        if (iequals(audio_output, "none")) {
40
                // allow none for no sound output
41
                audio_player = NULL;
42
        }
43
        else if (iequals(audio_output.substr(0, 3), "rsb")) {
44
#ifdef RSB_SUPPORT
45
                audio_player = new AudioPlayerRSB(audio_output);
46
#else
47
                printf("> ERROR: hlc is compiled without RSB support, RSB audio transport not available, defaulting to libao (default "
48
                       "output!)\n");
49
                audio_player = new AudioPlayerLibAO("");
50
#endif
51
        }
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

    
61
        gaze_state_animation_restart = true;
62
        emotion_target = &emotion_config_default;
63

    
64
        utterance = boost::shared_ptr<Utterance>(new Utterance());
65
}
66

    
67
Arbiter::~Arbiter() {
68
}
69

    
70
void Arbiter::set_default_emotion(EmotionState e) {
71
        // lock access
72
        mutex::scoped_lock scoped_lock(emotion_config_default_mutex);
73

    
74
        // update our config, ignore duration as this is the default emotion
75
        emotion_config_default.select_config(e.value);
76
        emotion_target = &emotion_config_default;
77

    
78
        gaze_state_animation_restart = true;
79

    
80
        printf("> stored default emotion\n");
81
}
82

    
83
void Arbiter::set_current_emotion(EmotionState e) {
84
        // lock access
85
        mutex::scoped_lock scoped_lock(emotion_config_current_mutex);
86

    
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
        emotion_target = &emotion_config_current;
93
        gaze_state_animation_restart = true;
94

    
95
        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
        mutex::scoped_lock scoped_lock(mouth_config_override_mutex);
101

    
102
        mouth_config = m;
103
}
104

    
105
void Arbiter::set_gaze_target(humotion::GazeState target) {
106
        // lock access
107
        mutex::scoped_lock scoped_lock(requested_gaze_state_mutex);
108

    
109
        requested_gaze_state = target;
110
}
111

    
112
void Arbiter::set_mouth_target(humotion::MouthState target) {
113
        // lock access
114
        mutex::scoped_lock scoped_lock(requested_mouth_state_mutex);
115

    
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
        mutex::scoped_lock lock_av(active_animation_vector_mutex);
124
        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
                }
159
                else {
160
                        ++it;
161
                }
162
        }
163
}
164

    
165
void Arbiter::speak(
166
   boost::shared_ptr<Utterance> u) { //, ao_sample_format audio_format, char *audio_data, unsigned int audio_len){
167
        // lock audio playback as such:
168
        mutex::scoped_lock scoped_lock_audio(audio_player_mutex);
169

    
170
        // lock utterance & store data:
171
        mutex::scoped_lock scoped_lock(utterance_mutex);
172
        utterance = u;
173
        scoped_lock.unlock();
174

    
175
        // start audio playback, this function returns once we started playback
176
        if (audio_player == NULL) {
177
                printf("> audio_player disabled, not speaking '%s'\n", u.get()->get_text().c_str());
178
        }
179
        else {
180
                audio_player->play(utterance->get_audio_data());
181
        }
182
        utterance->start_playback();
183

    
184
        // wait until audio playback was finished:
185
        if (audio_player != NULL) {
186
                while (audio_player->is_playing()) {
187
                        // save some cpu cycles:
188
                        usleep(1 * 1000); // 1ms
189
                }
190
        }
191

    
192
        // in case the audio output fails, we end up here before the utterance is finished.
193
        // so check now if the utterance finished as well:
194
        while (utterance->is_playing()) {
195
                // save some cpu cycles:
196
                usleep(1 * 1000); // 1ms
197
        }
198
}
199

    
200
bool Arbiter::speak_active() {
201
        if (audio_player == NULL) {
202
                return false;
203
        }
204
        else {
205
                return audio_player->is_playing();
206
        }
207
}
208

    
209
void Arbiter::arbitrate() {
210
        // handle arbitration, DO NOT RE-ORDER these calls!
211

    
212
        // try to process all incoming requests
213
        override_by_emotion();
214

    
215
        // overwrite with mouth targets from outside:
216
        override_by_mouth();
217

    
218
        // lip animation
219
        override_by_utterance();
220

    
221
        // animations
222
        override_by_animation();
223

    
224
        // gaze target is set by requested_gaze_target
225
        override_by_gaze();
226
}
227

    
228
void Arbiter::override_by_utterance() {
229
        // fetch MouthState by utterance:
230
        if (!utterance->is_playing()) {
231
                // not playing -> return
232
                return;
233
        }
234

    
235
        // fetch symbol
236
        string symbol = utterance->currently_active_phoneme();
237
        if ((symbol.empty()) || (symbol == "")) {
238
                return;
239
        }
240

    
241
        // fetch config
242
        mouth_config.init_by_symbol(symbol);
243

    
244
        // overwrite mouth with offsets from utterance player
245
        mouth_config.apply_on_mouth_state(&mouth_state);
246
}
247

    
248
void Arbiter::override_by_mouth() {
249
        // external mouth requests can overwrite the mouth targets!
250
        if ((requested_mouth_state.position_center + requested_mouth_state.position_left + requested_mouth_state.position_right) >
251
            0.0) {
252
                // values given -> override emotion data
253
                mouth_state = requested_mouth_state;
254
        }
255
        // requested_mouth_state.dump();
256
        // mouth_state.dump();
257
}
258

    
259
void Arbiter::override_by_gaze() {
260
        // copy from requested target:
261
        gaze_state.pan = requested_gaze_state.pan;
262
        gaze_state.tilt = requested_gaze_state.tilt;
263
        gaze_state.roll = requested_gaze_state.roll;
264

    
265
        gaze_state.gaze_type = requested_gaze_state.gaze_type;
266
        gaze_state.timestamp = requested_gaze_state.timestamp;
267
        gaze_state.vergence = requested_gaze_state.vergence;
268

    
269
        // add offset values from user:
270
        gaze_state.pan_offset += requested_gaze_state.pan_offset;
271
        gaze_state.tilt_offset += requested_gaze_state.tilt_offset;
272
}
273

    
274
void Arbiter::override_by_emotion() {
275
        if (emotion_target == NULL) {
276
                return;
277
        }
278

    
279
        // lock access
280
        mutex::scoped_lock scoped_lock1(emotion_config_default_mutex);
281

    
282
        // did the current emotion time out?
283
        if (!emotion_target->is_active()) {
284
                // revert to default:
285
                emotion_target = &emotion_config_default;
286
                // trigger soft fade
287
                gaze_state_animation_restart = true;
288
        }
289

    
290
        // emotions will set the mouth state here (will be overwritten by speak)
291
        mouth_state = emotion_target->mouth_override;
292
        // mouth_state = ...
293

    
294
        // emotions will add an offset to the gaze state:
295
        // pan,tilt,roll will be overwritten by override_by_gaze() we only keep the offset values from this!
296
        // FIXME: change to eyelid opening  upper/lower once humotion supports it
297
        // eyebrow angles come from this
298
        humotion::GazeState gaze_state_target = emotion_target->gaze_override;
299
        // apply a soft overblending to the new gaze target
300
        gaze_state = soft_overblend_gaze(gaze_state, gaze_state_target, emotion_target->overblend_time_ms);
301

    
302
        // gaze_state.dump();
303

    
304
        // IMPORTANT: clear request from emotion target:
305
        emotion_target->gaze_override.eyeblink_request_left = 0;
306
        emotion_target->gaze_override.eyeblink_request_right = 0;
307
}
308

    
309
humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, humotion::GazeState gaze_target,
310
                                                 unsigned int overblend_time) {
311
        humotion::GazeState result = gaze_now;
312

    
313
        result.eyeblink_request_left = 0;
314
        result.eyeblink_request_right = 0;
315

    
316
        if (gaze_state_animation_restart) {
317
                // new incoming target, set up soft fade:
318
                cout << "NEW EMOTION\n";
319
                gaze_state_old = gaze_state;
320
                gaze_state_end_time = get_system_time() + boost::posix_time::milliseconds(overblend_time);
321
                gaze_state_animation_restart = false;
322
        }
323
        // do smooth overblend, all targets should be reached at gaze_state_end_time
324
        boost::posix_time::time_duration tdiff = gaze_state_end_time - get_system_time();
325
        if (tdiff.is_negative()) {
326
                // animation is done, exit now
327
                return gaze_target;
328
        }
329
        else {
330
                // do smooth animation
331
                double diff_ms = tdiff.total_milliseconds();
332
                double tp = 1.0 - diff_ms / static_cast<double>(overblend_time);
333

    
334
                result.pan_offset = gaze_state_old.pan_offset + tp * (gaze_target.pan_offset - gaze_state_old.pan_offset);
335
                result.tilt_offset = gaze_state_old.tilt_offset + tp * (gaze_target.tilt_offset - gaze_state_old.tilt_offset);
336
                result.roll_offset = gaze_state_old.roll_offset + tp * (gaze_target.roll_offset - gaze_state_old.roll_offset);
337

    
338
                result.eyelid_opening_lower =
339
                   gaze_state_old.eyelid_opening_lower + tp * (gaze_target.eyelid_opening_lower - gaze_state_old.eyelid_opening_lower);
340
                result.eyelid_opening_upper =
341
                   gaze_state_old.eyelid_opening_upper + tp * (gaze_target.eyelid_opening_upper - gaze_state_old.eyelid_opening_upper);
342

    
343
                result.eyebrow_left = gaze_target.eyebrow_left;
344
                result.eyebrow_right = gaze_target.eyebrow_right;
345
        }
346

    
347
        return result;
348
}
349

    
350
void Arbiter::override_by_animation() {
351
        // lock access & iterate over vector:
352
        mutex::scoped_lock lock_av(active_animation_vector_mutex);
353
        active_animation_vector_t::iterator it;
354
        for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
355
                boost::shared_ptr<Animation> current_ani = *it;
356

    
357
                // gaze_state.dump();
358
                current_ani->apply_on_gazestate(&gaze_state);
359
                // gaze_state.dump();
360
        }
361
}
362

    
363
humotion::GazeState Arbiter::get_gaze_state() {
364
        // gaze_state.dump();
365
        return gaze_state;
366
}
367

    
368
humotion::MouthState Arbiter::get_mouth_state() {
369
        return mouth_state;
370
}