Audio clicks / Attempts from filter code

I am creating a C # script for sound filters in the Unity engine.

My problem is that after starting through my filter, the resulting sound has sequential and frequent “clicks”, “pops up” or “skips”. It is like an old radio.
I am not sure what causes this.

Here is my code:

public float cutoff; public float resonance; int sampleRate; void Start() { cutoff = 200; resonance = 1; sampleRate = AudioSettings.outputSampleRate; } void OnAudioFilterRead(float[] data, int channels) { float c = 2 * Mathf.PI * cutoff/sampleRate; float r = 1 / resonance; float v0 = 0; float v1 = 0; for (int i = 0; i < data.Length; i++) { v0 = (1 - r * c) * v0 - (c) * v1 + (c) * data[i]; v1 = (1 - r * c) * v1 + (c) * v0; data[i] = v1; } } 

Here is the documentation for OnAudioFilterRead () .
Here I got the original bottom pass code .

When cropping approaches its maximum value (127), clicks and pop-ups become quieter.

I am new to the audio program, as this may be obvious, so I'm not sure what it would cause.
Can someone more knowledgeable than me explain what I'm doing wrong?

Thanks!

+4
source share
2 answers

I solved it. My c and r variables should be stored in all OnAudioFilterRead () calls. Their members fixed it. Here is my full working code:

 using UnityEngine; using System.Collections; using System; public class LowPassFilter : MonoBehaviour { public float cutoff; public float resonance; const float CUTOFF_MAX = 128.0f; const float CUTOFF_MIN = 0.0f; const float RESONANCE_MAX = 128.0f; const float RESONANCE_MIN = 0.0f; float c; float r; float v0; float v1; int sampleRate; void Start() { cutoff = 20.0f; resonance = 0.0f; c = 0.0f; r = 0.0f; v0 = 0.0f; v1 = 0.0f; sampleRate = AudioSettings.outputSampleRate; } void OnAudioFilterRead(float[] data, int channels) { cutoff = Mathf.Clamp(cutoff, CUTOFF_MIN, CUTOFF_MAX); resonance = Mathf.Clamp(resonance, RESONANCE_MIN, RESONANCE_MAX); c = Mathf.Pow(0.5f, (128.0f - cutoff) / 16.0f); r = Mathf.Pow(0.5f, (resonance + 24.0f) / 16.0f); for (int i = 0; i < data.Length; i++) { v0 = ((1.0f - r * c) * v0) - (c * v1) + (c * data[i]); v1 = ((1.0f - r * c) * v1) + (c * v0); data[i] = Mathf.Clamp(v1, -1.0f, 1.0f); } } } 
+1
source

Common reasons for clicks and pop-ups (in the order of "generality"):

  • Incorrect buffer length (you have blocked the buffer or could not fill it to the border)
  • your sample is cropped and you don’t process it as you should, for example, you calculate everything in short s and don’t care about wrapping the values
  • your DSP algorithm is behaving badly.
  • for some reason your algorithm is too slow, and the sound sample is not delivered on time, which leads to a lack of sound.

One good debugging method for this is to try to narrow down the cause of the problem, for example by inserting PCM dumping directly into a routine that processes audio. In this way, you will find out whether the result of your subroutine matches or not, and you can accordingly focus your debugging efforts.

+3
source

Source: https://habr.com/ru/post/1440982/


All Articles