Search Unity

How do I keep audio active in a listener after muting it?

Discussion in 'Audio & Video' started by simpson45711, Dec 10, 2014.

  1. simpson45711

    simpson45711

    Joined:
    Mar 16, 2013
    Posts:
    18
    I want to know if the following is possible:
    I'm trying to create a mute button for my project which mutes the audio to a user only, but keeps it playing in the scene.

    Reason being, I have an audio visualizer/listener plugin in my scene which makes changes to the level based on background music(2d audio clip) playing attached to an audio listener.

    I want the user to be able to mute the audio, if the chose to, just encase they are listening to their own music....etc.

    I have tried muting the audio listener, and dropping the volume low as usual, but this stops the visualizer form making changes to the level.

    Just want to know if this is possible or I should give up on the idea.
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi,

    Your visualise plugin is most probably a component attached to the Listener GameObject. You could create your own component which zeroes data after that:

    Code (CSharp):
    1. public class Muter : MonoBehaviour
    2. {
    3.      public bool mute;
    4.  
    5.      void OnAudioFilterRead( floa[] data, int channels )
    6.      {
    7.           if( mute )
    8.           {
    9.                System.Array.Clear( data, 0, data.Length );
    10.           }
    11.      }
    12. }
    You'll also want to fade in / out when you mute / unmute to avoid nasty pops. Just multiply the audio data by a linear gain function, making sure you respect interleaving:

    Code (CSharp):
    1. // Example implementation as a static method
    2. public static void FadeAudioBuffer( data[] buffer, int channels, float fromGain, float toGain )
    3. {
    4.      float gainDelta = toGain - fromGain;
    5.      float increment = gainDelta / ( data.Length / channels );
    6.      float appliedGain;
    7.  
    8.      for( int channel = 0; channel < channels; channel++ )
    9.      {
    10.           appliedGain = fromGain;
    11.           for( int sample = channel; sample < data.Length; sample += channels )
    12.           {
    13.                data[ sample ] *= appliedGain;
    14.                appliedGain += increment;
    15.           }
    16.      }
    17. }
    Just call FadeAudioBuffer( data, channels, 0f, 1f ) to fade in, and FadeAudioBuffer( data, channels, 1f, 0f ) to fade out.

    Cheers,

    Gregzo
     
    Freaking-Pingo likes this.
  3. simpson45711

    simpson45711

    Joined:
    Mar 16, 2013
    Posts:
    18
    Thanks, i'll try my luck at this once I get back home and see what happens.
    Also, in ios/xcode, Is there a way to allow the native music player's audio override the app's(in this case unity's) audio source?
    I've seen some ios apps use this (songs playing in music player block out app audio), not sure how this is done exactly.