|
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.
(comments are locked)
|
|
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.
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.
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().
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)
|
|
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.
Feb 28 '12 at 01:30 PM
lvictorino
(comments are locked)
|
