Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Reflected AudioUtil class for making audio based Editor Extensions

Discussion in 'Extensions & OnGUI' started by Rtyper, Mar 7, 2015.

  1. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    322
    Hi guys,
    I've been working on an editor extension recently that required the ability to play/pause/stop and get waveforms of various AudioClips within the editor. I wanted the cleanest way of doing this possible (so no creating hidden GameObjects etc) so I went searching online, and found this thread.

    So, after shamelessly stealing A Shim.3D's code snippet there, I wrote a method to find all the static methods in that AudioUtil class, and started using that code to get access to the ones I needed.
    The end result is this, AudioUtility.cs. It has every public static method from AudioUtil available in it. Hopefully some other people will be able to get some use out of these features too!

    Simply copy it into your project, and import the "UnityEditor" namespace in your script to use it.

    Cheers!
     

    Attached Files:

    monremobile and LukaKotar like this.
  2. jhample

    jhample

    Joined:
    Jul 7, 2012
    Posts:
    41
    Thank you soooo much! I am working on a new editor for a rhythm game and this is amazingly helpful!
     
  3. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    322
    No problem, glad someone has some use for it :)
     
  4. jhample

    jhample

    Joined:
    Jul 7, 2012
    Posts:
    41
    I am having an issue with starting a song at a specific point. It seems to always start at the beginning. Is this a bug, or am I not using it properly?
    Code (CSharp):
    1. AudioUtility.PlayClip(song.song, songStart, false);
    Where song.song is an Audioclip and songStart is a value between 0 and AudioUtility.GetSampleCount()
     
  5. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    322
    Unfortunately that does seem to be a bug, I haven't had any luck getting that to work either. I included them in the class anyway just in case Unity fixes it in future.

    The best solution I've got so far is just playing it normally, then using SetClipSamplePosition straight afterwards. Seems to work just as well, but it's not quite as clean.
     
  6. jhample

    jhample

    Joined:
    Jul 7, 2012
    Posts:
    41
    I didn't even think to try that. That works perfect.
     
  7. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    99
    1. Code (CSharp):
      1.   sampleStart = (int)Math.Ceiling (audioClip.samples * ((__sequence.timeCurrent - node.startTime) / audioClip.length));//do your calculation
      2.                    
      3.                                  AudioUtilW.PlayClip (audioClip, 0, node.loop);//startSample doesn't work in this function????
      4.                                                              AudioUtilW.SetClipSamplePosition (audioClip, sampleStart);
    Problem that I couldn't solve was with Stop and StopAll not working when you have started more clips to play at once. Seem stoping last one only.
     
  8. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    322
    Yeah, I've come across similar problems (though only in Unity 5) with clips playing. It also sometimes breaks altogether for me, and stops the Unity editor from playing any audio at all until it's restarted. Unfortunately, I suppose this is stuff we just have to put up with unless Unity decides to make the AudioUtil class public and supports it.
     
  9. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    99
    Or some Unity inhous developer tell us about secret sauce. :). I notice that above happen even in Unity Audio Inspector where they use StopAll. You can play some sound outside and thru Audio Inspector in parallel. Then try to StopAll... I've played Vidoe Sound and play good when not played in parallel with some other sound then sample gone crazy.

     
  10. sugamo

    sugamo

    Joined:
    Nov 5, 2014
    Posts:
    1
    Hello. I greatly appreciate your sharing AudioUtility.cs!

    I use this for drawing wave-form of audioclip and play audio like preview window.

    However, in unity5.4, getting wave-form texture method disappear!
    If you know how to easily draw wave-form of audioclip without GetWaveFormFast() , please tell me..
     
  11. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    19
    3 years later and this still helps, once the few quirks are known (can't stop an audio when multiple are played, "start sample" is basically useless but SetClipSamplePosition fixes it, etc...) your class is really helpful, thanks !