speech
Loading...
Searching...
No Matches
SpeechRecognizerModule.h
1/*
2 * Copyright (C) 2011 EFAA Consortium, European Commission FP7 Project IST-270490
3 * Authors: Stephane Lallee
4 * email: stephane.lallee@gmail.com
5 * website: http://efaa.upf.edu/
6 * Permission is granted to copy, distribute, and/or modify this program
7 * under the terms of the GNU General Public License, version 2 or any
8 * later version published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details
14 */
15
16#include "stdafx.h"
17
18#if _MSC_VER > 1000
19#pragma once
20#endif // _MSC_VER > 1000
21
22#define MAX_EDIT_TEXT 1000
23#define GID_DICTATION 0 // Dictation grammar has grammar ID 0
24
25#define WM_RECOEVENT WM_APP // Window message used for recognition events
26using namespace std;
27using namespace yarp::os;
28using namespace yarp::sig;
29
30class SpeechRecognizerModule: public RFModule
31{
32 //Yarp related
33 Port m_portRPC;
34 Port m_portContinuousRecognition;
35 Port m_portContinuousRecognitionGrammar;
36 Port m_port2iSpeak;
37 Port m_port2iSpeakRpc;
38 BufferedPort<Sound> m_portSound;
39
40 //SAPI related
41 CComPtr<ISpAudio> m_cpAudio;
42 CComPtr<ISpRecognizer> m_cpRecoEngine;
43 CComPtr<ISpRecoContext> m_cpRecoCtxt;
44 CComPtr<ISpRecoGrammar> m_cpGrammarFromFile;
45 CComPtr<ISpRecoGrammar> m_cpGrammarRuntime;
46 CComPtr<ISpRecoGrammar> m_cpGrammarDictation;
47 BOOL m_bInSound;
48 BOOL m_bGotReco;
49 CSpStreamFormat m_cAudioFmt;
50 CComPtr <ISpStream> m_streamFormat;
51
52 //My Grammar management related
53 int m_timeout; //ms
54 map<string, list<string> > m_vocabulories;
55 bool m_useTalkBack;
56 bool USE_LEGACY;
57 bool m_forwardSound;
58 bool interruptRecognition;
59 string m_tmpFileFolder;
60
61
62public:
63
64 /************************************************************************/
65 bool configure(ResourceFinder &rf);
66
67 /************************************************************************/
68 bool updateModule();
69
70 /************************************************************************/
71 bool handleInterrupt(const Bottle& cmd, Bottle& reply);
72
73 /************************************************************************/
74 bool handleRGMCmd(const Bottle& cmd, Bottle& reply);
75
76 /************************************************************************/
77 bool handleAsyncRecognitionCmd(const Bottle& cmd, Bottle& reply);
78
79 /************************************************************************/
80 bool refreshFromVocabulories(CComPtr<ISpRecoGrammar> grammarToModify);
81
82 /************************************************************************/
83 bool handleVocabuloryCmd(const Bottle& cmd, Bottle& reply);
84
85 /************************************************************************/
86 bool handleRecognitionCmd(const Bottle& cmd, Bottle& reply);
87
88 /************************************************************************/
89 bool setGrammarCustom(CComPtr<ISpRecoGrammar> grammarToModify, string grammar, bool append);
90
91 /************************************************************************/
92 string getFromDictaction(int timeout, LPCWSTR options=NULL);
93
94 /************************************************************************/
95 list< pair<string, double> > waitNextRecognitionLEGACY(int timeout);
96
97 /************************************************************************/
98 Bottle waitNextRecognition(int timeout);
99
100 /************************************************************************/
101 void say(string s, bool wait=true);
102
103 /************************************************************************/
104 Bottle toBottle(SPPHRASE* pPhrase, const SPPHRASERULE* pRule );
105
106 /************************************************************************/
107 bool respond(const Bottle& cmd, Bottle& reply);
108
109 /************************************************************************/
110 bool interruptModule()
111 {
112 yInfo() <<"Interrupting ports...";
113 m_portRPC.interrupt();
114 m_portContinuousRecognition.interrupt();
115 m_portContinuousRecognitionGrammar.interrupt();
116 m_port2iSpeak.interrupt();
117 m_port2iSpeakRpc.interrupt();
118 m_portSound.interrupt();
119 yInfo() <<"ok";
120 return true;
121 }
122
123 /************************************************************************/
124 bool close()
125 {
126 yInfo() <<"Closing ports...";
127 m_portRPC.close();
128 m_portContinuousRecognition.close();
129 m_portContinuousRecognitionGrammar.close();
130 m_port2iSpeak.close();
131 m_port2iSpeakRpc.close();
132 m_portSound.close();
133 yInfo() <<"ok";
134 return true;
135 }
136
137 /************************************************************************/
138 double getPeriod()
139 {
140 return 0.1;
141 }
142
143 /************************************************************************/
144 bool loadGrammarFromRf(ResourceFinder &RF);
145
146 /************************************************************************/
147 yarp::sig::Sound toSound(CComPtr<ISpRecoResult> cpRecoResult);
148};
149