Trouble getting correct module inputs outputs when using sv_get_module_inputs

Multi-platform modular music creation studio
Post Reply
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

Hi, i've been trying to connect sunvox with unity using this https://github.com/AlRado/SunVox-Unity repo in combination with a node editor for unity and so far i could successfully get all the modules and module infos, but when i try to get module connections using commands sv_get_module_inputs or sv_get_module_outputs i get int[] arrays with a lot more values than actual connections.

For example i have a Compressor that has exactly 1 input connection and 1 output connection, but sv_get_module_inputs returns an int[] array of 12 values, some of which are negative integers.

I'm writing the node editor in C# and i use the sunvox_lib.dll version 1.9.6c so commands are forwarded to this library i'm 100% sure
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

I've tried to hack it by using sv_disconnect_module(slot, source, dest) but the result is always 0 (success) even if modules are already disconnected so i cannot properly get module connections with this approach :(
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by NightRadio »

For example i have a Compressor that has exactly 1 input connection and 1 output connection, but sv_get_module_inputs returns an int[] array of 12 values, some of which are negative integers.
It's ok for sv_get_module_inputs() :)
Just ignore the -1 values.
It returns an array of connector slots, some of which were filled in earlier but are currently unused and contain -1.
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

Ok but some returned values are like 516 or 12312312 or -13593423, and there is always a power of two length of arrays
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

Here is the usual output of sv_get_module_inputs(0, 3) and sv_get_module_outputs(0, 3) for module "03 Echo":

input_0 = 805306380
input_1 = 2147483647
input_2 = 536870912
input_3 = 9

output_0 = 0
output_1 = 0
output_2 = 1545589120
output_3 = 405

but when i look at the project inside Sunvox engine this module is 03 Echo and it has 1 input connection from module 02 Compressor and 1 output connection to 00 Output
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by NightRadio »

and there is always a power of two length of arrays
How do you get the size of the array?
Correct way:
int module_flags = sv_get_module_flags( slot, mod_num );
Number of inputs = ( module_flags & SV_MODULE_INPUTS_MASK ) >> SV_MODULE_INPUTS_OFF;
Number of outputs = ( module_flags & SV_MODULE_OUTPUTS_MASK ) >> SV_MODULE_OUTPUTS_OFF;
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

I'm sorry, maybe i stated what i don't understand incorrectly.

How do i get the indexes of modules that are connected to/from a target module.

For example if i have a module 03 Echo, and sv_get_module_input(0, 03) returns an array like this:
input_0 = 805306380
input_1 = 2147483647
input_2 = 536870912
input_3 = 9

how do i get the index of a module that is connected to 03 Echo?
NightRadio wrote: Thu Jul 08, 2021 9:23 am How do you get the size of the array?
Correct way:
int module_flags = sv_get_module_flags( slot, mod_num );
Number of inputs = ( module_flags & SV_MODULE_INPUTS_MASK ) >> SV_MODULE_INPUTS_OFF;
Number of outputs = ( module_flags & SV_MODULE_OUTPUTS_MASK ) >> SV_MODULE_OUTPUTS_OFF;
Yeah i've tried that and it correctly returns that a module has for example 1 input and 1 output but how do i know the index of a module that is connected to it?
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by NightRadio »

Yeah i've tried that and it correctly returns that a module has for example 1 input and 1 output but how do i know the index of a module that is connected to it?
I must clarify that here we are not talking about the number of inputs, but about the number of slots too:
Number of input slots = ( module_flags & SV_MODULE_INPUTS_MASK ) >> SV_MODULE_INPUTS_OFF;

The real number of inputs is the number of slots with values of 0 and higher.

The problem is that there should be -1 values in this array instead of the garbage, like in your case.
It works fine in the native C/C++ code.
So maybe this is some kind of C# feature when working with native unsafe pointers (which point to our arrays). Unfortunately i dont know C# :(
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

NightRadio wrote: Thu Jul 08, 2021 2:53 pm So maybe this is some kind of C# feature when working with native unsafe pointers (which point to our arrays). Unfortunately i dont know C# :(
Hmmm, i will try to enable unsafe code and mark the method body with unsafe brackets and see if it helps. This might be the reason behind unity crashing when actively using the library :)

The thing is that i use the sunvox_lib.dll which is as i understand a compiled cpp lib and all i do is call methods directly from the library by using "extern".

For example to call sv_get_module_inputs(0, 3) i call "public static extern int[] sv_get_module_inputs(0, 3);"
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

I have tried to change the return value of sv_get_module_inputs() to return a pointer* and tried reading an int[] array from it using helper methods in C# but still no luck. If i try to read an int[] array from a pointer* it returns a massive int[] array, but if i try to read a fixed array with a size returned by "Number of input slots = ( module_flags & SV_MODULE_INPUTS_MASK ) >> SV_MODULE_INPUTS_OFF;" then i get all 0 inside this array.

I also noticed that there is currently no way to get all the module indexes, you can only get module count but you have to manually guess module indexes.

For example i have 3 modules: 01 Echo, 03 Compressor and 00 Output. sv_get_number_of_modules(slot) returns 3, but how can i know the index of "03 Compressor" if i don't know the name of the module?

And more over, if there are two modules with the same name, which of them will be returned by sv_find_module(slot, name) ?
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by NightRadio »

Unfortunately I cannot yet figure out how to solve this in the context of C# :(

sv_get_number_of_modules(slot) returns 3, but how can i know the index of "03 Compressor"
The index of "03 compressor" is 03 :)

if there are two modules with the same name, which of them will be returned by sv_find_module(slot, name)?
the index of the first matched module will be returned.
Just use the modules with different names, or hardcode the module indexes (if you have some static routing by default).
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

NightRadio wrote: Fri Jul 09, 2021 4:14 pm
sv_get_number_of_modules(slot) returns 3, but how can i know the index of "03 Compressor"
The index of "03 compressor" is 03 :)
I meant if i have only module count returned by sv_get_number_of_modules() and some modules have their index higher than module count, it's impossible to get those module indexes from any helper methods of the library
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by NightRadio »

sv_get_number_of_modules() is always higher or equal to the largest module index. This function works similarly to the module inputs/outputs. It returns the number of module slots, some of which may be empty (without SV_MODULE_FLAG_EXISTS flag).
Module index = slot number
Dioinecail
Posts: 11
Joined: Wed Jul 07, 2021 2:02 pm

Re: Trouble getting correct module inputs outputs when using sv_get_module_inputs

Post by Dioinecail »

NightRadio wrote: Sat Jul 10, 2021 11:09 am sv_get_number_of_modules() is always higher or equal to the largest module index. This function works similarly to the module inputs/outputs. It returns the number of module slots, some of which may be empty (without SV_MODULE_FLAG_EXISTS flag).
Module index = slot number
Thank you very much for answering my stupid questions! I was able to figure out what the problem was and also learned how to handle int* pointers in C# a little bit. The problem was in the wrapper it stated that the sunvox should return an int[] array when in reality it returned a int* pointer which was clearly stated at the library website and i was so dumb and blind that i couldn't properly read it.

After changing the return type of sv_get_module_inputs(slot, num) to int* i was able to properly read the pointer values based on the number of inputs/outputs that you can get with "Number of inputs = ( module_flags & SV_MODULE_INPUTS_MASK ) >> SV_MODULE_INPUTS_OFF" and it properly returns actual inputs/outputs and "-1" as you stated before.

Again, thank you very much! :)
Post Reply