Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 06-01-2019, 06:05 PM   #1
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,108
Default [C++] GetEnvelopeStateChunk() - ensure NeedBig is big enough? (SOLVED)

edit:
Solved with new API addition added in Reaper v6.68:

Quote:
+ API: expose realloc_cmd_register_buf/realloc_cmd_clear to C/C++ API
Usage info here.

=== original post ===

How big of a buffer / c-string should I pass to GetEnvelopeStateChunk() to ensure to always get the complete chunk and not truncated - as envelope state chunks can theoretically grow 'unlimited' in size with lots and lots of envelope points?

Last edited by nofish; 12-20-2022 at 01:58 PM.
nofish is offline   Reply With Quote
Old 06-03-2019, 06:46 AM   #2
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,108
Default

bump, anyone?

Actually this question rises for me whenever wanting to get chunks which can grow relatively big, e.g. also GetTrackStateChunk() (containing all the FX subchunks).

Is there a way to query the current size of the chunk before passing a c-string perhaps, or how do I do this correctly?
nofish is offline   Reply With Quote
Old 06-03-2019, 06:56 AM   #3
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Taking the API at face value, the only way I can see is to start with some size, check if the call succeeded and if not, try again until it succeeds.

So, something like this, assuming GetEnvelopeStateChunk returns false because the buffer wasn't large enough :

Code:
std::vector<char> buf(1024); // initial size 1024 chars
while (GetEnvelopeStateChunk(env, buf.data(), buf.size(), true)==false)
{
  buf.resize(buf.size()*1.5); // grow buffer by 1.5x
}
This would blow up, however, if the GetEnvelopeStateChunk call fails because of some other reason...To handle that, could check inside the while loop if the buffer is being resized to some ridiculously large size.

To make it more efficient, the buffer could be a class member variable or global/static, so it can be reused with the already enlarged size if the operation needs to be done multiple times.
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.

Last edited by Xenakios; 06-03-2019 at 07:06 AM.
Xenakios is offline   Reply With Quote
Old 06-03-2019, 07:02 AM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,629
Default

Basically you could try checking if < and > at the beginning of a line match in numbers.
< open a new (sub)-chunk and > close them again.

If the count mismatch, try again.

Counting the envelopes(in Track/ItemStateChunks) or the number of envelope-points(in EnvStateChunks) could also help. If the number of envelopes/envelopepoints mismatch, you can estimate, how bigger the NeedBig should be.

Or featurerequest a "GetSizeOfStateChunk(object)"-function, which gets the size of a statechunk for a track/item/envelope-object in question, which would be the best IMHO.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 06-03-2019, 07:15 AM   #5
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,108
Default

@Xen

I can do e.g.
Code:
char envState[10]; // 100% not big enough
bool gotChunkOk = GetEnvelopeStateChunk(envelope, envState, sizeof(envState), false);
though GetEnvelopeStateChunk() returns true nonetheless.


So your approach doesn't seem to work?

edit:
Sorry, missed this:
Quote:
assuming GetEnvelopeStateChunk returns false because the buffer wasn't large enough :
nofish is offline   Reply With Quote
Old 06-03-2019, 07:19 AM   #6
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by nofish View Post
So your approach doesn't seem to work?
Then I guess need to do it like mespotine suggested, check that the buffer contains what is expected. Seems like a really badly designed API...
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Xenakios is offline   Reply With Quote
Old 06-03-2019, 07:25 AM   #7
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,108
Default

Quote:
Originally Posted by mespotine View Post
Or featurerequest a "GetSizeOfStateChunk(object)"-function, which gets the size of a statechunk for a track/item/envelope-object in question, which would be the best IMHO.
I think I'll do that if no other reasonable way comes up. Parsing the chunk everytime to check for correctness seems rather crutchy to me.
Thanks.
nofish is offline   Reply With Quote
Old 06-03-2019, 07:28 AM   #8
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,629
Default

I made a feature-request, as I think, it's helpful for SWS and other extensions as well:

https://forum.cockos.com/showthread....45#post2142245
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 06-03-2019, 07:33 AM   #9
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,108
Default

Another thought:

Recently realloc_cmd_ptr was added to the API to be able to grow buffers passed from ReaScript, but we can't use this for internal C++ API calls somehow, or can we?

@mespotine
Thanks for opening the FR.
nofish 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 10:01 AM.


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