Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / Arbiter.cpp @ d81c4f8b

History | View | Annotate | Download (11.079 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 <boost/algorithm/string/predicate.hpp>
32
#include <mutex>
33
#include <thread>
34
#include <iostream>
35

    
36
using namespace boost;
37
using namespace std;
38

    
39
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
        utterance = std::shared_ptr<Utterance>(new Utterance());
58
}
59

    
60
Arbiter::~Arbiter() {
61
}
62

    
63
void Arbiter::set_default_emotion(EmotionState e) {
64
        // lock access
65
        const std::lock_guard<std::mutex> lock(emotion_config_default_mutex);
66

    
67
        // 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

    
71
        gaze_state_animation_restart = true;
72

    
73
        printf("> stored default emotion\n");
74
}
75

    
76
void Arbiter::set_current_emotion(EmotionState e) {
77
        // lock access
78
        const std::lock_guard<std::mutex> lock(emotion_config_current_mutex);
79

    
80
        // 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

    
85
        emotion_target = &emotion_config_current;
86
        gaze_state_animation_restart = true;
87

    
88
        printf("> stored current emotion (val=%d, duration = %dms)\n", e.value, e.duration);
89
}
90

    
91
void Arbiter::set_mouth_config(MouthConfig m) {
92
        // lock access
93
        const std::lock_guard<std::mutex> lock(mouth_config_override_mutex);
94

    
95
        mouth_config = m;
96
}
97

    
98
void Arbiter::set_gaze_target(humotion::GazeState target) {
99
        // lock access
100
        const std::lock_guard<std::mutex> lock(requested_gaze_state_mutex);
101

    
102
        requested_gaze_state = target;
103
}
104

    
105
void Arbiter::set_mouth_target(humotion::MouthState target) {
106
        // lock access
107
        const std::lock_guard<std::mutex> lock(requested_mouth_state_mutex);
108

    
109
        requested_mouth_state = target;
110
}
111

    
112
//! note: this is blocking!
113
void Arbiter::play_animation(std::shared_ptr<Animation> incoming_animation) {
114
        // incoming animation, check if this would conflict with any pending animations:
115
        // lock access & iterate over vector:
116
        std::unique_lock<std::mutex> lock_av(active_animation_vector_mutex);
117
        active_animation_vector_t::iterator it;
118
        for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
119
                std::shared_ptr<Animation> current_ani = *it;
120
                // 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
                std::this_thread::sleep_for(std::chrono::milliseconds(1)); // save cpu cycles
141
        }
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
                std::shared_ptr<Animation> current_ani = *it;
147
                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
}
156

    
157
void Arbiter::speak(
158
   std::shared_ptr<Utterance> u) { //, ao_sample_format audio_format, char *audio_data, unsigned int audio_len){
159
        // lock audio playback as such:
160
        const std::lock_guard<std::mutex> lock_audio(audio_player_mutex);
161

    
162
        {
163
                // lock utterance & store data:
164
                const std::lock_guard<std::mutex> lock(utterance_mutex);
165
                utterance = u;
166
        }
167

    
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
                        std::this_thread::sleep_for(std::chrono::milliseconds(1)); // 1ms
182
                }
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
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
190
        }
191
}
192

    
193
bool Arbiter::speak_active() {
194
        if (audio_player == NULL) {
195
                return false;
196
        }
197
        else {
198
                return audio_player->is_playing();
199
        }
200
}
201

    
202
void Arbiter::arbitrate() {
203
        // handle arbitration, DO NOT RE-ORDER these calls!
204

    
205
        // try to process all incoming requests
206
        override_by_emotion();
207

    
208
        // overwrite with mouth targets from outside:
209
        override_by_mouth();
210

    
211
        // lip animation
212
        override_by_utterance();
213

    
214
        // animations
215
        override_by_animation();
216

    
217
        // gaze target is set by requested_gaze_target
218
        override_by_gaze();
219
}
220

    
221
void Arbiter::override_by_utterance() {
222
        // fetch MouthState by utterance:
223
        if (!utterance->is_playing()) {
224
                // not playing -> return
225
                return;
226
        }
227

    
228
        // fetch symbol
229
        string symbol = utterance->currently_active_phoneme();
230
        if ((symbol.empty()) || (symbol == "")) {
231
                return;
232
        }
233

    
234
        // fetch config
235
        mouth_config.init_by_symbol(symbol);
236

    
237
        // overwrite mouth with offsets from utterance player
238
        mouth_config.apply_on_mouth_state(&mouth_state);
239
}
240

    
241
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
}
251

    
252
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

    
258
        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

    
262
        // 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
}
266

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

    
272
        // lock access
273
        const std::lock_guard<std::mutex> lock1(emotion_config_default_mutex);
274

    
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
}
301

    
302
humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, humotion::GazeState gaze_target,
303
                                                 unsigned int overblend_time) {
304
        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
                gaze_state_end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(overblend_time);
314
                gaze_state_animation_restart = false;
315
        }
316
        // do smooth overblend, all targets should be reached at gaze_state_end_time
317
        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
                // 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
}
342

    
343
void Arbiter::override_by_animation() {
344
        // lock access & iterate over vector:
345
        const std::lock_guard<std::mutex> lock_av(active_animation_vector_mutex);
346
        active_animation_vector_t::iterator it;
347
        for (it = active_animation_vector.begin(); it < active_animation_vector.end(); it++) {
348
                std::shared_ptr<Animation> current_ani = *it;
349

    
350
                // gaze_state.dump();
351
                current_ani->apply_on_gazestate(&gaze_state);
352
                // gaze_state.dump();
353
        }
354
}
355

    
356
humotion::GazeState Arbiter::get_gaze_state() {
357
        // gaze_state.dump();
358
        return gaze_state;
359
}
360

    
361
humotion::MouthState Arbiter::get_mouth_state() {
362
        return mouth_state;
363
}