Unity Microphone detects sound size when recording

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.