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 Exalia · Nov 04, 2013 at 02:13 PM · audiomusic

Music Visualiser Help

Hi there, I'm working on a music visualiser and I would like to take a sample from an audiosource and obtain a volume sample for multiple frequencies.

I'm currently obtaining a sample using

AudioSource.GetOutputData(); AudioSource.GetSpectrumData();

I assume the first is volume(amplitude) while the second is frequency

I'm currently taking a sample size of 256 and applying the information I retrieve from the samples to the scale of the cubes to create a wave form. however I would like to have samples of specific frequencies so I can apply them to different objects and effectively have different dynamic objects responding to different frequencies.

If anyone has done this before/Has seen this somewhere before/has an Idea on how to do this I'd appreciate your assistance

Thanks in advance :)

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

2 Replies

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

Answer by zombience · Nov 04, 2013 at 05:19 PM

Hey, I've actually done a bit of work with this.

Here's the code I used to use to analyze audio in Unity. It was originally intended to analyze live audio input from your microphone. This ended up being a poor choice and extremely inefficient in Unity.

However, if you use this code to analyze playback of samples and files within Unity, it works just fine.

I haven't looked at this in a while as I've moved on to using external analysis, so some code cleanup might be necessary. But this should work for the most part.

 using UnityEngine;
 using System.Collections;
 
 
 public class FFT : MonoBehaviour {
         
     #region vars
     private struct AudioObj{
         public GameObject player;
         public AudioClip clip;
         public void SetClip(AudioClip c)
         {
             player.audio.clip = null;
             clip = null;
             clip = c;
             player.audio.clip = c;
         }
     }
     
     private AudioObj[] audioObj = new AudioObj[2];
     
     private const int BANDS = 4;
     
     public float[] curve = new float[BANDS];
     public float[] output = new float[BANDS];
     
     public string[] inputDevices;
     private int[] crossovers = new int[BANDS];
     private float[] freqData = new float[8192];
     private float[] band;
     
     private bool swap;
     
     public GameObject playerPrefab;
     private int index = 0;
     
     public static FFT Instance;
     private bool doSound = true;
     private int deviceNum= 0;
     #endregion
 
     #region Unity Methods
     void Start () 
     {
         Instance = this;
         
         crossovers[0] = 30;
         crossovers[1] = 50;
         crossovers[2] = 600;
         crossovers[3] = freqData.Length;
         
         band = new float[BANDS];
         output = new float[BANDS];
         
         for(int i = 0; i < 2; i ++)
         {
             audioObj[i].player  = (GameObject)Instantiate(playerPrefab);
             audioObj[i].player.transform.parent = transform;
             audioObj[i].player.transform.position = Vector3.zero;
             audioObj[i].clip = new AudioClip();
         
         }
         
         InvokeRepeating("Check", 0, 1.0f/15.0f);
         StartCoroutine(StartRecord());
         
         inputDevices = new string[Microphone.devices.Length];
         for (int i = 0; i < Microphone.devices.Length; i ++)
             inputDevices[i] = Microphone.devices[i].ToString();
     }
     
     void Update()
     {
         KeyInput();
     }
     #endregion
 
     #region Actions
     
     private void Check()
     {
         if(!doSound)
             return;
         
         AudioListener.GetSpectrumData(freqData, 0, FFTWindow.Hamming);
         bool cutoff = false;
         int k = 0;
         for(int i = 0; i < freqData.Length; i ++)
         {
             
             if(k > BANDS - 1)
                     break;
             
             float d = freqData[i];
             float b = band[k];
             band[k] = (d>b) ? d : b;
             if(i > crossovers[k] - 10)
             {
                 if(cutoff)
                     break;
                 
                 output[k] = band[k];
                 band[k] = 0;
                 
                 k++;
                 if(i > crossovers[BANDS - 1] - 10)
                     cutoff = true;
             }
         }
     
     }
     
     private IEnumerator StartRecord()
     {
         audioObj[index].SetClip(null);
         audioObj[index].clip = Microphone.Start(Microphone.devices[deviceNum], false, 2, 48000);
         print ("recording to audioObj " + index);
         StartCoroutine(StartPlay (audioObj[index].clip));
         yield return new WaitForSeconds(2);
         Microphone.End(Microphone.devices[deviceNum]);
         StartCoroutine(StartRecord());
         
     }
     
     private IEnumerator StartPlay(AudioClip buffer)
     {    
         audioObj[index].SetClip(buffer);
         
         yield return new WaitForSeconds(.05f);
         audioObj[index].player.SetActive(true);
         audioObj[index].player.audio.Play();
         
         audioObj[Mathf.Abs((index % 2) - 1)].player.audio.Stop();
         audioObj[Mathf.Abs((index % 2) - 1)].player.SetActive(false);
         
         index++;
         if(index > 1)
             index = 0;
         
         
     }
     
     private void KeyInput()
     {
         if(Input.GetKeyDown(KeyCode.A))
         {
             doSound = !doSound;
         }
         if(Input.GetKeyDown(KeyCode.Equals))
         {
             deviceNum ++;
             if(deviceNum > Microphone.devices.Length - 1)
                 deviceNum = 0;
         }
     }
     #endregion
     
     
 }
Comment
Add comment · Show 4 · 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 Exalia · Nov 04, 2013 at 05:23 PM 0
Share

Thanks for the help zombience! there is a lot of stuff here and I'm about to head out but when I come back i'll give it a thorough read. Thanks again! :)

avatar image zombience · Nov 04, 2013 at 07:33 PM 0
Share

definitely! use whatever works for you.
also, it bears mentioning that i was using this to split up the inco$$anonymous$$g data into only 4 bands (lows, low mids, high mids, and highs).

if you want something more like a 16 band EQ, you can do that with this code, but you'll have to do some work to figure out how to split the spectrum for analysis. There is a lot of help out there for this though, so you should be ok.

I chose my bands by listening, not by math, so your mileage may vary.

avatar image Exalia · Nov 06, 2013 at 09:31 PM 0
Share

This is great I got something working! Thanks a lot!

I'm definitely going to try and get more bands and get that working :) Hopefully I can find out somewhere else online how to go about doing that haha :)

avatar image zombience · Nov 06, 2013 at 09:47 PM 0
Share

great! glad it's working out for you. best of luck ^_^

avatar image
1

Answer by tanoshimi · Nov 04, 2013 at 02:38 PM

Like this?:

http://karllim.weebly.com/unity3d-simple-visualizer.html

(warning - turn your speaker volume down before clicking the link. You'll see why...)

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 Exalia · Nov 04, 2013 at 05:14 PM 0
Share

The webpage takes a very very very long time to load :\

I'll have a look when it loads at a decent speed.

Thanks in advance it looks promising

avatar image tanoshimi · Nov 04, 2013 at 05:17 PM 0
Share

It's got a 5$$anonymous$$b Unity Webplayer demo embedded in it, but that's all.

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

18 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

Related Questions

Audio controls disabled??! 0 Answers

Stereo Mix as Input? 1 Answer

IEnumerator issues 3 Answers

How To Turn Off Sound For Certain Objects? 1 Answer

Stop audio/background music from pausing when showing ads 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