Search Unity

Audio Play Same Audio Clip From Two Audio Sources (speakers)?

Discussion in 'Audio & Video' started by devotid, Apr 10, 2019.

  1. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    I am building a carnival with rides and all of them have a few speakers on the rides platforms. I have tried setting up the audio sources to make it seem like it was coming out of each speaker but with only one audio source it just doesnt work or sound very good from a single central location.

    What is the best way to get this sounding like its coming from multiple sources? I want the player to go around in a circle and hear the music get louder and fade and get louder again as they are going in a circle.

    Should I use one playing source and just move it to each location through code as the player moves around? I could find nothing on this and I have been using Unity for almost a decade... lol.

    Thanks for any advice.
     
  2. Bosozuki

    Bosozuki

    Joined:
    Dec 19, 2013
    Posts:
    63
    I am by no means an audio expert but this is the solution I used.

    Example:
    Two gameobjects in the scene
    Each have an audiosource
    Both of these audiosources point to the same clip

    Quick and dirty solution
    You have a script on both of the game objects that has the following
    Code (CSharp):
    1.  
    2.  
    3. public AudioClip Sound_Effect_Clip_Loop;
    4. public AudioSource Sound_Effect_Source_Loop;
    5.  
    6.     void Update()
    7.     {
    8.         if (Sound_Effect_Source_Loop.isPlaying == false)
    9.         {
    10.             Sound_Effect_Source_Loop.PlayOneShot(Sound_Effect_Clip_Loop);
    11.         }
    12. }
    13.  
    14.  
    The above code will check to see if the audio source on that particular game object is playing, if not then an instance of the audioclip will be created and player. Once the clip is over the process will repeat.

    Creating the instance with playoneshot keeps the audioclips from interfering with one another.

    The problem with this code is if you need to the two clips to be synced exactly

    If they need to be synced you may just want to use one script on one object that will reference the other script to grab the audiosource on that object.

    Then the above code would have a playoneshot for each audiosource. Even this may not get a perfect synced so you may have to add a timedelay to the playoneshot.

    Another way that I have experimented with is to use separate audio mixers/ snapshots to monitor audio playing and mix the audio sources appropriately. I used this technique for dynamically mixing multiple music, combat music and ambient tracks together so the beats line up.

    Hopefully this will get you started unless you already figured out a solution :)
     
    devotid likes this.
  3. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    I do thank you for the tip....but I ended up just making a simple controller that would get the distance to main player and then parent the audio source to the closest speaker. It checks position ever .5 seconds and then just moves the speaker to the closest "speaker gameobject" by setting the parent and reseting the local position to 0,0,0.

    This actually worked perfectly and plays the same audio clip and uses the same Game Object as you move around the map.

    Kevin