-
-
Save aybe/c6c990c20f8d8759307c310baef44df5 to your computer and use it in GitHub Desktop.
Crusher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BitCrusher : MonoBehaviour | |
{ | |
[Range (1f,16f)] | |
public float bits = 16f; | |
[Range (20f,48000f)] | |
public float sampleRate = 48000f; | |
private float phase = 0; | |
private float last = 0; | |
private float baseSampleRate; | |
public BitCrusher() | |
{ | |
baseSampleRate = (float)AudioSettings.outputSampleRate; | |
} | |
public void OnAudioFilterRead(float[] data, int channels) | |
{ | |
float freqDelta = sampleRate/baseSampleRate; | |
float quantizeReso = Mathf.Pow(2f,bits); | |
int length = data.Length; | |
for(int d = 0; d<length; d++) | |
{ | |
phase = phase + freqDelta; | |
if(phase >= 1f) | |
{ | |
phase = phase - 1f; | |
last = Mathf.Floor((data[d]*quantizeReso) + 0.5f) / quantizeReso; | |
} | |
data[d] = last; | |
data[++d] = last; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment