SpeechNode.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_SpeechNode_h_
00003 #define INCLUDED_SpeechNode_h_
00004
00005 #include <sstream>
00006
00007 #include "Behaviors/StateNode.h"
00008 #include "Sound/SoundManager.h"
00009
00010
00011
00012
00013
00014
00015
00016 class SpeechNode : public StateNode {
00017 public:
00018
00019 SpeechNode(const std::string& nodename, const std::string& text="")
00020 : StateNode(nodename), storedText(text), textstream(std::ios_base::ate),
00021 showText(true), curplay_id(SoundManager::invalid_Play_ID), autostop(false),
00022 savedText(), pos() {}
00023
00024
00025 virtual void preStart() {
00026 StateNode::preStart();
00027
00028
00029
00030 textstream.str(storedText);
00031 if ( event != NULL ) {
00032 const DataEvent<std::string> *datev = dynamic_cast<const DataEvent<std::string>*>(event);
00033 if ( datev != NULL )
00034 textstream << (storedText.empty() ? "" : " ") << datev->getData();
00035 }
00036 savedText = textstream.str();
00037 textstream << (textstream.str().empty() ? "" : " ");
00038 pos = textstream.tellp();
00039 }
00040
00041 virtual void postStart() {
00042 StateNode::postStart();
00043 if ( textstream.tellp() == pos )
00044 textstream.str(savedText);
00045 if ( textstream.str().empty() )
00046 textstream << "Speech node " + getName() + " has no text to speak";
00047 curplay_id = sndman->speak(textstream.str(), showText);
00048 if ( curplay_id == SoundManager::invalid_Play_ID )
00049 postStateCompletion();
00050 else
00051 erouter->addListener(this, EventBase::audioEGID, curplay_id, EventBase::deactivateETID);
00052 }
00053
00054
00055 virtual void stop() {
00056 if ( autostop )
00057 stopPlay();
00058 StateNode::stop();
00059 }
00060
00061
00062 virtual void doEvent() {
00063 curplay_id = SoundManager::invalid_Play_ID;
00064 postStateCompletion();
00065 }
00066
00067
00068 void stopPlay() {
00069 sndman->stopPlay(curplay_id);
00070 curplay_id = SoundManager::invalid_Play_ID;
00071 }
00072
00073
00074 const std::string getText() const { return storedText; }
00075
00076
00077 void setText(const std::string &text) { storedText = text; }
00078
00079
00080 void setShowText(bool s) { showText = s; }
00081
00082
00083 bool getAutoStop() const { return autostop; }
00084
00085
00086 void setAutoStop(bool astop) { autostop=astop; }
00087
00088 protected:
00089 std::string storedText;
00090 std::ostringstream textstream;
00091 bool showText;
00092 SoundManager::Play_ID curplay_id;
00093 bool autostop;
00094
00095 private:
00096 std::string savedText;
00097 std::streampos pos;
00098 };
00099
00100
00101
00102
00103
00104
00105 #endif