Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created March 26, 2021 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unitycoder/6503d9bcbe2bee4b98b0ea9a21408b96 to your computer and use it in GitHub Desktop.
Save unitycoder/6503d9bcbe2bee4b98b0ea9a21408b96 to your computer and use it in GitHub Desktop.
OnAudioFilterRead generate sinewave
// https://forum.unity.com/threads/onaudiofilterread-question.170273/#post-1164846
using UnityEngine;
using System; // Needed for Math
public class SineWave : MonoBehaviour
{
// un-optimized version
public double frequency = 440;
public double gain = 0.05;
private double increment;
private double phase;
private double sampling_frequency = 48000;
private double[] tones = { 440.0f, 1046.5f, 3729.3f, 1046.5f };
private int tone_index;
private float last_time;
void Start()
{
Debug.Log("" + tone_index + ": " + tones[tone_index]);
}
void Update()
{
if (Time.realtimeSinceStartup - last_time > 1)
{
last_time = Time.realtimeSinceStartup;
tone_index++;
if (tone_index >= tones.Length)
tone_index = 0;
Debug.Log("" + tone_index + ": " + tones[tone_index]);
}
}
void OnAudioFilterRead(float[] data, int channels)
{
// update increment in case frequency has changed
increment = tones[tone_index] * 2 * Math.PI / sampling_frequency;
for (var i = 0; i < data.Length; i += channels)
{
phase += increment;
// this is where we copy audio data to make them “available” to Unity
data[i] = (float) (gain * Math.Sin(phase));
// if we have stereo, we copy the mono data to each channel
if (channels == 2)
data[i + 1] = data[i];
if (phase > 2 * Math.PI)
phase = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment