本项目使用 Bing Speech API, Bot Framework, LUIS 实现在Unity内的中文语音文字互转,还可以在UWP应用完成语义分析。
1.添加必应语音 API 到你的订阅,地址 ,点击创建,并获取API密钥;
2.修改 SpeechManager.cs的Subscription Key;
3.进入 LUIS管理后台 ,新增LUIS App
4、获取 LUIS App Id 和 LUIS Endpoint Key
5.修改SpeechToText.cs的App Id
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; using System; public class TextToSpeech : Singleton<TextToSpeech> { private static string SSML = "<speak version='1.0' xml:lang='zh-CN'><voice xml:lang='zh-CN' xml:gender='Male' name='Microsoft Server Speech Text to Speech Voice (zh-CN, Kangkang, Apollo)'>{0}</voice></speak>"; AudioSource audioSource; public InputField inputText; // Use this for initialization void Start () { audioSource = gameObject.GetComponent<AudioSource>(); } // Update is called once per frame void Update () { } /// <summary> /// 使用bing speech api,将文字转为中文语音 /// </summary> /// <param name="text"></param> /// <returns></returns> public IEnumerator<object> TextToAudio(string text) { string requestUri = "https://speech.platform.bing.com/synthesize"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(string.Format(SSML, text)); var headers = new Dictionary<string, string>() { { "Authorization", "Bearer " + SpeechManager.Instance.GetToken() }, { "Content-Type", @"application/ssml+xml" }, { "X-Microsoft-OutputFormat", @"riff-16khz-16bit-mono-pcm"}, { "X-Search-AppId", Guid.NewGuid().ToString().Replace("-", "")}, { "X-Search-ClientID", Guid.NewGuid().ToString().Replace("-", "")}, { "User-Agent", "TTSHololens"} }; audioSource.Stop(); WWW www = new WWW(requestUri, buffer, headers); yield return www; audioSource.clip = www.GetAudioClip(false, true, AudioType.WAV); audioSource.Play(); } public void SpeakText(string text) { StartCoroutine(TextToAudio(text)); } public void SpeakText() { StartCoroutine(TextToAudio(inputText.text)); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System; using BotClient; using UnityEngine.UI; #if WINDOWS_UWP using System.Threading.Tasks; #endif public class SpeechToText :Singleton<SpeechToText> { public int messageLength = 3; //录音时间,单位:秒 private bool recording = false; private static string deviceName = string.Empty; //microphone设备名称 private int samplingRate; //采样率 private AudioClip audioClip; BotService botService; private AudioSource audioSource; void Start () { int unUsed; Microphone.GetDeviceCaps(deviceName, out unUsed, out samplingRate); botService = new BotService(); audioSource = gameObject.GetComponent<AudioSource>(); #if WINDOWS_UWP botService.StartConversation(); #endif } void Update () { if (recording && !Microphone.IsRecording(deviceName)) { RecordStop(); } } public bool IsRecording() { return recording; } /// <summary> /// 使用Bing Speech API,将语音文件转成text /// </summary> /// <param name="filepath"></param> /// <returns></returns> private IEnumerator<object> AudioToText(string filepath) { string requestUri = "https://speech.platform.bing.com/recognize"; requestUri += @"?scenarios=smd"; requestUri += @"&appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//你的APP ID. requestUri += @"&locale=zh-CN"; requestUri += @"&device.os=win10"; requestUri += @"&version=3.0"; requestUri += @"&format=json"; requestUri += @"&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3"; requestUri += @"&requestid=" + Guid.NewGuid().ToString(); FileStream fs = null; using (fs = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { byte[] buffer = null; buffer = new Byte[(int)fs.Length]; fs.Read(buffer, 0, buffer.Length); var headers = new Dictionary<string, string>() { { "Authorization", "Bearer " + SpeechManager.Instance.GetToken()}, { "Content-Type", @"audio/wav; codec=""audio/pcm""; samplerate=16000" } }; WWW www = new WWW(requestUri, buffer, headers); yield return www; try { string result = www.text; JSONObject jsonObj = new JSONObject(result); string resultStr = jsonObj.GetField("header").GetField("name").str; resultStr = TrimResultStr(resultStr); SpeechContronller.Instance.SetResponseText("口令:" + resultStr); TextToSpeech.Instance.SpeakText(resultStr); SpeechContronller.Instance.tipText.text =""; #if WINDOWS_UWP SendMessage(resultStr); #endif } catch { SpeechContronller.Instance.tipText.text="对不起,没听清"; TextToSpeech.Instance.SpeakText("对不起,没听清"); } } } //使用BotService进行语义分析(uwp平台执行) #if WINDOWS_UWP private async void SendMessage(string message) { string result = "对不起,无法回答您的问题"; if (await botService.SendMessage(message)) { ActivitySet messages = await botService.GetMessages(); if (messages != null) { for (int i = 1; i < messages.activities.Length; i++) { result = messages.activities[i].text; } } } UnityEngine.WSA.Application.InvokeOnAppThread(() => { //把返回的文字读出来 TextToSpeech.Instance.SpeakText(result); }, false); } #endif /// <summary> /// 对Speech API返回的结果进行处理,去除最后的句号,防止影响结果 /// </summary> /// <param name="result"></param> /// <returns></returns> private string TrimResultStr(string result) { string resultStr = result; if (resultStr != null) { int index = resultStr.LastIndexOf("。"); if (index > 0) { resultStr = resultStr.Remove(index, 1); } } return resultStr; } /// <summary> /// 开始录音 /// </summary> public void Record() { recording = true; audioSource.Stop(); SpeechContronller.Instance.SetMicrophoneIcon(true); SpeechContronller.Instance.SetTipText("正在聆听中"); SpeechContronller.Instance.SetResponseText(""); if (Microphone.IsRecording(deviceName)) { return; } audioClip = StartRecording(); } /// <summary> /// 停止录音,将语音保存成文件 /// </summary> public void RecordStop() { recording = false; SpeechContronller.Instance.SetMicrophoneIcon(false); SpeechContronller.Instance.SetTipText("思考中,请稍候"); StopRecording(); string filename = "myfile.wav"; var filepath = Path.Combine(Application.persistentDataPath, filename); SavWav.Save(filename, audioClip); StartCoroutine(AudioToText(filepath)); } /// <summary> /// 开始录音 /// </summary> /// <returns></returns> private AudioClip StartRecording() { return Microphone.Start(deviceName, false, messageLength, 16000); } /// <summary> /// 停止录音 /// </summary> private void StopRecording() { if (Microphone.IsRecording(deviceName)) { Microphone.End(deviceName); } } }
工程文件地址
http://download.csdn.net/download/shanguuncle/9967050
参考资料
Bing Speech API文档
Bot Framework API文档