Envelope in OnAudioFilterRead method

Hi all, I’m trying to make a sine wave in OnAudioFilterRead() method to generate it using an equation a = sin(2 * phi * frequency * sample length / sampling rate). But I have two problem with this,

  1. I want to limit the samples length to 100.000 samples how to limit it in this method?
  2. How to implement an envelope so my waveform has attack, decay, sustain, and release?

Below is simple code that I’ve implemented.

private double samplingRate = 48000;
private double frequency = 440;
    
private void OnAudioFilterRead (float[] data, int channels) {
                increment = 2.0 * Mathf.PI * frequency / samplingRate;
               
                for (int i = 0; i < data.Length; i += channels) {
                    phase         += increment;
                    data *= Mathf.Sin(phase);*

if (channels == 2) data[i + 1] = data*;*
if (phase > (Mathf.PI * 2)) phase = 0.0;
}

}
Thank you

Well, first of all currently you do not create a sine wave as you directly use the phase as data. This would result in a positive only sawtooth wave that would heavily clipping since it goes up to 2PI (6.28). So obviously to get a sine wave you either want to use Mathf.Sin and pass your phase as parameter, or you want to use a “complex phasor” by representing your phase and increment as complex number. If the frequency doesn’t change often a complex phasor would probably be better for performance as you only need to use Sin and Cos once when you change the frequency. If you need a complex number type i’ve [once created this one][1].

An envelope is a bit more complicated. See [this post here][2] for a good starting point.

ps: If you’re not familiar with complex numbers you can do something like this:

Complex phase = new Complex(1, 0);
float m_Frequency;
Complex increment;

public float Frequency
{
    get { return m_Frequency;}
    set
    {
        if (value != m_Frequency)
        {
            m_Frequency = value;
            float angle = (m_Frequency * 2.0 * Mathf.PI ) / samplingFq;
            increment = new Complex(Mathf.Cos(angle), Mathf.Sin(angle));
        }
    }
}

private void OnAudioFilterRead (float[] data, int channels)
{
    if (channels == 2)
    {
        for (int i = 0; i < data.Length; i += 2)
        {
            phase *= increment;
            data *= data[i+1] = phase.fReal;*

}
}
else
{
for (int i = 0; i < data.Length; i++)
{
phase *= increment;
data = phase.fReal;
}
}
double norm = (3d - phase.real * phase.real - phase.img * phase.img) * 0.5d;
phase *= norm;
}
As you can see we just need to calculate sin and cos once when we change the frequency. From now on we can simply “spin” the complex number. Note that the “normalization” at the end is an easy way to avoid drift over time. You could do it within the loops but usually it should be stable for several thousand complex rotations. Note that i splitted the loop into two seperate loops. Doing the if check every iteration is bad for performance. Branch prediction is pretty good nowadays, however it’s best to just avoid it in time critical code ^^.
_[1]: https://pastebin.com/LiTws7ND*_
_
[2]: https://christianfloisand.wordpress.com/2014/06/09/dynamics-processing-compressorlimiter-part-1/*_