博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity 声音处理 之 语音识别
阅读量:4683 次
发布时间:2019-06-09

本文共 4124 字,大约阅读时间需要 13 分钟。

音量检测

检测当前麦克风的输入音量

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();    }    ///     /// 获取最大的音量    ///     ///     /// 
/// 音量大小 ///
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 initialization19     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
().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
().Play();67 }68 }69 private void OnDestroy ()70 {71 //用完应该释放,否则会带来额外的开销72 m_PhraseRecognizer.Dispose ();73 }74 // Update is called once per frame75 void Update ()76 {77 78 }79 80 81 82 }

 

转载于:https://www.cnblogs.com/Fasty/p/11171168.html

你可能感兴趣的文章
7-2 树的同构 (25 分)
查看>>
学习node.js 第4篇 建立一个最小的web聊天系统
查看>>
SQL Server install SkipRules Cluster_VerifyForErrors or RebootRequiredCheck
查看>>
C# pdf打印 指定打印机
查看>>
poj 3468 A Simple Problem with Integers 线段树
查看>>
ASP.Net 验证视图状态 MAC 失败
查看>>
jQuery 在iframe中操作父页面某元素的方法
查看>>
微信小程序
查看>>
[题目] Luogu P3716 [CTSC2000]冰原探险
查看>>
linux下用phpize给PHP动态添加扩展
查看>>
php session 严格过期时间实现
查看>>
基于源码学习-fighting
查看>>
[转]LINUX新建和增加SWAP分区
查看>>
(上线时清缓存)laravel 5.1 的程序性能优化(配置文件) - 简书
查看>>
SettingsSVNPlugin
查看>>
华为经典问题汇总~
查看>>
linux桌面环境gnome,kde,xfce,lxde 使用比较(转)
查看>>
如何做自己不想做的事情,却必须要去做的事情
查看>>
JavaScript的深入理解(1)
查看>>
Go-TCP粘包
查看>>