Search Unity

Non-Streamed AudioClip Cuts Off When the Camera Faces a Certain Way

Discussion in 'Editor & General Support' started by SomeGuy22, Jan 10, 2020.

  1. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I've encountered an odd issue playing Audio both in the Editor and on Build where the AudioSource just seems to cut off or play a glitchy version of the sound. I had more info in this thread originally but now I'm starting to think this is more severe bug that relates to imported audio clips.

    The Problem

    I'm spawning an object that has an AudioSource as a child of it, and I'm calling AudioSource.Play() after setting the AudioClip to one of my assets. Here is the relevant code:

    Code (CSharp):
    1. if(effects.sliceEffectSource)
    2.         {
    3.          
    4.             if(effects.sliceEffectSource.isPlaying)
    5.             {
    6.                 effects.sliceEffectSource.Stop();
    7.             }
    8.          
    9.             effects.sliceEffectSource.clip = effects.startSliceClip;
    10.             effects.sliceEffectSource.Play();
    11.  
    12.         }
    I've ensured that this is the only code that actually effects the AudioSource. I do have another script which pauses it when the game is paused, but the issue persists even with that script removed. The object itself is spawned with Instantiate and then afterwards the function which has the above code is run. I figure it's pretty standard so I don't think the code itself is breaking anything, especially since it behaves as expected in certain conditions. Here's the settings for the component:



    The problem is that the sound plays perfectly fine as expected--only when the Camera (which has the AudioListener) is facing a certain way or close to a certain part of the scene. Otherwise, the audio appears to get cut short or clipped or something. The clip is either being manipulated for some reason with a different pitch or it gets corrupted. It seems to be related to the origin of the scene because it's happened across multiple scenes with varying amounts of objects. The problem starts happening when I move far away from the origin, though it's really hard to say because it's somewhat random. It doesn't happen all the time, but it happens fairly consistently when under the conditions I described. I've uploaded a video to show what I'm dealing with:



    Notice how up until 8 seconds the sounds play as expected--that's pretty much how they sound in the Inspector when previewing the asset. But when I teleport far away the sounds start getting corrupted when looking towards the tower. When I look away it appears to stop having that issue.

    What I've Investigated

    • I've checked the settings of the spawned object at runtime to ensure everything is the same as the prefab, and it is.
    • I've tried changing the clip that it sets it to, and the issue still happens with other clips.
    • I've tried adjusting every setting in the AudioSource prefab that I spawn--priority, pitch, spread, reverb zone mix, doppler. None of it helps the issue.
    • I'm not getting any warnings about approaching the voice limit, nor should I since it is practically the only sound playing.
    • I've debug.Logged if the AudioSource is virtual at the time of playing, and determined that it is indeed false. The audio is not being culled.
    • I've tried resetting the AudioSource.time to 0 in case the clip is being skipped for some reason, but that didn't help.
    • I've tried using a Coroutine to wait for .1 seconds before playing in case the spawn lag is causing it to stumble, but that didn't help.
    • I've tried stopping and playing multiple times in succession but it still had the issue.
    • I've checked the Audio Mixer and verified that the target channel is receiving the sound the same way I hear it--when it gets cut off the levels are shorter.
    • I've tried adjusting the DSP buffer but it made no difference.
    The only other setting which seems to make it stop is if I set it to a 2D source. When I do that, I hear it play the full sound. But the 3D part is still getting cut off if I have a mix between 2D and 3D. But obviously that's not going to work for me since I need it to be in 3D.

    Just recently I did some trial and error with the audio file itself. These were the settings I had it on:


    • Not preloading the data did nothing to help
    • Using the player settings to preload the clip asset did nothing either
    • Neither did changing the compression format to ADPCM
    • The sample rate setting also did nothing, optimizing it or manually setting it to 48k does nothing.
    However, when I changed the load type to "Streaming" something changed. I caught onto it due to this question which was asked. When I set it to streaming, I still hear the click/glitch occasionally but it's very faint and only for a brief moment before the rest of the audio plays correctly. It's almost the same as if I set the audio source to half 2D and half 3D--I hear both the correctly and incorrectly played sounds.

    Again, what's odd is that all of this seems to stem from the camera being oriented a certain way. It happens on multiple scenes that I've tested in and only on this specific instantiated object for some reason. I could do more tests in a blank project to narrow down the limits but I really want to avoid wasting even more time on this. My best guess is that it's just some quirk of the audio system in play here, and something about having it in memory instead of being streamed is causing it to fail the start of playing. At first I thought the camera orientation was merely causing lag due to looking at many objects in the center of the screen, but after my tests and realizing it happens on nearly blank scenes I really don't think that's what's causing it.

    I'd really like to avoid Streaming the clip instead of using the other loadtypes, but aside from changing the format I can't think of any other way to solve this issue. I don't even understand why it's happening in the first place so I'd appreciate any ideas or clues as to what's happening here. Thanks in advance.
     
  2. suIly

    suIly

    Joined:
    Jun 21, 2017
    Posts:
    16
    hmmm