SmoothAudioFade

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
(JavaScript - Fade.js)
Line 4: Line 4:
  
  
== JavaScript - Fade.js ==
 
<javascript>
 
// If you don't want to have the clip be audible to start off with,
 
// set the Audio Source "Volume" to zero, and disable the following "Play" option.
 
  
var play : boolean = true;  // if the attached Audio Source is playing
 
 
var loudness : float = 1;  // Use this to manually control the volume instead of using the default Audio Source variable.
 
// If you set "play" to false initially, set the Audio Source volume to zero.
 
// Otherwise, set this and the Audio Source volume setting to the same value, initially.
 
 
var fadeTime : float = .2;  // time (in seconds) in which the audio fades out
 
 
private var playing : boolean; // if the audio is playing
 
 
// to prevent fades from occurring right away
 
private var fadeReady : boolean;
 
private var early : boolean;
 
private var loudnessNow : float; // used for proper volume ramping if switching fade direction before fade completion
 
private var switchTime : float; // the time when you last switched the sound on or off
 
 
private var dB : float; // a value created by an exponential curve, used to scale
 
// the volume in a manner that sounds natural to humans
 
 
private var fadeScale : float; // a value created by an a further exponential curve;
 
// makes "dB" into a percentage to multiply the volume by for smooth fades
 
 
function Start () {
 
playing = play;
 
fadeReady = false;
 
}
 
 
function FixedUpdate () {
 
early = ( (Time.time < fadeTime) && (fadeReady == false) );
 
fadeScale = Mathf.Pow(10, fadeTime) - 1;
 
 
if (play) {
 
if (playing == false) {
 
playing = true;
 
switchTime = Time.time;
 
loudnessNow = audio.volume; // grabs the volume when switching the sound on
 
// works whether the sound is currently fading or not
 
 
if (fadeReady == false) {
 
fadeReady = true;
 
}
 
}
 
 
var fadedIn : boolean = Time.time >= (switchTime + fadeTime);
 
 
if (fadedIn || early) {
 
audio.volume = loudness;
 
}
 
 
else {
 
dB = Mathf.Pow(10, (Time.time - switchTime)) - 1;
 
audio.volume = (dB / fadeScale) * (loudness - loudnessNow) + loudnessNow;
 
}
 
 
}
 
 
else {
 
if (playing) {
 
playing = false;
 
switchTime = Time.time;
 
loudnessNow = audio.volume; // grabs the volume when switching the sound off
 
// works whether the sound is currently fading or not
 
if (fadeReady == false) {
 
fadeReady = true;
 
}
 
}
 
 
var fadedOut : boolean = ( (Time.time - switchTime - fadeTime) >=0 );
 
 
if (fadedOut || early) {
 
audio.volume = 0;
 
}
 
 
else {
 
dB = Mathf.Pow(10, Mathf.Abs(Time.time - switchTime - fadeTime)) - 1;
 
audio.volume = (dB / fadeScale) * loudnessNow;
 
}
 
 
}
 
}
 
</javascript>
 
  
 
== C# - Fade.cs ==
 
== C# - Fade.cs ==

Revision as of 18:47, 12 September 2008

Author: Jessy



C# - Fade.cs

<csharp> // If you don't want to have the clip be audible to start off with, // set the Audio Source "Volume" to zero, and disable the following "Play" option.

using UnityEngine; using System.Collections;

public class Fade : MonoBehaviour { public bool play; // if the attached Audio Source is playing

public float loudness; // Use this to manually control the volume instead of using the default Audio Source variable. // If you set "play" to false initially, set the Audio Source volume to zero. // Otherwise, set this and the Audio Source volume setting to the same value, initially.

public float fadeTime; // time (in seconds) in which the audio fades out

private bool playing; // if the audio is playing

// to prevent fades from occurring right away private bool fadeReady; private bool early;

// track if the fades have been completed private bool fadedIn; private bool fadedOut;

private float loudnessNow; // used for proper volume ramping if switching fade direction before fade completion private float switchTime; // the time when you last switched the sound on or off

private float dB; // a value created by an exponential curve, used to scale // the volume in a manner that sounds natural to humans

private float fadeScale; // a value created by an a further exponential curve; // makes "dB" into a percentage to multiply the volume by for smooth fades

void Start () { playing = play; fadeReady = false; }

void FixedUpdate () { early = ( (Time.time < fadeTime) && (fadeReady == false) ); fadeScale = Mathf.Pow(10, fadeTime) - 1;

if (play) { if (playing == false) { playing = true; switchTime = Time.time; loudnessNow = audio.volume; // grabs the volume when switching the sound on // works whether the sound is currently fading or not

if (fadeReady == false) { fadeReady = true; } }

fadedIn = Time.time >= (switchTime + fadeTime);

if (fadedIn || early) { audio.volume = loudness; }

else { dB = Mathf.Pow(10, (Time.time - switchTime)) - 1; audio.volume = (dB / fadeScale) * (loudness - loudnessNow) + loudnessNow; }

}

else { if (playing) { playing = false; switchTime = Time.time; loudnessNow = audio.volume; // grabs the volume when switching the sound on // works whether the sound is currently fading or not if (fadeReady == false) { fadeReady = true; } }

fadedOut = ( (Time.time - switchTime - fadeTime) >=0 );

if (fadedOut || early) { audio.volume = 0; }

else { dB = Mathf.Pow(10, Mathf.Abs(Time.time - switchTime - fadeTime)) - 1; audio.volume = (dB / fadeScale) * loudnessNow; } } } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox