Statistics
| Branch: | Tag: | Revision:

hlrc / server / include / ROS / UtteranceCallbackWrapperROS.h @ 831c27b2

History | View | Annotate | Download (5.272 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
#pragma once
30
#include "hlrc_server/utteranceAction.h"
31
#include "CallbackWrapperROS.h"
32
#include "hlrc_server/phoneme.h"
33
#include "hlrc_server/soundchunk.h"
34

    
35
//callback handler incoming gaze requests:
36
class UtteranceCallbackWrapper : CallbackWrapper<hlrc_server::utteranceAction>{
37
protected:
38
    hlrc_server::utteranceFeedback feedback;
39
    hlrc_server::utteranceResult result;
40

    
41
public:
42

    
43
    UtteranceCallbackWrapper(Middleware *mw, std::string scope, std::string name) : CallbackWrapper<hlrc_server::utteranceAction>(mw, scope, name, boost::bind(&UtteranceCallbackWrapper::call, this, _1)){
44
        //
45
    };
46

    
47
    void call(const GoalConstPtr &goal){
48
        hlrc_server::utteranceGoalConstPtr request = goal;
49
        printf("> incoming utterance '%s' (%d phone symbols)\n", request->text.c_str(), (int)request->phonemes.size());
50

    
51
        //everything is ok, will be cleared on failures
52
        feedback.result = 1;
53

    
54
        Utterance utterance;
55

    
56
        //copy values:
57
        utterance.set_text(request->text);
58
        AudioData audio_data;
59
        if (!extract_audio(request->audio, &audio_data)){
60
            feedback.result = 0;
61
        }
62

    
63
        utterance.set_audio_data(audio_data);
64
        utterance.set_phoneme_vector(extract_phoneme_vector(request->phonemes));
65

    
66
        //send to application;
67
        mw->utterance_callback(utterance);
68

    
69

    
70
        if (feedback.result){
71
            result.result = 1;
72
            as_.setSucceeded(result);
73
        }else{
74
            as_.setAborted(result);
75
        }
76
    }
77

    
78

    
79
    //convert ros message audio data to our own implementation
80
    bool extract_audio(hlrc_server::soundchunk sound_chunk, AudioData *audio_data){
81
        //extract data:
82
        unsigned int audio_len = sound_chunk.data.size();
83
        char *audio_data_char = (char *)sound_chunk.data.data();
84

    
85
        //audio.samples = vector<char>(audio_data_char, audio_data_char+audio_len);
86
        audio_data->samples.resize(audio_len);
87
        audio_data->samples.assign(audio_data_char, audio_data_char+audio_len);
88

    
89
        printf("audio samplesize is %d bytes\n",(int)audio_data->samples.size());
90

    
91
        //extract format:
92
        audio_data->sample_signed = true;
93
        switch (sound_chunk.sample_type){
94
            case(hlrc_server::soundchunk::SAMPLE_U8):  audio_data->sample_signed = false; //and fall through:
95
            case(hlrc_server::soundchunk::SAMPLE_S8):  audio_data->sample_bit =  8; break;
96

    
97
            case(hlrc_server::soundchunk::SAMPLE_U16): audio_data->sample_signed = false; //and fall through:
98
            case(hlrc_server::soundchunk::SAMPLE_S16): audio_data->sample_bit = 16; break;
99

    
100
            case(hlrc_server::soundchunk::SAMPLE_U24): audio_data->sample_signed = false; //and fall through:
101
            case(hlrc_server::soundchunk::SAMPLE_S24): audio_data->sample_bit = 24; break;
102

    
103
            default:
104
                printf("> invalid sample type %d in SoundChunk! ignoring request!\n", sound_chunk.sample_type);
105
                return false;
106
                //throw runtime_error("UtteranceRSB::convert_audio_data() unsupported sample type in soundchunk");
107
        }
108

    
109
        //bitrate
110
        audio_data->sample_rate = sound_chunk.rate;
111

    
112
        //endianness
113
        if (sound_chunk.endianess == hlrc_server::soundchunk::ENDIAN_LITTLE){
114
            audio_data->sample_big_endian = false;
115
        }else if (sound_chunk.endianess == hlrc_server::soundchunk::ENDIAN_BIG){
116
            audio_data->sample_big_endian = true;
117
        }else{
118
            printf("> invalid SoundChunk byte_format");
119
            throw std::runtime_error("UtteranceRSB::convert_audio_data() unsupported byte_format in soundchunk");
120
        }
121

    
122
        //number of channels
123
        audio_data->sample_channels = sound_chunk.channels;
124

    
125
        printf("> new AudioData: %s\n",audio_data->to_string().c_str());
126

    
127
        return true;
128
    }
129

    
130
    //convert ros phoneme vector to out own implementation
131
    Utterance::phonemes_vector_t extract_phoneme_vector(std::vector<hlrc_server::phoneme> pv){
132
        Utterance::phonemes_vector_t result;
133

    
134
        //extract phoneme vector
135
        for(unsigned int i=0; i<pv.size(); i++){
136
            hlrc_server::phoneme ros_phoneme = pv[i];
137
            Utterance::symbol_duration_pair_t phoneme = make_pair(ros_phoneme.symbol, ros_phoneme.duration);
138
            result.push_back(phoneme);
139
        }
140

    
141
        return result;
142
    }
143

    
144
};
145

    
146