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

Anyone know how to script an audio visualizer??

Discussion in 'Scripting' started by bahk007, Sep 25, 2016.

  1. bahk007

    bahk007

    Joined:
    Sep 24, 2016
    Posts:
    4
    Hello friends, would anyone have an idea of how to script an audio visualizer onto preexisting instanced objects? The same way this (
    ) video scales and gradients the objects it generates depending on the audio source, but without generating any objects and instead applying the scales/gradients to an existing group of objects. I'm a very novice is coder is why I ask.
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I know very little about audio. I do know when a song is playing its creating some waveform. Like a simple sin wave just makes a single tone. You'll need to break the problem into the following steps. Each one varies from easy to complex and you'll just have to tackle each one at a time till you figure out all of them:

    • Create a 3D cube Prefab from the basic Unity Primtive 3D object Cube
    • Create an algorithm to instantiate a lot of these Cubes in concentric circles (looks like about 25-30 circles in the video).
    • Create lists of objects in each ring so you can access them easily
    • Each Update, get the current wave being put out by the audio. Chop it up into the same number of segments as you have circles.
    • Use the height at each segment to Scale the Y value of all the cubes of that ring.
    • Color all the cubes of that ring based on the height
    • Finally. The circles above aren't "perfect". Their is a north/south and east/west axis perpendicular to each other. The circles draw straightlines along the radius at these axis, then interpolate the curve between the lines. The spinning motion comes from slowly rotating those axis and repositioning all the cubes.
    (Note: The first 2 steps listed are skipped if you have pre-existing objects. You will still need to arrange your objects into separate lists somehow. This will depend on what kinds of objects you have and how you've arranged them).

    Boom. Now you have the video above. Just to stress I do know very little about audio, so my assumption that you can get some kind of waveform each Update from from the audio that you can chop up into pieces may be erroneous. But I'm fairly sure you can.

    If you have problem on each of the steps then post another question to the forums and people will gladly help. Right now your question is so broad breaking down the steps is the best answer. Other than someone writing the entire app for you :)
     
    Last edited: Sep 25, 2016
    ericbegue likes this.
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    What you want to realize is a visually appealing spectrum analyser, which is a visualization of the frequencies and their intensity that exist in an audio wave. Visualizing the raw waveform (sound intensity in time) is not very interesting, because we (or our hears) are sensible to the pitches or notes present in the sound, which are characterized by the sound frequencies . So you need to extract these frequencies from the raw audio signal in order to get an information closer to what we are actually hearing. This process is usually done by applying the Fourier Transform, which transforms a waveform from the time domain to the frequency domain.

    There are several implementations of the Fourier Transform in C#, this thread can be a starting point to help you pick one.

    On the Unity side, you can access the waveform (the raw data) of a sound via AudioClip.GetData.
     
    takatok likes this.
  4. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
  5. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
  6. bahk007

    bahk007

    Joined:
    Sep 24, 2016
    Posts:
    4
    Thank you all for the responses!
    @takatok I do realize these are the steps I need to take to create said visual. I'm putting this on the Unity Forums because of my lack of coding ability. I've instanced my own objects already into my project, I just have no clue of how to code the parts I need coded. I'm wondering if anyone on the forum could help with the development of tying in these transformation and gradient effects into my project. Basically outlining the code I would need to make into a script so I can drop it onto the instanced group of objects I have.
    @ericbegue that seems like polish? Or maybe not, my lack of coding knowledge isn't much help. But, i've followed the links posted above and I don't really understand how to implement any of that into a script I can use in Unity.
    @IsGreen is this pseudo code I can translate into a Unity script?
    @MV10 I've looked over this asset and am trying to figure out how to implement it into this "audio visualizer"