Creating a structure from a number and index possibly using JS

miccat's picture

Hi,

I have searched the internet for hours on this problem and the closest thing I have come across is a structure merger built in javascript in this post: "Just modifing one (or many) members in a structure before using this structure..." I have tried to modify this so that the input is a number rather than a structure but as I have never studied JS I am finding it extremely difficult.

Basically, what I am trying to achieve is an object with a similar output to the 'audio input' object which comes with QC4. The difference being that the structure can have more than 16 bands.

Say I had 50 bands with index 0, 1, 2, 3, ...49 Each with a value between 0-1.

How can I form this into a structure (in a similar way to what 'audio input' does)?

Thanks so much for your help and I hope this makes sense.

usefuldesign.au's picture
Re: Creating a structure from a number and index possibly ...

The composition I'm posting is for demonstrating nested structures and overcoming some problems nesting can entail.* If you just look at the "Javascript 1 Patch" you have a simple structure building JS patch with each element a number based on an iterator index value.

Set the number of sets to 1 and elements to 50 in the Published Inputs of the comp to get a visualisation. 'L' key toggles a twinkling effect – modulating the numbers inside the structure by adding a random variation to the number values.

Hope this helps. It's not really clear to me what you are trying to achieve other than a structure of numbers? Yes/No/Maybe

  • (Like choosing sequential or leaved mapping of elements into sub-structures and the QC JS bug that mis-orders elements in terms of index and using a Key value to overcome this.)
PreviewAttachmentSize
nested structure (leafed:seq).qtz77.16 KB

miccat's picture
Re: Creating a structure from a number and index possibly ...

Hi,

Thanks for your quick response. The "Javascript 1" Patch is perhaps the most relevant part though I was wondering if there is anyway of writing values to each member. At the moment it seems that the member output is equally spaced and changes depending on the amount of elements specified.

e.g. for 5 elements, the member values (when lighting twinkle is off) are as follows:

0=0

1=0.2

2=0.4

3=0.6

4=0.8

I don't know how this would work as the JS patch would need another input which would somehow write its value into the member depending on index. Sorry, I'm finding it quite hard to explain myself.

If you imagine that I have two numbers as the input: the first being an index for the second which is the value. These numbers are continuously changing and I want to somehow match the indexes say 0 - 4 with those of the structure 0 - 4, and for each index, write it's corresponding value into the member. So member 0 = (index 0's value), member 1 = (index 1's value) member 2 = (index 2's value) etc. Sorry if this still isn't making sense. I have uploaded a picture of the audio spectrum output so that you can see what I am trying to achieve and a picture of your JS1 output to compare.

Thanks again

PreviewAttachmentSize
audio version.png
audio version.png87.81 KB
jsversion.png
jsversion.png45.44 KB

usefuldesign.au's picture
Re: Creating a structure from a number and index possibly ...

If you only have two inputs and one is an index and the other is a value, say index=4 and value =0.048372, then you are going to end up with a structure only one item long (if the structure is an array 5 elements long with four null value elements ).

You don't need JS patch to do this, you can use Kineme's structure patches in the Structure Tools plugin (goes in patches folder not plugins as it uses the 'skanky' SDK).

If you have an index which is changing and a value which is changing you can still do it with the structure tools but you will need a demultiplexer patch to port the 'value' to the correct 'index' input node on the structure maker patch.

If you are doing this with Javascript you have to a) give consideration to the fact that every time an input value on the patch changes, the javascript gets run and b) know a smidge of Javascript b/c working with structures in Javascript requires a little bit of knowledge about what Objects and/or Arrays are and how they can be made and manipulated. When the patch reruns it can overwrite the old values unless you move the Array initialisation to outside the main() loop. I've given both examples. Hook the other one (bad method) up to see visually what happens.

I've made a comp demonstrating various methods, javascript, queue and structure maker patches to do what I think you are attempting. Again, not clear what it is you need exactly. Study these methods and you should be able to figure something out that suits you.

EDit> Uploaded newer version of file with slight fix.

miccat's picture
Re: Creating a structure from a number and index possibly ...

Thanks a lot for your help.

I think I have managed to work something out from your patch. I may even be able to use the queue patch after all. I had tried it before, but with continuous evaluation on, so this could have been why I was struggling.

I am now just trying to work out a way of synchronising the values to the correct indexes.

Thanks again!

usefuldesign.au's picture
Re: Creating a structure from a number and index possibly ...

miccat wrote:
Thanks a lot for your help.
Pleasure.

miccat wrote:
I am now just trying to work out a way of synchronising the values to the correct indexes.

Don't really understand what you are describing.

BTW If you want help with a comp it's good manners to post it (unless it's private) because it can save whoever is helping you lots of their time trying to get the germane details. Especially so considering that English is a second language for lots of Kineme users.