How to avoid jumps in phase when altering the frequency of Periodic Signals Example? anyone?

Hi I really need to resolve this but cant understand where to start… The Periodic Signals example in the math folder displays some odd behaviour when manipulating the frequency of the oscillators. If you move the frequency (hz) slider all the phases of the oscillators jump around until the frequency remains fixed.

What I would expect is the same behaviour when observing a slowly rising frequency of a sine wave through an oscilloscope. The wave gradually increases the speed of oscillation. I understand that in this case the frequency would be updated at a faster rate on the sound card as opposed to 60 times a second in the update function but the behaviour of the Periodic Signals example seems like there is something more strange going on than that? … no?

Can anyone suggest how this example could be tweaked so as to create a smoother changing phase oscillation when interacting with the frequency? Any advice would be Hugely appreciated. :smile:

This works for me:
http://www.musicdsp.org/showone.php?id=152

Via @scanlime on twitter:

phase += time_difference * frequency
signal = sin(phase)
-> Changes to “frequency” won’t cause phase discontinuity.

To get the time_difference value in oF, keep track of time passed in between update() calls be keeping an extra float in your ofApp like this:

in ofApp.h

class ofApp : public ofBaseApp {
    ...
    float lastUpdate;
};

then in your update():

void ofApp::update() {
    ...
    float now = ofGetElapsedTimef();
    float time_difference = now - lastUpdate;
    lastUpdate = now;
1 Like

@admsyn that does the trick! thanks. so simple :slight_smile:
@robotfunk cheers that Lfo class looks super handy! Thanks guys.