关于八分音符C#脚本实现

publicclassMicPhone : MonoBehaviour {

    publicfloat volume;

    privatestring devicesName;

    AudioClip clip;

    privateint lenth = 128;

         // Use this for initialization

         void Start () {

        devicesName = Microphone.devices[0];

        clip = Microphone.Start(devicesName,true,999,44100);

         }   

         // Update is called once per frame

         void Update () {

        if (Microphone.IsRecording(devicesName))

        {

            volume =GetMaxVolume();

        }    

         }

    float GetMaxVolume()

    {

        float maxVolume = 0f;

        float[] volumeData = newfloat[lenth];

        int offSet = Microphone.GetPosition(devicesName)- lenth + 1;

        if (offSet < 0)

        {

            return 0;

        }

        clip.GetData(volumeData,offSet);

        for (int i = 0; i

        {

            float tempMax =volumeData[i];

            if (maxVolume

            {

                maxVolume =tempMax;

            }

        }

        return maxVolume;

    }

    privatevoid OnDestory()

    {

        Microphone.End(devicesName);

    }

}

你可能感兴趣的:(C#)