| Author |
Message |
catfingers
Joined: Jun 14, 2009 Posts: 3 Location: Australia
|
Posted: Tue Jun 01, 2010 4:01 am Post subject:
solved => sporking functions & global variables |
 |
|
hey, my first post to this forum... I'm a fairly chuck newbie but coming to it with some background in text-based audio & max...
I'm doing some exploratory hacking intended to link osc (for a UI) to chuck to manipulate MIDI input (& output it again). The plan is that variables to control the logic would be input via functions to handle osc input (& also provide feedback to the osc controller) & stored as globals.
I've been playing with receiving OSC in chuck (from TouchOSC) & noticed the following issue - how to pass a changed value back from the function running in a sporked shred.
Since I can't return a value like so:
fun int myFunc(args)...
spork ~ myFunc(args) => variable; (- which makes sense on reflection)
What are the other options?
a) pass a variable by reference to the function so it will be updated by what's going on in the function
b) simply change this variable's value inside the function.
I'm don't know how to do a). b) doesn't appear to work p value changes in the function but is not posted back to the parent shred. Scoping?
My issue may be more to do with attitude re-adjustment (to ChucK as opposed to my very rusty C & Perl mindset) than anything else. Any comments would be appreciated. This is roughly what I'm working with at the moment - the osc handling stuff less relevant to my actual problem but puts it in context:
***********************
// quite a bit rote-copied from an electro-music thread on OSC reception - //unrefined so far.
//globals to hold values got from osc
0 => float fader1;
//osc receive port
8000 => int oscin;
spork ~ oscListener(oscin,"/1/fader1,f",fader1);
me.yield();
function void oscListener(int oscport, string osctype, float vname) {
//create osc receiver
OscRecv or;
oscport => or.port;
or.listen();
or.event(osctype) @=> OscEvent oe;
//wait for events
while (true) {
oe => now;
//process event queue
while (oe.nextMsg() != 0) {
oe.getFloat() => float val;
osctype => string type;
val => vname;
<<< "got", type, val, vname >>>; //these update!
}
}
} //end oscListener func
while(true) {
1::second => now;
<<< "global value is", fader1>>>; //this never updates!
}
- would using ChucK Events be the answer to my problem maybe?
<EDIT (morning after) - or storing the data-to-be-accessed in a class with static members - I'll work along these lines> Last edited by catfingers on Tue Jun 01, 2010 9:21 pm; edited 2 times in total |
|
|
Back to top
|
|
 |
catfingers
Joined: Jun 14, 2009 Posts: 3 Location: Australia
|
Posted: Tue Jun 01, 2010 5:34 pm Post subject:
a solution |
 |
|
store the global or application-wide variables in a class with static member - you can access & change these from any shred.
RTFF (read the ... forum) - this thread:
http://electro-music.com/forum/topic-41883.html
here's the new version using an object to store values:
------------------
//touchosc to chuck - storing app-wide values in static object vars
// & sporking osc listeners to catch events
//oscListener & usage borrowed from electro-music chuck forum
//////app-wide storage///////
//uses workaround - declare params array outside of & then store ref inside class
//class to hold values - everything is a float in TouchOsc
float params[10] @=> storage.params;
class storage {
//variables held in static array - so they can be accessed via index
static float params[];
}
//create a storage object
storage Data;
//////OSCListener function//////
//no need to pass object name - there will only be one instance of...
function void oscListener(int oscport, string oscaddr, int idx) {
//create osc receiver
OscRecv or;
oscport => or.port;
or.listen();
or.event(oscaddr) @=> OscEvent oe;
//wait for events
while (true) {
oe => now;
//process event queue
while (oe.nextMsg() != 0) {
oe.getFloat() => float val;
oscaddr => string type;
val => Data.params[idx];
<<< "in child shred: ", type, idx, val>>>;
}
}
} //end oscListener func
//osc receive port
8000 => int oscin;
spork ~ oscListener(oscin,"/1/fader1,f",1);
me.yield();
//////main loop//////
while(true) {
1::second => now;
<<< "in parent shred: ", Data.params[1]>>>;
}
---------------------- |
|
|
Back to top
|
|
 |
|