Search Unity

Audio Visualization/Preview for Editor Tool

Discussion in 'Immediate Mode GUI (IMGUI)' started by kromenak, Mar 5, 2015.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    I'm working on a little authoring tool that allows you to place markers on a timeline to mark events during playback of an audio clip. It works fine, but sometimes it is a bit hard to visualize where you are in the audio clip exactly. To help combat this, I was hoping to display an audio visualization in the background of the tool's timeline. Ideally, this would look somewhat like the visualization when you're editing a clip in audacity: http://upload.wikimedia.org/wikipedia/commons/a/a2/Audacity-Screenshot.jpg

    I see that Unity is actually already doing this: when you select an AudioClip, the preview area shows almost exactly the kind of visualization I'd like to display within my tool.

    My question, then: is there a way to either directly use the visualization already used in Unity or to mimic it in some way using the AudioClip data? I've considered using the AudioClip.GetData method, but it seems to always return zeros for me (possibly because the clip is compressed, or possibly because the game isn't running?). Also, GetSpectrumData or similar doesn't seem to be available because this is an editor tool and I have no AudioSource/AudioListener that is actually playing the song.

    I think that if I'm able to access the appropriate data about the audio clip, I should be able to use the GL class to draw the visualization in the tool.

    Sounds possible or maybe not?
     
  2. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    452
    Yes, actually - Unity has a built in class for handling editor audio called AudioUtil, but you can only access it through reflection. It has a method called GetWaveForm.

    I actually just yesterday posted a script on this forum that you can use to access it (although it's simple enough to get at if you know how to use reflection). You can download it here if you like.
     
    kromenak and rakkarage like this.
  3. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    Awesome, thanks! Just gave it a shot, and it works really well! Crazy how much stuff is hidden under the hood with reflection, but glad it's possible.