Unity 实时接收麦克风 并 录音

using UnityEngine;

public class AudioVisualizationAndClone : MonoBehaviour
{
    public static AudioVisualizationAndClone instance;

    public static float volume;
    public bool isStart;//总开关
    public float maxY = 0.25f;
    public float minY = 0.01f;//0.003
    public bool isOver;

    private AudioClip micRecord;
    string device;
    private float speed;
    private float x;

    private float ProCD;//保护机制的CD
    private float ProCDTime = 0.01f;//保护机制的时间
    private float maxTime = 4.5f;//识别语音的最大时长\
    private int perfabCount;//Perfab的计数
    private bool newFather;//生成父物体的开关(仅一次)
    private float closeCD;//保护机制关闭的CD
    private float closeCDTime = 0.3f;//保护机制关闭的CD时间
    private bool reStart;//保护机制的开启判断

    private void Awake()
    {
        instance = this;
    }

    void Start()
    {
        //初始化速度的值
        speed = 0.1f;
        //device = Microphone.devices[0];//获取设备麦克风
        //Microphone.IsRecording(device)   检查物体是否正常录制
        string[] mydevice = Microphone.devices;
        if (mydevice.Length > 0)
        {
            Debug.Log(mydevice[0]);
            Debug.Log("设备有麦克风,mydeviceLength为:" + mydevice.Length + "Microphone.IsRecording(mydevice[n])是:" + Microphone.IsRecording(mydevice[0]));
        }
        else
        {
            Debug.Log("设备没有麦克风" + mydevice.Length);
        }
        micRecord = Microphone.Start(device, true, 999, 44100);//44100音频采样率   固定格式  48000
        newFather = false;
        isStart = true;
    }
    void Update()
    {
        volume = GetMaxVolume();

     
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
        if ((volume >= minY) && (volume <= maxY))
        {
            //符合音量大小的条件开启,设置一个保护机制。几秒之后依然有音量才开始,否则判定没人。
            if (isStart)
            {
                ProCD += Time.deltaTime;
                if (ProCD >= ProCDTime)
                {
                    //开关打开
                    //最大值时长,超过开关关闭
                    if (ProCD <= (ProCDTime + maxTime))
                    {
                        Debug.Log("开始录音");
                        closeCD = 0;
                        isOver = false;
                        reStart = true;
                    }
                    else
                    {
                        ProCD = 0;
                        //触发开关口令,接下来的操作

                        isStart = true;
                        reStart = false;
                        //呼喊结束,写个方法进行下一步 保存录音
                        SaveRecorder();
                        Debug.Log("停止录音");
                    }
                }
            }
        }
        else
        {
            //判断,断两秒不算断。开关关上
            if (reStart)
            {
                closeCD += Time.deltaTime;
                if (closeCD >= closeCDTime)
                {
                    ProCD = 0;
                    perfabCount = 0;
                    newFather = false;
                    closeCD = 0;
                    isStart = true;
                    reStart = false;
                    //呼喊中断,写个方法进行下一步 保存录音
                    SaveRecorder();
                    Debug.Log("停止录音");
                }
            }

        }
    }
    //每一振处理那一帧接收的音频文件
    float GetMaxVolume()
    {
        float maxVolume = 0f;
        //剪切音频
        float[] volumeData = new float[128];
        int offset = Microphone.GetPosition(device) - 128 + 1;
        if (offset < 0)
        {
            return 0;
        }
        micRecord.GetData(volumeData, offset);

        for (int i = 0; i < 128; i++)
        {
            float tempMax = volumeData[i];//修改音量的敏感值
            if (maxVolume < tempMax)
            {
                maxVolume = tempMax;
            }
        }
        return maxVolume;
    }

    public void SaveRecorder()
    {
       
    }

}

 

你可能感兴趣的:(unity)