- Home /
How to get current volume of an AudioSource.
Hi,
I have set a custom volume rolloff on the 3D settings of an AudioSource. But now I need to know what volume the AudioListener (ie. the camera) is hearing from this source.
Is there a way to tell an AudioSource "give me the volume I will hear from you if I am at 30 units of distance from you" ?
Thanks.
Answer by aldonaletto · Feb 28, 2012 at 12:56 PM
You could use AudioListener.GetOutputData (as @Vladimir 3 said) to get samples of what the AudioListener is hearing, then calculate the RMS value. Call the function GetRMS below for channels 0 and 1, then sum both results to get a more significant value.
There's a problem, however: the audio listener hears all sources, thus make sure that only the desired audio source is playing when you take the measurement.
var qSamples: int = 4096; // array size (corresponds to about 85mS)
private var samples: float[]; // audio samples
function Start () { samples = new float[qSamples]; }
function GetRMS(channel: int): float { AudioListener.GetOutputData(samples, channel); // fill array with samples var sum: float = 0; for (var i=0; i < qSamples; i++){ sum += samples[i]*samples[i]; // sum squared samples } return Mathf.Sqrt(sum/qSamples); // rms = square root of average }
// when you want to measure the volume, call GetRMS for channels 0 and 1, and sum them:
var vol: float = GetRMS(0); // get left channel...
vol += GetRMS(1); // and add to the right channel
Thanks for that solution. However I did the exact same work before realize that I need only one source... not every sources :/ This problem seem unsolvable. Thanks again.
Yes, this is a big problem: unfortunately, Unity doesn't give us access to the rolloff curve via script - if we had access to it, the attenuation value could be evaluated with AnimationCurve.Evaluate().
A possible work around could be to create an AnimationCurve imitating the rolloff curve, and use it to find the attenuation as a function of distance (audio listener script):
var rollOff: AnimationCurve; // define a similar curve here in the Inspector
function GetAttenuation(audioSourcePos: Vector3): float { var dist = Vector3.Distance(transform.position, audioSourcePos); return rollOff.Evaluate(dist); } This will return the attenuated volume as a function of distance (1.0 = 100%). If you need the current audio intensity, calculate the rms value with GetR$$anonymous$$S (use audio.GetOutputData ins$$anonymous$$d of AudioListener.GetOutputData) in the audio source and multiply by the attenuated volume.
Answer by wlad_s · Feb 28, 2012 at 10:36 AM
Hi,
I don't know if such a thing exists but if it were a logarithmic or linear Rolloff, you could easily write a simple logarithmic or linear function yourself in order to get the value. Unfortunately, since you made your custom rolloff graph, it won't work.
Also, I don't know what this "AudioListener.GetOutputData " does but maybe you can get some info on it and see if it can help.
However I use custom Rolloff :( that's why I was looking for a gentle solution... Thank you anyway.
Answer by TruffelsAndOranges · Jul 11, 2015 at 10:09 PM
I have this problem to this day. Did anybody find out a new way to get that current volume?
Using @aldonaletto 's example, you can replace the AudioListener with an AudioSource. That will give you the output data for a specific source, unaffected by rolloff/distance.
//C#
public AudioSource auSrc; //Assign in Editor or through Code
private float volume;
public int qSamples = 4096;
private float[] samples;
// Use this for initialization
void Start () {
samples = new float[qSamples];
}
// Update is called once per frame
void Update () {
volume = GetR$$anonymous$$S(0) + GetR$$anonymous$$S(1);
}
float GetR$$anonymous$$S(int channel){
//Replaced the AudioListener with the public AudioSource auSrc from above
auSrc.GetOutputData(samples, channel);
float sum = 0;
foreach(float f in samples){
sum += f*f;
}
return $$anonymous$$athf.Sqrt(sum/qSamples);
}
Hope this helps
Your answer
Follow this Question
Related Questions
webgl volume just doesnt work 0 Answers
Breathing volume increases as player gets more tired 0 Answers
How to duck a single audio channel? 0 Answers
Can 2 iTween components run on the same gameobject simultaneously? 2 Answers
Is there a way to limit the output volume on an audio mixer group? 1 Answer