electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
solved => sporking functions & global variables
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [2 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
catfingers



Joined: Jun 14, 2009
Posts: 3
Location: Australia

PostPosted: Tue Jun 01, 2010 4:01 am    Post subject: solved => sporking functions & global variables Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
catfingers



Joined: Jun 14, 2009
Posts: 3
Location: Australia

PostPosted: Tue Jun 01, 2010 5:34 pm    Post subject: a solution Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [2 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use