Search Unity

Audio Music volume automaticlly changes

Discussion in 'Audio & Video' started by Itay_O, Nov 3, 2019.

  1. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    Hello everyone!

    I'm working on replacing all audio (music and sfx) in the unity project "The Explorer 3D".
    I'm not using any audio middleware.

    For some reason, the music keeps fading in and out when the player reaches certain points in each level.
    I'm not much of a programmer so I couldn't really figure out why it happens from the script, and I didn't find any mentions about it at the project's documentation. All I can see is the volume of the music changes when the player reaches certain areas, and I would like to get rid of it because I will compose music of my own for the game.

    Any suggestions?
    Thanks in advance!
     
  2. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    Anyone??
     
  3. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    really? nobody knows?
     
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    yep nobody knows - there are literally zillions of reasons
    you might hit a trigger which transitions to mixer snapshot, or enables 3d on the audio source, or the audio source is 3d and its rolloff curve is so sudden that it looks like it changes volume immediately, or any other computationally plausible change based maybe on player location invoked from an user script which is possible to tell only with crystal ball
    (and crystal ball is not reliable as everybody knows)
     
  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Never touched the game kit, and won't be downloading and importing it just to find the answer.

    You SEE it? Where? In the AudioMixer? Or on the Audiosource directly?

    Anyway, unity likes to use what they've built, so they are probably using snapshot transitions for the audio based on triggers for different areas. You can do this kind of thing without snapshots, or even without an audiomixer, but I'm guessing that they just used snapshots.

    Those will be defined on the Audiomixer asset being used. As far as script goes, you do need to call snapshot transitions in code somewhere based on something happening. So you'll probably want to find those and comment them out. And you might want to make your own snapshots (and setup your own triggers) or just delete the snapshots entirely. Depends what you want to do with the audio.

    There are official tutorials about the audiomixer and snapshots, and how to set them up in the editor as well as how you work with them in code. I suggest checking those out, so that you can better understand what you are looking for, as well as what kinds of things you can do for your own audio implementation.
     
    Last edited: Nov 13, 2019
  6. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10





    Ok, my bad, here are some more details:

    I See the change in the audiosource directly.
    As far as I can tell, there's only one snapshot. On level 2 there is a transition to "battle music" towards the end which is great and I intend to keep.

    upload_2019-11-15_14-59-20.png

    The audiosource is 2D, not 3D as you can see here:

    upload_2019-11-15_15-4-30.png


    Do you see the volume level? because now, in a different area in the same level, the volume level transitioned to this:

    upload_2019-11-15_15-5-41.png


    This is the level 1 soundtrack prefab:
    upload_2019-11-15_15-7-7.png

    Only the first one is the music, the other ones are ambience so ignore those.
    There is a code, which im going to paste here. I'm a noob at coding, so I apologize if there's something obvious in there that affects that problem:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Gamekit3D
    6. {
    7.     public class SoundTrack : MonoBehaviour
    8.     {
    9.         public float soundTrackVolume = 1;
    10.         public float initialVolume = 1;
    11.         public float volumeRampSpeed = 4;
    12.         public bool playOnStart = true;
    13.         public AudioSource[] audioSources;
    14.  
    15.         AudioSource activeAudio, fadeAudio;
    16.         float volumeVelocity, fadeVelocity;
    17.         float volume;
    18.         Stack<string> trackStack = new Stack<string>();
    19.  
    20.         public void PushTrack(string name)
    21.         {
    22.             trackStack.Push(name);
    23.             Enqueue(name);
    24.         }
    25.  
    26.         public void PopTrack()
    27.         {
    28.             if (trackStack.Count > 1)
    29.                 trackStack.Pop();
    30.             Enqueue(trackStack.Peek());
    31.         }
    32.  
    33.         public void Enqueue(string name)
    34.         {
    35.             foreach (var i in audioSources)
    36.             {
    37.                 if (i.name == name)
    38.                 {
    39.                     fadeAudio = activeAudio;
    40.                     activeAudio = i;
    41.                     if (!activeAudio.isPlaying) activeAudio.Play();
    42.                     break;
    43.                 }
    44.             }
    45.         }
    46.  
    47.         public void Play()
    48.         {
    49.             if (activeAudio != null)
    50.                 activeAudio.Play();
    51.         }
    52.  
    53.         public void Stop()
    54.         {
    55.             foreach (var i in audioSources) i.Stop();
    56.         }
    57.  
    58.         void OnEnable()
    59.         {
    60.             trackStack.Clear();
    61.             if (audioSources.Length > 0)
    62.             {
    63.                 activeAudio = audioSources[0];
    64.                 foreach (var i in audioSources) i.volume = 0;
    65.                 trackStack.Push(audioSources[0].name);
    66.                 if (playOnStart) Play();
    67.             }
    68.             volume = initialVolume;
    69.         }
    70.  
    71.         void Reset()
    72.         {
    73.             audioSources = GetComponentsInChildren<AudioSource>();
    74.         }
    75.  
    76.         public void SetVolume(float volume)
    77.         {
    78.             this.volume = volume;
    79.         }
    80.  
    81.         void Update()
    82.         {
    83.             if (activeAudio != null)
    84.                 activeAudio.volume = Mathf.SmoothDamp(activeAudio.volume, volume * soundTrackVolume, ref volumeVelocity, volumeRampSpeed, 1);
    85.  
    86.             if (fadeAudio != null)
    87.             {
    88.                 fadeAudio.volume = Mathf.SmoothDamp(fadeAudio.volume, 0, ref fadeVelocity, volumeRampSpeed, 1);
    89.                 if (Mathf.Approximately(fadeAudio.volume, 0))
    90.                 {
    91.                     fadeAudio.Stop();
    92.                     fadeAudio = null;
    93.                 }
    94.             }
    95.         }
    96.     }
    97. }
    98.  
    I did see something in there about fade, but I coulde'nt understand anything beyond.
    I hope this helps to further understand the issue.
     
  7. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    This is generic beginner programming question, not an audio question -
    see how activeAudio.volume is dampened at line 84 in your post ?
    It depends on `volume` and `soundTrackVolume` - so see how/where those are changed - in other words look who's calling `SetVolume` method and/or modifying `soundTrackVolume` since that is public
    (I won't download and test
    , sorry)
     
    Last edited: Nov 15, 2019
  8. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    No worries my friend, I didnt expect you to download the project and dig inside to further help me.
    Thanks for the info! I will try to see if I can find what calls this part of the code and change it. Or, I wondered if it's ok or a really bad idea, to just delete this whole part?
     
  9. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    it's not clear whatsoever the project uses snapshots
    why do you have spamlink in your cited reply ?
    (or these bots are getting too advanced)
     
  10. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    Yea that link struck me as odd, though I don't think it's a bot.
    There's only one snapshot in the project, from what I've seen. So far I didn't find anything that could trigger a change in music volume. In the second level, there is clearly an object which triggers battle music once your'e in it's range, but I didn't find anything similar in level 1. I'll keep searching for it.
     
  11. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    You have already found and posted the code that does the fading on the Audiosources.

    It has an array of audiosources.
    Code (CSharp):
    1. public AudioSource[] audioSources;
    Then it also has these two temporary audiosources. It needs two, because it needs to transition between two audio files.
    Code (CSharp):
    1. AudioSource activeAudio, fadeAudio;
    This function takes a string name. It compares the name passed in to it, to the name property of each audiosource in the array. When it finds the matching audiosource, it relegates the currently active source to being a fading source. Then it assigns the found audiosource as the new active source. It then starts playing the active source.

    Code (CSharp):
    1.         public void Enqueue(string name)
    2.         {
    3.             foreach (var i in audioSources)
    4.             {
    5.                 if (i.name == name)
    6.                 {
    7.                     fadeAudio = activeAudio;
    8.                     activeAudio = i;
    9.                     if (!activeAudio.isPlaying) activeAudio.Play();
    10.                     break;
    11.                 }
    12.             }
    13.         }
    In the update, it fades both of these temporary audiosources using Mathf.SmoothDamp.

    Mathf.SoothDamp :

    Fade in the active audiosource volume
    Code (CSharp):
    1. activeAudio.volume = Mathf.SmoothDamp(activeAudio.volume, volume * soundTrackVolume, ref volumeVelocity, volumeRampSpeed, 1);
    Fade out the fade audiosource (previously active source)
    Code (CSharp):
    1. fadeAudio.volume = Mathf.SmoothDamp(fadeAudio.volume, 0, ref fadeVelocity, volumeRampSpeed, 1);
    If fade audiosource has finished fading to zero volume, stop fade audiosource from playing its audioclip. Set it to null.
    Code (CSharp):
    1.                 if (Mathf.Approximately(fadeAudio.volume, 0))
    2.                 {
    3.                     fadeAudio.Stop();
    4.                     fadeAudio = null;
    5.                 }
     
  12. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    I actually meant that i'm yet to find if there's an object that triggers this.
    thanks anyway!
     
  13. Itay_O

    Itay_O

    Joined:
    May 15, 2019
    Posts:
    10
    I got it!

    Apparently the ambience soundtracks are causing this - their objects include a sphere collider that functions as a trigger and are linked to the script I posted. When I step into their radius, the ambience sound that is attached to it fades in and the music fades out!
    I think i'll just delete the sphere collider and set the sound to 3D so it won't affect the music. Unless there's a better idea?

    Thanks everyone for your replies.