oxfLua & ofxOsc: Receive OSC msg content in a lua script

I am trying to receive in my lua sketch the amplitude that SuperCollider is detecting with the following code:

mygea = of.app()
function setup()
   of.setWindowTitle("adf")
   of.background(0)
end
----------------------------------------------------
function update()
   print(mygea:amp())
end
----------------------------------------------------
function draw()
   of.fill()
   of.setColor(100,0,0,10)
   of.rect(100,0,of.getWidth(), of.getHeight())
end
----------------------------------------------------

I 've created the function in the ofApp.cpp:

float ofApp::amp() {
    return amplitude;
//    return ofGetFrameNum();
}

and the bindings:

    class_<ofApp>("app")
        .def(constructor<>())
        .def("amp", (float(ofApp::*)()) &ofApp::amp)

When I change my function to:

float ofApp::amp() {
//   return amplitude;
    return ofGetFrameNum();
}

I am taking the ofGetFrameNum, otherwise:

8.4077907859489e-45
8.4077907859489e-45
8.4077907859489e-45
8.4077907859489e-45

Any suggestion - help?

The code can be viewed here: https://github.com/igoumeninja/ofxLua/tree/gea-osc/src

Thanks in advance for sharing @danomatika .
Aris

Did you see my last response on GitHub? https://github.com/danomatika/ofxLua/issues/12#issuecomment-35424725

You’re problem is that you haven’t updated the bindings to receive the global app pointer, judging from the code posted above. I’ve basically given you the bindings you need and an example on how to use them.

Here’s a repost for future reference:

You’re almost there, but you didn’t update the bindings to reflect how the new function needs to work.

“getApp” should be a global function and not a member of your “app” class aka you should be able to use it without having to make a new “app” instance since what we want is to get a reference to the existing “app”.

Your bindings should look like this:

return
    class_<ofxMidiLua>("midi")
        .def(constructor<>())
        .def("setup", (void(ofxMidiLua::*)()) &ofxMidiLua::setup)
        .def("update", (void(ofxMidiLua::*)()) &ofxMidiLua::update)
        .def("init", (void(ofxMidiLua::*)(int, float)) &ofxMidiLua::init)
        .def("map", (float(ofxMidiLua::*)(int, float, float)) &ofxMidiLua::map),

    // get global app instance
    def("getApp", &getApp),

    class_<ofApp>("app")
        .def(constructor<>())
        .def("amp", &ofApp::getAmp)
        .property("amp", &ofApp::getAmp) // readonly property
;

Then you can use them like:

-- get a reference to the global app
app = gea.getApp()

-- get data from the app
amp = app:getAmp()
amp = app.amp -- this works too as I added a property in the bindings

Make sense? Also, it’s very important to note the colon : with app:getAmp(). The colon is used when calling a member function on a class instance while the period . is used when accessing a property app.amp or calling a global function in a module gea.getApp(), where “gea” is the module name). Coming from C++, that still trips me up now and then :smiley:

Update: Don’t forget to define the global app getter at the top of the bindings file:

// get global app pointer and cast type to your custom ofApp
ofApp* getApp() {return (ofApp*) ofGetAppPtr();}

Hi @danomatika, with the above bindings I am taking an error:

clang: warning: -lpthread: 'linker' input unused
/Users/ari/Tools/openFrameworks/repository/openFrameworks/addons/ofxLua/src/bindings/Gea.cpp:47:30: error: use of undeclared identifier 'App'
                .def("amp", &App::getAmp) 

Did you mean ofApp instead of App?

Change “&App” to “&ofApp” since that’s the binding to your ofApp class. I’ll update my earlier post.

Thanks @danomatika for your reply. I change &App to &ofApp an getAmp to amp (I call my function amp) I compile successfully and I am using the following lua script:

mygea = of.getApp()

function setup()
   of.setWindowTitle("adf")
   of.background(0)
end
----------------------------------------------------
function update()
   
   print(mygea:amp())

end
----------------------------------------------------
function draw()
   of.fill()
   of.setColor(100,0,0,10)
   of.rect(100,0,of.getWidth(), of.getHeight())
end
----------------------------------------------------

I am taking the message at the console:

[notice ] got a script error: Error running update(): ...cripts/my-lua-scripts/lua-scripts/00-receive-amp.lua:10: attempt to call method 'amp' (a number value)

The amp() function is a part of ofApp class:

float ofApp::amp() {
    return amplitude;
//    return ofGetFrameNum();
}

Please solve this mystery…

That error message is telling you what the problem is. You are trying to call a member function called amp but amp is actually a number.

See the binding you are using (as per my previous post). You have two ways to get the float amplitude:

-- get data from the app
amp = app:getAmp()
amp = app.amp -- this works too as I added a property in the bindings

Thanks @danomatika! It works like a charm, with the following code:

mygea = of.getApp()

function setup()
end
----------------------------------------------------
function update()       
   print(mygea.amp)
end
----------------------------------------------------
function draw()
end

Keep going with ofxLua to create a heterogeneous engine for audiovisuals performances…