how many notes in a melody?

personal_username's icon

Hi all!

I'm trying to slim down some patches I did, and would love to simplify, if possible, this one.
It basically gets a list of 32 values, and reports how many values are there which are not final zeroes.
So 1 2 3 0 0 0 should get as a result 3 good values (1 2 3)
And 1 2 0 3 4 0 0 should get 5 good values (1 2 0 3 4).
Only final zeroes are ignored.

Max Patch
Copy patch and select New From Clipboard in Max.

So, this is it.
Any tip on how to simplify it?
I guess regexp should come handy

Rick's icon

missed the final zero part

Rodrigo's icon

Filtering out zeros would be really easy just using [zl filter 0]

So another way to do this would be to only store melody notes from 1+, so you can strip 0s with one object.

So your examples would be
2 3 4 0 0 0 0
2 3 1 4 5 0 0

And if you want you can use vexpr @scalarmode to convert the output back to 0+.

personal_username's icon

Thanks Rick,
I thought of that, but it actually filters all zeroes, not only last ones...
While I want to retain the zeroes which are inside the melody (they are pauses)

personal_username's icon

Hi Rodrigo,
thanks for the reply, I only see now...
the same as my below reply though, I need to mantain certain zeroes...

Rick's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Yup my misreading of what you asked for. Using Rodrigo's idea and substituting 255 for your pauses you could do this...

personal_username's icon

ok, thanks all, this should do in most cases, but I'm actually trying to analyze a multislider output, so I cannot really do a separation between zeroes that are pauses and those that are melody ending... unless i give them a -1 maybe... in a multislider I guess that would be handier than a 255...
anyway, I don't know if this would be worth for my usage the case, because I actually already have a system which does what I need (see my posted patch), I was just looking for a slimmed down version of it...

btw, thanks for your time replying!

Rodrigo's icon

Another option would be to just count the amount of melody elements you are creating/generating/analyzing/whatever before packing them into a multislider.

Rick's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Let's try [zl.stack]

personal_username's icon

That's indeed a step forward, thanks Rick! ;-)
And thank you Rodrigo!

Roman Thilenius's icon

vexpr $i1!=0 @scalarmode 1
accum

but there are also several zl based ways. besides the one above you could for example merge the list with a 0 and then measure the length of the output, then substract -1.

ak's icon
Max Patch
Copy patch and select New From Clipboard in Max.

So basically you want to cut trailing zeros and count elements?

Christopher Dobrian's icon

Save this script as a .js file and use js to run it. It will tell you the length of your list up to the trailing zeros, and you can send that number into the right inlet of zl slice.

function list(l) {
    var n = arguments.length;
    while (arguments[n-1]==0) n--;
    outlet(0, n);
}