Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Chiki40 · Jun 25, 2012 at 10:17 AM · androidaudiosamplefrequency

Audio frequency sample on Android

Hi! I'm new into unity and i'm trying to build an application that get information from the microphone and show the main frequency of the signal on the screen. I will use this to create an application that do some stuff when a tone in a specific frequency is detected (something like audio augmented reality). Looking in Unity community I found this pos which I found really useful (thank you all who helped with that topic): http://forum.unity3d.com/threads/118...-Microphone%29

I'm working now with this code

          private const int SAMPLECOUNT = 1024;
       private const float REFVALUE = 0.1f;
       private const float THRESHOLD = 0.02f;
      
       private float rmsValue;
       private float dbValue;  
      
       private float[] samples;        
       private float[] spectrum;        
  
       public void Start () {
         Application.RequestUserAuthorization (UserAuthorization.Microphone);
         if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
           samples = new float[SAMPLECOUNT];
           spectrum = new float[SAMPLECOUNT];
           StartMicListener();
         }
       }
      
       public void Update () {
         if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
           if (!audio.isPlaying) {
             StartMicListener();
           }
      
           AnalyzeSound();
     }
       }
      
       private void StartMicListener() {
         audio.clip = Microphone.Start(null, true, 1, AudioSettings.outputSampleRate);
         audio.loop = true;
         audio.mute = true;
         audio.Play();
       }
      
       private void AnalyzeSound() {
      
         // Get samples from microphone
         audio.GetOutputData(samples, 0);
      
         // Sums squared samples
         float sum = 0;
         for (int i = 0; i < SAMPLECOUNT; i++){
           sum += samples[i] * samples[i];
         }
      
         rmsValue = Mathf.Sqrt(sum / SAMPLECOUNT);
         dbValue = 20 * Mathf.Log10(rmsValue / REFVALUE);
      
         if (dbValue < -160) {
           dbValue = -160;
         }
      
         // Gets the sound spectrum.
         audio.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
         float maxV = 0;
         int maxN = 0;
      
         // Find the highest sample.
         for (int i = 0; i < SAMPLECOUNT; i++){
           if (spectrum[i] > maxV && spectrum[i] > THRESHOLD){
             maxV = spectrum[i];
             maxN = i; // maxN is the index of max
           }
         }
      
         float freq = maxN * 24000 / SAMPLECOUNT;
         gameObject.guiText.text = freq.ToString ();
       }

I've run the application on a Macbook, and it works fine, I generate tones of several frequencies and it shows correctly. However, if I run on an Android device, the frequency shown is incorrect (it's value seems to be halved in most cases, although in some frequencies it keeps another proportion). I tested a native android application which shows audio spectrum and it works fine, so I don't think it's a hardware problem of my device. Is this a Unity bug compiling into Android? I'm using Unity 3.5.1.

Thank you

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
0

Answer by Tongie · Dec 14, 2013 at 03:09 PM

I have this problem too anyone?

Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Touch returns twice on Android 1 Answer

App restarts Android device on launch 1 Answer

Is sound in background/screen off possible yet (Unity 5)? 1 Answer

Decrease Android build size when using a lot of sounds 1 Answer

Ambisonic Audio working on PC but not Android 3 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