Welcome to %s forums

BrainModular Users Forum

Login Register

slowing down in onProcess

Create your own modules in C++
Post Reply
User avatar
oli_lab
Member
Posts: 1261
Location: Brittany, France
Contact:

Unread post by oli_lab » 17 Dec 2017, 17:59

Hi !
I'm trying to build a physics engine for a new module,
most of the updates are done inside callback (record, play, mouse, inputs...)

but still the physics should be computed regularly even so there is no callbacks called back.

so some computation should be done in the onProcess() loop

what would be the best option(s) to have the physic engine tamed to reasonable speed ?

- just leave it as it is in the onProcess to be computed each time onProcess is called ?
- use samplerate and blocksize to have the computing done once every "n" blocks ?
- using system time ?

if someone is willing to share some code that just does that, it will be cool !

thanx

Olivar
http://oli-lab.org

Win11 Ryzen9/32GB RAM - RME MADIFACE - SSL alpha link 4-16 - OSC capable interfaces

follow OLI_LAB adventures on Mastodon
@olivar_premier@mastodon.social

sm_jamieson
Member
Posts: 551
Contact:

Unread post by sm_jamieson » 17 Dec 2017, 21:40

If you don't need it calculated every bloc then to do so is a waste of CPU so I would not do that.

If you use system time by checking it in every bloc, its more efficient to count blocs since it uses less CPU instructions.

You can use sample rate and bloc size to work out how many blocs make up the time interval, then just set a counter to the value and subtract 1 every bloc until the value gets to zero, then do the action and reset the bloc counter.
An example of counting blocs with interval approximately 0.1 seconds:

void ModuleClass::onInitModule (MasterInfo* pMasterInfo, ModuleInfo* pModuleInfo)
{
double interval_secs = 0.1; // Use double data type since sdkGetSampleRate returns double

int numblocs = (sdkGetSampleRate() / (double)sdkGetBlocSize()) * interval_secs; // "int" numblocs to truncate result

counter = numblocs;
}

void ModuleClass::onProcess(void)
{
if (--counter == 0) {
doAction();
counter = numblocs;
}
}

This is not always accurate due to rounding off to get an exact number of blocs. If you need it to be accurate on average over time, you can keep track of the truncated fraction and use it to adjust the bloc count.

If exact timing is required (perhaps part way through a bloc), a timer might be required (thread timer / windows timer, etc). If you use a callback timer the callback might be in a different thread so you might need thread protection (mutexes, etc.)

Hope that helps.

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 17 Dec 2017, 23:13

yes do kinda same here, increment a counter in process, and past max_count it resets, to 0 trigg the action, max count being defined relative to blocsize

User avatar
oli_lab
Member
Posts: 1261
Location: Brittany, France
Contact:

Unread post by oli_lab » 17 Dec 2017, 23:38

thank you !
I'll try that asap. definitely going somewhere with this next module !
stay tune.

Olivar
http://oli-lab.org

Win11 Ryzen9/32GB RAM - RME MADIFACE - SSL alpha link 4-16 - OSC capable interfaces

follow OLI_LAB adventures on Mastodon
@olivar_premier@mastodon.social

Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests