Repository navigation
Expand file tree
/
Copy pathSpRecoUI.cpp
More file actions
87 lines (73 loc) · 2.58 KB
/
Copy pathSpRecoUI.cpp
File metadata and controls
87 lines (73 loc) · 2.58 KB
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
#include "stdafx.h"
#include "SpRecoUI.h"
#include <QtWidgets/QMessageBox>
SpRecoUI::SpRecoUI(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.pbVoiceInput, SIGNAL(pressed()), this, SLOT(onVoiceStart()));
connect(ui.pbVoiceInput, SIGNAL(released()), this, SLOT(onVoiceStop()));
//SAPI init
m_bSoundStart = false;
m_bSoundEnd = false;
if(FAILED(m_SREngine.InitializeSapi((HWND)this->winId(), SREngine::WM_RECOEVENT, L"./SpeechGrammar.xml")))
QMessageBox::information(NULL, "Error", "Initialize speech engine failed!", MB_OK);
}
SpRecoUI::~SpRecoUI()
{
}
void SpRecoUI::onVoiceStart()
{
VERIFY_RES(m_SREngine.SetRuleState(NULL, TRUE));
setWindowTitle("Sound started");
}
void SpRecoUI::onVoiceStop()
{
VERIFY_RES(m_SREngine.SetRuleState(NULL, FALSE));
setWindowTitle("Sound stopped");
}
bool SpRecoUI::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* pMsg = (MSG*) message;
setWindowTitle("Control - Debug: winEvent");
if(pMsg->message == SREngine::WM_RECOEVENT)
*result = this->OnRecoEvent();
return false;
}
// Speech Recognition Event Process
LRESULT SpRecoUI::OnRecoEvent()
{
if (m_SREngine.m_cpRecoContext == NULL)
return FALSE;
CSpEvent spEvent;
HRESULT hr = S_OK;
while(spEvent.GetFrom(m_SREngine.m_cpRecoContext) == S_OK) {
setWindowTitle("Control - Debug");
switch(spEvent.eEventId){
case SPEI_SOUND_START: m_bSoundStart = true; break;
case SPEI_SOUND_END: m_bSoundEnd = true; break;
case SPEI_RECOGNITION: if (m_bSoundStart && m_bSoundEnd) Recognized(spEvent); break;
}
}
return TRUE;
}
void SpRecoUI::Recognized(CSpEvent &spEvent)
{
USES_CONVERSION;
CComPtr<ISpRecoResult> cpResult = spEvent.RecoResult();
CSpDynamicString dstrText;
cpResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &dstrText, NULL);
QString strResult = dstrText.CopyToChar(); // W2T(dstrText);
SPPHRASE* pPhrase = NULL;
if (SUCCEEDED(cpResult->GetPhrase(&pPhrase))){
strResult += tr(" RuleName:") + QString::fromStdWString(pPhrase->Rule.pszName);
strResult += tr(" PropName:") + QString::fromStdWString(pPhrase->pProperties->pszName);
if (pPhrase->pProperties->pNextSibling)
strResult += tr(" Sibling:") + QString::fromStdWString(pPhrase->pProperties->pNextSibling->pszName);
if (pPhrase->pProperties->pFirstChild)
strResult += tr(" Child:") + QString::fromStdWString(pPhrase->pProperties->pFirstChild->pszName);
}
if (pPhrase)
::CoTaskMemFree(pPhrase);
ui.textEdit->insertPlainText(strResult+"\n");
}