• 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 ForgeStudios · Jan 28, 2016 at 05:54 AM · c#audioprogramminggetspectrumdata

AudioSource.GetSpectrumData Failure

I am currently working on making an audio visualizer in unity, all has been going well, but then all of a sudden, I stopped receiving spectrum data from my script. I had a project like this in the past and the same thing happened to me while I was experimenting with subdividing my sampling ranges. Does anyone know why this happens? The script is attached if you want to try it, you just need to have an audio source and listener on the scene. All audio data should be processed to the barData float array, but it remains at all 0's.

 using UnityEngine;
 using System.Collections;
 
 public class Visual1 : MonoBehaviour {
 
     public int samplesCount;
     public float[] barData;
     public float limit;
     
     float[] samples;
     int count;
     float diff, curValues;
     AudioSource audio;
     
     void Start(){
         audio = GetComponent<AudioSource>();
         samples = new float[samplesCount];
     }
     
     void FixedUpdate(){
         count = 0; diff = 0;
         AudioListener.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris);
         for(int i = 0; i < barData.Length; i ++ /*i += 4*/){
             barData[i] = sampleAudio(i, i, (int)Mathf.Pow(2, i + 1));
             //Audio Subdivisions for adding more bars and more percision
                 //barData[i + 1] = sampleAudio(i+1, i, (int)(Mathf.Pow(2, i + 1) + (Mathf.Pow(2, i + 1)/4)));
                 //barData[i + 2] = sampleAudio(i+2, i, (int)(Mathf.Pow(2, i + 1) + (Mathf.Pow(2, i + 1)/2)));
                 //barData[i + 3] = sampleAudio(i+3, i, (int)(Mathf.Pow(2, i + 1) + (Mathf.Pow(2, i + 1)/2) + (Mathf.Pow(2, i + 1)/4)));
         }
     }
     
     float sampleAudio(int c, int h, int sampleCount){
         //print(h + " - " + c + ": " + sampleCount); //Debugging
         float average = 0;
         
         for(int i = 0; i < sampleCount; i ++){
             average += samples[count] * (count + 1);
             count++;
         }
         average /= count;
         diff = Mathf.Clamp(average * 10 - curValues, 0, limit);
         curValues = average * 10; //Prepare to subtract all previous acconted noise
         return diff;
     }
 }
 

Thank you in advance for any help!

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

67 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trying to make an audio visualizer, not sure how to distinguish frequencies with GetSpectrumData 0 Answers

How to play "audiosource" component in a prefab from script? 0 Answers

GetComponent C# 1 Answer


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