1. 程式人生 > >unity接科大訊飛語音識別Windows平臺(補充前面的文章,添加了錄音功能)

unity接科大訊飛語音識別Windows平臺(補充前面的文章,添加了錄音功能)

其他操作就不在一一贅述了,直說一下新增的負責錄音的那段程式碼

1、首先利用Microphone類開始錄製和結束錄製音訊

2、利用AudioSource類播放錄製的音訊

3、然後用自己寫的方法Float2Byte將錄製的音訊轉成byte資料,供訊飛語音識別方法呼叫

詳細程式碼如下:

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

public class SoundRecording : MonoBehaviour {
    public static Text t;
    GameObject obj;
    //中文:zh_cn 英文:en_us
    private string audio_path = null;
    private const string app_id = "appid = 5ac9f8d1";
    private const string session_begin_params = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = utf-8";

    public AudioSource audio;//儲存錄製的音訊
    private int frequency = 16000;//取樣率
     void Start()
    {
      obj= GameObject.FindGameObjectWithTag("Text");
        t = obj.GetComponent<Text>();
    }
    //開始錄音、播放音訊
    public void StartRecorde() {
        audio.Stop();//停止上一個音訊的播放
        audio.clip = Microphone.Start(null, false, 5, frequency);//開始錄製音訊,3秒
       // Microphone.GetPosition(null);//得到當前所處的錄音樣本的位置
        while (Microphone.GetPosition(null) <= 0) { Debug.Log("null"); }//防止音訊播放出錯
    }

    //停止錄音、停止播放音訊
    public void StopRecorde() {
        if (Microphone.IsRecording(null)) { Microphone.End(null); }//如果麥克沒有在錄製音訊
        Debug.Log("開始播放音訊");
        audio.Play();
       // audio.Stop();//停止播放音訊
        Debug.Log("停止錄音");
      byte[] AudioData=  Float2Byte();
        //呼叫語音識別函式
        Debug.Log("開始語音識別");
        Inter_Audio_word.init_audio(app_id, session_begin_params,AudioData);
    }

    //將clip中的float音訊資料儲存到byte陣列中
    public byte[] Float2Byte() {

        if (audio.clip == null) {
            Debug.Log("clip is null!");
            return null;
        }
        float[] samples = new float[audio.clip.samples];

        audio.clip.GetData(samples, 0);

        short[] intData = new short[samples.Length];
        //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]

        byte[] bytesData = new byte[samples.Length * 2];
        //bytesData array is twice the size of
        //dataSource array because a float converted in Int16 is 2 bytes.

        int rescaleFactor = 32767; //to convert float to Int16

        for (int i = 0; i < samples.Length; i++)
        {
            intData[i] = (short)(samples[i] * rescaleFactor);
            byte[] byteArr = new byte[2];
            byteArr = BitConverter.GetBytes(intData[i]);
            byteArr.CopyTo(bytesData, i * 2);
        }

        return bytesData;

    }
}

案列demo: