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.

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_*samples*; // 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*_
_*
*_

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.

I have this problem to this day. Did anybody find out a new way to get that current volume?