Hello,
First time using audio in unity, the sound is playing constantly, i want the frequency to change when i hit the button “change frequency”. This works fine but there is a 0.4seconds lag. Any idea why ?
I use this function audio.create (doc here Unity - Scripting API: AudioClip.Create)
and pcmreadercallback :
here is the code :
void Start()
{
audio = GetComponent<AudioSource>();
audio.loop = true;
audio.clip = AudioClip.Create("Stream", 120, 1, audioSamplingFreq, true, onReaderCallback);
audio.Play();
}
private bool low_f=false;
void onReaderCallback(float[] data)
{
for (var i = 0; i < data.Length; i++, t++)
{
float f;
if (low_f)
f = 600;
else
f = 800;
data[i] = 0.2f * (float)Math.Sin(2.0f * (float)Math.PI * f * (float)t / audioSamplingFreq);
}
}
void OnGUI()
{
if (GUI.Button(new Rect(Screen.width - 135, 40, 100, 25), "change frequency"))
{
low_f = !low_f;
}
}