21
2016
04

Unity鍒╃敤Sapi杩涜璇煶寮鍙

杞欢涓殑璇煶鎶鏈富瑕佸寘鍚袱绉嶏細璇煶璇嗗埆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

芦 涓婁竴绡 涓嬩竴绡 禄