- Home /
Question by
Winklehner · Sep 06, 2017 at 10:05 AM ·
audiogenerationsine-wave
Quirks with generating sin tones on the fly
I'm generating sinus tones in Unity 5.6.3f1 using the following code:
private double _factor = 2.0 * Math.PI / AudioSettings.outputSampleRate;
public double Frequency = 440;
public double Gain = 1;
public int Location = 1;
private void OnAudioFilterRead(float[] data, int channels)
{
_increment = Frequency * _factor;
for (var i = 0; i < data.Length; i = i + channels)
{
_phase = _phase + _increment;
float value = (float) (Gain * Math.Sin(_phase));
if (Location == 2) // left & right
{
data[i] = value;
if (channels == 2) data[i + 1] = value;
} else
data[i + Location] = value;
}
}
Now I'm experiencing two major issues with this:
The generated sinus tone sounds not so smooth, as if I would use any online web based generator. Sounds more like triangle instead sinus. As lower the frequency as worst it seems to be.
When I change fast the gain (e.g. using the Unity slider from the script), changes in loudness are not smooth. There is some kind of scratching noise to hear.
What have I done wrong? What can I do to fix these issues? Any help is appreciated.
Thanks, Thomas
Comment
Your answer
Follow this Question
Related Questions
Envelope in OnAudioFilterRead method 1 Answer
Analyzing an audio file. 0 Answers
How do I add to channels or sine waves? 0 Answers
Procedural Generated Galaxy 2 Answers