Unity中使用百度中文語音識別功能
阿新 • • 發佈:2018-01-02
來源 openapi ner key nbsp 語音識別 ann .text esp
下面是API類
Asr.cs
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 用戶解析token的json數據 /// </summary> class TokenResponse { public string access_token = null; } public class Asr { public string SecretKey { get; privateset; } public string APIKey { get; private set; } public string Token { get; private set; } public Asr(string apiKey, string secretKey) { APIKey = apiKey; SecretKey = secretKey; } public IEnumerator GetAccessToken() { var uri = string.Format( "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}", APIKey, SecretKey); var www = new WWW(uri); yield return www; if (string.IsNullOrEmpty(www.error)) {var result = JsonUtility.FromJson<TokenResponse>(www.text); Token = result.access_token; Debug.Log("Get access_token successfully"); } else { Debug.LogError(www.error); } } public IEnumerator Recognize(byte[] data, Action<string> callback) { var uri = string.Format("http://vop.baidu.com/server_api?lan=zh&cuid={0}&token={1}", SystemInfo.deviceUniqueIdentifier, Token); var headers = new Dictionary<string, string> { { "Content-Type", "audio/pcm;rate=16000" } }; var www = new WWW(uri, data, headers); yield return www; if (string.IsNullOrEmpty(www.error)) { Debug.Log(www.text); callback(www.text); } else Debug.LogError(www.error); } /// <summary> /// 將Unity的AudioClip數據轉化為PCM格式16bit數據 /// </summary> /// <param name="clip"></param> /// <returns></returns> public static byte[] ConvertAudioClipToPCM16(AudioClip clip) { var samples = new float[clip.samples * clip.channels]; clip.GetData(samples, 0); var samples_int16 = new short[samples.Length]; for (var index = 0; index < samples.Length; index++) { var f = samples[index]; samples_int16[index] = (short)(f * short.MaxValue); } var byteArray = new byte[samples_int16.Length * 2]; Buffer.BlockCopy(samples_int16, 0, byteArray, 0, byteArray.Length); return byteArray; } }
下面是測試類
main.cs
using UnityEngine; using System.Collections; using UnityEngine.UI; public class main : MonoBehaviour { public string APIKey = ""; public string SecretKey = ""; public Button StartButton; public Button StopButton; public Text DescriptionText; private AudioClip _clipRecord = new AudioClip(); private Asr _asr; void Start() { _asr = new Asr(APIKey, SecretKey); StartCoroutine(_asr.GetAccessToken()); StartButton.gameObject.SetActive(true); StopButton.gameObject.SetActive(false); DescriptionText.text = ""; StartButton.onClick.AddListener(OnClickStartButton); StopButton.onClick.AddListener(OnClickStopButton); } private void OnClickStartButton() { StartButton.gameObject.SetActive(false); StopButton.gameObject.SetActive(true); DescriptionText.text = "Listening..."; _clipRecord = Microphone.Start(null, false, 30, 16000); } private void OnClickStopButton() { StartButton.gameObject.SetActive(false); StopButton.gameObject.SetActive(false); DescriptionText.text = "Recognizing..."; Microphone.End(null); Debug.Log("end record"); var data = Asr.ConvertAudioClipToPCM16(_clipRecord); StartCoroutine(_asr.Recognize(data, s => { DescriptionText.text = s; StartButton.gameObject.SetActive(true); })); } }
資源來源於關爾Manic的技術園
http://blog.csdn.net/zhenghongzhi6/article/details/78688571#comments
Unity中使用百度中文語音識別功能