Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by landings · Apr 26, 2019 at 07:25 AM · audioclipmicrophone

How to get current gain from microphone?

I have the following code to check the pcm data from my microphone:

 private const int NUM_SAMPLES_TO_AVERAGE = 8;
 private const int PCM_SAMPLE_RATE = 16000;
 private float[] _pcm = new float[NUM_PCM_SAMPLES];
 private float _gain = 0;
 private AudioClip _audio;
 public GameObject gainBar;

 void Start()
 {
     if (Microphone.devices.Length == 0) {
         Debug.LogError("No Microphone");
         return;
     }
     _deviceName = Microphone.devices[0];
     _audio = Microphone.Start(_deviceName, true, 1, PCM_SAMPLE_RATE);
 }

 void Update()
 {
     var audioPosition = Microphone.GetPosition(_deviceName);
     _audio.GetData(_pcm, audioPosition);
     _gain = 0;
     for(int i = 0; i < NUM_SAMPLES_TO_AVERAGE; i++) {
         _gain += Mathf.Abs(_pcm[NUM_PCM_SAMPLES - i - 1]);
     }
     _gain /= NUM_SAMPLES_TO_AVERAGE;
     gainBar.transform.localScale = new Vector3(4, _gain, 1);
 }

So I have my _pcm array every frame. But it's not like what I expected. I want to visualize the gain of current time using _gainBar.transform.localScale, but its scale seems irrelevant to my loudness. Did I make a mistake?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Apr 26, 2019 at 09:03 AM

First of all the average over 8 samples is really not enough. You have to average over way more samples. At a sample rate of 16k and a framerate of 60 fps you get about 266 samples in between two frames.


So you should probably average over the last 1000 samples. Also note that you probably have a lot of noise with rather low amplitude. You can try to shift the sensitivity up by simply squaring each sample before adding. The highest value of 1 will stay 1. However a sample of 0.5 will become 0.25 and a sample of .0.25 would become 0.0625. Of course the overall value would get smaller, but you should get more sensitivity the higher the amplitude. After the division you can take the square root of the value to get back the previous range which should be "more" linear.


Just for example a 440 Hz tone has 440 cycles in a second. At a sample rate of 16kHz it would take 36 samples to cover the whole cycle. Deeper frequencies require even more samples (half the frequency, double the samples). If you only take 8 samples they could be right between the relatively low up and down half cycle you get almost no output even though there are much louder parts . With about 1000 samples you would capture frequencies down to about 16Hz. Just a few PCM samples always just look like random noise since there's no real structure in it.


ps: Note that you have an early exit in your Start method (which is fine). However if there's no microphone and you return, your "_audio" and "_deviceName" variables aren't initialized and would cause a null reference exception inside Update. You probably want to disable the script in Start when there's no mic and maybe print a Debug.LogWarning to the console / log

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image landings · Apr 26, 2019 at 12:00 PM 0
Share

Thank you so much!

avatar image landings · May 02, 2019 at 03:01 PM 0
Share

Hello, Now I have encountered another problem.

Even if I average over 1000 samples, I still find the gain incorrect. I want to visualize the PC$$anonymous$$ wave, and I find the curve is not a nicely continuous one. It looks like the array _pcm is divided into sections, and the newest wave front is not appearing from the end of the array. Could you help me figuring out how PC$$anonymous$$ data stores and updates?

I have my full code attached. Need a Text component for the public infoText field.

link text

visualizevoice.txt (6.2 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Microphone in 3.5 beta 1 Answer

Is it possible to record using Microphone.start and have non-fixed Audio clip length? 1 Answer

Extract pointer from Audioclip's buffer 0 Answers

Microphone recording problems (double sample, audiosource stops working) 0 Answers

Save Mic input and have it load when game starts up again 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges