- Home /
onFilterAudioRead Problems
Hi Everyone I beg for help on this problem I'm having using onAudioFilterRead()
I know how digital audio works and also what onAudioFilterRead does, but I can't debug my self on this one. I'm trying to build a ramp to smooth out amplitude variations between each call of the method. This is because I'm using the mouse as a pointer with an AudioListener component. When the mouse gets near the object (in 2D, by the way), I want to hear its audiosource louder. Of course if it was an audioclip that wouldn't be much of a problem. But since I'm using onAudioFilterRead to procedurally create oscilators, I can't access to the audiosource features —or if there is a workaround to do this, let me know—. If I don't smooth out this variations I'm getting annoying clicks and noises due to precipitation in amplitud variation. So long this is what I've been trying with no success, it either sounds awful or audio crashes. I have tried to print the values to see what I'm getting and I've spotted some issues but no fixes. Could anyone take a look to the code and see what could be the problem?
Here is the code with comments:
void OnAudioFilterRead(float[] data, int channels)
{
//
//HERE ARE SOME OSCILATORS THAT ARE NOT WORTH MENTIONING. THEY WORK GREAT, BY THE WAY
//Before I declared some global variables, I also left them out of here
//THIS IS THE BEGINING OF MY RAMP
//obtains the target amplitude value to ramp on
NewProximityGain = AudioUpdate();
//checks if there is not an amplitud variation (in other words, if mouse pointer hasn't moved)
if(PreviousProximityGain != NewProximityGain)
{
float InterpolatedStep;
for (var i = 0; i < data.Length; i += 2)
{
//obtains an interpolated value between the last amplitud and the new target amplitud
InterpolatedStep = Mathf.Lerp(PreviousProximityGain, NewProximityGain, (float)(i + 1 / data.Length));
//multiplies the sample from the array by the interpolated value
data[i] = data[i] * InterpolatedStep;
//copies the sample value for the interleaved second channel
data[i + 1] = data[i];
}
//Updates the previous value variable to retain the target value from
//this call and use it as previous value in the next call
PreviousProximityGain = NewProximityGain;
}
}
private float AudioUpdate()
{
// Returns a float corresponding to the current amplitude of the oscilators
// determined by the proximity of the mouse pointer to the audio Source
Vector2 PointerPositionInWorld = PointerListener.worldposition;
float DistanceFromAudioSource = Vector2.Distance(PointerPositionInWorld, AudioSourcePosition);
float clampedDistance = Mathf.Clamp(DistanceFromAudioSource, 0, 4);
float mapedDistance = Mathf.Lerp(0.28f, 0.008f, clampedDistance / 4);
return mapedDistance;
}
Your answer
Follow this Question
Related Questions
AudioHighPassFilter with multiple AudioSource 1 Answer
Global audio option 2 Answers
Audio loops too early 2 Answers
Playing Sound 2 Answers
Hi guys I am wondering how can I stop the audio from playing if I click the object twice? 2 Answers