wxWidgets and microsoft SAPI for speech recognition

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
brightening-eyes
Knows some wx things
Knows some wx things
Posts: 27
Joined: Fri Jun 28, 2013 2:14 pm
Location: tehran, iran
Contact:

wxWidgets and microsoft SAPI for speech recognition

Post by brightening-eyes »

hello everyone
i'm coding a software that requires the speech recognition events (i need to use ISpNotifySource and other stuff) with speech recognition in order to get speech recognition
but i dont know how can i assign windows events (things that are passed to window proc function of the window in order for it to be processed)
if it is possible to use custom events, please tell me how to use that, or what can i do in order to make it work?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets and microsoft SAPI for speech recognition

Post by doublemax »

Override

Code: Select all

WXLRESULT wxWindow::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
Use the source, Luke!
brightening-eyes
Knows some wx things
Knows some wx things
Posts: 27
Joined: Fri Jun 28, 2013 2:14 pm
Location: tehran, iran
Contact:

Re: wxWidgets and microsoft SAPI for speech recognition

Post by brightening-eyes »

doublemax wrote:Override

Code: Select all

WXLRESULT wxWindow::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
hello,
i've overrided this and an access vialation occured
this is the function that i've implemented

Code: Select all

//our window proc
WXLRESULT window::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
switch(nMsg)
{
case event_sr:
sr_event();
}
return wxFrame::MSWWindowProc(nMsg, wParam, lParam);
}
and here is the sr_event function

Code: Select all

//speech recognition callback
void sr_event()
{
//speech recognizer event:
CSpEvent e;
while(e.GetFrom(context)==S_OK)
{
switch(e.eEventId)
{
case SPEI_HYPOTHESIS:
{
wxMessageBox(get_stt_text(e), "info", wxOK|wxICON_INFORMATION, nullptr, wxDefaultCoord, wxDefaultCoord);
}
}

}
}
it gives an access vialation to a null pointer (in the while loop)
maybe MSWWindowProc() is called before app::OnInit()? because i initialize my speech recognition there
and, this is the sr_event macro:

Code: Select all

#define sr_event WM_USER+1
whats your idea, how can i make it work?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets and microsoft SAPI for speech recognition

Post by doublemax »

It's probably not the reason for the crash, but you shouldn't call wxMessageBox from there. Use wxLogDebug for non-intrusive debug logging.

What is your code based on? I only know code for TTS using SAPI and the event handling looks different there:
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
Use the source, Luke!
brightening-eyes
Knows some wx things
Knows some wx things
Posts: 27
Joined: Fri Jun 28, 2013 2:14 pm
Location: tehran, iran
Contact:

Re: wxWidgets and microsoft SAPI for speech recognition

Post by brightening-eyes »

doublemax wrote:It's probably not the reason for the crash, but you shouldn't call wxMessageBox from there. Use wxLogDebug for non-intrusive debug logging.

What is your code based on? I only know code for TTS using SAPI and the event handling looks different there:
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
ok, let me explain it:
in app::OnInit(), beside creation of the window, it initializes com then creates the interfaces for speech recognition
after showing the window, it sets the window handle with w->GetHandle() as speech recognition's notify window with the event of event_sr which is processed in the MSWWindowProc() function
this is the OnInit code:

Code: Select all

bool app::OnInit()
{
//initialize com
if(CoInitializeEx(nullptr, COINIT_MULTITHREADED)==S_FALSE)
{
return false;
}
//set application name and vendor names:
SetAppDisplayName("myCrew");
SetAppName("myCrew");
SetClassName("myCrew");
SetVendorDisplayName("amir ramezani");
SetVendorName("amir ramezani");
//create our window:
w=new window();
//set it as  top-level window (if closed, the app will be quit)
SetTopWindow(w);
//show it
w->Show();
//center the window
w->Center();
//initialize speech recognition
hr=CoCreateInstance(CLSID_SpSharedRecognizer, nullptr, CLSCTX_ALL, IID_ISpRecognizer, reinterpret_cast<void**>(&recognizer));
if(FAILED(hr))
{
return false;
}
//create speech recognition context manager
hr=recognizer->CreateRecoContext(&context);
if(FAILED(hr))
{
return false;
}
//set intrest for our event
const ULONGLONG interest = SPFEI(SPEI_SOUND_START) | SPFEI(SPEI_SOUND_END) |
                                      SPFEI(SPEI_PHRASE_START) | SPFEI(SPEI_RECOGNITION) |
                                      SPFEI(SPEI_FALSE_RECOGNITION) | SPFEI(SPEI_HYPOTHESIS) |
                                      SPFEI(SPEI_INTERFERENCE) | SPFEI(SPEI_RECO_OTHER_CONTEXT) |
                                      SPFEI(SPEI_REQUEST_UI) | SPFEI(SPEI_RECO_STATE_CHANGE) |
                                      SPFEI(SPEI_PROPERTY_NUM_CHANGE) | SPFEI(SPEI_PROPERTY_STRING_CHANGE);
hr=context->SetInterest(interest, interest);
if(FAILED(hr))
{
return false;
}
//create the grammar
hr=context->CreateGrammar(maingramid, &main_grammar);
if(FAILED(hr))
{
return false;
}
//load dictation into our grammar
hr=main_grammar->LoadDictation(0, SPLO_STATIC);
if(FAILED(hr))
{
return false;
}
//make our dictation active
hr=main_grammar->SetDictationState(SPRS_ACTIVE);
if(FAILED(hr))
{
return false;
}
//set our event thing
hr=context->SetNotifyWindowMessage(w->GetHandle(), event_sr, 0, 0);
if(FAILED(hr))
{
return false;
}
//return true to continue execution
return true;
}
i dont know what the problem is here because the application crashes before eaven it is started
in the event loop of the speech recognition that i've called, it shows that context which is of type ISpRecoContext is null and CspEvent gives error
this is the code of my event:

Code: Select all

//speech recognition callback
void sr_event()
{
//speech recognizer event:
CSpEvent e;
while(e.GetFrom(context)==S_OK)
{
switch(e.eEventId)
{
case SPEI_HYPOTHESIS:
{
wxMessageBox(get_stt_text(e), "info", wxOK|wxICON_INFORMATION, nullptr, wxDefaultCoord, wxDefaultCoord);
}
}

}
}
it gives an access violation exception which it cant access the memory pointed to by the context although it has been initialized in the OnInit()
maybe the window proc is called before app::OnInit()?
brightening-eyes
Knows some wx things
Knows some wx things
Posts: 27
Joined: Fri Jun 28, 2013 2:14 pm
Location: tehran, iran
Contact:

Re: wxWidgets and microsoft SAPI for speech recognition

Post by brightening-eyes »

hi again,
another thing that comes to my mind is using custom events although i have no clew how to do that (with windows events as they have wParam and lParam)
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets and microsoft SAPI for speech recognition

Post by doublemax »

Can you provide a complete, compilable sample?

Just for a test, don't initialize the speech api immediately. Do it only after clicking a button/selecting a menu entry.
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxWidgets and microsoft SAPI for speech recognition

Post by PB »

I do not have any experience with SAPI, but this sample using Windows message queue looks that pretty simple and easy to "translate" to wxWidgets code:
https://github.com/pauldotknopf/Windows ... edictation. It only gets the text, the more complex one is the dictation pad example.
Post Reply