-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommandGroup.cpp
270 lines (227 loc) · 7.05 KB
/
CommandGroup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "CommandGroup.h"
#include "Util.h"
//#include <windows.h>
#include <stdexcept>
#include <algorithm>
#include <string_view>
#include <string>
//=================================//
// HNx Voice Command Grammar class //
//=================================//
// Contains all of the words for //
// a group of voice commands //
//=================================//
using namespace HNx;
constexpr short GRAMMAR_LANG_NO_RULES = 409;
HNx::CommandGroup::CommandGroup()
{}
HNx::CommandGroup::CommandGroup(ISpRecoContext* t_ctx,
std::wstring_view t_grammarName,
unsigned long long t_grammarID)
: m_grammarName(t_grammarName)
, m_gramID(t_grammarID)
{
if(!t_ctx)
throw std::invalid_argument("Context pointer was NULL");
if(t_grammarID == 0)
throw std::invalid_argument("grammarID cant be 0");
HRESULT hr = t_ctx->CreateGrammar(t_grammarID, &m_cpGram);
if(FAILED(hr))
throw std::runtime_error("Failed to create grammar.\nError: " + std::to_string(hr));
hr = m_cpGram->ResetGrammar(409);
if(FAILED(hr))
throw std::runtime_error("Failed to reset grammar.\nError: " + std::to_string(hr));
hr = m_cpGram->SetGrammarState(SPGS_DISABLED);
if(FAILED(hr))
throw std::runtime_error("Failed to disable grammar while creating.\nError: " + std::to_string(hr));
currentState = CGState::Inactive;
hr = m_cpGram->GetRule(m_grammarName.c_str(), m_gramID, SPRAF_TopLevel | SPRAF_Active, true, nullptr);
if(FAILED(hr))
throw std::runtime_error("Failed to create new grammar rule.\nError: " + std::to_string(hr));
hr = m_cpGram->SetRuleIdState(m_gramID, SPRS_ACTIVE);
if(FAILED(hr))
throw std::runtime_error("Failed to activate rule.\nError: " + std::to_string(hr));
}
HNx::CommandGroup::~CommandGroup()
{
try
{
deactivate();
m_cpGram->ClearRule(0);
} catch(std::runtime_error e)
{
// TODO
// log error to application-wide
// log
}
}
HNx::CommandGroup::CommandGroup(CommandGroup&& t)
: m_gramID(std::move(t.m_gramID))
, m_grammarName(std::move(t.m_grammarName))
, m_hState(std::move(t.m_hState))
, currentState(t.currentState)
{
m_cpGram.Attach(t.m_cpGram.Detach());
m_cmds.swap(t.m_cmds);
t.m_hState = nullptr;
}
CommandGroup&
HNx::CommandGroup::operator=(CommandGroup&& t)
{
if(&t != this)
{
m_cpGram.Attach(t.m_cpGram.Detach());
m_gramID = t.m_gramID;
t.m_gramID = 0;
m_grammarName.swap(t.m_grammarName);
m_cmds.swap(t.m_cmds);
m_hState = std::move(t.m_hState);
t.m_hState = nullptr;
currentState = t.currentState;
t.currentState = CGState::Unknown;
}
return *this;
}
void
HNx::CommandGroup::activate()
{
HRESULT hr = m_cpGram->SetGrammarState(SPGS_ENABLED);
if(FAILED(hr))
{
throw std::runtime_error("Failed to activate grammar.\nError: " + std::to_string(hr));
}
currentState = CGState::Active;
}
void
HNx::CommandGroup::deactivate()
{
HRESULT hr = m_cpGram->SetGrammarState(SPGS_DISABLED);
if(FAILED(hr))
{
throw std::runtime_error("Failed to deactivate grammar.\nError: " + std::to_string(hr));
}
currentState = CGState::Inactive;
}
void
HNx::CommandGroup::addCommand(Command const& t_cmd)
{
// check for duplicate phrase
for(auto cmd : m_cmds)
if(cmd.phrase().size() == t_cmd.phrase().size())
if(std::equal(cmd.phrase().begin(), cmd.phrase().end(), t_cmd.phrase().begin(), icase_cmp_wchar))
return;
// deactivate grammar if active
CGState lastState {CGState::Unknown};
if(currentState != CGState::Inactive)
{
lastState = currentState;
deactivate();
}
// get our rule and initial state handle
SPSTATEHANDLE hInit = {0};
HRESULT hr = m_cpGram->GetRule(m_grammarName.c_str(), m_gramID, 0, false, &hInit);
// create our state if we havent yet
if(SUCCEEDED(hr))
if(!m_hState)
hr = m_cpGram->CreateNewState(hInit, &m_hState);
// add phrase to our new state
if(SUCCEEDED(hr))
hr = m_cpGram->AddWordTransition(hInit,
m_hState,
t_cmd.phrase().c_str(),
nullptr,
SPWT_LEXICAL,
1,
nullptr);
// save our changes and activate grammar;
if(SUCCEEDED(hr))
hr = m_cpGram->Commit(0);
if(SUCCEEDED(hr))
hr = m_cpGram->SetRuleIdState(m_gramID, SPRS_ACTIVE);
if(FAILED(hr))
throw std::runtime_error("Grammar Disabled!\nFailed to add word to grammar.\nError: " + std::to_string(hr));
// add our command to the vector
m_cmds.emplace_back(t_cmd);
// activate if we were active before
if(lastState == CGState::Active)
activate();
}
void
HNx::CommandGroup::removeCommand(std::wstring_view t_phrase)
{
// ignore empty words
if(t_phrase.empty())
{
return;
}
for(auto it = m_cmds.begin(); it != m_cmds.end(); ++it)
if(it->phrase().size() == t_phrase.size())
if(std::equal(it->phrase().begin(), it->phrase().end(), t_phrase.begin(), icase_cmp_wchar))
{
m_cmds.erase(it);
update_grammar();
break;
}
}
std::vector<std::wstring>
HNx::CommandGroup::getWords() const
{
std::vector<std::wstring> words;
for(auto cmd : m_cmds)
words.push_back(cmd.phrase());
return words;
}
Command
HNx::CommandGroup::getCommandByPhrase(std::wstring_view t_phrase)
{
for(auto cmd : m_cmds)
if(t_phrase.size() == cmd.phrase().size())
if(std::equal(cmd.phrase().begin(), cmd.phrase().end(), t_phrase.begin(), icase_cmp_wchar))
return cmd;
return {};
}
void
HNx::CommandGroup::update_grammar()
{
// stop grammar so we don't have unwanted
// recognitions if active
CGState lastState {CGState::Unknown};
if(currentState != CGState::Inactive)
{
lastState = currentState;
deactivate();
}
// erase everything in this rule
HRESULT hr = m_cpGram->ClearRule(m_hState);
// not needed
//if(SUCCEEDED(hr))
//hr = m_cpGram->ResetGrammar(MAKELANGID(ENGLISH_LANG_ID, SUBLANG_ENGLISH_US));
// create our rule
m_hState = nullptr;
SPSTATEHANDLE hInit = nullptr;
if(SUCCEEDED(hr))
hr = m_cpGram->GetRule(m_grammarName.c_str(), m_gramID, 0, false, &hInit);
// create our rule state
if(SUCCEEDED(hr))
if(!m_hState)
hr = m_cpGram->CreateNewState(hInit, &m_hState);
// populate rule state with phrases
if(SUCCEEDED(hr))
for(auto cmd : m_cmds)
if(SUCCEEDED(hr))
hr = m_cpGram->AddWordTransition(hInit,
m_hState,
cmd.phrase().c_str(),
nullptr,
SPWT_LEXICAL,
1,
nullptr);
// activate rule state
if(SUCCEEDED(hr))
hr = m_cpGram->SetRuleIdState(m_gramID, SPRS_ACTIVE);
if(FAILED(hr))
throw std::runtime_error("update_grammar() failed.\nError: " + std::to_string(hr));
// reactivate grammar if it was deactivated
if(lastState == CGState::Active)
activate();
}