美文网首页
百度语音识别

百度语音识别

作者: 萧非子 | 来源:发表于2017-08-03 10:58 被阅读66次

    using System.Collections;
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using LitJson;
    using System.Text;

    public class BaiduYY : MonoBehaviour {
    private string token; //access_token
    private string cuid = "9963623"; //百度App ID
    private string format = "wav"; //语音格式
    private int rate = 8000; //采样率
    private int channel = 1; //声道数
    private string speech; //语音数据,进行base64编码
    private int len; //原始语音长度
    private string lan = "zh"; //语种

    private string grant_Type = "client_credentials"; //授权类型
    private string client_ID = "yOoLpRwnLYslpFcOxmPsQHcy";                       //百度appkey
    private string client_Secret = "3e0980100dafbb8cbb88d74353654911";           //百度Secret Key
    
    private string baiduAPI = "http://vop.baidu.com/server_api";
    private string getTokenAPIPath = "https://openapi.baidu.com/oauth/2.0/token";
    
    private Byte[] clipByte;
    
    public Button start;
    public Button end;
    public Button quit;
    public Text debugText;
    
    /// <summary>
    /// 语音转换出来的文字
    /// </summary>
    public static string audioToString;
    
    private AudioSource aud;
    private int audioLength;//录音的长度
    
    private void Awake()
    {
        if (GetComponent<AudioSource>() == null)
            aud = gameObject.AddComponent<AudioSource>();
        else
            aud = gameObject.GetComponent<AudioSource>();
        aud.playOnAwake = false;
    }
    
    void Update () {
    
        debugText.text = audioToString;
    
    }
    /// <summary>
    /// 获取百度用户令牌
    /// </summary>
    /// <param name="url">获取的url</param>
    /// <returns></returns>
    private IEnumerator GetToken(string url)
    {
        WWWForm getTForm = new WWWForm();
        getTForm.AddField("grant_type", grant_Type);
        getTForm.AddField("client_id", client_ID);
        getTForm.AddField("client_secret", client_Secret);
    
        WWW getTW = new WWW(url, getTForm);
        yield return getTW;
        if (getTW.isDone)
        {
            if (getTW.error == null)
            {
                token = JsonMapper.ToObject(getTW.text)["access_token"].ToString();
                StartCoroutine(GetAudioString(baiduAPI));
            }
            else
                Debug.LogError(getTW.error);
        }
    }
    
    
    /// <summary>
    /// 开始录音
    /// </summary>
    public void StartMic()
    {
        if (Microphone.devices.Length == 0) return;
        Microphone.End(null);
        Debug.Log("Start");
        aud.clip = Microphone.Start(null, false, 10, rate);
    }
    /// <summary>
    /// 结束录音
    /// </summary>
    public void EndMic()
    {
        int lastPos = Microphone.GetPosition(null);
        if (Microphone.IsRecording(null))
            audioLength = lastPos / rate;//录音时长  
        else
            audioLength = 10;
        Debug.Log("Stop");
        Microphone.End(null);
    
        clipByte = GetClipData();
        len = clipByte.Length;
        speech = Convert.ToBase64String(clipByte);
        StartCoroutine(GetToken(getTokenAPIPath));
        Debug.Log(len);
        Debug.Log(audioLength);
    }
    
    /// <summary>
    /// 把录音转换为Byte[]
    /// </summary>
    /// <returns></returns>
    public Byte[] GetClipData()
    {
        if (aud.clip == null)
        {
            Debug.LogError("录音数据为空");
            return null;
        }
    
        float[] samples = new float[aud.clip.samples];
    
        aud.clip.GetData(samples, 0);
    
    
        Byte[] outData = new byte[samples.Length * 2];
    
        int rescaleFactor = 32767; //to convert float to Int16   
    
        for (int i = 0; i < samples.Length; i++)
        {
            short temshort = (short)(samples[i] * rescaleFactor);
    
            Byte[] temdata = System.BitConverter.GetBytes(temshort);
    
            outData[i * 2] = temdata[0];
            outData[i * 2 + 1] = temdata[1];
        }
        if (outData == null || outData.Length <= 0)
        {
            Debug.LogError("录音数据为空");
            return null;
        }
    
        //return SubByte(outData, 0, audioLength * 8000 * 2);
        return outData;
    }
    
    /// <summary>
    /// 把语音转换为文字
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    private IEnumerator GetAudioString(string url)
    {
        JsonWriter jw = new JsonWriter();
        jw.WriteObjectStart();
        jw.WritePropertyName("format");
        jw.Write(format);
        jw.WritePropertyName("rate");
        jw.Write(rate);
        jw.WritePropertyName("channel");
        jw.Write(channel);
        jw.WritePropertyName("token");
        jw.Write(token);
        jw.WritePropertyName("cuid");
        jw.Write(cuid);
        jw.WritePropertyName("len");
        jw.Write(len);
        jw.WritePropertyName("speech");
        jw.Write(speech);
        jw.WriteObjectEnd();
        WWWForm w = new WWWForm();
    
    
        WWW getASW = new WWW(url, Encoding.Default.GetBytes(jw.ToString()));
        yield return getASW;
        if (getASW.isDone)
        {
            if (getASW.error == null)
            {
                JsonData getASWJson = JsonMapper.ToObject(getASW.text);
                if (getASWJson["err_msg"].ToString() == "success.")
                {
                    audioToString = getASWJson["result"][0].ToString();
                    if (audioToString.Substring(audioToString.Length - 1) == ",")
                        audioToString = audioToString.Substring(0, audioToString.Length - 1);
                    Debug.Log(audioToString);
                }
            }
            else
            {
                Debug.LogError(getASW.error);
            }
        }
    }
    public void QuitGame()
    {
        Application.Quit();
    }
    

    }

    //////////////////////////
    using UnityEngine;
    using UnityEngine.EventSystems;
    using System.Collections;
    /// <summary>
    /// 脚本位置:UGUI按钮组件身上
    /// 脚本功能:实现按钮长按状态的判断
    /// 创建时间:2017
    /// </summary>

    // 继承:按下,抬起和离开的三个接口
    //鼠标按下开始录音,抬起,结束录音,并且百度语音识别
    public class OnButtonPressed : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
    {
    // 延迟时间
    private float delay = 0.2f;

    // 按钮是否是按下状态  
    private bool isDown = false;
    
    // 按钮最后一次是被按住状态时候的时间  
    private float lastIsDownTime;
    
    
    void Update()
    {
        // 如果按钮是被按下状态  
        if (isDown)
        {
            // 当前时间 -  按钮最后一次被按下的时间 > 延迟时间0.2秒  
            if (Time.time - lastIsDownTime > delay)
            {
                // 触发长按方法  
                Debug.Log("长按");
                // 记录按钮最后一次被按下的时间  
                lastIsDownTime = Time.time;
            }
        }
    }
    
    // 当按钮被按下后系统自动调用此方法  
    public void OnPointerDown(PointerEventData eventData)
    {
        isDown = true;
        lastIsDownTime = Time.time;
        BaiduYY._instanve.StartMic();
    }
    
    // 当按钮抬起的时候自动调用此方法  
    public void OnPointerUp(PointerEventData eventData)
    {
        isDown = false;
        BaiduYY._instanve.EndMic();
    }
    
    // 当鼠标从按钮上离开的时候自动调用此方法  
    public void OnPointerExit(PointerEventData eventData)
    {
        isDown = false;
    }
    

    }

    相关文章

      网友评论

          本文标题:百度语音识别

          本文链接:https://www.haomeiwen.com/subject/ttaulxtx.html