Skip to content

Instantly share code, notes, and snippets.

@ciwolsey
Created June 4, 2019 19:28
Show Gist options
  • Save ciwolsey/a1791bf08522c61948c0796d50a8cc27 to your computer and use it in GitHub Desktop.
Save ciwolsey/a1791bf08522c61948c0796d50a8cc27 to your computer and use it in GitHub Desktop.
audiogen.cs
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