Created
June 4, 2019 19:28
-
-
Save ciwolsey/a1791bf08522c61948c0796d50a8cc27 to your computer and use it in GitHub Desktop.
audiogen.cs
This file contains 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
void OnAudioFilterRead(float[] data, int channels) | |
{ | |
for(int i = 0; i < data.Length; i+= channels) | |
{ | |
data[i] = CreateSine(timeIndex, frequency1, sampleRate); | |
if(channels == 2) | |
data[i+1] = CreateSine(timeIndex, frequency2, sampleRate); | |
timeIndex++; | |
//if timeIndex gets too big, reset it to 0 | |
if(timeIndex >= (sampleRate * waveLengthInSeconds)) | |
{ | |
timeIndex = 0; | |
} | |
} | |
} | |
//Creates a sinewave | |
public float CreateSine(int timeIndex, float frequency, float sampleRate) | |
{ | |
return Mathf.Sin(2 * Mathf.PI * timeIndex * frequency / sampleRate); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment