Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity Audio Spectrum Visualisation problem

Discussion in 'Scripting' started by shadow_monk, Dec 6, 2014.

?

The code compiled, the cubes does't move Unity Audio Spectrum Visualisation

Poll closed Dec 6, 2015.
  1. the problem starts in for loop

    0 vote(s)
    0.0%
  2. have no freaking idea

    0 vote(s)
    0.0%
  3. take a brake

    0 vote(s)
    0.0%
  1. shadow_monk

    shadow_monk

    Joined:
    Dec 3, 2014
    Posts:
    7
    I have a code from tutorial..The code compiles but no effect of scaling objects. In addition i dont quite undersund thse spectrum things. I know that a spectrum number represents Hz (spectrum of 1024 devided by 22050 Hz) so spectrum[1]= around 21Hz

    but what I can se there is something like var c1=spectrum[1]+spectrum[2] and the question is why?
    i know that C2 represents a sound of specific frq. but why these spectrums are added.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8. var spectrum :float[]=AudioListener.GetSpectrumData(1024,0,FFTWindow.Hamming);
    9.  
    10.  
    11. var c1:float=spectrum[1]+spectrum[1]+spectrum[1];
    12. var c2:float=spectrum[11]+spectrum[12]+spectrum[13];
    13. var c3:float=spectrum[22]+spectrum[23]+spectrum[24];
    14. var c4:float=spectrum[44]+spectrum[45]+spectrum[46]+spectrum[47]+spectrum[48]+spectrum[49];
    15.  
    16.  
    17. var cubes: GameObject[] = GameObject.FindGameObjectsWithTag("cube");
    18. for( var  i=0;i<cubes.length;i++){
    19.      switch(cubes[i].name){
    20.      case "c1":cubes[i].transform.localScale.x=c1;Debug.Log("super"  +c2);break;
    21.      case "c2":cubes[i].transform.localScale.x=c2;break;
    22.      case "c3":cubes[i].transform.localScale.x=c3;break;
    23.      case "c1":cubes[i].transform.localScale.x=c4;break;
    24.      }}
    25.  
    26.      }

    the code is from

    thx in advance. This post will be seen for 12 months to help others to understand that thing
     
    Last edited: Dec 6, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Fffff that's terrible
    Anyway, tagged the cubes as cube?

    Its because the spectrum values are small, by summing multiple bands you get a larger value, and your bars will scale more across a range of frequencies
     
  3. shadow_monk

    shadow_monk

    Joined:
    Dec 3, 2014
    Posts:
    7
    tags are fine.. there is a yellow warning"
    Assets/musick/musick analiser.js(8,37): BCW0012: WARNING: 'UnityEngine.AudioListener.GetSpectrumData(int, int, UnityEngine.FFTWindow)' is obsolete. GetSpectrumData returning a float[] is deprecated, use GetOutputData and pass a pre allocated array instead."
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Not sure that would change anything, but worth trying.
    It works like this:
    Code (CSharp):
    1. var spectrum : float[] = new float[1024];
    2. AudioListener.GetSpectrumData(spectrum, 0, FFTWindow)
    (not spellchecked)
     
  5. shadow_monk

    shadow_monk

    Joined:
    Dec 3, 2014
    Posts:
    7
    1. Assets/musick/musick analiser.js(9,30): BCE0023: No appropriate version of 'UnityEngine.AudioListener.GetSpectrumData' for the argument list '(float[], int, System.Type)' was found.
    the main script is in js
     
  6. shadow_monk

    shadow_monk

    Joined:
    Dec 3, 2014
    Posts:
    7
    for example this thing code wokrs
    #pragma strict
    var object:GameObject;
    function Start () {

    }

    function Update () {
    var spectrum :float[]=AudioListener.GetSpectrumData(1024,0,FFTWindow.Hamming);


    var c1:float=spectrum[1]+spectrum[1]+spectrum[1];
    var c2:float=spectrum[11]+spectrum[12]+spectrum[13];
    var c3:float=spectrum[22]+spectrum[23]+spectrum[24];
    var c4:float=spectrum[44]+spectrum[45]+spectrum[46]+spectrum[47]+spectrum[48]+spectrum[49];

    object.transform.localScale.y=c1*39;

    }

    so the problem must be in other part