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
0
Question by dskillsaw · Apr 02, 2014 at 08:35 PM · audionangetspectrumdata

Audio.GetSpectrumData() returns NaN

This is driving me nuts and I'm not sure if it's just a windows 8.1 bug or something, but audio.GetSpectrumData() sometimes returns NaN, and sometimes works with the exact same code. I am so confused.

 using UnityEngine;
 using System.Collections;
 
 public class AudioSpectrumExample : MonoBehaviour {
     void Update() {
         float[] spectrum = new float[256];
         audio.GetSpectrumData( spectrum, 0, FFTWindow.Hamming);
         Debug.Log (spectrum [54]);
         int i = 1;
         while (i < 255) {
             Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red);
             Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan);
             Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green);
             Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.yellow);
             i++;
         }
     }
 }

This line: Debug.Log (spectrum [54), shows me the NaN.

 NaN
 UnityEngine.Debug:Log(Object)
 AudioSpectrumExample:Update() (at Assets/Scripts/MusicVisualizer/AudioSpectrumExample.cs:8)


Thanks for any help!! (And yes I do have an audio source attached to the same game object that's running that script.)

Comment
Add comment · Show 1
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 Gruffy · Apr 05, 2014 at 02:54 PM 0
Share

Hello bud, did you sort your issue, or have you taken a look at the answer posted below. This explains your issue, I hope that clears things up for you. Please mark as answer, unless you feel the problem is not sorted, in which case return here to continue through to a solution for you. Take care bud. Gruffy

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gruffy · Apr 02, 2014 at 08:50 PM

Hey bud, you need to add an audio source component to your GameObject holding this script.

You must then supply that AudioSource with an AudioClip (wav,mp3, ogg file).

These are why your above script(taken from Unity docs) is not working, I just copied your script and added the AudioSource compoent to the object with script and imported a quick track...all working fine. Check you console, when you have done the above and see the Spectrum data recorded in there at runtime.

Image showing it working alt text Take care bud Gruffy


visualanswer.png (144.4 kB)
Comment
Add comment · Show 7 · 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 dskillsaw · Apr 05, 2014 at 04:59 PM 0
Share

Hey Gruffy! Thanks so much for the detailed response, but unfortunately I think the issue is weirder than that :P In my case I do have an Audio Source attached which I've tried with both an ogg and an mp3.

What's frustrating is I just fired up my test so that I could post a screenshot of it to share, but now it's working. However I'm certain that eventually it will stop working and start outputting those NaN's again. When it does I'll post the screenshot.

Thanks again for the response!

avatar image Gruffy dskillsaw · Apr 05, 2014 at 06:00 PM 0
Share

cool man, when it happens do chuck it up here, because I just copied your code from above and console gave tge same errors as you stated and the audio source and clip were what console specified were resolution, these were added including a wav file and it worked, no code changes or anything.. strange one bud, so yeah interested tobsee it when it happens. Anywaym if u like I can zip up a package to show what im saying, it may be cheaper file size wise though for u to add ur own audioclip to the audiosource component in the scene I woukd be sending etc, your call bossman take care bud gruffy

avatar image dskillsaw dskillsaw · Apr 05, 2014 at 06:25 PM 0
Share

Ok thanks again for the help! I did just repro my issue (image attached), but then when I recreated a new project from scratch - probably identical to the one that you made - yet again the problem went away. Something sooooooooo strange is going on.

To be totally clear, you were able to fix the errors by adding the AudioSource and .wav file to the game object that had the AudioSpectrumExample component?

Here's my failure case example: alt text

audiofailure.png (52.3 kB)
avatar image Gruffy dskillsaw · Apr 05, 2014 at 07:10 PM 0
Share

here you go bud, you will have to add your own music (wav, ogg, mp3) in yourself due to file size , but simply unzip the zip, extract package and import to a new or open project.

Find the scene and double click it, then go to the GameObject childed to the $$anonymous$$ain Camera, on that gameobject add a song into the AudioSource component there. Press Play. Stop and Press Play again, as the first run might be slow to catch up on analysis etc, will be fine for second time round though etc.

ZIPPAckage

You should see you have the same code and should also have the same(or similar at least) setup, the only other thing I can suggest is that your audio may need optimizing like making sur eit is a 2d sound, that it is set to loop and play on awake etc, all inside the AudioSource component attributes in inspector, excpet for changin to 2d Sound, that`s done through the audio inspector for the clip you are using, by clicking the audioclip n all that. Anyway take care bud. hope that helps in some way to solve this for you. ...& remember, this package needs an audio clip due to file size restrictions on here, sorry. :) Gruffy

getspectrumdata.zip (4.9 kB)
Show more comments
avatar image dskillsaw · Apr 06, 2014 at 11:06 PM 0
Share

Hey Gruffy!

So I had a chance to try his out and all seems good. It continues to be super strange - I did have one hiccup where my code went back to NaN (not your code), but after doing more investigation it simply fixed itself again. I'm going to keep an eye on this for a few more days. If I doesn't come back, I suppose I'll just mark this thread as answered. Gonna wait a bit on that just to make sure - still feels like there could be a deeper issue with windows 8.1 or something.

Ok thanks again for the help, i owe you one!

avatar image drudiverse · Dec 01, 2014 at 08:02 AM 0
Share

http://answers.unity3d.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html

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

21 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

Related Questions

Can I GetSpectrumData from an AudioMixerGroup? I need that data with the filters applied 0 Answers

GetSpectrumData over entire file 1 Answer

AudioSource.GetSpectrumData Failure 0 Answers

Problem with GetSpectrumData 0 Answers

GetSpectrumData or equivalent in non-realtime mode? 2 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