Search Unity

Accurate, overlapping sound

Discussion in 'Scripting' started by fabian-mkv, Dec 1, 2015.

  1. fabian-mkv

    fabian-mkv

    Joined:
    Nov 14, 2015
    Posts:
    13
    In short
    How to play the same audio multiple times (overlapping) using an AudioSource where I need accurate timing the way PlayScheduled() provides?

    In full
    I recently began making a rhythm game where sounds such as drum beats are played in certain patterns.
    At first I used the Update() function to see whether enough time has elapsed to play the next drum beat sound, but the Update() function has poor timing accuracy/consistency - it's generally good enough for graphics and simple sound effects, but noticeably inaccurate for rhythmic patterns.
    With PlayScheduled() I can control exactly when an AudioSource should play using the DSP-level, double precision AudioSettings.dspTime function.
    The problem is that PlayScheduled() can't play different or overlapping audio the way PlayOneShot() does, and PlayOneShot() doesn't have the DSP-level accuracy of the PlayScheduled() function.
    How can I achieve the timing accuracy of PlayScheduled() and the versatility (in terms of multiple and overlapping audio) of PlayOneShot()?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I'm just reading the docs here and have no experience with this, but nothing tells me you are limited to using one AudioSource. So using several seems to be what you need. If memory serves PlayOneShot spawns a new instance of a gameobject with an audiosource component on it playing the clip, and removes it after.
     
  3. fabian-mkv

    fabian-mkv

    Joined:
    Nov 14, 2015
    Posts:
    13
    I have ~10 different samples ranging up to ~1sec, and beats may be played consecutively as frequently as 8 times per second. So your solution is for me to create and manage 10 x 8 = 80 AudioSources on my GameController GameObject (all playback is done on this object) to ensure that no sound will cut off another sound? Can unity (or the devices that play unity) always handle this number of AudioSources assuming that only up to ~20 sounds will be playing at once?

    (I'll give it a go and post my findings.)
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    There is a limit to simultaneously playing sounds, couldn't find much information. I suppose it relies on your sound hardware.
    Not sure if there is any software mixing going on, but otherwise you may be limited to the amount of hardware sound channels supported. Sorces that are not playing should not affect playback. You can assign a priority to sounds to make sure the most important ones dont get interrupted.