internal class VoiceCoreWrapper
{
[DllImport("VoiceCore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool SetSpeechCallback(TTSCallback callback);
}
public delegate void TTSCallback(RecognizeEvent evt, string text);
public bool SetCallback(TTSCallback callback)
{
if (_selectEngine == SpeechEngine.VSI)
{
return VoiceCoreWrapper.SetSpeechCallback(callback);
}
return true;
}
typedef void(CALLBACK *TTSCallback)(int, char*);
VoiceRecognizeController.Instance.SetSpeechCallback(delegate (RecognizeEvent a, string t)
{
if (a == RecognizeEvent.speech_final)
{
if (isWakeupWordRepeat)
{
VoiceRecognizeController.Instance.RecognizeStart("click", 15000);
}
else
{
VoiceServiceController.Instance.OnVoiceTaskEndEvent(TaskEndEventType.WakeupAgain);
}
}
else if (a == RecognizeEvent.speech_cancel)
{
//
}
});
namespace VSISDK_RT
{
public delegate void VSICALLBACK(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam);
public ref class VSISDK sealed
{
public:
VSISDK();
event VSICALLBACK^ delegateCallBack;
......
void CXCallBack(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam);
}
}
VSISDK^ pThis;
VSISDK::VSISDK()
{
pThis = this;
}
void VSICB(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam)
{
pThis->CXCallBack(message, hIns, hUserData, fstParam, sndParam, thdParam);
}
void VSISDK_RT::VSISDK::CXCallBack(int message, VSI_UNIVERSAL_TYPE hIns, VSI_UNIVERSAL_TYPE hUserData, VSI_UNIVERSAL_TYPE fstParam, VSI_UNIVERSAL_TYPE sndParam, VSI_UNIVERSAL_TYPE thdParam)
{
pThis->delegateCallBack(message, hIns, hUserData, fstParam, sndParam, thdParam);
}
The function VSICB is the C style function,In the Native Code it used as a CallBack function,it May Called by another function or another thread from Native Code.
4. The Implement in C#
namespace CXTestApp
{
public delegate void VSICALLBACK(int message, UInt64 hIns, UInt64 hUserData, UInt64 fstParam, UInt64 sndParam, UInt64 thdParam);
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
VSISDK rt = new VSISDK();
rt.delegateCallBack += new VSISDK_RT.VSICALLBACK(VSICallBack);
rt.VSISetOpt(0, "sss", "ssss");
}
void VSICallBack(int message, UInt64 hIns, UInt64 hUserData, UInt64 fstParam, UInt64 sndParam, UInt64 thdParam)
{
int i = 0;
i = i + 10;
}
}
}
Task-based asynchronous programming detail reference:
Task task = new Task(() =>
{
string path = @"E:\git-code\CUISDKrt\bin\x64\Debug\AppX\music5.wav";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
VSISDKrt.VSISetOpt(handle, CONS_DEFINE.VSI_OPTION_POSTVOICE, "1");
VSISDKrt.VSIPost(handle, CONS_DEFINE.L_MSG_REQUEST_STARTREC, 0, 0);
while (true)
{
byte[] byteArray = br.ReadBytes(3200);
IntPtr pBuffer = Marshal.AllocHGlobal(3200);
Marshal.Copy(byteArray, 0, pBuffer, byteArray.Length);
VSISDKrt.VSIPost(handle,
CONS_DEFINE.L_MSG_ACTION_POSTDATA,
(UInt64)byteArray.Length,
(UInt64)pBuffer.ToInt64());
Marshal.FreeHGlobal(pBuffer);
if (byteArray.Length < 3200)
{
VSISDKrt.VSIPost(handle, CONS_DEFINE.L_MSG_REQUEST_STOPREC, 0, 0);
System.Diagnostics.Debug.WriteLine("The Post Voice Task Have Done.");
break;
}
Task.Delay(100).Wait();
}
});
task.Start();