杞欢涓殑璇煶鎶鏈富瑕佸寘鍚袱绉嶏細璇煶璇嗗埆speech recognition鍜岃闊冲悎鎴恠peech synthesis銆備竴鑸湴锛屽紑鍙戣呬細鍥犱负鎶鏈疄鍔涘拰璧勯噾瀹炲姏绛夊悇鏂归潰鐨勯棶棰樻棤鍔涘畬鎴愪笓涓氱殑璇煶寮曟搸锛屽洜姝ら氬父閫夋嫨鐜版湁鐨勮緝涓轰笓涓氱殑璇煶寮曟搸鏉ュ畬鎴愮浉鍏崇殑寮鍙戯紝姣斿鍥藉唴闈炲父鍑哄悕鐨勭澶ц椋烇紝鐧惧害璇煶绛夌瓑銆傚綋鐒跺浗澶栫殑杩樻湁Google璇煶锛屽井杞湁SAPI绛夌瓑銆
鍦╒R寮鍙戣繃绋嬩腑锛岀敱浜庤繍琛屽湪Windows鐜涓嬶紝閭d箞鑷劧鑰岀劧锛屾垜浠閫塖API鏉ヨ繘琛岃闊冲紑鍙戙備竴鏄拰Windows鍘熺敓锛屼簩鏄绾夸笉闇瑕佺綉缁滐紝涓夋槸涓嶉渶瑕佷换浣曟彃浠躲傚彟澶栧氨鏄疭API鍙戦煶锛屽挨鍏舵槸鑻辨枃鍙戦煶锛岃繕鏄浉瀵规潵璇磋川閲忎笉閿欑殑銆傦紙Win7浠ヤ笂鑷甫锛
浣跨敤SAPI锛岄渶瑕佷娇鐢ㄥ埌鐨勬槸System.Speech.dll鏂囦欢銆傜敱浜嶶nity闇瑕佸皢Dll鏂囦欢鏀惧湪Asset鐩綍涓嬶紝鑰岃繖鏍风殑缁撴灉浼氬彂鐜皊api failed to initialize銆傚師鍥犳鐤戜负闇瑕佺壒瀹氱殑涓婁笅鏂囩幆澧冩墠鑳借繍琛宒ll鐨刟pi锛屼互鑷充簬鎷疯礉鍒癆sset鐩綍瀵艰嚧涓婁笅鏂囩幆澧冪己澶辫屾棤娉曡繍琛屻
浣嗘槸濡傛灉鍋氳繃杩欐柟闈㈠紑鍙戠殑鐭ラ亾锛屽湪C#鐨勫叾浠栧簲鐢ㄩ噷闈㈠紩鐢⊿ystem.Speech.dll鏄畬鍏ㄦ病鏈夐棶棰樼殑銆傞偅涔堟槸涓嶆槸鎴戜滑鍙互寮鍙戜竴涓笓闂ㄧ殑绗笁鏂圭▼搴忥紝鐒跺悗unity杩涜璋冪敤鍛紵鎸夌収杩欎釜鎬濊矾锛屾垜浠紑鍙戜簡涓涓帶鍒跺彴绋嬪簭Speech.exe锛屼富瑕佸姛鑳芥槸鏍规嵁杈撳叆鏂囨湰杩涜璇煶鍚堟垚銆
浠g爜杈冧负绠鍗
/*绠鍗曠殑SAPI璇煶鍚堟垚鎺у埗鍙扮▼搴*/
using System.Speech.Synthesis;
using SpeechTest.Properties;
namespace SpeechTest
{
class Program {
static void Main(string[] args) {
var speaker = new SpeechSynthesizer(); speaker.Speak(鈥渢est鈥); } }
}
OK锛岃繍琛屽氨鍙互鍚埌鏈哄櫒鍙戦煶Test浜嗐
鎴戜滑淇敼涓涓嬶紝鏀逛负浠庡弬鏁颁腑璇诲彇锛岃繖鏍风殑璇濓紝鎴戜滑鍙互鍦╱nity涓埄鐢≒rocess杩愯Speech.exe锛屽苟浼犵粰Speech鍙傛暟銆
/*浠庡弬鏁拌鍙栭渶瑕佸彂闊崇殑鏂囨湰*/
using System.Speech.Synthesis; using SpeechTest.Properties; namespace SpeechTest { class Program { static void Main(string[] args) { var speaker = new SpeechSynthesizer(); var res = args.Length == 0 ? "璇疯" : args[0]; speaker.Speak(res); } } }
鎴戜滑鍏堜娇鐢–MD鍛戒护琛岋紝cd鍒癝peech.exe鎵鍦ㄧ殑鐩綍锛岀劧鍚庤緭鍏peech.exe test锛屽鎴戜滑棰勬兂鐨勯偅鑸紝鏈哄櫒鍙戦煶test銆傛祴璇曢氳繃銆
涓轰簡鑳藉鏇存敼鍙戦煶鐨勯厤缃紝澧炲姞涓浜涗唬鐮侊紝浠嶴etting涓鍙栫浉鍏崇殑閰嶇疆鏁版嵁锛屼唬鐮佹洿鏀瑰涓嬶細
/*鑳藉閰嶇疆鐨勬帶鍒跺彴绋嬪簭*/
using System.Speech.Synthesis; using SpeechTest.Properties; namespace SpeechTest { class Program { static void Main(string[] args) { var speaker = new SpeechSynthesizer(); speaker.Volume = Settings.Default.SpeakVolume; speaker.Rate = Settings.Default.SpeakRate; var voice = Settings.Default.SpeakVoice; if (!string.IsNullOrEmpty(voice)) speaker.SelectVoice(voice); var res = args.Length == 0 ? "璇疯" : args[0]; speaker.Speak(res); } } }
鎺ヤ笅鏉ユ垜浠湪Unity涓娇鐢≒rocess鏉ュ紑鍚繖涓猄peech.exe锛屼唬鐮佸涓嬶細
/*Unity涓紑鍚疭peech.exe杩涚▼*/
using System.Diagnostics; public class Speecher: MonoBehaviour { public static void Speak(string str) { var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "speech.exe", Arguments = "\"" + str + "\"", } }; proc.Start(); } /***娴嬭瘯浠g爜锛屽彲鍒犻櫎Start***/ protected void Start() { Speak("test"); } /***娴嬭瘯浠g爜锛屽彲鍒犻櫎End***/ }
灏嗚剼鏈寕鍦ㄤ换浣曚竴涓狦O锛圙ameObject锛変笂锛岃繍琛岋紝榛戞鍑虹幇锛屽悓鏃跺惉鍒板彂闊筹紝娴嬭瘯瀹屾垚銆
鎺ヤ笅鏉ユ垜浠殣钘忚繖涓粦妗嗐備唬鐮佷慨鏀瑰涓嬶細
/*Unity寮鍚棤妗嗙殑Speech.exe杩涚▼*/
using System.Diagnostics; public class Speecher: MonoBehaviour { public static void Speak(string str) { var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "speech.exe", Arguments = "\"" + str + "\"", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, } }; proc.Start(); } /***娴嬭瘯浠g爜锛屽彲鍒犻櫎Start***/ protected void Start() { Speak("test"); } /***娴嬭瘯浠g爜锛屽彲鍒犻櫎End***/ }
鍏跺疄鍒颁簡杩欎竴姝ワ紝涓昏鐨勫姛鑳介兘瀹屾垚浜嗐備絾鏄粏蹇冪殑浼氬彂鐜帮紝杩欐牱涓嶆柇鍒涘缓杩涚▼鐒跺悗鍏抽棴杩涚▼鐨勬柟寮忎細涓嶄細澶浜嗐傚彲涓嶅彲浠ヨSpeech杩欎釜杩涚▼涓鐩村紑鍚潃锛屾敹鍒皍nity鐨勪俊鎭椂灏卞彂闊冲憿锛熻繖灏辨秹鍙婂埌杩涚▼闂撮氫俊浜嗐
Windows鐨勮繘绋嬫槸鐩镐簰鐙珛鐨勶紝鍚勮嚜鏈夊悇鑷殑鍒嗛厤绌洪棿銆備絾鏄苟涓嶆剰鍛宠繖涓嶈兘鐩镐簰閫氫俊銆傛柟娉曟湁寰堝锛屾瘮濡傝鍐欐枃浠讹紝鍙戦佹秷鎭紙hook锛夛紝Socket绛夌瓑銆傚叾涓璖ocket瀹炵幇璧锋潵鐩稿绠鍗曪紝灏ゅ叾鏄垜浠凡缁忔嫢鏈塖ocket灏佽搴撶殑鎯呭喌涓嬶紝鍙灏戦噺浠g爜灏辫浜嗐
浜庢槸鍦⊿peech鏀规垚涓涓猄ocket鏈嶅姟鍣紝浠g爜濡備笅锛
/*Speech 鏈嶅姟绔*/
using System; using System.Linq; using System.Speech.Synthesis; using System.Text; using Speech.Properties; namespace Speech { class Program { static void Main(string[] args) { var server = new NetServer(); server.StartServer(); while (true) { var res = Console.ReadLine(); if (res == "exit") break; } } } public class NetServer : SocketExtra.INetComponent { private readonly Speecher m_speecher; private readonly SocketExtra m_socket; public NetServer() { m_speecher = new Speecher(); m_socket = new SocketExtra(this); } public void StartServer() { m_socket.Bind("127.0.0.1", Settings.Default.Port); } public bool NetSendMsg(byte[] sendbuffer) { return true; } public bool NetReciveMsg(byte[] recivebuffer) { var str = Encoding.Default.GetString(recivebuffer); Console.WriteLine(str); m_speecher.Speak(str);return true; } public bool Connected { get { return m_socket.Connected; } } } public class Speecher { private readonly SpeechSynthesizer m_speaker; public Speecher() { m_speaker = new SpeechSynthesizer(); var installs = m_speaker.GetInstalledVoices(); m_speaker.Volume = Settings.Default.SpeakVolume; m_speaker.Rate = Settings.Default.SpeakRate; var voice = Settings.Default.SpeakVoice; var selected = false; if (!string.IsNullOrEmpty(voice)) { if (installs.Any(install => install.VoiceInfo.Name == voice)) { m_speaker.SelectVoice(voice); selected = true; } } if (!selected) { foreach (var install in installs.Where(install => install.VoiceInfo.Culture.Name == "en-US")) { m_speaker.SelectVoice(install.VoiceInfo.Name); break; } } } public void Speak(string msg) { m_speaker.Speak(msg); } } }
鍚屾椂淇敼Unity浠g爜锛屽鍔燬ocket鐩稿叧浠g爜锛
/*Unity瀹㈡埛绔唬鐮*/
using System.Collections; using System.Diagnostics; using System.Text; using UnityEngine; public class Speecher : MonoBehaviour, SocketExtra.INetComponent { private SocketExtra m_socket; private Process m_process; protected void Awake() { Ins = this; m_process = new Process { StartInfo = new ProcessStartInfo { FileName = "speech.exe", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }, }; m_process.Start(); } /***娴嬭瘯浠g爜锛屽彲鍒犻櫎Start***/ protected IEnumerator Start() { yield return StartCoroutine(Connect()); Speak("test"); } /***娴嬭瘯浠g爜锛屽彲鍒犻櫎End***/ public IEnumerator Connect() { m_socket = new SocketExtra(this); m_socket.Connect("127.0.0.1", 9903); while (!m_socket.Connected) { yield return 1; } } protected void OnDestroy() { if (m_process != null && !m_process.HasExited) m_process.Kill(); m_process = null; } public static Speecher Ins; public static void Speak(string str) { #if UNITY_EDITOR||UNITY_STANDALONE_WIN Ins.Speech(str); #endif } public void Speech(string str) { if (m_socket.Connected) { var bytes = Encoding.Default.GetBytes(str); m_socket.SendMsg(bytes); } } public bool NetReciveMsg(byte[] recivebuffer) { return true; } public bool NetSendMsg(byte[] sendbuffer) { return true; } }
OK锛屽ぇ鍔熷憡鎴愩傚伐绋嬭Github
https://github.com/CodeGize/UnitySapi/
杞浇璇锋敞鏄庡嚭澶www.codegize.com