At first, I thought I would only take the last sample of the recording to determine the volume size, and found that it was undetectable.
Later search, the original need to take a sample to judge, some of this section to take the average as the volume size. I'm here to take out a sample of the peak as the volume
Audio.clip.GetData got a value of 0-1, so I took the 99来 to divide the volume into four levels (0-24-49-74-99)
1 Public floatVolume2 {3 Get4 {5 if(Microphone.isrecording (NULL))6 {7 //Number of Samples8 intSampleSize = -;9 float[] Samples =New float[samplesize];Ten intStartPosition = Microphone.getposition (NULL)-(samplesize+1); One //Get the data A This. Audio.clip.GetData (samples, startposition); - - //Getting a peak on the last samples the floatLevelmax =0; - for(inti =0; i < samplesize; ++i) - { - floatWavepeak =Samples[i]; + if(Levelmax <wavepeak) -Levelmax =Wavepeak; + } A at returnLevelmax * About; - } - return 0; - } -}
Unity Microphone detects sound size when recording