You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

172 lines
4.9 KiB

#include "SpeechSdkHelper.h"
#include <msclr\marshal_cppstd.h>
using namespace msclr::interop;
using namespace System;
using namespace System::Runtime::InteropServices;
namespace SpeechSdkHelperCLR {
SpeechSdkHelper::SpeechSdkHelper() {
mStartSilence=5000;//µ¥Î»ºÁÃë
mEndSilence=1000; //µ¥Î»ºÁÃë
mEnablePartial=false;
mEnableVolume=false;
mPerfectContextStr="";
mPartialContextStr="";
mLanguage = "";
mSpeaker = "";
mSpeed = "";
}
SpeechSdkHelper::~SpeechSdkHelper() {
mRecognizerPointer = nullptr;
}
char* ManagedString2UnmanagedStringA(String^ strIn)
{
IntPtr ip = Marshal::StringToHGlobalAnsi(strIn);
const char* pTemp = static_cast<const char*>(ip.ToPointer());
char *pOut = new char[strlen(pTemp) + 1];
strcpy_s(pOut, strlen(pTemp) + 1, pTemp);
Marshal::FreeHGlobal(ip);
return pOut;
}
std::string SystemStringToStdString(System::String^ sData)
{
int q = (int)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(sData);
char* p = (char*)q;
return std::string(p);
}
void SpeechSdkHelper::SetSdkConfigPath(String ^ path) {
std::string pathStr = marshal_as<std::string>(path);
mobvoi::sds::SetSdkConfigPath(pathStr);
}
void SpeechSdkHelper::SetSdkLogOn(bool on) {
mobvoi::sds::SetSdkLogOn(on);
}
void SpeechSdkHelper::SetRecognitionParams(bool enablePartial, bool enableVolume) {
SetRecognitionParamsInner(5000, 1000, enablePartial,enableVolume);
}
void SpeechSdkHelper::SetRecognitionParamsInner(int startSilence, int endSilence, bool enablePartial, bool enableVolume) {
mStartSilence = startSilence;
mEndSilence = endSilence;
mEnablePartial = enablePartial;
mEnableVolume = enableVolume;
}
void SpeechSdkHelper::SetContext(String ^ perfectKeywords, String ^ partialKeywords) {
mPerfectContextStr = ManagedString2UnmanagedStringA(perfectKeywords);
mPartialContextStr = ManagedString2UnmanagedStringA(partialKeywords);
}
bool SpeechSdkHelper::StartRecognition(ResultDelegate ^ callback) {
OnlineRecognizerHelper *pRecognizer = new OnlineRecognizerHelper();
mRecognizerPointer = pRecognizer;
bool res = false;
if (pRecognizer != nullptr) {
pRecognizer->setRecognitionParams(mStartSilence, mEndSilence, mEnablePartial, mEnableVolume);
pRecognizer->setContext(mPerfectContextStr, mPartialContextStr);
void* delegatePointer = (void*)Marshal::GetFunctionPointerForDelegate(callback).ToPointer();
res = pRecognizer->startRecognition(delegatePointer);
}
return res;
}
bool SpeechSdkHelper::StopRecognition(bool immediate) {
bool res = false;
if (mRecognizerPointer != nullptr) {
res = mRecognizerPointer->stopRecognition(immediate);
mRecognizerPointer = nullptr;
}
else {
res = true;
}
return res;
}
bool SpeechSdkHelper::StartTts(System::String^ ttsContent , System::String^ language, System::String^ speaker, System::String^ speed) {
OnlineTtsHelper* ttsPointer = OnlineTtsHelper::GetInstance();
mTtsPointer = ttsPointer;
//String^ to std
std::string contentStr = marshal_as<std::string>(ttsContent);
std::string languageStr = marshal_as<std::string>(language);
std::string speakerStr = marshal_as<std::string>(speaker);
std::string speedStr = marshal_as<std::string>(speed);
ttsPointer->SetTtsParas(languageStr, speakerStr, speedStr);
bool res = ttsPointer->StartTts(contentStr);
if (res == true) {
std::cout << std::endl;
std::cout << "tts ³É¹¦!" << std::endl;
std::cout << std::endl;
} else {
std::cout << std::endl;
std::cout << "tts ʧ°Ü!" << std::endl;
std::cout << std::endl;
}
return res;
}
bool SpeechSdkHelper::StartTts() {
OnlineTtsHelper* ttsPointer = OnlineTtsHelper::GetInstance();
mTtsPointer = ttsPointer;
bool res = ttsPointer->StartTts("");
if (res == true) {
std::cout << std::endl;
std::cout << "tts ³É¹¦!" << std::endl;
std::cout << std::endl;
}
else {
std::cout << std::endl;
std::cout << "tts ʧ°Ü!" << std::endl;
std::cout << std::endl;
}
return res;
}
bool SpeechSdkHelper::StopTts() {
bool res = false;
if (mTtsPointer != nullptr) {
res = mTtsPointer->StopTts();
mTtsPointer = nullptr;
}
else {
res = true;
}
return res;
}
void SpeechSdkHelper::SetBeamDir(System::String^ serialPortname, int dir) {
WWSerialPortApi serialPortTool;
char* portname = ManagedString2UnmanagedStringA(serialPortname);
serialPortTool.open(portname);
serialPortTool.setBeamDir(dir);
serialPortTool.close();
}
int SpeechSdkHelper::AddCli(int a, int b){
int result =0;
if (mRecognizerPointer != nullptr) {
result = mRecognizerPointer->Add(3, 2);
Console::WriteLine(L"Result is {0}", result);;
}
return result;
}
void SpeechSdkHelper::TestDelegate(int number, MessageDelegate^ messageDelegate) {
void* delegatePointer = (void*)Marshal::GetFunctionPointerForDelegate(messageDelegate).ToPointer();
//m_pRecognizer->WriteMessage(number, delegatePointer);
}
}