commit 2e526a15bb388ce5f0b2e50365f4bdfc7ee2fb57
Author: Simon Schulz <sschulz@cit-ec.uni-bielefeld.de>
Date:   Wed Jun 10 11:05:16 2015 +0200

    smart pointers everywhere

diff --git a/server/include/Arbiter.h b/server/include/Arbiter.h
index 9304e8e..feafe97 100644
--- a/server/include/Arbiter.h
+++ b/server/include/Arbiter.h
@@ -48,7 +48,7 @@ public:
     void set_default_emotion(EmotionState e);
     void set_current_emotion(EmotionState e);
     void set_mouth_config(MouthConfig m);
-    void speak(Utterance u);
+    void speak(boost::shared_ptr<Utterance> u);
     bool speak_active();
 
     void set_gaze_target(humotion::GazeState g);
@@ -73,7 +73,7 @@ private:
 
     MouthConfig mouth_config;
 
-    Utterance utterance;
+    boost::shared_ptr<Utterance> utterance;
 
     humotion::GazeState requested_gaze_state;
     humotion::MouthState requested_mouth_state;
diff --git a/server/include/AudioPlayer.h b/server/include/AudioPlayer.h
index 9182a48..0a43fea 100644
--- a/server/include/AudioPlayer.h
+++ b/server/include/AudioPlayer.h
@@ -49,11 +49,11 @@ public:
         CLOSED
     } PLAYBACKSTATE_T;
 
-    virtual void play(AudioData audio) = 0;
+    virtual void play(boost::shared_ptr<AudioData> audio) = 0;
     bool is_playing();
 
 protected:
-    AudioData audio_data;
+    boost::shared_ptr<AudioData> audio_data;
     PLAYBACKSTATE_T playback_state;
     std::string audio_driver;
     bool playback_requested;
diff --git a/server/include/AudioPlayerLibAO.h b/server/include/AudioPlayerLibAO.h
index e745308..aadf904 100644
--- a/server/include/AudioPlayerLibAO.h
+++ b/server/include/AudioPlayerLibAO.h
@@ -37,13 +37,13 @@ public:
     ~AudioPlayerLibAO();
 
     void playback_thread();
-    void play(AudioData audio);
+    void play(boost::shared_ptr<AudioData> audio);
     bool is_playing();
 
 
 
 private:
-    ao_sample_format extract_ao_format(AudioData audio);
+    ao_sample_format extract_ao_format(boost::shared_ptr<AudioData> audio);
     boost::thread *playback_thread_ptr;
 
     //std::vector<char> audio_data;
diff --git a/server/include/AudioPlayerRSB.h b/server/include/AudioPlayerRSB.h
index 3aadeae..59d1cd4 100644
--- a/server/include/AudioPlayerRSB.h
+++ b/server/include/AudioPlayerRSB.h
@@ -42,7 +42,7 @@ public:
     ~AudioPlayerRSB();
 
     void playback_thread();
-    void play(AudioData audio);
+    void play(boost::shared_ptr<AudioData> audio);
     bool is_playing();
 
 private:
diff --git a/server/include/Middleware.h b/server/include/Middleware.h
index d6a792c..431e4bb 100644
--- a/server/include/Middleware.h
+++ b/server/include/Middleware.h
@@ -41,14 +41,14 @@ public:
     virtual void tick() = 0;
 
     void speak_callback(std::string text);
-    void utterance_callback(Utterance u);
+    void utterance_callback(boost::shared_ptr<Utterance> u);
     void gaze_callback(humotion::GazeState gaze);
     void mouth_callback(humotion::MouthState mouth);
     void default_emotion_callback(EmotionState emotion_state);
     void current_emotion_callback(EmotionState emotion_state);
     void animation_callback(boost::shared_ptr<Animation> ani);
 
-    virtual Utterance tts_call(std::string text) = 0;
+    virtual boost::shared_ptr<Utterance> tts_call(std::string text) = 0;
 
 protected:
     Arbiter *arbiter;
diff --git a/server/include/MiddlewareROS.h b/server/include/MiddlewareROS.h
index 5b1a13e..227a287 100644
--- a/server/include/MiddlewareROS.h
+++ b/server/include/MiddlewareROS.h
@@ -58,8 +58,8 @@ public:
     void init(){};
     void tick(){};
 
-    Utterance tts_call(std::string text){
-        return Utterance();
+    boost::shared_ptr<Utterance> tts_call(std::string text){
+        return boost::shared_ptr<Utterance>(new Utterance());
     }
 
 #else
@@ -68,7 +68,7 @@ public:
     ~MiddlewareROS();
     void init();
     void tick();
-    Utterance tts_call(std::string text);
+    boost::shared_ptr<Utterance> tts_call(std::string text);
 
 private:
     //boost::shared_ptr<ros::NodeHandle> node_handle;
diff --git a/server/include/MiddlewareRSB.h b/server/include/MiddlewareRSB.h
index 4e8dca7..e8f86cb 100644
--- a/server/include/MiddlewareRSB.h
+++ b/server/include/MiddlewareRSB.h
@@ -46,8 +46,8 @@ public:
     ~MiddlewareRSB(){}
     void init(){};
     void tick(){};
-    Utterance tts_call(std::string text){
-        return Utterance();
+    boost::shared_ptr<Utterance> tts_call(std::string text){
+        return boost::shared_ptr<Utterance>(new Utterance());
     }
 
 #else
@@ -56,7 +56,7 @@ public:
     ~MiddlewareRSB();
     void init();
     void tick();
-    Utterance tts_call(std::string text);
+    boost::shared_ptr<Utterance> tts_call(std::string text);
 
 private:
     void init_callbacks();
diff --git a/server/include/ROS/UtteranceCallbackWrapperROS.h b/server/include/ROS/UtteranceCallbackWrapperROS.h
index 918b95c..8cbcc9e 100644
--- a/server/include/ROS/UtteranceCallbackWrapperROS.h
+++ b/server/include/ROS/UtteranceCallbackWrapperROS.h
@@ -51,17 +51,18 @@ public:
         //everything is ok, will be cleared on failures
         feedback.result = 1;
 
-        Utterance utterance;
+        boost::shared_ptr<Utterance> utterance(new Utterance());
 
         //copy values:
-        utterance.set_text(request->text);
-        AudioData audio_data;
-        if (!extract_audio(request->audio, &audio_data)){
+        utterance->set_text(request->text);
+
+        boost::shared_ptr<AudioData> audio_data(new AudioData());
+        if (!extract_audio(request->audio, audio_data)){
             feedback.result = 0;
         }
 
-        utterance.set_audio_data(audio_data);
-        utterance.set_phoneme_vector(extract_phoneme_vector(request->phonemes));
+        utterance->set_audio_data(audio_data);
+        utterance->set_phoneme_vector(extract_phoneme_vector(request->phonemes));
 
         //send to application;
         mw->utterance_callback(utterance);
@@ -77,7 +78,7 @@ public:
 
 
     //convert ros message audio data to our own implementation
-    bool extract_audio(hlrc_server::soundchunk sound_chunk, AudioData *audio_data){
+    bool extract_audio(hlrc_server::soundchunk sound_chunk, boost::shared_ptr<AudioData> audio_data){
         //extract data:
         unsigned int audio_len = sound_chunk.data.size();
         char *audio_data_char = (char *)sound_chunk.data.data();
diff --git a/server/include/RSB/UtteranceCallbackWrapper.h b/server/include/RSB/UtteranceCallbackWrapper.h
index bd45e11..a86a5b4 100644
--- a/server/include/RSB/UtteranceCallbackWrapper.h
+++ b/server/include/RSB/UtteranceCallbackWrapper.h
@@ -39,7 +39,7 @@ class UtteranceCallbackWrapper : public CallbackWrapper<rst::audition::Utterance
             rst::audition::Utterance *rst_utterance = param.get();
             printf("> incoming utterance '%s' (%d phone symbols)\n", param->text().c_str(), (int)rst_utterance->phonemes().size());
 
-            Utterance utterance = UtteranceRSB(*rst_utterance);
+            boost::shared_ptr<Utterance> utterance(new UtteranceRSB(*rst_utterance));
 
             //send to application;
             mw->utterance_callback(utterance);
diff --git a/server/include/RSB/UtteranceRSB.h b/server/include/RSB/UtteranceRSB.h
index 5e43ee9..2b36b25 100644
--- a/server/include/RSB/UtteranceRSB.h
+++ b/server/include/RSB/UtteranceRSB.h
@@ -56,22 +56,22 @@ class UtteranceRSB : public Utterance {
             char *audio_data_char = (char *)sound_chunk.data().c_str();
 
             //audio.samples = vector<char>(audio_data_char, audio_data_char+audio_len);
-            audio_data.samples.resize(audio_len);
-            audio_data.samples.assign(audio_data_char, audio_data_char+audio_len);
+            audio_data->samples.resize(audio_len);
+            audio_data->samples.assign(audio_data_char, audio_data_char+audio_len);
 
-            printf("audio samplesize is %d bytes\n",(unsigned int)audio_data.samples.size());
+            printf("audio samplesize is %d bytes\n",(unsigned int)audio_data->samples.size());
 
             //extract format:
-            audio_data.sample_signed = true;
+            audio_data->sample_signed = true;
             switch (sound_chunk.sample_type()){
-                case(rst::audition::SoundChunk::SAMPLE_U8):  audio_data.sample_signed = false; //and fall through:
-                case(rst::audition::SoundChunk::SAMPLE_S8):  audio_data.sample_bit =  8; break;
+                case(rst::audition::SoundChunk::SAMPLE_U8):  audio_data->sample_signed = false; //and fall through:
+                case(rst::audition::SoundChunk::SAMPLE_S8):  audio_data->sample_bit =  8; break;
 
-                case(rst::audition::SoundChunk::SAMPLE_U16): audio_data.sample_signed = false; //and fall through:
-                case(rst::audition::SoundChunk::SAMPLE_S16): audio_data.sample_bit = 16; break;
+                case(rst::audition::SoundChunk::SAMPLE_U16): audio_data->sample_signed = false; //and fall through:
+                case(rst::audition::SoundChunk::SAMPLE_S16): audio_data->sample_bit = 16; break;
 
-                case(rst::audition::SoundChunk::SAMPLE_U24): audio_data.sample_signed = false; //and fall through:
-                case(rst::audition::SoundChunk::SAMPLE_S24): audio_data.sample_bit = 24; break;
+                case(rst::audition::SoundChunk::SAMPLE_U24): audio_data->sample_signed = false; //and fall through:
+                case(rst::audition::SoundChunk::SAMPLE_S24): audio_data->sample_bit = 24; break;
 
                 default:
                     printf("> invalid sample type %d in RST SoundChunk! ignoring request!\n", sound_chunk.sample_type());
@@ -79,22 +79,22 @@ class UtteranceRSB : public Utterance {
             }
 
             //bitrate
-            audio_data.sample_rate = sound_chunk.rate();
+            audio_data->sample_rate = sound_chunk.rate();
 
         //endianness
         if (sound_chunk.endianness() == rst::audition::SoundChunk::ENDIAN_LITTLE){
-            audio_data.sample_big_endian = false;
+            audio_data->sample_big_endian = false;
         }else if (sound_chunk.endianness() == rst::audition::SoundChunk::ENDIAN_BIG){
-            audio_data.sample_big_endian = true;
+            audio_data->sample_big_endian = true;
         }else{
             printf("> invalid SoundChunk byte_format");
             throw runtime_error("UtteranceRSB::convert_audio_data() unsupported byte_format in rst::audition::SoundChunk");
         }
 
         //number of channels
-        audio_data.sample_channels = sound_chunk.channels();
+        audio_data->sample_channels = sound_chunk.channels();
 
-        printf("> new AudioData: %s\n",audio_data.to_string().c_str());
+        printf("> new AudioData: %s\n",audio_data->to_string().c_str());
     }
 
     void extract_phonemes(rst::audition::Utterance rst_utterance){
diff --git a/server/include/Utterance.h b/server/include/Utterance.h
index 3525731..b8ca9d2 100644
--- a/server/include/Utterance.h
+++ b/server/include/Utterance.h
@@ -44,16 +44,16 @@ public:
 
     void set_phoneme_vector(phonemes_vector_t p);
     void set_text(std::string t);
-    void set_audio_data(AudioData a);
+    void set_audio_data(boost::shared_ptr<AudioData> a);
 
     void start_playback();
     bool is_playing();
     std::string currently_active_phoneme();
-    AudioData get_audio_data();
+    boost::shared_ptr<AudioData> get_audio_data();
     std::string get_text();
 
 protected:
-    AudioData audio_data;
+    boost::shared_ptr<AudioData> audio_data;
     std::string text;
     phonemes_vector_t phonemes_vector;
 
diff --git a/server/src/Arbiter.cpp b/server/src/Arbiter.cpp
index 335612a..666825e 100644
--- a/server/src/Arbiter.cpp
+++ b/server/src/Arbiter.cpp
@@ -52,6 +52,8 @@ Arbiter::Arbiter(std::string audio_output){
     emotion_config_current.init_sad();
     emotion_config_current.set_duration(2000);
     emotion_config_default.init_neutral();
+
+    utterance = boost::shared_ptr<Utterance>(new Utterance());
 }
 
 Arbiter::~Arbiter(){
@@ -145,7 +147,7 @@ void Arbiter::play_animation(boost::shared_ptr<Animation> incoming_animation){
     }
 }
 
-void Arbiter::speak(Utterance u){ //, ao_sample_format audio_format, char *audio_data, unsigned int audio_len){
+void Arbiter::speak(boost::shared_ptr<Utterance> u){ //, ao_sample_format audio_format, char *audio_data, unsigned int audio_len){
     //lock audio playback as such:
     mutex::scoped_lock scoped_lock_audio(audio_player_mutex);
 
@@ -155,8 +157,8 @@ void Arbiter::speak(Utterance u){ //, ao_sample_format audio_format, char *audio
     scoped_lock.unlock();
 
     //start audio playback, this function returns once we started playback
-    audio_player->play(utterance.get_audio_data());
-    utterance.start_playback();
+    audio_player->play(utterance->get_audio_data());
+    utterance->start_playback();
 
     //wait until audio playback was finished:
     while(audio_player->is_playing()){
@@ -165,7 +167,7 @@ void Arbiter::speak(Utterance u){ //, ao_sample_format audio_format, char *audio
     }
     //in case the audio output fails, we end up here before the utterance is finished.
     //so check now if the utterance finished as well:
-    while (utterance.is_playing()){
+    while (utterance->is_playing()){
         //save some cpu cycles:
         usleep(1*1000); //1ms
     }
@@ -197,13 +199,13 @@ void Arbiter::arbitrate(){
 
 void Arbiter::override_by_utterance(){
     //fetch MouthState by utterance:
-    if (!utterance.is_playing()){
+    if (!utterance->is_playing()){
         //not playing -> return
         return;
     }
 
     //fetch symbol
-    string symbol = utterance.currently_active_phoneme();
+    string symbol = utterance->currently_active_phoneme();
     if ((symbol.empty()) || (symbol == "")){
         return;
     }
diff --git a/server/src/AudioPlayerLibAO.cpp b/server/src/AudioPlayerLibAO.cpp
index 5fb748c..b86a25e 100644
--- a/server/src/AudioPlayerLibAO.cpp
+++ b/server/src/AudioPlayerLibAO.cpp
@@ -52,13 +52,13 @@ AudioPlayerLibAO::AudioPlayerLibAO(string driver): AudioPlayer(driver){
 
 
 //this will return once we start playing
-void AudioPlayerLibAO::play(AudioData audio){
+void AudioPlayerLibAO::play(boost::shared_ptr<AudioData> audio){
     audio_data = audio;
 
-    printf("> AudioPlayerLibAO: play() %d samples requested\n",(int)audio_data.samples.size());
+    printf("> AudioPlayerLibAO: play() %d samples requested\n",(int)audio_data->samples.size());
 
     //skip empty audio data
-    if (audio_data.samples.size() == 0){
+    if (audio_data->samples.size() == 0){
         printf("> warning: empty audio data - skipped.\n");
         return;
     }
@@ -82,17 +82,17 @@ void AudioPlayerLibAO::play(AudioData audio){
 }
 
 
-ao_sample_format AudioPlayerLibAO::extract_ao_format(AudioData audio){
+ao_sample_format AudioPlayerLibAO::extract_ao_format(boost::shared_ptr<AudioData> audio){
     ao_sample_format ao_format;
 
     //get bits per sample
-    ao_format.bits = audio.sample_bit;
+    ao_format.bits = audio->sample_bit;
     //bitrate
-    ao_format.rate = audio.sample_rate;
+    ao_format.rate = audio->sample_rate;
     //endianess
-    ao_format.byte_format = audio.sample_big_endian?AO_FMT_BIG:AO_FMT_LITTLE;
+    ao_format.byte_format = audio->sample_big_endian?AO_FMT_BIG:AO_FMT_LITTLE;
     //number of channels
-    ao_format.channels = audio.sample_channels;
+    ao_format.channels = audio->sample_channels;
 
     //format matrix:
     if (ao_format.channels == 1){
@@ -156,7 +156,7 @@ void AudioPlayerLibAO::playback_thread(){
                     playback_state = PLAYING;
                     playback_active = true;
 
-                    ao_play(device, audio_data.samples.data(), audio_data.samples.size());
+                    ao_play(device, audio_data->samples.data(), audio_data->samples.size());
                     printf("> AudioPlayer: finished playback\n");
                 }
                 break;
diff --git a/server/src/AudioPlayerRSB.cpp b/server/src/AudioPlayerRSB.cpp
index 6da3b3b..3663f89 100644
--- a/server/src/AudioPlayerRSB.cpp
+++ b/server/src/AudioPlayerRSB.cpp
@@ -63,10 +63,10 @@ AudioPlayerRSB::AudioPlayerRSB(string driver): AudioPlayer(driver){
 
 
 //this will return once we start playing
-void AudioPlayerRSB::play(AudioData _audio_data){
+void AudioPlayerRSB::play(boost::shared_ptr<AudioData> _audio_data){
     audio_data = _audio_data;
 
-    printf("> AudioPlayerRSB: play() %d samples requested\n",(int)audio_data.samples.size());
+    printf("> AudioPlayerRSB: play() %d samples requested\n",(int)audio_data->samples.size());
 
     //check if we can play this file:
     while(playback_state != IDLE){
@@ -156,8 +156,8 @@ void AudioPlayerRSB::playback_thread(){
 void AudioPlayerRSB::publish_audio_data(){
     boost::shared_ptr<rst::audition::SoundChunk> request(new rst::audition::SoundChunk());
 
-    request->set_channels(audio_data.sample_channels);
-    request->set_data(audio_data.samples.data());
+    request->set_channels(audio_data->sample_channels);
+    request->set_data(audio_data->samples.data());
 /*
     if (audio_data.sample_big_endian){
         request->set_endianness(rst::audition::SoundChunk_EndianNess_ENDIAN_BIG);
diff --git a/server/src/Middleware.cpp b/server/src/Middleware.cpp
index cbc2a7f..0be5e5a 100644
--- a/server/src/Middleware.cpp
+++ b/server/src/Middleware.cpp
@@ -44,14 +44,14 @@ void Middleware::speak_callback(string text){
     printf("> %s(%s) called\n", __FUNCTION__,text.c_str());
 
     //call a tts system to convert the text to an utterance:
-    Utterance utterance = tts_call(text);
+    boost::shared_ptr<Utterance> utterance = tts_call(text);
 
     //and then process it
     utterance_callback(utterance);
 }
 
-void Middleware::utterance_callback(Utterance utterance){
-    printf("> %s(text=%s) called\n", __FUNCTION__,utterance.get_text().c_str());
+void Middleware::utterance_callback(boost::shared_ptr<Utterance> utterance){
+    printf("> %s(text=%s) called\n", __FUNCTION__,utterance->get_text().c_str());
 
     //can we speak this now?
     if (arbiter->speak_active()){
diff --git a/server/src/MiddlewareROS.cpp b/server/src/MiddlewareROS.cpp
index 6c16b33..1de32b3 100644
--- a/server/src/MiddlewareROS.cpp
+++ b/server/src/MiddlewareROS.cpp
@@ -93,10 +93,10 @@ void MiddlewareROS::tick(){
 }
 
 //call a tts system to convert a string to an utterance
-Utterance MiddlewareROS::tts_call(string text){
+boost::shared_ptr<Utterance> MiddlewareROS::tts_call(string text){
     //double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
 
-    Utterance utterance;
+    boost::shared_ptr<Utterance> utterance(new Utterance());
 
     printf("> WARNING: ros tts call not implemented yet\n");
 
@@ -120,6 +120,7 @@ Utterance MiddlewareROS::tts_call(string text){
 
     printf("> done. got utterance (text=%s)\n",utterance.get_text().c_str());
     */
+
     return utterance;
 }
 
diff --git a/server/src/MiddlewareRSB.cpp b/server/src/MiddlewareRSB.cpp
index 2ce7116..444621d 100644
--- a/server/src/MiddlewareRSB.cpp
+++ b/server/src/MiddlewareRSB.cpp
@@ -136,11 +136,9 @@ void MiddlewareRSB::init_callbacks(){
 }
 
 //call a tts system to convert a string to an utterance
-Utterance MiddlewareRSB::tts_call(string text){
+boost::shared_ptr<Utterance> MiddlewareRSB::tts_call(string text){
     double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
 
-    Utterance utterance;
-
     //build request
     boost::shared_ptr<std::string> request(new string(text));
 
@@ -150,7 +148,11 @@ Utterance MiddlewareRSB::tts_call(string text){
 
         //try to fetch the result
         boost::shared_ptr<rst::audition::Utterance> utterance_ptr = future_ptr.get(tts_timeout);
-        utterance = UtteranceRSB(*(utterance_ptr.get()));
+
+        //done, return utterance ptr
+        boost::shared_ptr<Utterance> utterance(new UtteranceRSB(*(utterance_ptr.get())));
+        printf("> done. got utterance (text=%s)\n",utterance->get_text().c_str());
+        return utterance;
 
     }catch(rsc::threading::FutureTimeoutException e){
         printf("> error: tts_call timed out after %3.1f seconds.\n", tts_timeout);
@@ -158,7 +160,8 @@ Utterance MiddlewareRSB::tts_call(string text){
         printf("> error: tts_call failed: %s\n", e.what());
     }
 
-    printf("> done. got utterance (text=%s)\n",utterance.get_text().c_str());
+    printf("> failed... got no utterance\n");
+    boost::shared_ptr<Utterance> utterance(new Utterance());
     return utterance;
 }
 
diff --git a/server/src/Utterance.cpp b/server/src/Utterance.cpp
index 2dd5619..7dc945c 100644
--- a/server/src/Utterance.cpp
+++ b/server/src/Utterance.cpp
@@ -35,6 +35,7 @@ using namespace boost;
 Utterance::Utterance(){
     playing = false;
     text = "UNINITIALISED UTTERANCE";
+    audio_data = boost::shared_ptr<AudioData>(new AudioData());
 }
 
 void Utterance::set_phoneme_vector(phonemes_vector_t p){
@@ -45,7 +46,7 @@ void Utterance::set_text(string t){
     text = t;
 }
 
-void Utterance::set_audio_data(AudioData a){
+void Utterance::set_audio_data(boost::shared_ptr<AudioData> a){
     audio_data = a;
 }
 
@@ -56,7 +57,7 @@ string Utterance::get_text(){
     return text;
 }
 
-AudioData Utterance::get_audio_data(){
+boost::shared_ptr<AudioData> Utterance::get_audio_data(){
     return audio_data;
 }
 
