windows - SAPI: Speech to Text example -
i new sapi, , appreciate if of can provide me speech text hello world example in sapi. know ms got examples "dictation" etc, start small one. glad if can help.
i played bit windows voice recognition using sapi, isn't user friendly. here example of code wrote (in c++) :
#include <sphelper.h> #include <sapi.h> #include <iostream> #include <string> const ulonglong grammarid = 0; const wchar_t* rulename1 = l"rulename1"; int start_listening(const std::string& word); isprecogrammar* init_grammar(isprecocontext* recocontext, const std::string& command); void get_text(isprecocontext* reco_context); void check_result(const hresult& result); int main(int argc, char** argv) { start_listening("hello"); return exit_success; } // function exits when word passed parameter said user int start_listening(const std::string& word) { // initialize com library if (failed(::coinitialize(nullptr))) { return exit_failure; } std::cout << "you should start windows recognition" << std::endl; std::cout << "just \""<< word << "\"" << std::endl; hresult hr; isprecognizer* recognizer; hr = cocreateinstance(clsid_spsharedrecognizer, nullptr, clsctx_all, iid_isprecognizer, reinterpret_cast<void**>(&recognizer)); check_result(hr); isprecocontext* recocontext; hr = recognizer->createrecocontext(&recocontext); check_result(hr); // disable context hr = recocontext->pause(0); check_result(hr); isprecogrammar* recogrammar = init_grammar(recocontext, word); hr = recocontext->setnotifywin32event(); check_result(hr); handle handleevent; handleevent = recocontext->getnotifyeventhandle(); if(handleevent == invalid_handle_value) { check_result(e_fail); } ulonglong interest; interest = spfei(spei_recognition); hr = recocontext->setinterest(interest, interest); check_result(hr); // activate grammar hr = recogrammar->setrulestate(rulename1, 0, sprs_active); check_result(hr); // enable context hr = recocontext->resume(0); check_result(hr); // wait reco handle handles[1]; handles[0] = handleevent; waitformultipleobjects(1, handles, false, infinite); get_text(recocontext); std::cout << "hello user" << std::endl; recogrammar->release(); ::couninitialize(); system("pause"); return exit_success; } /** * create , initialize grammar. * create rule grammar. * add word grammar. */ isprecogrammar* init_grammar(isprecocontext* recocontext, const std::string& command) { hresult hr; spstatehandle sate; isprecogrammar* recogrammar; hr = recocontext->creategrammar(grammarid, &recogrammar); check_result(hr); word langid = makelangid(lang_french, sublang_french); hr = recogrammar->resetgrammar(langid); check_result(hr); // todo: catch error , use default langid => getuserdefaultuilanguage() // create rules hr = recogrammar->getrule(rulename1, 0, spraf_toplevel | spraf_active, true, &sate); check_result(hr); // add word const std::wstring commandwstr = std::wstring(command.begin(), command.end()); hr = recogrammar->addwordtransition(sate, null, commandwstr.c_str(), l" ", spwt_lexical, 1, nullptr); check_result(hr); // commit changes hr = recogrammar->commit(0); check_result(hr); return recogrammar; } void get_text(isprecocontext* reco_context) { const ulong maxevents = 10; spevent events[maxevents]; ulong eventcount; hresult hr; hr = reco_context->getevents(maxevents, events, &eventcount); // warning hr equal s_false if ok // eventcount < requestedeventcount if(!(hr == s_ok || hr == s_false)) { check_result(hr); } isprecoresult* recoresult; recoresult = reinterpret_cast<isprecoresult*>(events[0].lparam); wchar_t* text; hr = recoresult->gettext(sp_getwholephrase, sp_getwholephrase, false, &text, null); check_result(hr); cotaskmemfree(text); } void check_result(const hresult& result) { if (result == s_ok) { return; } std::string message; switch(result) { case e_invalidarg: message = "one or more arguments invalids."; case e_accessdenied: message = "acces denied."; case e_nointerface: message = "interface not exist."; case e_notimpl: message = "not implemented method."; case e_outofmemory: message = "out of memory."; case e_pointer: message = "invalid pointer."; case e_unexpected: message = "unexpecter error."; case e_fail: message = "failure"; default: message = "unknown : " + std::to_string(result); } throw std::exception(message.c_str()); }
as said, it's bit complicated. think should wrap code library make easier use.
Comments
Post a Comment