300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Unity语音识别(百度AI长语句语音识别Unity原生短语语音识别)

Unity语音识别(百度AI长语句语音识别Unity原生短语语音识别)

时间:2021-02-26 20:15:06

相关推荐

Unity语音识别(百度AI长语句语音识别Unity原生短语语音识别)

Unity语音识别[百度AI语音识别&Unity原生短语语音识别]

一、百度AI语音识别1.代码块讲解2.操作流程3.主要功能完整代码二、Unity原生语音识别主要功能完整代码三、Button长按点击方法的重写1. 主要功能完整代码2.使用方法三、工程下载链接

一、百度AI语音识别

1.代码块讲解

(1)首先,初始化一些常量信息,我们在这个工程中需要API Key、Secret Key,这些会在下面流程中讲到,并通过这两个key获取URL请求地址中的Token参数,用于对百度AI语音识别API进行请求

void Start(){aipClient = new Asr(API_KEY, SECRET_KEY); // 创建SDK的实例aipClient.Timeout = 6000; // 超时时长为6000毫秒accessToken = GetAccessToken(); // 保存当前应用的TokenlistenBtn = GetComponentInChildren<ListenButton>(); // 获取自定义Button的实例listenBtn.OnStartRecordEvent += StartRecord;listenBtn.OnStopRecordEvent += StopRecord;recordSource = GetComponent<AudioSource>();}/// <summary>/// 获取URL请求地址中的Token参数/// </summary>private string GetAccessToken(){HttpClient client = new HttpClient();List<KeyValuePair<string, string>> paraList = new List<KeyValuePair<string, string>>();paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));paraList.Add(new KeyValuePair<string, string>("client_id", API_KEY));paraList.Add(new KeyValuePair<string, string>("client_secret", SECRET_KEY));HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;string result = response.Content.ReadAsStringAsync().Result;//Debug.Log("result is " + result);//if (result != null) tImage.color = tokenGotColor;return result;}

(2)第二步,使用和停止麦克风录制声音的方法

/// <summary>/// 点击按下说话开始录音/// </summary>public void StartRecord(){if (Microphone.devices.Length > 0){string device = Microphone.devices[0];//指定默认麦克风AudioClip clip = Microphone.Start(device, true, 60, 16000);//设置麦克风参数,并将麦克风获取的语音数据保存到clip变量中,用以后续播放使用recordSource.clip = clip;recordClip = clip;}else{//只是一些关于文本错误信息的提示SetRecognizeText(TipsReference.CANT_FIND_MICROPHONE);listenBtn.ReleaseClickEvent(TipsReference.RECORD_TYPE.NoMicroPhone);}}/// <summary>/// 松开按下说话停止录音并发送识别/// </summary>public void StopRecord(){Microphone.End(Microphone.devices[0]);StartCoroutine(Recognition(recordClip));}

(3)第二步,使用麦克风录制声音的方法

/// <summary>/// 传入语音数据流并对其数据进行操作以符合百度官方文档中请求API的要求/// </summary>IEnumerator Recognition(AudioClip clip2Send){float[] sample = new float[recordClip.samples];recordClip.GetData(sample, 0);short[] intData = new short[sample.Length];byte[] byteData = new byte[intData.Length * 2];for (int i = 0; i < sample.Length; i++){intData[i] = (short)(sample[i] * short.MaxValue);}Buffer.BlockCopy(intData, 0, byteData, 0, byteData.Length);var result = aipClient.Recognize(byteData, "pcm", 16000); //将语音文件格式设置为pcm格式,按照百度官方文档进行参数设置var speaking = result.GetValue("result");if (speaking == null){SetRecognizeText(TipsReference.NOTHING_RECORD);StopAllCoroutines();yield return null;}string usefulText = speaking.First.ToString();SetRecognizeText(usefulText);yield return 0;}

2.操作流程

1.首先我们需要进入百度智能云官网,按顺序点击并在注册账号的前提下,再点击立即使用进入百度智能云的控制台。

2.点击创建应用,填入相应信息,相信这些都没什么难点。

3.这时我们获得了相应的key

4.将key填入代码相应的位置即可

3.主要功能完整代码

因为其中包含了按键相关的脚本,它们在其他脚本中,所以不直接下载相关工程的同学需要将其剔除,以方便自己使用(与listenButton相关的所有内容),或者使用第三部分中的内容,重写按钮方法。

using System.Collections;using System.Collections.Generic;using UnityEngine;using Baidu.Aip.Speech;using .Http;using UnityEngine.UI;using System;/// <summary>/// 百度语音识别技术的交互类/// </summary>[RequireComponent(typeof(AudioSource))]public class AipController : MonoBehaviour{private ListenButton listenBtn; // 继承重写的Button类private AudioSource recordSource;private AudioClip recordClip;#region UI面板控件//public Image tImage;public Text recognizeText;public Color tokenGotColor;#endregionprivate string accessToken; // 访问AIP需要用的Token#region 百度语音技术应用private string API_KEY = "IBCbYFAbZBGkfbqpf6UBO1svY6WB6ISQ";private string SECRET_KEY = "XcPMlGudUm8o558YCT6Rjz1pGRSqcHW7";private string authHost = "/oauth/2.0/token";#endregionprivate Asr aipClient; // 百度语音识别SDKvoid Start(){aipClient = new Asr(API_KEY, SECRET_KEY); // 创建SDK的实例aipClient.Timeout = 6000; // 超时时长为6000毫秒accessToken = GetAccessToken(); // 保存当前应用的Token// 获取自定义Button的实例listenBtn = GetComponentInChildren<ListenButton>();listenBtn.OnStartRecordEvent += StartRecord;listenBtn.OnStopRecordEvent += StopRecord;recordSource = GetComponent<AudioSource>();}/// <summary>/// 点击按下说话开始录音/// </summary>public void StartRecord(){if (Microphone.devices.Length > 0){string device = Microphone.devices[0];AudioClip clip = Microphone.Start(device, true, 60, 16000);recordSource.clip = clip;recordClip = clip;}else{SetRecognizeText(TipsReference.CANT_FIND_MICROPHONE);listenBtn.ReleaseClickEvent(TipsReference.RECORD_TYPE.NoMicroPhone);}}/// <summary>/// 松开按下说话停止录音并发送识别/// </summary>public void StopRecord(){Microphone.End(Microphone.devices[0]);StartCoroutine(Recognition(recordClip));}public void SetRecognizeText(string result){recognizeText.text = result;}IEnumerator Recognition(AudioClip clip2Send){float[] sample = new float[recordClip.samples];recordClip.GetData(sample, 0);short[] intData = new short[sample.Length];byte[] byteData = new byte[intData.Length * 2];for (int i = 0; i < sample.Length; i++){intData[i] = (short)(sample[i] * short.MaxValue);}Buffer.BlockCopy(intData, 0, byteData, 0, byteData.Length);var result = aipClient.Recognize(byteData, "pcm", 16000);var speaking = result.GetValue("result");if (speaking == null){SetRecognizeText(TipsReference.NOTHING_RECORD);StopAllCoroutines();yield return null;}string usefulText = speaking.First.ToString();SetRecognizeText(usefulText);yield return 0;}private string GetAccessToken(){HttpClient client = new HttpClient();List<KeyValuePair<string, string>> paraList = new List<KeyValuePair<string, string>>();paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));paraList.Add(new KeyValuePair<string, string>("client_id", API_KEY));paraList.Add(new KeyValuePair<string, string>("client_secret", SECRET_KEY));HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;string result = response.Content.ReadAsStringAsync().Result;//Debug.Log("result is " + result);//if (result != null) tImage.color = tokenGotColor;return result;}public void DisplayClip(){recordSource.Play();}}

二、Unity原生语音识别

主要功能完整代码

由于代码量较小且注释详细,所以直接放源码

因为其中包含了按键相关的脚本(与listenButton相关的所有内容),它们在其他脚本中,所以不直接下载相关工程的同学需要将其剔除,以方便自己使用,或者学习使用第三部分中的内容,重写按钮方法。

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Windows.Speech;public class PhraseRecognition : MonoBehaviour{//继承重写的Buttion类private ListenButton listenButton;// 短语识别器private PhraseRecognizer m_PhraseRecognizer;// 关键字public string[] keywords = {"开始测试", "长语音","结束测试" };// 可信度public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;void Start(){listenButton = GetComponentInChildren<ListenButton>();listenButton.OnStartRecordEvent += StartRecognizePhrase;listenButton.OnStopRecordEvent += StopRecognizePhrase;}void StartRecognizePhrase(){if (m_PhraseRecognizer == null){//创建一个识别器m_PhraseRecognizer = new KeywordRecognizer(keywords, m_confidenceLevel);//通过注册监听的方法m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;//开启识别器m_PhraseRecognizer.Start();Debug.Log("正在监听");}else {m_PhraseRecognizer.Stop(); }}void StopRecognizePhrase(){//判断场景中是否存在语音识别器,如果有,释放if (m_PhraseRecognizer != null)m_PhraseRecognizer.Stop();Debug.Log("结束监听");}/// <summary> 当识别到关键字时,会调用这个方法 </summary>private void M_PhraseRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args){_SpeechRecognition(args.text);print(args.text);}private void OnDestroy(){//判断场景中是否存在语音识别器,如果有,释放if (m_PhraseRecognizer != null)m_PhraseRecognizer.Dispose();}/// <summary> 识别到语音的操作 </summary>void _SpeechRecognition(string msg){switch (msg){case "长语音":Debug.Log("转换长语音识别");break;case "开始测试":Debug.Log("开始测试");break;case "结束测试":Debug.Log("结束测试");break;}}}

三、Button长按点击方法的重写

1. 主要功能完整代码

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;/// <summary>/// 重写Button类,检测长按事件/// </summary>public class ListenButton : Button{public delegate void RecordDelegate();public event RecordDelegate OnStartRecordEvent;public event RecordDelegate OnStopRecordEvent;public PointerEventData currentData; // 保存一份PointerEventData,方便控制释放鼠标点击事件private TipsReference.RECORD_TYPE _TYPE = TipsReference.RECORD_TYPE.None;/// <summary>/// 释放鼠标长按事件/// </summary>public void ReleaseClickEvent(TipsReference.RECORD_TYPE type){_TYPE = type;switch (type){case TipsReference.RECORD_TYPE.Normal:break;case TipsReference.RECORD_TYPE.NoMicroPhone:base.OnPointerUp(currentData);break;}}public override void OnPointerDown(PointerEventData eventData){base.OnPointerDown(eventData);currentData = eventData;OnStartRecordEvent();}public override void OnPointerUp(PointerEventData eventData){base.OnPointerUp(eventData);if(_TYPE != TipsReference.RECORD_TYPE.NoMicroPhone)OnStopRecordEvent();}}

2.使用方法

使用起来很简单只需要将Button原有的Button组件删除,替换成这个脚本即可,它会监听按下(OnPointerDown),抬起(OnPointerUp)的事件

三、工程下载链接

当然什么都没有比直接下载工程更易学习,工程链接点击此处

内容分别在两个Scenes中

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。