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

一、百度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(); // 保存当前应用的Token
        listenBtn = 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 System.Net.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;
    #endregion

    private string accessToken; // 访问AIP需要用的Token

    #region 百度语音技术应用
    private string API_KEY = "IBCbYFAbZBGkfbqpf6UBO1svY6WB6ISQ";
    private string SECRET_KEY = "XcPMlGudUm8o558YCT6Rjz1pGRSqcHW7";
    private string authHost = "https://aip.baidubce.com/oauth/2.0/token";
    #endregion

    private Asr aipClient;  // 百度语音识别SDK

    void 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中
在这里插入图片描述

  • 7
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大可iii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值