Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Audio is clipping and popping

Discussion in 'Audio & Video' started by NerdRageStudios, Mar 15, 2015.

  1. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Hi all, since moving to Unity5 I have an issue where my audio is popping and playing at different volumes. I have a simple tiny audio file for player footsteps and I call the public function at the correct frame in the animation.

    Here is my audio code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5.  
    6. public class script_player_sprite_audio : MonoBehaviour {
    7.  
    8.     public AudioClip[] audioFootsteps;
    9.     public AudioClip[] audioJump;
    10.  
    11.     public float volumeFoosteps;
    12.     public float volumeJump;
    13.  
    14.     AudioSource audio;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.         audio = GetComponent<AudioSource>();
    20.    
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.    
    26.     }
    27.  
    28.     void AudioPlayFoostep()
    29.     {
    30.         audio.PlayOneShot(audioFootsteps[Random.Range(0, audioFootsteps.Length)], volumeFoosteps);
    31.     }
    32.  
    33.     void AudioPlayJump()
    34.     {
    35.         audio.PlayOneShot(audioJump[Random.Range(0, audioJump.Length)], volumeJump);
    36.     }
    37. }
    I have no idea why this is causing popping noise? There can only really be a max of 3 sounds playing at any one time...

    Does anyone have any ideas why this might be happening?
     
  2. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    I am really stuck with this at the moment and I cant figure out why its happening, does anyone have any ideas please?
     
  3. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    119
    Unfortunately, I can't be sure about this since I've never used PlayOneShot (I stick to Play or PlayScheduled only, and change the AudioClip prior to playing), but I'll work with the assumption that PlayOneShot is not multitimbral. That would mean new footstep and jump sounds are interrupting the old ones, and sounds just being cut off like that easily results in clicks and pops. Have you considered instantiating a few more AudioSources? Make sure your pool is big enough so that a new sound doesn't have to interrupt anything, and rotate through which AudioSources you play sounds on.

    Edit: Actually, since you say the sounds are playing at different volumes, I don't really know. Could be a Unity bug with PlayOneShot?
     
    Last edited: Mar 16, 2015
  4. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    PlayOneShot has been an anger management test for many users over the years.
    Pool sources and use Play, you'll be much happier.
     
  5. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Thank you for taking the time to reply :)

    So, I will try recoding to use play only and see how that works... the weird thing is, I have been using this method in my games with no issues, in unity4 it worked fine, so I guess this must be an issue with 5.

    for example, I had machine guns firing, all using the same sound using the method above, many times per second and never had an issue...
     
  6. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Ah, sorry reading above you are talking about pooling audio sources? Sorry, but could you explain that a bit more please? do you have multiple audio sources attached to an object and use them for separate sounds?
     
  7. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    119
    Not quite. You do need a bunch of AudioSources, but each is not dedicated for a specific sound. Instead, let's say you have 4 AudioSources. This will allow four sounds to play simultaneously. When you first play a sound, you assign the clip to the first AudioSource and call Play on it. When you next play a sound, assign it to the second AudioSource and call Play. Once you've reached the fourth source, you play the next sound on the first source. This allows new sounds to begin to play when old ones are still playing.

    When you're comfortable with working with this, there are even better ways to do select which Audio Source to use to try minimizing how much old ones will be interrupted, but this should be a good start.
     
    Mifasoft and FlightOfOne like this.