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"
26 
27 #include <Ice/ObjectAdapter.h>
28 
29 using namespace armarx;
30 
32 {
33 }
34 
36 {
37  usingTopic(getProperty<std::string>("TextToSpeechTopicName").getValue());
38  usingTopic(getProperty<std::string>("TextToSpeechStateTopicName").getValue());
39 }
40 
42 {
43  offerChannel("TextToSpeech", "TextToSpeech channel");
44  offerDataFieldWithDefault("TextToSpeech", "Text", Variant(""), "Current text");
45  offerDataFieldWithDefault("TextToSpeech", "TextChangeCounter", Variant(reportTextCounter), "Counter for text updates");
46 
47  offerDataFieldWithDefault("TextToSpeech", "State", Variant(""), "Current TTS state");
48  offerDataFieldWithDefault("TextToSpeech", "StateChangeCounter", Variant(reportStateCounter), "Counter for state updates");
49 
50  offerChannel("SpeechToText", "SpeechToText channel");
51  offerDataFieldWithDefault("SpeechToText", "RecognizedText", Variant(std::string()), "Text recognized by the SpeechRecogntion");
52 
53  auto proxy = getObjectAdapter()->addWithUUID(new SpeechListenerImpl(this));
54  getIceManager()->subscribeTopic(proxy, "Speech_Commands");
55 }
56 
57 std::string SpeechObserver::SpeechStateToString(TextToSpeechStateType state)
58 {
59  switch (state)
60  {
61  case eIdle:
62  return "Idle";
63  case eStartedSpeaking:
64  return "StartedSpeaking";
65  case eFinishedSpeaking:
66  return "FinishedSpeaking";
67 
68  default:
69  throw LocalException("Unsupported TextToSpeechStateType: ") << state;
70  }
71 }
72 
73 void SpeechObserver::reportState(TextToSpeechStateType state, const Ice::Current&)
74 {
75  std::unique_lock lock(dataMutex);
76  reportStateCounter++;
77  setDataField("TextToSpeech", "State", Variant(SpeechStateToString(state)));
78  setDataField("TextToSpeech", "StateChangeCounter", Variant(reportStateCounter));
79  updateChannel("TextToSpeech");
80 }
81 
82 void SpeechObserver::reportText(const std::string& text, const Ice::Current& c)
83 {
84  std::unique_lock lock(dataMutex);
85  reportTextCounter++;
86  setDataField("TextToSpeech", "Text", Variant(text));
87  setDataField("TextToSpeech", "TextChangeCounter", Variant(reportTextCounter));
88  updateChannel("TextToSpeech");
89 }
90 
91 void SpeechObserver::reportTextWithParams(const std::string& text, const Ice::StringSeq& params, const Ice::Current&)
92 {
93  std::unique_lock lock(dataMutex);
94  ARMARX_WARNING << "reportTextWithParams is not implemented";
95 }
96 
98  obs(obs)
99 {
100 
101 }
102 
103 
104 void armarx::SpeechListenerImpl::reportText(const std::string& text, const Ice::Current&)
105 {
106  std::unique_lock lock(dataMutex);
107  JSONObject json;
108  json.fromString(text);
109  obs->setDataField("SpeechToText", "RecognizedText", Variant(json.getString("text")));
110 }
111 
112 void armarx::SpeechListenerImpl::reportTextWithParams(const std::string&, const Ice::StringSeq&, const Ice::Current&)
113 {
114  std::unique_lock lock(dataMutex);
115  ARMARX_WARNING << "reportTextWithParams is not implemented";
116 }
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:715
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:353
armarx::SpeechObserver::reportTextWithParams
virtual void reportTextWithParams(const std::string &text, const Ice::StringSeq &params, const Ice::Current &=Ice::emptyCurrent) override
Definition: SpeechObserver.cpp:91
armarx::SpeechObserver::onConnectObserver
void onConnectObserver() override
Framework hook.
Definition: SpeechObserver.cpp:41
armarx::SpeechListenerImpl::reportTextWithParams
void reportTextWithParams(const std::string &, const Ice::StringSeq &, const Ice::Current &) override
Definition: SpeechObserver.cpp:112
armarx::SpeechListenerImpl::reportText
void reportText(const std::string &, const Ice::Current &) override
Definition: SpeechObserver.cpp:104
JSONObject.h
armarx::ManagedIceObject::getObjectAdapter
Ice::ObjectAdapterPtr getObjectAdapter() const
Returns object's Ice adapter.
Definition: ManagedIceObject.cpp:140
armarx::SpeechListenerImpl::SpeechListenerImpl
SpeechListenerImpl(SpeechObserver *obs)
Definition: SpeechObserver.cpp:97
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:43
armarx::SpeechObserver::SpeechListenerImpl
friend class SpeechListenerImpl
Definition: SpeechObserver.h:61
armarx::SpeechObserver::onInitObserver
void onInitObserver() override
Framework hook.
Definition: SpeechObserver.cpp:35
armarx::JSONObject::getString
std::string getString(const ::std::string &key) const override
Definition: JSONObject.cpp:375
armarx::SpeechObserver::reportState
virtual void reportState(armarx::TextToSpeechStateType state, const Ice::Current &=Ice::emptyCurrent) override
Definition: SpeechObserver.cpp:73
SpeechObserver.h
armarx::JSONObject::fromString
void fromString(const ::std::string &jsonString, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: JSONObject.cpp:86
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:465
armarx::SpeechObserver::SpeechObserver
SpeechObserver()
Definition: SpeechObserver.cpp:31
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
armarx::SpeechObserver::SpeechStateToString
static std::string SpeechStateToString(TextToSpeechStateType state)
Definition: SpeechObserver.cpp:57
armarx::SpeechObserver::reportText
virtual void reportText(const std::string &text, const Ice::Current &=Ice::emptyCurrent) override
Definition: SpeechObserver.cpp:82
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:152
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::SpeechObserver
Definition: SpeechObserver.h:57
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::Observer::offerChannel
void offerChannel(std::string channelName, std::string description)
Offer a channel.
Definition: Observer.cpp:126