x


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.

more ▼

asked Feb 27 '12 at 03:18 PM

lvictorino gravatar image

lvictorino
654 7 17 19

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Feb 28 '12 at 12:56 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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.

Feb 28 '12 at 01:32 PM lvictorino

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 GetRMS (use audio.GetOutputData instead of AudioListener.GetOutputData) in the audio source and multiply by the attenuated volume.

Feb 28 '12 at 06:58 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 28 '12 at 10:36 AM

uberwleiss gravatar image

uberwleiss
198 10 13 22

However I use custom Rolloff :( that's why I was looking for a gentle solution... Thank you anyway.

Feb 28 '12 at 01:30 PM lvictorino
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1029
x171
x74
x11

asked: Feb 27 '12 at 03:18 PM

Seen: 1024 times

Last Updated: Feb 28 '12 at 06:58 PM