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 
30 using namespace armarx;
31 
33 {
34 }
35 
36 void
38 {
39  usingTopic(getProperty<std::string>("TextToSpeechTopicName").getValue());
40  usingTopic(getProperty<std::string>("TextToSpeechStateTopicName").getValue());
41 }
42 
43 void
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 
69 std::string
70 SpeechObserver::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 
86 void
87 SpeechObserver::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 
96 void
97 SpeechObserver::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 
106 void
107 SpeechObserver::reportTextWithParams(const std::string& text,
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 
116 {
117 }
118 
119 void
120 armarx::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 
128 void
130  const Ice::StringSeq&,
131  const Ice::Current&)
132 {
133  std::unique_lock lock(dataMutex);
134  ARMARX_WARNING << "reportTextWithParams is not implemented";
135 }
armarx::Observer::updateChannel
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
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:366
armarx::SpeechObserver::reportTextWithParams
virtual void reportTextWithParams(const std::string &text, const Ice::StringSeq &params, const Ice::Current &=Ice::emptyCurrent) override
Definition: SpeechObserver.cpp:107
armarx::SpeechObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: SpeechObserver.cpp:44
armarx::SpeechListenerImpl::reportTextWithParams
void reportTextWithParams(const std::string &, const Ice::StringSeq &, const Ice::Current &) override
Definition: SpeechObserver.cpp:129
armarx::SpeechListenerImpl::reportText
void reportText(const std::string &, const Ice::Current &) override
Definition: SpeechObserver.cpp:120
JSONObject.h
armarx::ManagedIceObject::getObjectAdapter
Ice::ObjectAdapterPtr getObjectAdapter() const
Returns object's Ice adapter.
Definition: ManagedIceObject.cpp:144
armarx::SpeechListenerImpl::SpeechListenerImpl
SpeechListenerImpl(SpeechObserver *obs)
Definition: SpeechObserver.cpp:115
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::SpeechObserver::SpeechListenerImpl
friend class SpeechListenerImpl
Definition: SpeechObserver.h:66
armarx::SpeechObserver::onInitObserver
void onInitObserver() override
Framework hook.
Definition: SpeechObserver.cpp:37
armarx::JSONObject::getString
std::string getString(const ::std::string &key) const override
Definition: JSONObject.cpp:403
armarx::SpeechObserver::reportState
virtual void reportState(armarx::TextToSpeechStateType state, const Ice::Current &=Ice::emptyCurrent) override
Definition: SpeechObserver.cpp:87
SpeechObserver.h
armarx::JSONObject::fromString
void fromString(const ::std::string &jsonString, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: JSONObject.cpp:88
armarx::Observer::setDataField
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
armarx::SpeechObserver::SpeechObserver
SpeechObserver()
Definition: SpeechObserver.cpp:32
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:254
armarx::SpeechObserver::SpeechStateToString
static std::string SpeechStateToString(TextToSpeechStateType state)
Definition: SpeechObserver.cpp:70
armarx::SpeechObserver::reportText
virtual void reportText(const std::string &text, const Ice::Current &=Ice::emptyCurrent) override
Definition: SpeechObserver.cpp:97
armarx::Observer::offerDataFieldWithDefault
void offerDataFieldWithDefault(std::string channelName, std::string datafieldName, const Variant &defaultValue, std::string description)
Offer a datafield with default value.
Definition: Observer.cpp:160
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::SpeechObserver
Definition: SpeechObserver.h:64
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Observer::offerChannel
void offerChannel(std::string channelName, std::string description)
Offer a channel.
Definition: Observer.cpp:131