SpeechObserver.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#include "SpeechObserver.h"
25
26#include <Ice/ObjectAdapter.h>
27
29
30using namespace armarx;
31
35
36void
38{
39 usingTopic(getProperty<std::string>("TextToSpeechTopicName").getValue());
40 usingTopic(getProperty<std::string>("TextToSpeechStateTopicName").getValue());
41}
42
43void
45{
46 offerChannel("TextToSpeech", "TextToSpeech channel");
47 offerDataFieldWithDefault("TextToSpeech", "Text", Variant(""), "Current text");
48 offerDataFieldWithDefault("TextToSpeech",
49 "TextChangeCounter",
50 Variant(reportTextCounter),
51 "Counter for text updates");
52
53 offerDataFieldWithDefault("TextToSpeech", "State", Variant(""), "Current TTS state");
54 offerDataFieldWithDefault("TextToSpeech",
55 "StateChangeCounter",
56 Variant(reportStateCounter),
57 "Counter for state updates");
58
59 offerChannel("SpeechToText", "SpeechToText channel");
60 offerDataFieldWithDefault("SpeechToText",
61 "RecognizedText",
62 Variant(std::string()),
63 "Text recognized by the SpeechRecogntion");
64
65 auto proxy = getObjectAdapter()->addWithUUID(new SpeechListenerImpl(this));
66 getIceManager()->subscribeTopic(proxy, "Speech_Commands");
67}
68
69std::string
70SpeechObserver::SpeechStateToString(TextToSpeechStateType state)
71{
72 switch (state)
73 {
74 case eIdle:
75 return "Idle";
76 case eStartedSpeaking:
77 return "StartedSpeaking";
78 case eFinishedSpeaking:
79 return "FinishedSpeaking";
80
81 default:
82 throw LocalException("Unsupported TextToSpeechStateType: ") << state;
83 }
84}
85
86void
87SpeechObserver::reportState(TextToSpeechStateType state, const Ice::Current&)
88{
89 std::unique_lock lock(dataMutex);
90 reportStateCounter++;
91 setDataField("TextToSpeech", "State", Variant(SpeechStateToString(state)));
92 setDataField("TextToSpeech", "StateChangeCounter", Variant(reportStateCounter));
93 updateChannel("TextToSpeech");
94}
95
96void
97SpeechObserver::reportText(const std::string& text, const Ice::Current& c)
98{
99 std::unique_lock lock(dataMutex);
100 reportTextCounter++;
101 setDataField("TextToSpeech", "Text", Variant(text));
102 setDataField("TextToSpeech", "TextChangeCounter", Variant(reportTextCounter));
103 updateChannel("TextToSpeech");
104}
105
106void
108 const Ice::StringSeq& params,
109 const Ice::Current&)
110{
111 std::unique_lock lock(dataMutex);
112 ARMARX_WARNING << "reportTextWithParams is not implemented";
113}
114
118
119void
120armarx::SpeechListenerImpl::reportText(const std::string& text, const Ice::Current&)
121{
122 std::unique_lock lock(dataMutex);
123 JSONObject json;
124 json.fromString(text);
125 obs->setDataField("SpeechToText", "RecognizedText", Variant(json.getString("text")));
126}
127
128void
130 const Ice::StringSeq&,
131 const Ice::Current&)
132{
133 std::unique_lock lock(dataMutex);
134 ARMARX_WARNING << "reportTextWithParams is not implemented";
135}
constexpr T c
Property< PropertyType > getProperty(const std::string &name)
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
std::string getString(const ::std::string &key) const override
void fromString(const ::std::string &jsonString, const ::Ice::Current &=Ice::emptyCurrent) override
Ice::ObjectAdapterPtr getObjectAdapter() const
Returns object's Ice adapter.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
IceManagerPtr getIceManager() const
Returns the IceManager.
void offerChannel(std::string channelName, std::string description)
Offer a channel.
Definition Observer.cpp:131
void updateChannel(const std::string &channelName, const std::set< std::string > &updatedDatafields=std::set< std::string >())
Update all conditions for a channel.
Definition Observer.cpp:788
void offerDataFieldWithDefault(std::string channelName, std::string datafieldName, const Variant &defaultValue, std::string description)
Offer a datafield with default value.
Definition Observer.cpp:160
void setDataField(const std::string &channelName, const std::string &datafieldName, const Variant &value, bool triggerFilterUpdate=true)
set datafield with datafieldName and in channel channelName
Definition Observer.cpp:508
void reportText(const std::string &, const Ice::Current &) override
SpeechListenerImpl(SpeechObserver *obs)
void reportTextWithParams(const std::string &, const Ice::StringSeq &, const Ice::Current &) override
void onConnectObserver() override
Framework hook.
virtual void reportTextWithParams(const std::string &text, const Ice::StringSeq &params, const Ice::Current &=Ice::emptyCurrent) override
virtual void reportText(const std::string &text, const Ice::Current &=Ice::emptyCurrent) override
friend class SpeechListenerImpl
static std::string SpeechStateToString(TextToSpeechStateType state)
void onInitObserver() override
Framework hook.
virtual void reportState(armarx::TextToSpeechStateType state, const Ice::Current &=Ice::emptyCurrent) override
The Variant class is described here: Variants.
Definition Variant.h:224
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.