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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating objects to rhytm of music

Discussion in 'Audio & Video' started by lushao, Oct 17, 2015.

  1. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    Hello,
    How can I do to the rhythm of the music created objects?

    I know I have to do this using Audio Spectrum, but I do not know what to do next

    Please, tips, videos, links, whatever.
     
  2. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    Hi!
    The GetSpectrumData Method will give you what you need.
    The example provided there should be enough to get you started.
    Divide the returned data in bands if you want to move objects according to specific frequencies.
     
  3. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    @WoozyBytes How can I do that? There he finds himself in this topic :c
     
  4. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    The GetSpectrumData() Method will return you a block of the data currently playing this will be a float array of the specified size(1024 or 2048 samples should be enough) containing the audio data.
    This is your frequency range.
    Here's an rough example of how to bounce an object according to the bass frequency
    i hope it helps please let me know if you have any questions
    This Line --> " objectToMove.position.y = spectrumSamples[smplN] * moveMultiplier ;"
    Will move your object but you better of using Mathf.SmoothDamp() method to smoth out the movement.

    var qSamples : int = 2048; // QWindow
    var channels : int = 2;
    var bassMinHz : int = 30; //Min bass Frequency in Hz
    var bassMaxHz : int = 120; // Max bass Frequency in Hz
    private var bassMinSamples: int ; //Min bass in samples
    private var bassMaxSamples : int ; // Max bass in samples
    private var spectrumSamples : float[];
    var objectToMove : Transform; // Add you object here
    var moveMultiplier : float = 10; // Adjust this to move more or less
    var audioListener : AudioListener; // Assign one audioListener here
    var audioisPlaying : boolean = false;

    function Start()
    {
    spectrumSamples = new float[ qSamples];
    bassMinSamples = (bassMinHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
    bassMaxSamples = (bassMaxHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
    }

    function Update ()
    {
    if( audioListener != null && audioisPlaying == true)
    AnalizeSpectrum() ;
    }
    function AnalizeSpectrum(){
    audioListener.GetSpectrumData( spectrumSamples, 0, FFTWindow.Rectangular );
    for ( var smplN = 0; smplN < spectrumSamples.Length ; smplN ++ ){
    // If the current sample is withing the bass range move the object
    if( smplN >= bassMinSamples && smplN <= bassMaxSamples ){
    objectToMove.position.y = spectrumSamples[smplN] * moveMultiplier ;
    }
    }
    }
     
    johnnsAlex and lushao like this.
  5. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    @WoozyBytes Thank you for your help. more keen on creating objects when there's a bass. How can I use Instantiate in easiest method?
     
  6. WoozyBytes

    WoozyBytes

    Joined:
    Mar 16, 2015
    Posts:
    8
    Instead of this -- > objectToMove.position.y = spectrumSamples[smplN] * moveMultiplier ;
    use Instantiate() if the spectrumSamples[smplN] value is higher than a pre defined threshold( you will need to play around to get the right value , also it will depend on the sound itself , some sounds have less/more energy at specific frequencies);
    Be careful since AnalizeSpectrum() runs every frame you don't want to instantiate stuff every frame.
     
    lushao likes this.
  7. lushao

    lushao

    Joined:
    Oct 17, 2015
    Posts:
    27
    @WoozyBytes thanks for Help <3 You could write a complete script? That is, lest objects formed by Instantiate only when the music beat?