Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AudioSource.getSpectrumData questions

Discussion in 'Audio & Video' started by jameskyle, Jun 23, 2015.

  1. jameskyle

    jameskyle

    Joined:
    May 7, 2013
    Posts:
    37
    I've been muddling with this all day now and have a few probably very easily answered questions.

    I'm getting spectrum data with the following line:
    Code (CSharp):
    1. spectrum = audio.GetSpectrumData(64, 0, FFTWindow.BlackmanHarris);
    2.  
    I'm unclear on a few things, but they're likely related.

    Firstly the values seem very low and drop lower the further into the spectrum data I go.

    Secondly I get what seems to be more desirable results when I only use the first 1/8th of the spectrum, though the first few spectrum values seem higher than they should be, or at least higher than the others as the value drop-off seems present here too.

    I presume that I have to reformat the spectrum array somehow to scale each entry, increasing the scale factor as the array entry increases though the maths of it are lost to me. I've seen mentions of Mathf.Log and scaling the spectrum entries with Mathf.Log(spectrum * i * i) (where i is the index) but this had little effect, or perhaps I just implemented it incorrectly. I don't know. :/

    I'd be grateful for any aid, as I'm not sure what to do here besides buying something from the asset store to do the job for me.
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    No idea about the maths either, What exactly are you trying to do?

    Here is some code to just get the decibals at any given time. Maybe this is helpful to you, or maybe it isn't.

    Even then, the values you get will be very erratic.
    When I use the averaged Decibal to move a transform position for example, I don't apply it directly, I smooth the movement by adding or subtracting a tiny value to the position, moving towards whatever the DB based value is.

    Code (csharp):
    1.  
    2.         private float[] samps;            //Audio samples of clip
    3.         ...........
    4.         //In UPDATE
    5.         //Collect some data
    6.         audSauce[0].GetOutputData(samps, 0);
    7.  
    8.         //Sum the current batch
    9.         float Sum = 0;
    10.         for (int i=0; i < 1024; i++)
    11.         {
    12.             Sum += samps[i]*samps[i]; // sum squared samples
    13.         }
    14.  
    15.         float rmsValue = Mathf.Sqrt(Sum/1024); // rms = square root of average
    16.         float dbValue = 20*Mathf.Log10(rmsValue/0.1f); // calculate dB
    17.         if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
    18.  
     
    Last edited: Jun 24, 2015
  3. jameskyle

    jameskyle

    Joined:
    May 7, 2013
    Posts:
    37
    My current code takes sets of spectrum data (1/8ths) and averages them, and also keeps averages of these over time for smoother transitions when this data is used to affect game objects. It's all in service of creating a visualisation of audio like you would see on a hifi/music player, with the different frequencies being displayed as meters.

    I'll look into getOutputData to see if that's what I need, as I've only tried getSpectrumData.
     
  4. jameskyle

    jameskyle

    Joined:
    May 7, 2013
    Posts:
    37
    I just bought Visualizer Studio on the asset store which seems to do what I need.
     
  5. Kamil_Reich

    Kamil_Reich

    Joined:
    Aug 14, 2014
    Posts:
    195
    Hi, may you say how to get dB of every i? I would like to know how loud is every part of my audioclip.