Search Unity

Reflected AudioUtil class for making audio based Editor Extensions

Discussion in 'Immediate Mode GUI (IMGUI)' started by Rtyper, Mar 7, 2015.

  1. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    393
    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:
    44
    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:
    393
    No problem, glad someone has some use for it :)
     
  4. jhample

    jhample

    Joined:
    Jul 7, 2012
    Posts:
    44
    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:
    393
    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:
    44
    I didn't even think to try that. That works perfect.
     
  7. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    118
    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:
    393
    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:
    118
    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:
    23
    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 !
     
  12. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    207
    This is great! Is there a way to change the playback volume, though?
     
  13. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    463
    Hi, has anyone reflected AudioUtil.cs for 2019.x? Seems like there are some changes and code above does not work. Or parts of it do not work. Thank you, Marek.
     
  14. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    393
    I haven't really updated this in a long time - I switched to using a hidden GameObject with an AudioSource on a long time ago (round about when Unity 5 was new I think) because of problems with sounds refusing to stop and then cutting out entirely! They also removed the waveform texture methods in 5.5 or 5.6 I think. You're better off going with the GameObject method these days - you can set its hideFlags from a script so it doesn't get saved with your scene or show up in the hierarchy.