Unity 聲音處理 之 語音識別


音量檢測

檢測當前麥克風的輸入音量

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

public class NewBehaviourScript2 : MonoBehaviour
{
     private static int VOLUME_DATA_LENGTH = 128;    //錄制的聲音長度

    public float volume;        //音量

    public Text text;

    public Slider slider;

    private AudioClip mMicrophoneRecode;  //錄制的音頻
    private string mDeviceName;           //設備名稱

    public int xishu=1000;
    private const int frequency = 44100; //碼率
    private const int lengthSec = 999;   //錄制時長

    // Use this for initialization
    void Start () {
        //獲取設備名稱
        mDeviceName = Microphone.devices[0];
        
      
        //錄制一段音頻
        mMicrophoneRecode = Microphone.Start(mDeviceName, true, lengthSec, frequency);
    }
    
    // Update is called once per frame
    void Update () {
        volume = GetMaxVolume();
        volume*=xishu;
        slider.value=Mathf.Lerp(slider.value,volume/200,0.1f);
        text.text=volume.ToString();
    }

    /// <summary>
    /// 獲取最大的音量
    /// </summary>
    /// 
    /// <returns>
    /// 音量大小
    /// </returns>
    private float GetMaxVolume()
    {
        float maxVolume = 0f;

        //用於儲存一段時間內的音頻信息
        float[] volumeData = new float[VOLUME_DATA_LENGTH];

        int offset;
        //獲取錄制的音頻的開頭位置
        offset = Microphone.GetPosition(mDeviceName) - VOLUME_DATA_LENGTH + 1;

        if(offset < 0)
        {
            return 0f;
        }

        //獲取數據
        mMicrophoneRecode.GetData(volumeData, offset);

        //解析數據
        for(int i = 0;i < VOLUME_DATA_LENGTH; i++)
        {
            float tempVolume = volumeData[i];
            if(tempVolume > maxVolume)
            {
                maxVolume = tempVolume;
            }
        }

        return maxVolume;
    }
}

 

關鍵字識別

此處利用win10自帶的識別

記得引入

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.Windows.Speech;//引入命名空間  利用
 5 using SpeechLib;
 6 public class NewBehaviourScript1 : MonoBehaviour
 7 {
 8     // 短語識別器
 9     private PhraseRecognizer m_PhraseRecognizer;
10     // 關鍵字
11 
12     public string[] keywords;
13 
14     public GameObject xiangdu;
15    
16     // 可信度
17     public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;
18     // Use this for initialization
19     void Start ()
20     {
21     
22         //創建一個識別器
23         m_PhraseRecognizer = new KeywordRecognizer (keywords, m_confidenceLevel);
24         //通過注冊監聽的方法
25         m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;
26         //開啟識別器
27         m_PhraseRecognizer.Start ();
28     }
29 
30     // 當識別到關鍵字時,會調用這個方法
31 
32     private void M_PhraseRecognizer_OnPhraseRecognized (PhraseRecognizedEventArgs args)
33     {
34         print (args.text);
35         if (args.text.Equals("小愛"))
36         {
37             SpVoice v = new SpVoice();
38             v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);
39             v.Speak("我在");
40         }
41 
42         if (args.text.Equals("幫我倒杯水"))
43         {
44             SpVoice v = new SpVoice();
45             v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);
46             v.Speak("是的主人");
47         }
48 
49         if (args.text.Equals("播放七里香"))
50         {
51             SpVoice v = new SpVoice();
52             v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);
53             v.Speak("好的,主人,開始播放七里香");
54             gameObject.SetActive(false);
55             xiangdu.SetActive(true);
56             xiangdu.GetComponent<AudioSource>().Play();
57         }
58 
59         if (args.text.Equals("哈哈"))
60         {
61             SpVoice v = new SpVoice();
62             v.Voice = v.GetVoices(string.Empty, string.Empty).Item(0);
63             v.Speak("主人,我沒聽懂");
64             gameObject.SetActive(false);
65             xiangdu.SetActive(true);
66             xiangdu.GetComponent<AudioSource>().Play();
67         }
68     }
69     private void OnDestroy ()
70     {
71         //用完應該釋放,否則會帶來額外的開銷
72         m_PhraseRecognizer.Dispose ();
73     }
74     // Update is called once per frame
75     void Update ()
76     {
77 
78     }
79 
80     
81 
82 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM