Search Unity

Audio How does audiosource.PlayOneShot works?

Discussion in 'Audio & Video' started by gamrDev, Apr 24, 2020.

  1. gamrDev

    gamrDev

    Joined:
    Jul 7, 2017
    Posts:
    12
    By default audiosources can play only one sound and the clip they are playing is shown at audioClip field in audiosource component.

    And when I tried playing music clips with playoneshot they were playing fine but
    1) I couldn't see either of those clips in the audioClip field and
    2) I could control the volume, pitch of both the clips using that single audiosource.

    If anyone has understood the concept then please tell me how it works or point me to the resources to understand.
     
    neonblitzer likes this.
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    140
    Using Play One Shot plays a sound once through the audio source. It doesn't require an Audio Clip in the field because you can play multiple sounds from one audio source using Play One Shot, which is why you need to specify the Audio Clip when calling it.

    The Audio Source volume will control the level of any sound playing through that audio source. When you call Play One Shot you can choose to add a volume scale parameter, which will be a percentage (between 0 and 1) of the Audio Source's volume.

    This means you can play two sounds from one Audio Source using Play One Shot at different volumes but turn them both down using the Audio Source volume control if you want.

    Generally it's best to use Play One Shot for sound effects and other short audio clips and use Play for music and long loops (as Play One Shot can't loop audio)

    I wrote a guide on the difference between audio triggers in Unity, and when to use them, on my blog here: https://gamedevbeginner.com/how-to-play-audio-in-unity-with-examples/

    Hope that helps.
     
  3. gamrDev

    gamrDev

    Joined:
    Jul 7, 2017
    Posts:
    12
    So does that mean that unity internally creates multiple audiosources (because by default only one sound can be played from an audiosource) and it then associates all the parameters like pitch, volume, etc with calling audiosource.
    And what happens if I use playoneshot for a bullet sound. Will it have any performance issues?

    And I read your blog and in it you say playoneshot will count as voice what do you mean by that?
     
  4. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    140
    You can play multiple Audio Clips through one Audio Source but only by using Play One Shot. The other play functions interrupt the playing sound.

    Every Audio Clip you play through that Audio Source will use the Audio Source's settings. It's not the case that Unity creates multiple Audio Sources, it's just one with multiple clips playing through it (i.e. changing something on the audio source affects all the clips playing through it).

    A voice is a playing audio clip and there's a default limit of 32 at once. A single audio source can play multiple clips (i.e. multiple voices).

    For example an Audio Source playing 10 clips all at once would mean 10 voices playing. 10 Audio Sources playing one audio clip each, would still be 10 voices.

    Play One Shot can increase the voice count because, for example, if you have a machine gun shot sound that starts before the previous shot ends then it counts as 2 voices. If the audio clips are long, or triggered close together, this can stack up and push you closer to the voice limit.

    I have other posts on my blog that cover some of these points in more detail too. A post on unity audio optimisation and a general audio tips post that includes a graphic about voice limits:

    Hope that helps,

    John.
     
  5. gamrDev

    gamrDev

    Joined:
    Jul 7, 2017
    Posts:
    12
    Ok thanks I will checkout those posts
     
  6. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    Any idea how this works with respect to spacialization?
    Specifically, does using playOneShot to a single AudioSource and playing many sounds count as 1 spacialization or 1 spacialization per voice being played?
    In general my understanding is that spacializing sounds is very resource intensive so if playOneshot would be more performant that would be great.

    Thanks in advance!
     
  7. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    140
    I can't speak for all of the different options, but I kind of know how it worked for Resonance Audio, which I realise is possibly deprecated now.

    I'm pretty sure that the spatialisation processing is done at an Audio Source level (i.e. other than adding to your voice count, multiple one-shots shouldn't affect it, so long as they use the same audio source).

    Resonance Audio appeared to be quite scalable too. There's an initial hit for using it but, after that, the performance hit for adding more Audio Sources seems to be small until you get to around 32-64 Audio Sources. I wrote an article about it which includes some basic tests: https://gamedevbeginner.com/vr-sound-design-in-unity-getting-started-guide/#spatial_performance

    I would imagine that other spatial plugins operate in a similar way but I don't know for sure.

    Hope that helps.

    John.
     
    leni8ec likes this.
  8. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    Thank you for the info.
     
  9. leni8ec

    leni8ec

    Joined:
    May 23, 2013
    Posts:
    24
    Hello John,
    First, thank you for your answers - very informative!

    Perhaps you are familiar with the performance comparison of "AudioSource.PlayOneShot()" vs "Pool of the Audio Sources"? Means for frequent reuse.

    Because I can't figure out how AudioSource.PlayOneShot() works behind the scenes and does it create any overhead (new components, variables, etc.) or other performance issues?

    For example: AudioSource.PlayClipAtPoint() - is a very heavy method where a new object is created, then a new component is created, and then they are removed.
     
  10. leni8ec

    leni8ec

    Joined:
    May 23, 2013
    Posts:
    24
    neonblitzer likes this.
  11. AunShiLord

    AunShiLord

    Joined:
    Jan 19, 2018
    Posts:
    2
    Just find out, that PlayOneShot is not using all volume rollOff features, which is sad... Didn't checked any other settings, but afraid that other fetures are cut off too.
    Is there any way take advantage of roll off curves with PlayOneShot, instead of calculating distance by myself.
     
    angeldevelopment likes this.
  12. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    It seems that PlayOneShot is more performant, and certainly convenient, but does PlayOneShot move the audio with the audio source?

    If I call play on a moving audiosource with 3D sound I can tell it is moving, but does play one shot do the same? Im gonna go test this now.
     
  13. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    What do you mean? Are you talking about the 3D rolloff? PlayOnShot supports this doesnt it? Ive never noticed my sounds not being 3d?