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