鍓嶈█
鍦ㄤ箣鍓嶃Unity鍒╃敤Sapi杩涜windows璇煶寮鍙銆嬩腑锛屾湰璁″垝涓嶅噯澶囩户缁仛璇煶璇嗗埆銆傚洜涓哄湪unity3d涓凡缁忔彁渚涗簡璇煶璇嗗埆鐨勭浉鍏虫柟娉曪紝璇﹁unity3d鐨勫畼鏂规枃妗o細https://docs.unity3d.com/ScriptReference/Windows.Speech.KeywordRecognizer.html銆備絾鏄湁涓鐐规槸杩欎釜鍙敮鎸亀in10銆傚浜巜in7鐢ㄦ埛鏉ヨ锛屽鏋滀笉浣跨敤鐧惧害璇煶鎴栬呯澶ц椋炶闊崇殑璇濓紝閭d箞浣跨敤SAPI灏辨槸鏈濂界殑鏂瑰紡浜嗐傚悓鏍风殑锛岀敱浜嶶nity涓棤娉曠洿鎺ヤ娇鐢⊿API锛屾墍浠ュ彧鑳芥寜鐓у師鏉ョ殑鎬濊矾锛屾妸瀹冨啓鍒颁竴涓猠xe宸ュ叿涓紝鐒跺悗鍐嶇敱unity3d鏉ヨ皟鐢ㄣ
宸ョ▼鍙樻洿鍜岄噸鏋
杩樻槸缁х画涔嬪墠鐨勫伐绋嬶紝浣嗘槸涓轰簡瑙勮寖涓浜涳紝鎴戞妸宸ョ▼鍚嶇О浠嶴peechTest鏀逛负浜哠peech銆傚苟涓旈噸鏋勪簡Socket閫氫俊缁撴瀯锛屽皢鍘熸湰鐨凷ocketExtra鍒嗕负浜哠ocketBase锛孲ocketServer鍜孲ocketClient銆傝繖鏍峰仛鐨勭洰鐨勫氨鏄畝鍗曞寲锛屽苟涓斿鍔燬ocketServer鍙戦佷俊鎭埌SocketClient鐨勫姛鑳姐
閫氫俊鏁版嵁缁撴瀯
鐢变簬speech鍔犲叆浜嗚闊宠瘑鍒姛鑳斤紝鎵浠ヤ负浜嗗尯鍒箣鍓嶇殑閫氫俊淇℃伅锛岄噸鏂版暣鐞嗗畾涔変簡鏂扮殑閫氫俊鏁版嵁缁撴瀯銆傞氫俊鏁版嵁缁撴瀯瀹氫负浜岃繘鍒舵暟鎹紝鍓4浣嶆槸涓涓猧nt32鍨嬬殑鍛戒护鐮侊紝杩欎簺鍛戒护鐮佹湁涓嶅悓鐨勫惈涔夊拰鍐呭锛
1锛氳〃绀哄垵濮嬪寲锛
2锛氳〃绀烘枃瀛楄浆璇煶锛屽悗闈㈢揣璺熺潃涓涓猻tring
3锛氳〃绀鸿闊宠瘑鍒紝鍚庨潰鏄竴涓猧nt32鍨嬶紝0琛ㄧず缁撴潫璇嗗埆锛1琛ㄧず寮濮嬭瘑鍒
褰搒peech鏀跺埌1鏃讹紝杩涜鍒濆鍖栥傛敹鍒2鐨勪俊鎭椂锛岃鍙栦箣鍚庣殑鏂囨湰鏁版嵁锛岀劧鍚庝氦鐢盨peecher鏉ュ彂闊炽傛敹鍒3鏃讹紝璇诲彇鍚庨潰鐨勬暟鎹紝濡傛灉鏄1锛屽垯寮濮嬭繘琛屽綍闊宠瘑鍒紝濡傛灉鏄0鍒欏仠姝㈠綍闊宠瘑鍒
Speech浠g爜NetServer.cs
public bool NetReciveMsg(byte[] recivebuffer, int netID)
{
var arg = new ByteOutArg(recivebuffer);
var cmd = arg.ReadInt32();
switch ((EmCmd)cmd)
{
case EmCmd.Init:
Init();
break;
case EmCmd.Speak:
var str = arg.ReadString();
Console.WriteLine(str);
m_speecher.Speak(str);
return true;
case EmCmd.Recognize:
var scmd = arg.ReadInt32();
if (scmd == 1)
m_recognizer.BeginRec();
if (scmd == 0)
m_recognizer.EndRec();
return true;
default:
throw new ArgumentOutOfRangeException();
}
return false;
}鍦╱nity鍙戦佺殑浠g爜銆傦紙ByteInArg鏄竴涓畝鍗曞湴鍐欏byte[]鏁版嵁鐨勭被锛
/***娴嬭瘯浠g爜锛屽彲鍒犻櫎Start***/
public void OnGUI()
{
if (GUILayout.Button("Connect"))
{
StartCoroutine(Connect());
}
if (GUILayout.Button("InitServer"))
{
StartCoroutine(InitServer());
}
if (GUILayout.Button("Speak"))
{
Speak("hello world");
}
if (GUILayout.Button("Recognize Start"))
{
Recognize(true);
}
if (GUILayout.Button("Recognize End"))
{
Recognize(false);
}
}
/***娴嬭瘯浠g爜锛屽彲鍒犻櫎End***/
private void Recognize(bool tf)
{
var arg = new ByteInArg();
arg.Write(3);
arg.Write(tf ? 1 : 0);
NetSendMsg(arg.GetBuffer());
}
public IEnumerator Connect()
{
m_socket = new SocketClient(this);
m_socket.Connect("127.0.0.1", 9903);
while (!m_socket.Connected)
{
yield return 1;
}
}
public IEnumerator InitServer()
{
var arg = new ByteInArg();
arg.Write(1);
NetSendMsg(arg.GetBuffer());
yield return 1;
}
public void Speech(string str)
{
if (m_socket.Connected)
{
var arg = new ByteInArg();
arg.Write(2);
arg.Write(str);
NetSendMsg(arg.GetBuffer());
}
}灏佽SAPI璇煶璇嗗埆妯″潡
灏佽SAPI鐨勮闊宠瘑鍒ā鍧楄繖鏂归潰鐨勪唬鐮佸緢澶氾紝涔熸瘮杈冪畝鍗曘傝繖閲屽垱寤虹殑Recognizer绫伙紝浠g爜鏇存敼鑷http://kevin19900306.iteye.com/blog/1206534锛屽叾涓殑鍚箟鍙互鏌ラ槄SAPI鐨勭浉鍏虫枃妗c俁ecognizer绫绘湁鏋勯犲嚱鏁帮紝涓や釜鎺ュ彛锛屼竴涓洖璋冨悜澶栨彁渚涖傝繖涓や釜鎺ュ彛涓哄紑濮嬭瘑鍒獴eginRec鍜屽仠姝㈣瘑鍒獷ndRec锛屽綋璇嗗埆瀹屾垚鏃讹紝浣跨敤鍥炶皟锛屽皢璇嗗埆鐨勬枃瀛椾紶鍑恒
鍦╯peech鐨凬etServer.cs涓紝鍥炶皟鐩稿叧鐨勪唬鐮佸涓
public void Init()
{
m_speecher = new Speecher();
m_recognizer = new Recognizer
{
OnRecognized = OnRecognized
};
Console.WriteLine("鍒濆鍖栧畬鎴");
}
private void OnRecognized(string text)
{
var arg = new ByteInArg();
arg.Write(text);
NetSendMsg(arg.GetBuffer());
}娴嬭瘯
杩愯unity3d锛岀敾闈腑鍑虹幇5涓寜閽紝鍒嗗埆涓篊onnect锛孖nitServer锛孲peak锛孯ecognize Start锛孯ecognize End銆備緷娆$偣鍑籆onnect锛孖nitServer銆傜劧鍚庣偣鍑籗peak锛屽惉鍒扳渉ello world鈥濊闊炽傜偣鍑籖ecognize Start锛屽楹﹀厠椋庤浠讳綍涓枃锛屽彲浠ョ湅鍒癲ebug杈撳嚭瀵瑰簲鐨勬枃瀛椼傜偣鍑籖ecognize End鍋滄璇嗗埆銆
Github婧愪唬鐮
婧愪唬鐮佷笅杞https://github.com/CodeGize/UnitySapi/
杞浇澹版槑
杞浇璇蜂繚鐣