danomatika / ofxPd

(maintained) a Pure Data addon for OpenFrameworks using libpd
Other
202 stars 45 forks source link

[solved] how to handle multiple trig draws on List callback (faster than update framerate) #74

Closed moebiussurfing closed 5 years ago

moebiussurfing commented 5 years ago

Hey I am receiving some lists from the ofxPd pd instance and it's being tricky because there are more flood speed that the app update() / framerate happens... (more than one incoming messages in a 'frame pass')

To avoid drawing from the callback function (void ofApp::receiveList(const std::string& dest, const List& list), [because even calling another drawing in ofApp methods makes the app unstable and crashing], I store the message and I use a bool trigger that I listen on ofApp update().

But when it's trigged, the list message (that I packed into an osc message) has changed. So there's arriving too much list callbacks than framerate can handle.

Maybe I should enqueue all the osc messages/lists into a vector and then process one by one when trig is detected on update(). Do you think It's the clean way?

I see on the example some related things but I don't know how to handle in a good way. Any tips or example to check into?

//setup()
    // setup Pd
    //
    // set 4th arg to true for queued message passing using an internal ringbuffer,
    // this is useful if you need to control where and when the message callbacks
    // happen (ie. within a GUI thread)
    //
    // note: you won't see any message prints until update() is called since
    // the queued messages are processed there, this is normal
    //
    if(!pd.init(2, numInputs, 44100, ticksPerBuffer, false)) {
        OF_EXIT_APP(1);
    }

//update()
    // since this is a test and we don't know if init() was called with
    // queued = true or not, we check it here
    if(pd.isQueued()) {
        // process any received messages, if you're using the queue and *do not*
        // call these, you won't receieve any messages or midi!
        pd.receiveMessages();
        pd.receiveMidi();
    }
moebiussurfing commented 5 years ago

i made a vector to enqueue all the osc/lists received and to trig them on next update. it seems to work nice.


//ofApp.h
vector<ofxOscMessage> mOled_messages;

//ofApp.cpp
void ofApp::receiveList(const std::string& dest, const List& list)
{
                //...
                else if (list.isFloat(1))
                {
                    float listF = list.getFloat(i);
                    if (address == "/gDrawInfoBar")
                    {
                        ofLogNotice() << "   " << i << "  LIST FLOAT " << ofToString(listF) << " ";
                        mOled.addFloatArg(listF);
                    }
                }
                //...
}

//update()
    // dequeue all osc messages and trig their drawing commands
    if (mOled_messages.size() > 0)
    {
        for (int i = 0; i < mOled_messages.size(); i++)
        {
            oled.OSC_filter(mOled_messages[i]);
        }
        mOled_messages.clear();
    }