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