Search Unity

Attenuating Stereo Audio

Discussion in 'Scripting' started by xemplifly, Apr 29, 2008.

  1. xemplifly


    Nov 23, 2007
    I am implementing several triggers for stereo audio in my map using streaming ogg files. When I trigger one and then trigger a second, i want to fade down or cut the first and play the second. Is this possible or do i have to use Mono Audio files? Right now my stereo files play on top of one another even though i set

    Code (csharp):
    2. if(audio.isPlaying){
    3. audio.Stop();
    4. }
    Thanks for the help!
  2. xemplifly


    Nov 23, 2007
    With the help of some guys from a unity3d channel on mirc, i was able to get the audio to stop before playing a new one.

    I was using an invisible box trigger with code attached that would pull in stereo audio from a remote server to be used as narration.*pheww* They told me to place all my instanced triggers into a container empty game object. At that point i was able to rework the code to tell all my instances to kill the audio before playing a new one. When i get back to work tomorrow i'll share the line of code.

    p.s. - i'm still wanting to attenuate the audio and not stop it... ideas?

    Augustine Web Design
  3. Adrian


    Apr 5, 2008
    You cannot attenuate stereo audio. It would be technically possible to select stereo audio as 3d sound and attenuate it - but only by merging it to mono.

    Stereo sound is only a trick used to give a recording more depth. A single channel audio lacks positioning information that can be simulated by using multiple channels.

    Usually, audio sources are mono anyways. A nock, a voice or a musical instrument only have a single mono source. Using multiple channels only makes sense to simulate the positioning of such sources in 3d space to the listener.

    If your using a 3d application and position the sources and listener in 3d space, then making those source stereo makes no sense. The stereo channels would conflict with the 3d positioning and attenuation of the audio sources because you'd have conflicting positional information from the stereo source and the 3d positioning.

    Using mono audio for 3d audio sources is not a limitation but rather the only way to go. If you want to use stereo tracks as a 3d source, you have to convert them to mono first (Import Settings > Channels > Force mono...).

    You could also manually fade the audio basing on the distance of the listener to the source. But in that case you wouldn't really have 3d audio since the rotation of the listener would have no effect on the output.
  4. Jessy


    Jun 7, 2007
    Yes, you can. At least, you can in the way you can in the real world...

    If you don't want attenuation in 3D space for a stereo sound, it's really easy (volume control). If you do, then just split your stereo file into two mono files. Now, it is true that in most cases, you will want mono, but let's say you have a boombox in the 3D world. Put the appropriate half of the audio where the appropriate speaker is.

    Here are some scripts I contributed that may help you out. In particular, my Smooth Audio Fade (or something like it) should be used instead of stopping a sound. A sound should never be "switched off", because the waveform will have to snap to zero, creating a terrible clicking sound.