Bing Speech微软必应语音认知服务-文字语音互转

本项目使用 Bing Speech APIBot FrameworkLUIS 实现在Unity内的中文语音文字互转,还可以在UWP应用完成语义分析。

Bing Speech微软必应语音认知服务-文字语音互转_第1张图片

1.添加必应语音 API 到你的订阅,地址 ,点击创建,并获取API密钥

Bing Speech微软必应语音认知服务-文字语音互转_第2张图片

2.修改 SpeechManager.cs的Subscription Key;

Bing Speech微软必应语音认知服务-文字语音互转_第3张图片

3.进入 LUIS管理后台 ,新增LUIS App  

4、获取 LUIS App Id 和 LUIS Endpoint Key 

Bing Speech微软必应语音认知服务-文字语音互转_第4张图片

Bing Speech微软必应语音认知服务-文字语音互转_第5张图片


5.修改SpeechToText.cs的App Id

Bing Speech微软必应语音认知服务-文字语音互转_第6张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;

public class TextToSpeech : Singleton
{
    private static string SSML = "{0}";
    AudioSource audioSource;
    public InputField inputText;
    // Use this for initialization
    void Start () {
        audioSource = gameObject.GetComponent();
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    /// 
    /// 使用bing speech api,将文字转为中文语音
    /// 
    /// 
    /// 
    public IEnumerator 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() {
            { "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
{
    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();
#if WINDOWS_UWP
        botService.StartConversation();
#endif
    }

    void Update () {
        if (recording && !Microphone.IsRecording(deviceName))
        {
            RecordStop();
        }
	}


    public bool IsRecording()
    {
        return recording;
    }

    /// 
    /// 使用Bing Speech API,将语音文件转成text
    /// 
    /// 
    /// 
    private IEnumerator 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() {
                { "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

    /// 
    /// 对Speech API返回的结果进行处理,去除最后的句号,防止影响结果
    /// 
    /// 
    /// 
    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;
    }

    /// 
    /// 开始录音
    /// 
    public void Record()
    {
        recording = true;
        audioSource.Stop();

        SpeechContronller.Instance.SetMicrophoneIcon(true);
        SpeechContronller.Instance.SetTipText("正在聆听中");
        SpeechContronller.Instance.SetResponseText("");

        if (Microphone.IsRecording(deviceName))
        {
            return;
        }
        audioClip = StartRecording();
    }

    /// 
    /// 停止录音,将语音保存成文件
    /// 
    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));
    }

    /// 
    /// 开始录音
    /// 
    /// 
    private AudioClip StartRecording()
    {
        return Microphone.Start(deviceName, false, messageLength, 16000);
    }

    /// 
    /// 停止录音
    /// 
    private void StopRecording()
    {
        if (Microphone.IsRecording(deviceName))
        {
            Microphone.End(deviceName);
        }
    }
}
 
  

 
  

工程文件地址

http://download.csdn.net/download/shanguuncle/9967050

参考资料

Bing Speech API文档
Bot Framework API文档



你可能感兴趣的:(知识库)