COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :

Go Back   Cockos Incorporated Forums > Cockos and Friends Software Discussion > WDL users forum

Reply
 
Thread Tools Display Modes
Old 02-24-2017, 04:49 AM   #1
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default How would you share a "global" variable for each plugin instance?

Hi all,

I'd like to share for every classes within the same plugin's instance scope a common/global variable.

Such as "gLastTriggeredVoiceIndex" within my VoiceManager class, accessible for each other classes that needs it (Filters, Envelopes, and so on), but of course without passing to them a reference/pointer (keeping all stuff separated).

Each instance of the plugin should have its own VoiceManager instance, so its own gLastTriggeredVoiceIndex (not shared across all instances so).

Do you know any way in IPlug/C++?
static (internal) or external linkage won't works, since it will be the same for every plugin intance.

Thanks
Nowhk is offline   Reply With Quote
Old 02-24-2017, 05:10 AM   #2
bFooz
Human being with feelings
 
bFooz's Avatar
 
Join Date: Jul 2010
Location: Slovakia
Posts: 444
Default

Maybe writing to a project itself with

reaper.SetExtState( section, key, value, persist )

would be useful.
__________________
Donations | Mixing & Mastering Services
bFooz is offline   Reply With Quote
Old 02-24-2017, 05:12 AM   #3
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by bFooz View Post
Maybe writing to a project itself with

reaper.SetExtState( section, key, value, persist )

would be useful.
I'm using VST "standards" and FL Studio/Live. And should works for any DAW.
Thus, not specific for Reaper
Nowhk is offline   Reply With Quote
Old 02-24-2017, 08:41 AM   #4
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 6,523
Default

Quote:
Originally Posted by Nowhk View Post
but of course without passing to them a reference/pointer (keeping all stuff separated).
The problem sounds like you should just do the pointer/reference passing. Why would you want to avoid doing that anyway, if the last voice variable is just an int? It would be more painful if the passed variable was something more complicated like a pointer to an object, that all the other involved classes would then need to know about.
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 02-24-2017, 08:50 AM   #5
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by Xenakios View Post
The problem sounds like you should just do the pointer/reference passing. Why would you want to avoid doing that anyway, if the last voice variable is just an int? It would be more painful if the passed variable was something more complicated like a pointer to an object, that all the other involved classes would then need to know about.
Let say I have a VoiceManager class (which got that index), which has 16 Voice object,; each of them got 10 Envelopes and 4 LFOs: I need to pass that reference to all Voice/Envelope/LFO class every time I instance them, creating a sort of "linking" between VoiceManager and Voice/Envelope/LFO classes.

This create a relationship that I'd like to avoid, that's all Since that variable is the same (for this purpose, customers will only "read" it), would be pretty fast make it global.
Nowhk is offline   Reply With Quote
Old 02-24-2017, 09:01 AM   #6
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 6,523
Default

Quote:
Originally Posted by Nowhk View Post
I need to pass that reference to all Voice/Envelope/LFO class every time I instance them, creating a sort of "linking" between VoiceManager and Voice/Envelope/LFO classes.

This create a relationship that I'd like to avoid, that's all Since that variable is the same (for this purpose, customers will only "read" it), would be pretty fast make it global.
Sure, sounds like a familiar problem. But within a plugin type context I haven't really found another way than to pass around a pointer to the variable of interest. (In standalone applications I've used global variables and "extern" a bit too eagerly, just because it's possible...) If there's some other technique that I may have forgotten, it'd be interesting if someone will mention that.
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 02-25-2017, 11:54 AM   #7
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Yep, would be nice to know
Nowhk is offline   Reply With Quote
Old 02-27-2017, 06:02 PM   #8
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 6,914
Default

How about using a "signals and slots" approach ?

e.g.
https://github.com/pbhogan/Signals
nofish is offline   Reply With Quote
Old 02-28-2017, 01:33 AM   #9
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by nofish View Post
How about using a "signals and slots" approach ?

e.g.
https://github.com/pbhogan/Signals
I already use EventEmitter for binds and OnParamChange callbacks. But within ProcessDoubleReplace this approach become really expensive for CPU...
Nowhk is offline   Reply With Quote
Old 02-28-2017, 03:09 PM   #10
gstuff
Human being with feelings
 
Join Date: Feb 2014
Posts: 42
Default

Each instance could write it's gLastTriggeredVoiceIndex to a shared static array member variable, assuming each plugin gets an incremented index number on instantiation, using a static counter variable.

Last edited by gstuff; 02-28-2017 at 03:20 PM.
gstuff is offline   Reply With Quote
Old 03-01-2017, 12:51 AM   #11
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by gstuff View Post
Each instance could write it's gLastTriggeredVoiceIndex to a shared static array member variable, assuming each plugin gets an incremented index number on instantiation, using a static counter variable.
Nice idea, but than its the same problem: I need to share to every classes the index number of the current instance (which must be global per instance, as for gLastTriggeredVoiceIndex)
Nowhk is offline   Reply With Quote
Old 03-01-2017, 03:53 AM   #12
gstuff
Human being with feelings
 
Join Date: Feb 2014
Posts: 42
Default

That's just an extra static member int that can be set by the active instance, you aren't passing object references around. I've used a similar technique successfully in one of my plugins.
gstuff is offline   Reply With Quote
Old 03-01-2017, 06:58 AM   #13
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by gstuff View Post
That's just an extra static member int that can be set by the active instance, you aren't passing object references around. I've used a similar technique successfully in one of my plugins.
A static member is the same for every class you will instanciate on the same "context" (process/thread). On different plugin instance, the static member is the same.

It would depends on how DAW will handle the process/thread when you open a new instance of the plugin I think; on FL Studio for example the "context" is the same, so it won't work...
Nowhk is offline   Reply With Quote
Old 03-01-2017, 08:28 AM   #14
gstuff
Human being with feelings
 
Join Date: Feb 2014
Posts: 42
Default

That's correct, static member variables are shared by all instances in the same process. This allows you to count/track your instances and use an array/vector to hold data specific to each instance in one global location, without using pointers directly, akin to an Observer pattern using pull (polling). If that's not what you're trying to do then please ignore.

Last edited by gstuff; 03-01-2017 at 08:34 AM.
gstuff is offline   Reply With Quote
Old 03-01-2017, 08:33 AM   #15
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by gstuff View Post
That's correct, static member variables are shared by all instances in the same process. This allows you to count/track your instances and use an array/vector to hold data specific to each instance in one global location, without using pointers directly to objects. If that's not what you're trying to do then please ignore.
I meant: when I need (later) to access to this array/vector on a single instance, I need to do pPlugInstance->mIndex. So every object need to know about pPlugInstance. Thus, its like to pass that reference instead of gLastTriggeredVoiceIndex; thus, in the end, change nothing I always need to pass a "reference".
Nowhk is offline   Reply With Quote
Old 03-01-2017, 08:55 AM   #16
gstuff
Human being with feelings
 
Join Date: Feb 2014
Posts: 42
Default

Quote:
Originally Posted by Nowhk View Post
I meant: when I need (later) to access to this array/vector on a single instance, I need to do pPlugInstance->mIndex. So every object need to know about pPlugInstance. Thus, its like to pass that reference instead of gLastTriggeredVoiceIndex; thus, in the end, change nothing I always need to pass a "reference".
There's no reference to pass. Each instance only needs to know its own integer index, it can then read through the array when it needs see others instances gLastTriggeredVoiceIndex value or write it's own.
gstuff is offline   Reply With Quote
Old 03-01-2017, 09:06 AM   #17
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by gstuff View Post
Each instance only needs to know its own integer index
Of course, but how would my Filter know about integer index (for the current instance) if I don't pass to it?
Nowhk is offline   Reply With Quote
Old 03-01-2017, 09:19 AM   #18
gstuff
Human being with feelings
 
Join Date: Feb 2014
Posts: 42
Default

Quote:
Originally Posted by Nowhk View Post
Of course, but how would my Filter know about integer index (for the current instance) if I don't pass to it?
Because in the constructor of your plugin class you increment a static member variable and save the value in a member variable. So each instance gets a unique index number based on total number of instances. I use a similar approach in my Auto Time adjuster plugin.

Last edited by gstuff; 03-01-2017 at 09:27 AM.
gstuff is offline   Reply With Quote
Old 03-01-2017, 09:25 AM   #19
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by gstuff View Post
Because in the constructor of your plugin class you increment a static member variable and save the value in a member variable. So each instance gets a unique index number based on total number of instances.
Yes sir, but every kind of class within my plugin (Filter? Envelope? Voice?) need to know about my "plugin class". Thus, I need to pass it as reference (to every component). Which is what I don't want
Nowhk is offline   Reply With Quote
Old 03-01-2017, 09:32 AM   #20
gstuff
Human being with feelings
 
Join Date: Feb 2014
Posts: 42
Default

You started of asking for a way to do this "without passing to them a reference/pointer (keeping all stuff separated)." Best of luck.
gstuff is offline   Reply With Quote
Old 03-01-2017, 09:46 AM   #21
Nowhk
Human being with feelings
 
Join Date: Mar 2016
Posts: 193
Default

Quote:
Originally Posted by gstuff View Post
You started of asking for a way to do this "without passing to them a reference/pointer (keeping all stuff separated)." Best of luck.
Yep. But if I save to pass it via reference but instead I pass the whole class via reference, nothing change so much. The "problem" is to pass a reference, not a kind (int) of reference. Thanks for the help anyway!
Nowhk is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 05:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2017, vBulletin Solutions Inc.