Revision f150aab5 server/src/Arbiter.cpp
server/src/Arbiter.cpp | ||
---|---|---|
1 | 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 |
*/ |
|
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 | 28 |
|
29 | 29 |
#include "Arbiter.h" |
30 | 30 |
#include "AudioPlayerLibAO.h" |
... | ... | |
34 | 34 |
using namespace boost; |
35 | 35 |
using namespace std; |
36 | 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()); |
|
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()); |
|
62 | 65 |
} |
63 | 66 |
|
64 |
Arbiter::~Arbiter(){ |
|
67 |
Arbiter::~Arbiter() {
|
|
65 | 68 |
} |
66 | 69 |
|
67 |
void Arbiter::set_default_emotion(EmotionState e){ |
|
68 |
//lock access
|
|
69 |
mutex::scoped_lock scoped_lock(emotion_config_default_mutex);
|
|
70 |
void Arbiter::set_default_emotion(EmotionState e) {
|
|
71 |
// lock access
|
|
72 |
mutex::scoped_lock scoped_lock(emotion_config_default_mutex);
|
|
70 | 73 |
|
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 |
// 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;
|
|
74 | 77 |
|
75 |
gaze_state_animation_restart = true;
|
|
78 |
gaze_state_animation_restart = true;
|
|
76 | 79 |
|
77 |
printf("> stored default emotion\n");
|
|
80 |
printf("> stored default emotion\n");
|
|
78 | 81 |
} |
79 | 82 |
|
80 |
void Arbiter::set_current_emotion(EmotionState e){ |
|
81 |
//lock access
|
|
82 |
mutex::scoped_lock scoped_lock(emotion_config_current_mutex);
|
|
83 |
void Arbiter::set_current_emotion(EmotionState e) {
|
|
84 |
// lock access
|
|
85 |
mutex::scoped_lock scoped_lock(emotion_config_current_mutex);
|
|
83 | 86 |
|
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);
|
|
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);
|
|
88 | 91 |
|
89 |
emotion_target = &emotion_config_current;
|
|
90 |
gaze_state_animation_restart = true;
|
|
92 |
emotion_target = &emotion_config_current;
|
|
93 |
gaze_state_animation_restart = true;
|
|
91 | 94 |
|
92 |
printf("> stored current emotion (val=%d, duration = %dms)\n",e.value, e.duration);
|
|
95 |
printf("> stored current emotion (val=%d, duration = %dms)\n", e.value, e.duration);
|
|
93 | 96 |
} |
94 | 97 |
|
95 |
void Arbiter::set_mouth_config(MouthConfig m){ |
|
96 |
//lock access
|
|
97 |
mutex::scoped_lock scoped_lock(mouth_config_override_mutex);
|
|
98 |
void Arbiter::set_mouth_config(MouthConfig m) {
|
|
99 |
// lock access
|
|
100 |
mutex::scoped_lock scoped_lock(mouth_config_override_mutex);
|
|
98 | 101 |
|
99 |
mouth_config = m;
|
|
102 |
mouth_config = m;
|
|
100 | 103 |
} |
101 | 104 |
|
102 |
void Arbiter::set_gaze_target(humotion::GazeState target){ |
|
103 |
//lock access
|
|
104 |
mutex::scoped_lock scoped_lock(requested_gaze_state_mutex);
|
|
105 |
void Arbiter::set_gaze_target(humotion::GazeState target) {
|
|
106 |
// lock access
|
|
107 |
mutex::scoped_lock scoped_lock(requested_gaze_state_mutex);
|
|
105 | 108 |
|
106 |
requested_gaze_state = target;
|
|
109 |
requested_gaze_state = target;
|
|
107 | 110 |
} |
108 | 111 |
|
109 |
void Arbiter::set_mouth_target(humotion::MouthState target){ |
|
110 |
//lock access
|
|
111 |
mutex::scoped_lock scoped_lock(requested_mouth_state_mutex);
|
|
112 |
void Arbiter::set_mouth_target(humotion::MouthState target) {
|
|
113 |
// lock access
|
|
114 |
mutex::scoped_lock scoped_lock(requested_mouth_state_mutex);
|
|
112 | 115 |
|
113 |
requested_mouth_state = target;
|
|
116 |
requested_mouth_state = target;
|
|
114 | 117 |
} |
115 | 118 |
|
116 | 119 |
//! 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 |
} |
|
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 |
} |
|
159 | 163 |
} |
160 | 164 |
|
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 |
|
|
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 |
} |
|
193 | 198 |
} |
194 | 199 |
|
195 |
bool Arbiter::speak_active(){ |
|
196 |
if (audio_player == NULL){ |
|
197 |
return false; |
|
198 |
}else{ |
|
199 |
return audio_player->is_playing(); |
|
200 |
} |
|
200 |
bool Arbiter::speak_active() { |
|
201 |
if (audio_player == NULL) { |
|
202 |
return false; |
|
203 |
} |
|
204 |
else { |
|
205 |
return audio_player->is_playing(); |
|
206 |
} |
|
201 | 207 |
} |
202 | 208 |
|
203 |
void Arbiter::arbitrate(){ |
|
204 |
//handle arbitration, DO NOT RE-ORDER these calls!
|
|
209 |
void Arbiter::arbitrate() {
|
|
210 |
// handle arbitration, DO NOT RE-ORDER these calls!
|
|
205 | 211 |
|
206 |
//try to process all incoming requests
|
|
207 |
override_by_emotion();
|
|
212 |
// try to process all incoming requests
|
|
213 |
override_by_emotion();
|
|
208 | 214 |
|
209 |
//overwrite with mouth targets from outside:
|
|
210 |
override_by_mouth();
|
|
215 |
// overwrite with mouth targets from outside:
|
|
216 |
override_by_mouth();
|
|
211 | 217 |
|
212 |
//lip animation
|
|
213 |
override_by_utterance();
|
|
218 |
// lip animation
|
|
219 |
override_by_utterance();
|
|
214 | 220 |
|
215 |
//animations
|
|
216 |
override_by_animation();
|
|
221 |
// animations
|
|
222 |
override_by_animation();
|
|
217 | 223 |
|
218 |
//gaze target is set by requested_gaze_target
|
|
219 |
override_by_gaze();
|
|
224 |
// gaze target is set by requested_gaze_target
|
|
225 |
override_by_gaze();
|
|
220 | 226 |
} |
221 | 227 |
|
222 |
void Arbiter::override_by_utterance(){ |
|
223 |
//fetch MouthState by utterance:
|
|
224 |
if (!utterance->is_playing()){
|
|
225 |
//not playing -> return
|
|
226 |
return;
|
|
227 |
}
|
|
228 |
void Arbiter::override_by_utterance() {
|
|
229 |
// fetch MouthState by utterance:
|
|
230 |
if (!utterance->is_playing()) {
|
|
231 |
// not playing -> return
|
|
232 |
return;
|
|
233 |
}
|
|
228 | 234 |
|
229 |
//fetch symbol
|
|
230 |
string symbol = utterance->currently_active_phoneme();
|
|
231 |
if ((symbol.empty()) || (symbol == "")){
|
|
232 |
return;
|
|
233 |
}
|
|
235 |
// fetch symbol
|
|
236 |
string symbol = utterance->currently_active_phoneme();
|
|
237 |
if ((symbol.empty()) || (symbol == "")) {
|
|
238 |
return;
|
|
239 |
}
|
|
234 | 240 |
|
235 |
//fetch config
|
|
236 |
mouth_config.init_by_symbol(symbol);
|
|
241 |
// fetch config
|
|
242 |
mouth_config.init_by_symbol(symbol);
|
|
237 | 243 |
|
238 |
//overwrite mouth with offsets from utterance player
|
|
239 |
mouth_config.apply_on_mouth_state(&mouth_state);
|
|
244 |
// overwrite mouth with offsets from utterance player
|
|
245 |
mouth_config.apply_on_mouth_state(&mouth_state);
|
|
240 | 246 |
} |
241 | 247 |
|
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(); |
|
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(); |
|
250 | 257 |
} |
251 | 258 |
|
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;
|
|
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;
|
|
257 | 264 |
|
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;
|
|
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;
|
|
261 | 268 |
|
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;
|
|
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;
|
|
265 | 272 |
} |
266 | 273 |
|
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; |
|
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; |
|
301 | 307 |
} |
302 | 308 |
|
303 |
humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, |
|
304 |
humotion::GazeState gaze_target, |
|
309 |
humotion::GazeState Arbiter::soft_overblend_gaze(humotion::GazeState gaze_now, humotion::GazeState gaze_target, |
|
305 | 310 |
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; |
|
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; |
|
341 | 348 |
} |
342 | 349 |
|
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 |
} |
|
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 |
} |
|
355 | 361 |
} |
356 | 362 |
|
357 |
humotion::GazeState Arbiter::get_gaze_state(){ |
|
358 |
//gaze_state.dump();
|
|
359 |
return gaze_state;
|
|
363 |
humotion::GazeState Arbiter::get_gaze_state() {
|
|
364 |
// gaze_state.dump();
|
|
365 |
return gaze_state;
|
|
360 | 366 |
} |
361 | 367 |
|
362 |
humotion::MouthState Arbiter::get_mouth_state(){ |
|
363 |
return mouth_state;
|
|
368 |
humotion::MouthState Arbiter::get_mouth_state() {
|
|
369 |
return mouth_state;
|
|
364 | 370 |
} |
Also available in: Unified diff